diff --git a/lego.js b/lego.js index b220745..845e6c5 100644 --- a/lego.js +++ b/lego.js @@ -1,70 +1,104 @@ 'use strict'; -/** - * Сделано задание на звездочку - * Реализованы методы or и and - */ -exports.isStar = true; - -/** - * Запрос к коллекции - * @param {Array} collection - * @params {...Function} – Функции для запроса - * @returns {Array} - */ +exports.isStar = false; + +// Чем больше число тем ниже приоритет функции +var priority = { + filterIn: 1, + sortBy: 2, + select: 3, + limit: 4, + format: 5 +}; + exports.query = function (collection) { - return collection; + var newCollection = copyCollection(collection); + var functions = Array.prototype.slice.call(arguments).slice(1); + function compare(one, another) { + return priority[one.name] - priority[another.name]; + } + functions.sort(compare); + functions.forEach(function (eachQuery) { + newCollection = eachQuery(newCollection); + }); + + return newCollection; }; -/** - * Выбор полей - * @params {...String} - */ +function copyCollection(collection) { + + return (collection.map(function (friend) { + + return Object.assign({}, friend); + })); +} + exports.select = function () { - return; -}; + var fields = Array.prototype.slice.call(arguments); -/** - * Фильтрация поля по массиву значений - * @param {String} property – Свойство для фильтрации - * @param {Array} values – Доступные значения - */ -exports.filterIn = function (property, values) { - console.info(property, values); + return function select(collection) { - return; + return collection.reduce(function (prev, curr) { + var friend = {}; + for (var field in curr) { + if (fields.indexOf(field) !== -1) { + friend[field] = curr[field]; + } + } + prev.push(friend); + + return prev; + }, []); + + }; }; -/** - * Сортировка коллекции по полю - * @param {String} property – Свойство для фильтрации - * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) - */ exports.sortBy = function (property, order) { - console.info(property, order); + return function sortBy(collection) { + function compare(one, another) { + return (one[property] > another[property]) ? 1 : -1; + } - return; + if (order === 'asc') { + collection.sort(compare); + } else { + collection.sort(compare).reverse(); + } + + return collection; + }; }; -/** - * Форматирование поля - * @param {String} property – Свойство для фильтрации - * @param {Function} formatter – Функция для форматирования - */ -exports.format = function (property, formatter) { - console.info(property, formatter); +exports.limit = function (count) { + return function limit(collection) { + return collection.slice(0, count); + }; +}; - return; +exports.filterIn = function (property, values) { + return function filterIn(collection) { + var newSorted = []; + collection.forEach(function (friend) { + for (var field in friend) { + if (property === field.toString() && + values.indexOf(friend[field]) !== -1) { + newSorted.push(Object.assign({}, friend)); + } + } + }); + + return newSorted; + }; }; -/** - * Ограничение количества элементов в коллекции - * @param {Number} count – Максимальное количество элементов - */ -exports.limit = function (count) { - console.info(count); +exports.format = function (property, formatter) { + return function format(collection) { + return collection.map(function (friend) { + friend[property] = formatter(friend[property]); - return; + return friend; + }); + }; }; if (exports.isStar) {