diff --git a/index.js b/index.js index 9240afb..826d27c 100644 --- a/index.js +++ b/index.js @@ -85,16 +85,14 @@ var bestFriends = lego.query( lego.limit(4) ); -console.info(bestFriends); - -/* Выведет: -[ - { name: 'Стелла', gender: 'Ж', email: 'waltersguzman@example.com' }, - { name: 'Мэт', gender: 'М', email: 'danamcgee@example.com' }, - { name: 'Шерри', gender: 'Ж', email: 'danamcgee@example.com' }, - { name: 'Сэм', gender: 'М', email: 'luisazamora@example.com' } -] -*/ + /* Выведет: + [ + { name: 'Стелла', gender: 'Ж', email: 'waltersguzman@example.com' }, + { name: 'Мэт', gender: 'М', email: 'danamcgee@example.com' }, + { name: 'Шерри', gender: 'Ж', email: 'danamcgee@example.com' }, + { name: 'Сэм', gender: 'М', email: 'luisazamora@example.com' } + ] + */ if (lego.isStar) { // Билли был бы по-настоящему счастлив, если бы ему удалось провести сразу две вечеринки: @@ -123,11 +121,11 @@ if (lego.isStar) { /* Выведет [ - { name: 'Сэм' }, - { name: 'Эмили' }, - { name: 'Мэт' }, - { name: 'Шерри' }, - { name: 'Стелла' } + { name: 'Сэм' }, + { name: 'Эмили' }, + { name: 'Мэт' }, + { name: 'Шерри' }, + { name: 'Стелла' } ] */ } diff --git a/lego.js b/lego.js index b220745..78ed05e 100644 --- a/lego.js +++ b/lego.js @@ -6,84 +6,119 @@ */ exports.isStar = true; -/** - * Запрос к коллекции - * @param {Array} collection - * @params {...Function} – Функции для запроса - * @returns {Array} - */ + exports.query = function (collection) { - return collection; + var copyOfCollection = collection.map(function (entry) { + return Object.assign({}, entry); + }); + var operators = [].slice.call(arguments, 1); + operators.sort(sortByPriority); + copyOfCollection = operators.reduce(function (collect, opepator) { + return opepator(collect); + }, copyOfCollection); + + return copyOfCollection; }; -/** - * Выбор полей - * @params {...String} - */ +function sortByPriority(a, b) { + var priority = ['filterIn', 'and', 'or', 'sortBy', 'select', 'limit', 'format']; + + return priority.indexOf(b.name) < priority.indexOf(a.name); +} + + exports.select = function () { - return; + var params = [].slice.call(arguments); + + return function select(collection) { + return collection.slice().map(function (entry) { + return selectByParam(params, entry); + }); + }; }; -/** - * Фильтрация поля по массиву значений - * @param {String} property – Свойство для фильтрации - * @param {Array} values – Доступные значения - */ +function selectByParam(params, entry) { + var newEntry = {}; + params.forEach(function (param) { + if (entry[param]) { + newEntry[param] = entry[param]; + } + }); + + return newEntry; +} + exports.filterIn = function (property, values) { console.info(property, values); - return; + return function filterIn(collection) { + return collection.slice().filter(function (entry) { + return values.some(function (value) { + return entry[property] === value; + }); + }); + }; }; -/** - * Сортировка коллекции по полю - * @param {String} property – Свойство для фильтрации - * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) - */ + exports.sortBy = function (property, order) { console.info(property, order); - return; + return function sortBy(collection) { + return collection.slice().sort(function (entryOne, entryTwo) { + if (order === 'asc') { + return entryOne[property]> entryTwo[property]; + } + + return entryTwo[property]> entryOne[property]; + }); + }; }; -/** - * Форматирование поля - * @param {String} property – Свойство для фильтрации - * @param {Function} formatter – Функция для форматирования - */ exports.format = function (property, formatter) { console.info(property, formatter); - return; + return function format(collection) { + return collection.slice().map(function (entry) { + entry[property] = formatter(entry[property]); + + return entry; + }); + }; }; -/** - * Ограничение количества элементов в коллекции - * @param {Number} count – Максимальное количество элементов - */ exports.limit = function (count) { console.info(count); - return; + return function limit(collection) { + return collection.slice(0, count); + }; }; if (exports.isStar) { - /** - * Фильтрация, объединяющая фильтрующие функции - * @star - * @params {...Function} – Фильтрующие функции - */ exports.or = function () { - return; + var filters = [].slice.call(arguments); + + return function or(collection) { + return collection.slice().filter(function (item) { + return filters.some(function (filter) { + return filter(collection.slice()).indexOf(item) !== -1; + }); + }); + }; }; - /** - * Фильтрация, пересекающая фильтрующие функции - * @star - * @params {...Function} – Фильтрующие функции - */ exports.and = function () { - return; + var filters = [].slice.call(arguments); + + return function and(collection) { + var copyOfCollection = collection.slice(); + filters.forEach(function (filter) { + copyOfCollection = filter(copyOfCollection); + }); + + return copyOfCollection; + }; }; }

AltStyle によって変換されたページ (->オリジナル) /