From e5028773561baf24acf28e1ae98f780197312235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:26:13 +0500 Subject: [PATCH 01/11] Update lego.js --- lego.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 81 insertions(+), 18 deletions(-) diff --git a/lego.js b/lego.js index b220745..8f237f3 100644 --- a/lego.js +++ b/lego.js @@ -6,14 +6,31 @@ */ exports.isStar = true; -/** - * Запрос к коллекции - * @param {Array} collection - * @params {...Function} – Функции для запроса - * @returns {Array} - */ -exports.query = function (collection) { - return collection; +function compareFunctions(a, b) { + var obj = { + select: 3, + format: 2, + filterIn: 4, + sortBy: 5, + limit: 1 + } + return (obj[a.name] < obj[b.name]) ? 1 : -1; +} + + +exports.query = function () { + var queryArgs = [].slice.call(arguments); + if (queryArgs.length === 1) { + return queryArgs[0]; + } + var changedCollection = queryArgs[0]; + + queryArgs.splice(0, 1); + queryArgs.sort(compareFunctions); + for (var i = 0; i < queryArgs.length; i++) { + changedCollection = queryArgs[i](changedCollection); + } + return changedCollection; }; /** @@ -21,7 +38,18 @@ exports.query = function (collection) { * @params {...String} */ exports.select = function () { - return; + var args = [].slice.call(arguments); + + return function select(collection) { + var result = []; + for (var i = 0; i < collection.length; i++) { + result.push({}); + for (var j = 0; j < args.length; j++) { + result[i][args[j]] = collection[i][args[j]]; + } + } + return result; + } }; /** @@ -30,9 +58,18 @@ exports.select = function () { * @param {Array} values – Доступные значения */ exports.filterIn = function (property, values) { - console.info(property, values); - return; + return function filterIn(collection) { + var result = []; + for (var i = 0; i < collection.length; i++) { + for (var j = 0; j < values.length; j++) { + if (collection[i][property] === values[j]) { + result.push(collection[i]); + } + } + } + return result; + } }; /** @@ -41,9 +78,27 @@ exports.filterIn = function (property, values) { * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) */ exports.sortBy = function (property, order) { - console.info(property, order); + return function sortBy(collection) { + collection.sort(function (a, b) { + var coefficient; + if (order==='asc') { + coefficient = -1; + } + if (order==='desc') { + coefficient = 1; + } + if (a[property]b[property]) { + return -1 * coefficient; + } - return; + return 0; + }); + + return collection; + } }; /** @@ -52,9 +107,16 @@ exports.sortBy = function (property, order) { * @param {Function} formatter – Функция для форматирования */ exports.format = function (property, formatter) { - console.info(property, formatter); - return; + return function format(collection) { + var result = []; + for (var i = 0; i < collection.length; i++) { + var a = formatter(collection[i][property]); + collection[i][property] = a; + result.push(collection[i]); + }s + return result; + } }; /** @@ -62,9 +124,10 @@ exports.format = function (property, formatter) { * @param {Number} count – Максимальное количество элементов */ exports.limit = function (count) { - console.info(count); - - return; + return function limit(collection) { + var result = collection.splice(0, count); + return result; + } }; if (exports.isStar) { From 18e3cd602ece4f616c144ddb8e718ccfafd8d4d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:26:30 +0500 Subject: [PATCH 02/11] Update lego.js --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 8f237f3..b3ccf01 100644 --- a/lego.js +++ b/lego.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализованы методы or и and */ -exports.isStar = true; +exports.isStar = false; function compareFunctions(a, b) { var obj = { From 6618a5ce653b9fea2fe13f4c659acebe42ceaa70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:27:10 +0500 Subject: [PATCH 03/11] Update lego.js --- lego.js | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/lego.js b/lego.js index b3ccf01..b84fe7e 100644 --- a/lego.js +++ b/lego.js @@ -1,9 +1,5 @@ 'use strict'; -/** - * Сделано задание на звездочку - * Реализованы методы or и and - */ exports.isStar = false; function compareFunctions(a, b) { @@ -17,7 +13,6 @@ function compareFunctions(a, b) { return (obj[a.name] < obj[b.name]) ? 1 : -1; } - exports.query = function () { var queryArgs = [].slice.call(arguments); if (queryArgs.length === 1) { @@ -33,10 +28,6 @@ exports.query = function () { return changedCollection; }; -/** - * Выбор полей - * @params {...String} - */ exports.select = function () { var args = [].slice.call(arguments); @@ -52,11 +43,6 @@ exports.select = function () { } }; -/** - * Фильтрация поля по массиву значений - * @param {String} property – Свойство для фильтрации - * @param {Array} values – Доступные значения - */ exports.filterIn = function (property, values) { return function filterIn(collection) { @@ -72,11 +58,6 @@ exports.filterIn = function (property, values) { } }; -/** - * Сортировка коллекции по полю - * @param {String} property – Свойство для фильтрации - * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) - */ exports.sortBy = function (property, order) { return function sortBy(collection) { collection.sort(function (a, b) { @@ -101,11 +82,6 @@ exports.sortBy = function (property, order) { } }; -/** - * Форматирование поля - * @param {String} property – Свойство для фильтрации - * @param {Function} formatter – Функция для форматирования - */ exports.format = function (property, formatter) { return function format(collection) { @@ -119,10 +95,6 @@ exports.format = function (property, formatter) { } }; -/** - * Ограничение количества элементов в коллекции - * @param {Number} count – Максимальное количество элементов - */ exports.limit = function (count) { return function limit(collection) { var result = collection.splice(0, count); @@ -132,20 +104,10 @@ exports.limit = function (count) { if (exports.isStar) { - /** - * Фильтрация, объединяющая фильтрующие функции - * @star - * @params {...Function} – Фильтрующие функции - */ exports.or = function () { return; }; - /** - * Фильтрация, пересекающая фильтрующие функции - * @star - * @params {...Function} – Фильтрующие функции - */ exports.and = function () { return; }; From 1bdf135c619bf25ccd6ce146cb2ea6c6e1e31e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:30:41 +0500 Subject: [PATCH 04/11] Update lego.js --- lego.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lego.js b/lego.js index b84fe7e..f9bcaec 100644 --- a/lego.js +++ b/lego.js @@ -9,7 +9,8 @@ function compareFunctions(a, b) { filterIn: 4, sortBy: 5, limit: 1 - } + }; + return (obj[a.name] < obj[b.name]) ? 1 : -1; } @@ -25,6 +26,7 @@ exports.query = function () { for (var i = 0; i < queryArgs.length; i++) { changedCollection = queryArgs[i](changedCollection); } + return changedCollection; }; @@ -39,8 +41,9 @@ exports.select = function () { result[i][args[j]] = collection[i][args[j]]; } } + return result; - } + }; }; exports.filterIn = function (property, values) { @@ -55,7 +58,7 @@ exports.filterIn = function (property, values) { } } return result; - } + }; }; exports.sortBy = function (property, order) { @@ -90,7 +93,7 @@ exports.format = function (property, formatter) { var a = formatter(collection[i][property]); collection[i][property] = a; result.push(collection[i]); - }s + } return result; } }; From 14f505bb3020ce2666a671824c305b4b309ae802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:39:19 +0500 Subject: [PATCH 05/11] Update lego.js --- lego.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lego.js b/lego.js index f9bcaec..92faaff 100644 --- a/lego.js +++ b/lego.js @@ -14,6 +14,14 @@ function compareFunctions(a, b) { return (obj[a.name] < obj[b.name]) ? 1 : -1; } +function filterCollection(property, values, collection, result) { + for (var j = 0; j < values.length; j++) { + if (collection[i][property] === values[j]) { + result.push(collection[i]); + } + } +} + exports.query = function () { var queryArgs = [].slice.call(arguments); if (queryArgs.length === 1) { @@ -51,12 +59,9 @@ exports.filterIn = function (property, values) { return function filterIn(collection) { var result = []; for (var i = 0; i < collection.length; i++) { - for (var j = 0; j < values.length; j++) { - if (collection[i][property] === values[j]) { - result.push(collection[i]); - } - } + filterCollection(property, values, collection, result); } + return result; }; }; @@ -65,24 +70,24 @@ exports.sortBy = function (property, order) { return function sortBy(collection) { collection.sort(function (a, b) { var coefficient; - if (order==='asc') { + if (order === 'asc') { coefficient = -1; } if (order==='desc') { coefficient = 1; } - if (a[property]b[property]) { - return -1 * coefficient; + if (a[property] > b[property]) { + return -1 * Number(coefficient); } return 0; }); return collection; - } + }; }; exports.format = function (property, formatter) { @@ -94,15 +99,17 @@ exports.format = function (property, formatter) { collection[i][property] = a; result.push(collection[i]); } + return result; - } + }; }; exports.limit = function (count) { return function limit(collection) { var result = collection.splice(0, count); + return result; - } + }; }; if (exports.isStar) { From 3cd100374cbec6cb9bb3d2e029089c04bb3167a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:40:23 +0500 Subject: [PATCH 06/11] Update lego.js --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 92faaff..a9b8df6 100644 --- a/lego.js +++ b/lego.js @@ -14,7 +14,7 @@ function compareFunctions(a, b) { return (obj[a.name] < obj[b.name]) ? 1 : -1; } -function filterCollection(property, values, collection, result) { +function filterCollection(property, values, collection, result, i) { for (var j = 0; j < values.length; j++) { if (collection[i][property] === values[j]) { result.push(collection[i]); From a60bd8e7968017191cb253dccec311af63992166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:41:17 +0500 Subject: [PATCH 07/11] Update --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index a9b8df6..36ef128 100644 --- a/lego.js +++ b/lego.js @@ -59,7 +59,7 @@ exports.filterIn = function (property, values) { return function filterIn(collection) { var result = []; for (var i = 0; i < collection.length; i++) { - filterCollection(property, values, collection, result); + filterCollection(property, values, collection, result, i); } return result; From 00e5eb6f07278be752cabcea4904c46b4a1abe32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:47:23 +0500 Subject: [PATCH 08/11] Update lego.js --- lego.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lego.js b/lego.js index 36ef128..4716ad2 100644 --- a/lego.js +++ b/lego.js @@ -14,10 +14,10 @@ function compareFunctions(a, b) { return (obj[a.name] < obj[b.name]) ? 1 : -1; } -function filterCollection(property, values, collection, result, i) { +function filterCollection(property, values, record, result) { for (var j = 0; j < values.length; j++) { - if (collection[i][property] === values[j]) { - result.push(collection[i]); + if (record[property] === values[j]) { + result.push(record); } } } @@ -59,7 +59,7 @@ exports.filterIn = function (property, values) { return function filterIn(collection) { var result = []; for (var i = 0; i < collection.length; i++) { - filterCollection(property, values, collection, result, i); + filterCollection(property, values, collection[i], result); } return result; @@ -73,7 +73,7 @@ exports.sortBy = function (property, order) { if (order === 'asc') { coefficient = -1; } - if (order==='desc') { + if (order === 'desc') { coefficient = 1; } if (a[property] < b[property]) { From 3b1b8f15c05cd6694b75bd4fdfa019614b1f9996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:50:39 +0500 Subject: [PATCH 09/11] Update lego.js --- lego.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index 4716ad2..c461353 100644 --- a/lego.js +++ b/lego.js @@ -68,7 +68,7 @@ exports.filterIn = function (property, values) { exports.sortBy = function (property, order) { return function sortBy(collection) { - collection.sort(function (a, b) { + var a = collection.sort(function (a, b) { var coefficient; if (order === 'asc') { coefficient = -1; @@ -86,7 +86,7 @@ exports.sortBy = function (property, order) { return 0; }); - return collection; + return a; }; }; From a8c3518e45ecee7173a9ef2db96ccbd6c999be24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:52:28 +0500 Subject: [PATCH 10/11] Update lego.js --- lego.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index c461353..8df354b 100644 --- a/lego.js +++ b/lego.js @@ -68,7 +68,7 @@ exports.filterIn = function (property, values) { exports.sortBy = function (property, order) { return function sortBy(collection) { - var a = collection.sort(function (a, b) { + var c = collection.sort(function (a, b) { var coefficient; if (order === 'asc') { coefficient = -1; @@ -86,7 +86,7 @@ exports.sortBy = function (property, order) { return 0; }); - return a; + return c; }; }; From 821393e5d368f9f010d7e20bd43515be6bd09ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD?= Date: Sun, 6 Nov 2016 03:54:20 +0500 Subject: [PATCH 11/11] Update lego.js --- lego.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index 8df354b..4716ad2 100644 --- a/lego.js +++ b/lego.js @@ -68,7 +68,7 @@ exports.filterIn = function (property, values) { exports.sortBy = function (property, order) { return function sortBy(collection) { - var c = collection.sort(function (a, b) { + collection.sort(function (a, b) { var coefficient; if (order === 'asc') { coefficient = -1; @@ -86,7 +86,7 @@ exports.sortBy = function (property, order) { return 0; }); - return c; + return collection; }; };