From c8b7dc6949c13db11fcfdd3670c3d86b7a51b695 Mon Sep 17 00:00:00 2001 From: NeSmogPridymatNick Date: Wed, 2 Nov 2016 21:32:12 +0500 Subject: [PATCH 1/3] Update lego.js --- lego.js | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 128 insertions(+), 11 deletions(-) diff --git a/lego.js b/lego.js index b220745..34d0d0b 100644 --- a/lego.js +++ b/lego.js @@ -1,10 +1,28 @@ 'use strict'; +var ORDER = { + 'filterIn': false, + 'sortBy': false, + 'format': false, + 'limit': false, + 'select': false +}; +var KEY = ['filterIn', 'sortBy', 'format', 'limit', 'select']; +function priority() { + for (var key in ORDER) { + if (ORDER[key] === false) { + + return true; + } + } + + return false; +} /** * Сделано задание на звездочку * Реализованы методы or и and */ -exports.isStar = true; +exports.isStar = false; /** * Запрос к коллекции @@ -13,58 +31,157 @@ exports.isStar = true; * @returns {Array} */ exports.query = function (collection) { - return collection; + var arg = arguments; + var newCollection = collection; + var i = 1; + while (priority()) { + if (i > arg.length - 1) { + i = 1; + } + newCollection = arg[i](newCollection); + i++; + } + + return newCollection; }; +function helpSelect(arg, collection, i) { + var dict = {}; + for (var j = 0; j < arg.length; j++) { + if (!collection[i][arg[j]] !== undefined) { + dict[arg[j]] = collection[i][arg[j]]; + } + } + + return dict; +} /** * Выбор полей * @params {...String} + * @returns {Array} */ exports.select = function () { - return; + var arg = arguments; + + return function (collection) { + if (ORDER[KEY[3]] === false) { + + return collection; + } + ORDER[KEY[4]] = true; + var newCollection = []; + for (var i = 0; i < collection.length; i++) { + newCollection.push(helpSelect(arg, collection, i)); + } + + return newCollection; + }; + }; +function filter(value, collection, property) { + var newCollection = []; + var fn = new RegExp(value, 'i'); + for (var j = 0; j < collection.length; j++) { + if (fn.exec(collection[j][property])) { + newCollection.push(collection[j]); + } + } + + return newCollection; +} /** * Фильтрация поля по массиву значений * @param {String} property – Свойство для фильтрации * @param {Array} values – Доступные значения + * @returns {Array} */ exports.filterIn = function (property, values) { - console.info(property, values); + ORDER[KEY[0]] = true; - return; + return function (collection) { + var newCollection = []; + for (var i = 0; i < values.length; i++) { + newCollection = newCollection.concat(filter(values[i], collection, property)); + } + + return newCollection; + }; }; /** * Сортировка коллекции по полю * @param {String} property – Свойство для фильтрации * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) + * @returns {Array} */ exports.sortBy = function (property, order) { - console.info(property, order); + var type; + if (order === 'asc') { + type = 1; + } else { + type = -1; + } + + return function (collection) { + if (ORDER[KEY[0]] === false) { + + return collection; + } + ORDER[KEY[1]] = true; + + return collection.sort(function (cur, next) { + if (cur[property] > next[property]) { + + return type; + } + if (cur[property] < next[property]) { + + return -type; + } - return; + return 0; + }); + }; }; /** * Форматирование поля * @param {String} property – Свойство для фильтрации * @param {Function} formatter – Функция для форматирования + * @returns {Array} */ exports.format = function (property, formatter) { - console.info(property, formatter); + return function (collection) { + if (ORDER[KEY[1]] === false) { - return; + return collection; + } + ORDER[KEY[2]] = true; + for (var i = 0; i < collection.length; i++) { + collection[i][property] = formatter(collection[i][property]); + } + + return collection; + }; }; /** * Ограничение количества элементов в коллекции * @param {Number} count – Максимальное количество элементов + * @returns {Array} */ exports.limit = function (count) { - console.info(count); - return; + return function (collection) { + if (ORDER[KEY[2]] === false) { + + return collection; + } + ORDER[KEY[3]] = true; + + return collection.slice(0, count); + }; }; if (exports.isStar) { From 5cbe4f701cd92aef81e607caec4806177f5d06ed Mon Sep 17 00:00:00 2001 From: NeSmogPridymatNick Date: Wed, 2 Nov 2016 23:20:49 +0500 Subject: [PATCH 2/3] Update lego.js --- lego.js | 57 ++++++++++++++------------------------------------------- 1 file changed, 14 insertions(+), 43 deletions(-) diff --git a/lego.js b/lego.js index 34d0d0b..71ac82f 100644 --- a/lego.js +++ b/lego.js @@ -1,21 +1,10 @@ 'use strict'; -var ORDER = { - 'filterIn': false, - 'sortBy': false, - 'format': false, - 'limit': false, - 'select': false -}; -var KEY = ['filterIn', 'sortBy', 'format', 'limit', 'select']; -function priority() { - for (var key in ORDER) { - if (ORDER[key] === false) { - - return true; - } - } - return false; +var KEY = ['filterIn', 'sortBy', 'limit', 'format', 'select']; +function copy(collection) { + return collection.map(function (entry) { + return Object.assign({}, entry); + }); } /** @@ -31,15 +20,14 @@ exports.isStar = false; * @returns {Array} */ exports.query = function (collection) { - var arg = arguments; - var newCollection = collection; - var i = 1; - while (priority()) { - if (i > arg.length - 1) { - i = 1; - } + var arg = [].slice.call(arguments).slice(1); + var newCollection = copy(collection); + arg.sort(function (a, b) { + + return KEY.indexOf(a.name) - KEY.indexOf(b.name); + }); + for (var i = 0; i < arg.length; i++) { newCollection = arg[i](newCollection); - i++; } return newCollection; @@ -64,11 +52,7 @@ exports.select = function () { var arg = arguments; return function (collection) { - if (ORDER[KEY[3]] === false) { - return collection; - } - ORDER[KEY[4]] = true; var newCollection = []; for (var i = 0; i < collection.length; i++) { newCollection.push(helpSelect(arg, collection, i)); @@ -97,7 +81,6 @@ function filter(value, collection, property) { * @returns {Array} */ exports.filterIn = function (property, values) { - ORDER[KEY[0]] = true; return function (collection) { var newCollection = []; @@ -116,6 +99,7 @@ exports.filterIn = function (property, values) { * @returns {Array} */ exports.sortBy = function (property, order) { + var type; if (order === 'asc') { type = 1; @@ -124,11 +108,6 @@ exports.sortBy = function (property, order) { } return function (collection) { - if (ORDER[KEY[0]] === false) { - - return collection; - } - ORDER[KEY[1]] = true; return collection.sort(function (cur, next) { if (cur[property] > next[property]) { @@ -152,12 +131,9 @@ exports.sortBy = function (property, order) { * @returns {Array} */ exports.format = function (property, formatter) { + return function (collection) { - if (ORDER[KEY[1]] === false) { - return collection; - } - ORDER[KEY[2]] = true; for (var i = 0; i < collection.length; i++) { collection[i][property] = formatter(collection[i][property]); } @@ -174,11 +150,6 @@ exports.format = function (property, formatter) { exports.limit = function (count) { return function (collection) { - if (ORDER[KEY[2]] === false) { - - return collection; - } - ORDER[KEY[3]] = true; return collection.slice(0, count); }; From 91d4f562a0d36fa59834dc7c8a8897f44d130413 Mon Sep 17 00:00:00 2001 From: NeSmogPridymatNick Date: Wed, 2 Nov 2016 23:32:55 +0500 Subject: [PATCH 3/3] Update lego.js --- lego.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lego.js b/lego.js index 71ac82f..2b223e4 100644 --- a/lego.js +++ b/lego.js @@ -20,9 +20,9 @@ exports.isStar = false; * @returns {Array} */ exports.query = function (collection) { - var arg = [].slice.call(arguments).slice(1); + var arg = [].slice.call(arguments, 1); var newCollection = copy(collection); - arg.sort(function (a, b) { + arg = arg.sort(function (a, b) { return KEY.indexOf(a.name) - KEY.indexOf(b.name); }); @@ -51,7 +51,7 @@ function helpSelect(arg, collection, i) { exports.select = function () { var arg = arguments; - return function (collection) { + return function select(collection) { var newCollection = []; for (var i = 0; i < collection.length; i++) { @@ -82,7 +82,7 @@ function filter(value, collection, property) { */ exports.filterIn = function (property, values) { - return function (collection) { + return function filterIn(collection) { var newCollection = []; for (var i = 0; i < values.length; i++) { newCollection = newCollection.concat(filter(values[i], collection, property)); @@ -107,7 +107,7 @@ exports.sortBy = function (property, order) { type = -1; } - return function (collection) { + return function sortBy(collection) { return collection.sort(function (cur, next) { if (cur[property] > next[property]) { @@ -132,7 +132,7 @@ exports.sortBy = function (property, order) { */ exports.format = function (property, formatter) { - return function (collection) { + return function format(collection) { for (var i = 0; i < collection.length; i++) { collection[i][property] = formatter(collection[i][property]); @@ -149,7 +149,7 @@ exports.format = function (property, formatter) { */ exports.limit = function (count) { - return function (collection) { + return function limit(collection) { return collection.slice(0, count); };