-
Notifications
You must be signed in to change notification settings - Fork 70
Вихарев Вячеслав #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Вихарев Вячеслав #25
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,89 +1,153 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Сделано задание на звездочку | ||
| * Реализованы методы or и and | ||
| */ | ||
| exports.isStar = true; | ||
|
|
||
| /** | ||
| * Запрос к коллекции | ||
| * @param {Array} collection | ||
| * @params {...Function} – Функции для запроса | ||
| * @returns {Array} | ||
| */ | ||
| exports.query = function (collection) { | ||
| return collection; | ||
|
|
||
| // Constants | ||
| // | ||
| var FUNC_ORDER = [ | ||
| 'and', | ||
| 'or', | ||
| 'filterIn', | ||
| 'sortBy', | ||
| 'select', | ||
| 'limit', | ||
| 'format' | ||
| ]; | ||
|
|
||
|
|
||
| // Functions-helpers | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если хочешь писать доки -- у тебя есть пример, как их оформлять)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это не доки, это просто разделение на логические части, просто для того чтоб удобнее было ориентироваться There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кстати, почему удалил все доки?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мешают ориентироваться в коде |
||
| // | ||
| var _functionSorter = function (one, another) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можешь объяснить, почему у тебя названия функций начинаются с земли?)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это функции, которые не предполагается использовать где-то еще, кроме как в этом коде, тогда как функции из следующего блока можно использовать где-то еще There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И по правилам название функции должно начинаться с глагола.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ну не всегда There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я бы всё равно избавилась на твоем месте от земли.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хорошо, уберу |
||
| var nameOne = one.name.split(' ')[1]; | ||
| var nameAnother = another.name.split(' ')[1]; | ||
|
|
||
| return FUNC_ORDER.indexOf(nameOne) > FUNC_ORDER.indexOf(nameAnother); | ||
| }; | ||
|
|
||
| /** | ||
| * Выбор полей | ||
| * @params {...String} | ||
| */ | ||
| exports.select = function () { | ||
| return; | ||
| var _functionApplyer = function (obj, f) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем эта функция?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я ее передаю как аргумент в <список функций>.map с присвоенный объектом |
||
| return f(obj); | ||
| }; | ||
|
|
||
| var _objectBuilder = function (obj, keyValuePair) { | ||
| var key = keyValuePair[0]; | ||
| var value = keyValuePair[1]; | ||
|
|
||
| var newObject = Object.assign({}, obj); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В ES5.1 еще нет
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Упс :) |
||
| newObject[key] = value; | ||
|
|
||
| return newObject; | ||
| }; | ||
|
|
||
| /** | ||
| * Фильтрация поля по массиву значений | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {Array} values – Доступные значения | ||
| */ | ||
| exports.filterIn = function (property, values) { | ||
| console.info(property, values); | ||
| var _in = function (collection, obj) { | ||
| return collection.indexOf(obj) !== -1; | ||
| }; | ||
|
|
||
| var _objIn = function (obj, collection) { | ||
| return _in(collection, obj); | ||
| }; | ||
|
|
||
| return; | ||
| var _takeKeyValuePair = function (item, key) { | ||
| return [key, item[key]]; | ||
| }; | ||
|
|
||
| /** | ||
| * Сортировка коллекции по полю | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) | ||
| */ | ||
| exports.sortBy = function (property, order) { | ||
| console.info(property, order); | ||
|
|
||
| return; | ||
| // Query functions | ||
| // | ||
| var select = function (keys, collection) { | ||
| return collection | ||
| .map(function (item) { | ||
| return Object.keys(item) | ||
| .filter(_in.bind(null, keys)) | ||
| .map(_takeKeyValuePair.bind(null, item)) | ||
| .reduce(_objectBuilder, {}); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Форматирование поля | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {Function} formatter – Функция для форматирования | ||
| */ | ||
| exports.format = function (property, formatter) { | ||
| console.info(property, formatter); | ||
| var filterIn = function (key, values, collection) { | ||
| return collection | ||
| .filter(function (item) { | ||
| return _in(values, item[key]); | ||
| }); | ||
| }; | ||
|
|
||
| return; | ||
| var sortBy = function (key, order, collection) { | ||
| return collection | ||
| .sort(function (one, another) { | ||
| return order === 'asc' | ||
| ? one[key] > another[key] | ||
| : one[key] < another[key]; | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Ограничение количества элементов в коллекции | ||
| * @param {Number} count – Максимальное количество элементов | ||
| */ | ||
| exports.limit = function (count) { | ||
| console.info(count); | ||
| var format = function (key, formatter, collection) { | ||
| return collection | ||
| .map(function (item) { | ||
| var itemCopy = Object.assign({}, item); | ||
| itemCopy[key] = formatter(itemCopy[key]); | ||
|
|
||
| return; | ||
| return itemCopy; | ||
| }); | ||
| }; | ||
|
|
||
| if (exports.isStar) { | ||
| var limit = function (count, collection) { | ||
| return collection.slice(0, count); | ||
| }; | ||
|
|
||
| /** | ||
| * Фильтрация, объединяющая фильтрующие функции | ||
| * @star | ||
| * @params {...Function} – Фильтрующие функции | ||
| */ | ||
| var or = function (filters, collection) { | ||
| return collection | ||
| .filter(function (item) { | ||
| return filters | ||
| .map(_functionApplyer.bind(null, collection)) | ||
| .some(_objIn.bind(null, item)); | ||
| }); | ||
| }; | ||
|
|
||
| var and = function (filters, collection) { | ||
| return collection | ||
| .filter(function (item) { | ||
| return filters | ||
| .map(_functionApplyer.bind(null, collection)) | ||
| .every(_objIn.bind(null, item)); | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
| // Export | ||
| // | ||
| exports.query = function (collection) { | ||
| return [].slice.call(arguments, 1) | ||
| .sort(_functionSorter) | ||
| .reduce(_functionApplyer, collection.slice()); | ||
| }; | ||
|
|
||
| exports.select = function () { | ||
| return select.bind(null, [].slice.call(arguments)); | ||
| }; | ||
|
|
||
| exports.filterIn = function (key, values) { | ||
| return filterIn.bind(null, key, values); | ||
| }; | ||
|
|
||
| exports.sortBy = function (key, order) { | ||
| return sortBy.bind(null, key, order); | ||
| }; | ||
|
|
||
| exports.format = function (key, formatter) { | ||
| return format.bind(null, key, formatter); | ||
| }; | ||
|
|
||
| exports.limit = function (count) { | ||
| return limit.bind(null, count); | ||
| }; | ||
|
|
||
|
|
||
| if (exports.isStar) { | ||
| exports.or = function () { | ||
| return; | ||
| return or.bind(null, [].slice.call(arguments)); | ||
| }; | ||
|
|
||
| /** | ||
| * Фильтрация, пересекающая фильтрующие функции | ||
| * @star | ||
| * @params {...Function} – Фильтрующие функции | ||
| */ | ||
| exports.and = function () { | ||
| return; | ||
| return and.bind(null, [].slice.call(arguments)); | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Название не совсем корректно отображает содержимое