-
Notifications
You must be signed in to change notification settings - Fork 70
Козлов Александр #63
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?
Козлов Александр #63
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,111 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Сделано задание на звездочку | ||
| * Реализованы методы or и and | ||
| */ | ||
| exports.isStar = true; | ||
| exports.isStar = false; | ||
|
|
||
| var FUNCTION_PRIORITY = ['filterIn', 'sortBy', 'select', 'limit', 'format']; | ||
|
|
||
| /** | ||
| * Запрос к коллекции | ||
| * @param {Array} collection | ||
| * @params {...Function} – Функции для запроса | ||
| * @returns {Array} | ||
| */ | ||
|
|
||
| exports.query = function (collection) { | ||
| return collection; | ||
| var functions = [].slice.call(arguments, 1); | ||
| var changedCollection = collection.map(function (el) { | ||
| return Object.assign({}, el); | ||
| }); | ||
| functions.sort(function (first, second) { | ||
| return FUNCTION_PRIORITY.indexOf(first.name) - FUNCTION_PRIORITY.indexOf(second.name); | ||
| }) | ||
| .forEach (function (query) { | ||
| changedCollection = query(changedCollection); | ||
| }); | ||
|
|
||
| return changedCollection; | ||
| }; | ||
|
|
||
| /** | ||
| * Выбор полей | ||
| * @params {...String} | ||
| * @returns {Function} | ||
| */ | ||
| exports.select = function () { | ||
| return; | ||
| var fieldsToChoose = [].slice.call(arguments); | ||
|
|
||
| return function select(collection) { | ||
| return collection.map(function (el) { | ||
|
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.
|
||
| return fieldsToChoose.reduce(function (newCollection, property) { | ||
|
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. Первый аргумент назвал |
||
| if (el.hasOwnProperty(property)) { | ||
| newCollection[property] = el[property]; | ||
| } | ||
|
|
||
| return newCollection; | ||
| }, {}); | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
|
|
||
| /** | ||
| * Фильтрация поля по массиву значений | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {Array} values – Доступные значения | ||
| * @returns {Function} | ||
| */ | ||
| exports.filterIn = function (property, values) { | ||
| console.info(property, values); | ||
|
|
||
| return; | ||
| return function filterIn(collection) { | ||
|
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. Снова |
||
| return collection.filter(function (el) { | ||
| return values.indexOf(el[property]) !== -1; | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
|
|
||
| /** | ||
| * Сортировка коллекции по полю | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) | ||
| * @returns {Function} | ||
| */ | ||
| exports.sortBy = function (property, order) { | ||
| console.info(property, order); | ||
|
|
||
| return; | ||
| return function sortBy(collection) { | ||
| var changedCollection = collection.sort(function (first, second) { | ||
|
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. Зачем еще одну переменную создавать? Можно просто |
||
|
|
||
| return (first[property] > second[property]); | ||
|
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. Функция сравнения должна возвращать 1 (если |
||
| }); | ||
|
|
||
| return order === 'asc' ? changedCollection : changedCollection.reverse(); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Форматирование поля | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {Function} formatter – Функция для форматирования | ||
| * @returns {Function} | ||
| */ | ||
| exports.format = function (property, formatter) { | ||
| console.info(property, formatter); | ||
| return function format(collection) { | ||
| return collection.map(function (el) { | ||
| if (el.hasOwnProperty(property)) { | ||
| el[property] = formatter(el[property]); | ||
| } | ||
|
|
||
| return; | ||
| return el; | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Ограничение количества элементов в коллекции | ||
| * @param {Number} count – Максимальное количество элементов | ||
| * @returns {Function} | ||
| */ | ||
| exports.limit = function (count) { | ||
| console.info(count); | ||
|
|
||
| return; | ||
| }; | ||
|
|
||
| if (exports.isStar) { | ||
|
|
||
| /** | ||
| * Фильтрация, объединяющая фильтрующие функции | ||
| * @star | ||
| * @params {...Function} – Фильтрующие функции | ||
| */ | ||
| exports.or = function () { | ||
| return; | ||
| return function limit(collection) { | ||
| return collection.slice(0, count); | ||
| }; | ||
|
|
||
| /** | ||
| * Фильтрация, пересекающая фильтрующие функции | ||
| * @star | ||
| * @params {...Function} – Фильтрующие функции | ||
| */ | ||
| exports.and = function () { | ||
| return; | ||
| }; | ||
| } | ||
| }; | ||
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.
Для функции сортировки можно параметры называть
aиb