-
Notifications
You must be signed in to change notification settings - Fork 70
Новоселова Наталья #57
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?
Changes from 4 commits
d7abc05
046870b
26a375d
2d47144
331c0df
d984d85
d5093d7
ee4b074
d228c8b
7be0eba
9d416d2
35e3d2e
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 |
|---|---|---|
|
|
@@ -4,7 +4,15 @@ | |
| * Сделано задание на звездочку | ||
| * Реализованы методы or и and | ||
| */ | ||
| exports.isStar = true; | ||
| exports.isStar = false; | ||
|
|
||
| var priorityFunctions = ['filterIn', 'sortBy', 'select', 'limit', 'format']; | ||
|
|
||
| function getCopyCollection(collection) { | ||
| return collection.map(function (element) { | ||
| return Object.assign({}, element); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Запрос к коллекции | ||
|
|
@@ -13,77 +21,100 @@ exports.isStar = true; | |
| * @returns {Array} | ||
| */ | ||
| exports.query = function (collection) { | ||
| return collection; | ||
| var copyCollection = getCopyCollection(collection); | ||
| var functions = [].slice.call(arguments, 1); | ||
| functions.sort(function (a, b) { | ||
| return priorityFunctions.indexOf(a.name) - priorityFunctions.indexOf(b.name); | ||
| }) | ||
| .forEach (function (query) { | ||
| copyCollection = query(copyCollection); | ||
| }); | ||
|
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 copyCollection; | ||
| }; | ||
|
|
||
| /** | ||
| * Выбор полей | ||
| * @params {...String} | ||
| * @returns {Function} | ||
| */ | ||
|
|
||
| exports.select = function () { | ||
| return; | ||
| var fieldsCollection = [].slice.call(arguments); | ||
|
|
||
| return function select(collection) { | ||
| return collection.map(function (element) { | ||
| return fieldsCollection.reduce(function (newCollection, field) { | ||
| if (element.hasOwnProperty(field)) { | ||
| newCollection[field] = element[field]; | ||
| } | ||
|
|
||
| return newCollection; | ||
| }, {}); | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Фильтрация поля по массиву значений | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {Array} values – Доступные значения | ||
| * @returns {Function} | ||
| */ | ||
| exports.filterIn = function (property, values) { | ||
| console.info(property, values); | ||
|
|
||
| return; | ||
| return function filterIn(collection) { | ||
| return collection.filter(function (note) { | ||
| return values.indexOf(note[property]) >= 0; | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Сортировка коллекции по полю | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) | ||
| * @returns {Function} | ||
| */ | ||
| exports.sortBy = function (property, order) { | ||
| console.info(property, order); | ||
| return function sortBy(collection) { | ||
| var copyCollection = getCopyCollection(collection); | ||
|
|
||
| return copyCollection.sort(function (first, second) { | ||
| if (order === 'asc') { | ||
| return first[property] > second[property]; | ||
| } | ||
|
|
||
| return; | ||
| 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.
|
||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Форматирование поля | ||
| * @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 (element) { | ||
| var copyCollection = Object.assign({}, element); | ||
|
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 (property in element) { | ||
| copyCollection[property] = formatter(copyCollection[property]); | ||
| } | ||
|
|
||
| return; | ||
| return copyCollection; | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Ограничение количества элементов в коллекции | ||
| * @param {Number} count – Максимальное количество элементов | ||
| * @returns {Function} | ||
| */ | ||
| exports.limit = function (count) { | ||
| console.info(count); | ||
|
|
||
| return; | ||
| }; | ||
|
|
||
| if (exports.isStar) { | ||
|
|
||
| /** | ||
| * Фильтрация, объединяющая фильтрующие функции | ||
| * @star | ||
| * @params {...Function} – Фильтрующие функции | ||
| */ | ||
| exports.or = function () { | ||
| return; | ||
| }; | ||
|
|
||
| /** | ||
| * Фильтрация, пересекающая фильтрующие функции | ||
| * @star | ||
| * @params {...Function} – Фильтрующие функции | ||
| */ | ||
| exports.and = function () { | ||
| return; | ||
| return function limit(collection) { | ||
| return collection.slice(0, count); | ||
| }; | ||
| } | ||
| }; | ||
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.
Object.assign-- этоES2015. Мы пока остаёмся в рамкахES5, так что эту функцию использовать нельзя