From 63da3af54cd9448d9f3deb75aa85015b09b08fc8 Mon Sep 17 00:00:00 2001 From: Uliana1997 Date: Wed, 2 Nov 2016 20:32:45 +0600 Subject: [PATCH 01/18] First --- lego.js | 151 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 102 insertions(+), 49 deletions(-) diff --git a/lego.js b/lego.js index b220745..c86ea93 100644 --- a/lego.js +++ b/lego.js @@ -1,70 +1,123 @@ 'use strict'; -/** - * Сделано задание на звездочку - * Реализованы методы or и and - */ -exports.isStar = true; - -/** - * Запрос к коллекции - * @param {Array} collection - * @params {...Function} – Функции для запроса - * @returns {Array} - */ +exports.isStar = false; + +var priority = { + 'filterIn': 1, + 'and': 2, + 'or': 3, + 'sortBy': 4, + 'select': 5, + 'limit': 6, + 'format': 7 +}; + exports.query = function (collection) { - return collection; + var newCollection = copyCollections(collection); + var fields = Array.prototype.slice.call(arguments).slice(1); + function compare(one, another) { + + return priority[one.name] - priority[another.name]; + } + fields.sort(compare); + fields.forEach(function (func) { + newCollection = func(newCollection); + }); + + return newCollection; }; -/** - * Выбор полей - * @params {...String} - */ +function copyCollections(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) { + var result = []; + collection.forEach(function (friend) { + var newFriend = {}; + for (var field in friend) { + if (fields.indexOf(field) >= 0) { + newFriend[field] = friend[field]; + } + } + result.push(newFriend); + }); - return; + return result; + }; }; -/** - * Сортировка коллекции по полю - * @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]; + } + if (order === 'asc') { + collection.sort(compare); + } else { + collection.sort(!compare); + } + + return collection; + }; +}; - return; +exports.limit = function (count) { + return function limit(collection) { + return collection.slice(0, count); + }; }; -/** - * Форматирование поля - * @param {String} property – Свойство для фильтрации - * @param {Function} formatter – Функция для форматирования - */ -exports.format = function (property, formatter) { - console.info(property, formatter); +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]) >= 0 && + !containsObject(friend, newSorted)) { + newSorted.push(Object.assign({}, friend)); + } + } + }); - return; + return newSorted; + }; }; -/** - * Ограничение количества элементов в коллекции - * @param {Number} count – Максимальное количество элементов - */ -exports.limit = function (count) { - console.info(count); +function containsObject(obj, collection) { + var x; + for (x in collection) { + if (collection.hasOwnProperty(x) && collection[x] === obj) { + return true; + } + } + + return false; +} - return; +exports.format = function (property, formatter) { + return function format(collection) { + collection.forEach(function (friend, i) { + var newFriendList = {}; + for (var field in friend) { + if (field !== undefined) { + newFriendList[field] = field === property + ? formatter(friend[field]) : friend[field]; + } + } + collection[i] = newFriendList; + }); + + return collection; + }; }; if (exports.isStar) { From 38a3b7431af8e43e2b4f91bb2ac3d3f3d97db3c5 Mon Sep 17 00:00:00 2001 From: Uliana1997 Date: Wed, 2 Nov 2016 21:00:22 +0600 Subject: [PATCH 02/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sort --- lego.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lego.js b/lego.js index c86ea93..83889ec 100644 --- a/lego.js +++ b/lego.js @@ -4,12 +4,10 @@ exports.isStar = false; var priority = { 'filterIn': 1, - 'and': 2, - 'or': 3, - 'sortBy': 4, - 'select': 5, - 'limit': 6, - 'format': 7 + 'sortBy': 2, + 'select': 3, + 'limit': 4, + 'format': 5 }; exports.query = function (collection) { @@ -59,10 +57,13 @@ exports.sortBy = function (property, order) { function compare(one, another) { return one[property] - another[property]; } + function comparedesc(one, another) { + return another[property] - one[property]; + } if (order === 'asc') { collection.sort(compare); } else { - collection.sort(!compare); + collection.sort(comparedesc); } return collection; From b03ad9722acef0c94fbd5a2f01aa9368e0a8e4b2 Mon Sep 17 00:00:00 2001 From: Uliana1997 Date: Wed, 2 Nov 2016 21:31:21 +0600 Subject: [PATCH 03/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sortby --- lego.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lego.js b/lego.js index 83889ec..d7d5e7f 100644 --- a/lego.js +++ b/lego.js @@ -60,10 +60,25 @@ exports.sortBy = function (property, order) { function comparedesc(one, another) { return another[property] - one[property]; } - if (order === 'asc') { - collection.sort(compare); + if (property === 'age') { + if (order === 'asc') { + collection.sort(compare); + } else { + collection.sort(comparedesc); + } } else { - collection.sort(comparedesc); + collection.sort(function (one, another) { + var x = one.name.toLowerCase(); + var y = another.name.toLowerCase(); + if (x < y) { + return -1; + } + if (x > y) { + return 1; + } + + return 0; + }); } return collection; @@ -94,8 +109,7 @@ exports.filterIn = function (property, values) { }; function containsObject(obj, collection) { - var x; - for (x in collection) { + for (var x in collection) { if (collection.hasOwnProperty(x) && collection[x] === obj) { return true; } From d940baeb950444c6d798962e606e68f57521b98f Mon Sep 17 00:00:00 2001 From: Uliana1997 Date: Wed, 2 Nov 2016 22:22:13 +0600 Subject: [PATCH 04/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit check --- lego.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/lego.js b/lego.js index d7d5e7f..877e85b 100644 --- a/lego.js +++ b/lego.js @@ -41,7 +41,7 @@ exports.select = function () { collection.forEach(function (friend) { var newFriend = {}; for (var field in friend) { - if (fields.indexOf(field) >= 0) { + if (fields.indexOf(field) !== -1) { newFriend[field] = friend[field]; } } @@ -97,8 +97,7 @@ exports.filterIn = function (property, values) { collection.forEach(function (friend) { for (var field in friend) { if (property === field.toString() && - values.indexOf(friend[field]) >= 0 && - !containsObject(friend, newSorted)) { + values.indexOf(friend[field]) !== -1) { newSorted.push(Object.assign({}, friend)); } } @@ -108,16 +107,6 @@ exports.filterIn = function (property, values) { }; }; -function containsObject(obj, collection) { - for (var x in collection) { - if (collection.hasOwnProperty(x) && collection[x] === obj) { - return true; - } - } - - return false; -} - exports.format = function (property, formatter) { return function format(collection) { collection.forEach(function (friend, i) { From 4dbb2897e9dd4943f6f8ecba8347280fdc054419 Mon Sep 17 00:00:00 2001 From: Uliana1997 Date: Wed, 2 Nov 2016 22:43:41 +0600 Subject: [PATCH 05/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit format --- lego.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lego.js b/lego.js index 877e85b..2104b4e 100644 --- a/lego.js +++ b/lego.js @@ -109,18 +109,11 @@ exports.filterIn = function (property, values) { exports.format = function (property, formatter) { return function format(collection) { - collection.forEach(function (friend, i) { - var newFriendList = {}; - for (var field in friend) { - if (field !== undefined) { - newFriendList[field] = field === property - ? formatter(friend[field]) : friend[field]; - } - } - collection[i] = newFriendList; - }); + return collection.map(function (friend) { + friend[property] = formatter(friend[property]); - return collection; + return friend; + }); }; }; From bbc66096e20a9b09c177049825d10d3b0dbe1a55 Mon Sep 17 00:00:00 2001 From: Uliana1997 Date: Wed, 2 Nov 2016 22:57:33 +0600 Subject: [PATCH 06/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ( --- lego.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index 2104b4e..8d59c13 100644 --- a/lego.js +++ b/lego.js @@ -68,8 +68,8 @@ exports.sortBy = function (property, order) { } } else { collection.sort(function (one, another) { - var x = one.name.toLowerCase(); - var y = another.name.toLowerCase(); + var x = one.name; + var y = another.name; if (x < y) { return -1; } From 5fed378872239d680c26c50f017ba9e4d34cb9d3 Mon Sep 17 00:00:00 2001 From: Uliana1997 Date: Wed, 2 Nov 2016 23:30:41 +0600 Subject: [PATCH 07/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit last --- lego.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 8d59c13..df84ab1 100644 --- a/lego.js +++ b/lego.js @@ -14,8 +14,16 @@ exports.query = function (collection) { var newCollection = copyCollections(collection); var fields = Array.prototype.slice.call(arguments).slice(1); function compare(one, another) { + var x = priority[one.name]; + var y = priority[another.name]; + if (x < y) { + return -1; + } + if (x > y) { + return 1; + } - return priority[one.name] - priority[another.name]; + return 0; } fields.sort(compare); fields.forEach(function (func) { From 69b2a4a4b4ca77cad992f4502b46fccc08146ea6 Mon Sep 17 00:00:00 2001 From: Uliana1997 Date: Wed, 2 Nov 2016 23:38:06 +0600 Subject: [PATCH 08/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit laaaast check --- lego.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/lego.js b/lego.js index df84ab1..dcea0b3 100644 --- a/lego.js +++ b/lego.js @@ -74,19 +74,35 @@ exports.sortBy = function (property, order) { } else { collection.sort(comparedesc); } - } else { - collection.sort(function (one, another) { - var x = one.name; - var y = another.name; - if (x < y) { - return -1; - } - if (x > y) { - return 1; - } - - return 0; - }); + } + if (property === 'desc') { + if (order === 'asc') { + collection.sort(function (one, another) { + var x = one.name; + var y = another.name; + if (x < y) { + return -1; + } + if (x > y) { + return 1; + } + + return 0; + }); + } else { + collection.sort(function (one, another) { + var x = one.name; + var y = another.name; + if (x < y) { + return 1; + } + if (x > y) { + return -1; + } + + return 0; + }); + } } return collection; From e4743a7cd9a1d19b3c66fe220185b5f2975be964 Mon Sep 17 00:00:00 2001 From: Uliana1997 Date: Wed, 2 Nov 2016 23:40:20 +0600 Subject: [PATCH 09/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=83=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit check --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index dcea0b3..3697c76 100644 --- a/lego.js +++ b/lego.js @@ -75,7 +75,7 @@ exports.sortBy = function (property, order) { collection.sort(comparedesc); } } - if (property === 'desc') { + if (property !== 'age') { if (order === 'asc') { collection.sort(function (one, another) { var x = one.name; From e04b0427a15f3e661d693f659ccbab81ebfcd108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Sun, 6 Nov 2016 20:59:37 +0500 Subject: [PATCH 10/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 72 ++++++++++++++------------------------------------------- 1 file changed, 17 insertions(+), 55 deletions(-) diff --git a/lego.js b/lego.js index 3697c76..34edc15 100644 --- a/lego.js +++ b/lego.js @@ -3,27 +3,21 @@ exports.isStar = false; var priority = { - 'filterIn': 1, - 'sortBy': 2, - 'select': 3, - 'limit': 4, - 'format': 5 + filterIn: 1, + sortBy: 2, + select: 3, + limit: 4, + format: 5 }; exports.query = function (collection) { - var newCollection = copyCollections(collection); + var newCollection = copyCollection(collection); var fields = Array.prototype.slice.call(arguments).slice(1); function compare(one, another) { var x = priority[one.name]; var y = priority[another.name]; - if (x < y) { - return -1; - } - if (x > y) { - return 1; - } - return 0; + return x - y; } fields.sort(compare); fields.forEach(function (func) { @@ -33,7 +27,7 @@ exports.query = function (collection) { return newCollection; }; -function copyCollections(collection) { +function copyCollection(collection) { return (collection.map(function (friend) { @@ -65,44 +59,19 @@ exports.sortBy = function (property, order) { function compare(one, another) { return one[property] - another[property]; } - function comparedesc(one, another) { - return another[property] - one[property]; - } if (property === 'age') { if (order === 'asc') { collection.sort(compare); } else { - collection.sort(comparedesc); + collection.sort(compare).reverse(); } } if (property !== 'age') { - if (order === 'asc') { - collection.sort(function (one, another) { - var x = one.name; - var y = another.name; - if (x < y) { - return -1; - } - if (x > y) { - return 1; - } - - return 0; - }); - } else { - collection.sort(function (one, another) { - var x = one.name; - var y = another.name; - if (x < y) { - return 1; - } - if (x > y) { - return -1; - } - - return 0; - }); - } + var reverse = (order === 'asc') ? 1 : -1; + + return function (one, another) { + return reverse * ((one > another) - (another > one)); + }; } return collection; @@ -117,17 +86,10 @@ exports.limit = function (count) { 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 collection.filter(function (friend) { + return (friend.hasOwnProperty(property) && + values.indexOf(friend[property]) !== -1); }); - - return newSorted; }; }; From b139d4d2258820414bd4ecac00cf251f642389a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Sun, 6 Nov 2016 21:02:04 +0500 Subject: [PATCH 11/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/lego.js b/lego.js index 34edc15..0ccf771 100644 --- a/lego.js +++ b/lego.js @@ -63,15 +63,37 @@ exports.sortBy = function (property, order) { if (order === 'asc') { collection.sort(compare); } else { - collection.sort(compare).reverse(); + collection.sort(compare).reverse; } } if (property !== 'age') { - var reverse = (order === 'asc') ? 1 : -1; - - return function (one, another) { - return reverse * ((one > another) - (another > one)); - }; + if (order === 'asc') { + collection.sort(function (one, another) { + var x = one.name; + var y = another.name; + if (x < y) { + return -1; + } + if (x > y) { + return 1; + } + + return 0; + }); + } else { + collection.sort(function (one, another) { + var x = one.name; + var y = another.name; + if (x < y) { + return 1; + } + if (x > y) { + return -1; + } + + return 0; + }); + } } return collection; From c9800678a7fec7ec8a56f9748d3dbf748df7e2c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Sun, 6 Nov 2016 21:03:17 +0500 Subject: [PATCH 12/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 0ccf771..efe71aa 100644 --- a/lego.js +++ b/lego.js @@ -63,7 +63,7 @@ exports.sortBy = function (property, order) { if (order === 'asc') { collection.sort(compare); } else { - collection.sort(compare).reverse; + collection.sort(compare).reverse(); } } if (property !== 'age') { From 319c9fcfd6c24cd49962c4814458fdfe3afbbe85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Sun, 6 Nov 2016 21:35:43 +0500 Subject: [PATCH 13/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sort --- lego.js | 66 ++++++++++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/lego.js b/lego.js index efe71aa..42270d7 100644 --- a/lego.js +++ b/lego.js @@ -12,15 +12,15 @@ var priority = { exports.query = function (collection) { var newCollection = copyCollection(collection); - var fields = Array.prototype.slice.call(arguments).slice(1); + var functions = Array.prototype.slice.call(arguments).slice(1); function compare(one, another) { var x = priority[one.name]; var y = priority[another.name]; return x - y; } - fields.sort(compare); - fields.forEach(function (func) { + functions.sort(compare); + functions.forEach(function (func) { newCollection = func(newCollection); }); @@ -56,44 +56,28 @@ exports.select = function () { exports.sortBy = function (property, order) { return function sortBy(collection) { - function compare(one, another) { - return one[property] - another[property]; - } - if (property === 'age') { - if (order === 'asc') { - collection.sort(compare); - } else { - collection.sort(compare).reverse(); - } - } - if (property !== 'age') { - if (order === 'asc') { - collection.sort(function (one, another) { - var x = one.name; - var y = another.name; - if (x < y) { - return -1; - } - if (x > y) { - return 1; - } - - return 0; - }); - } else { - collection.sort(function (one, another) { - var x = one.name; - var y = another.name; - if (x < y) { - return 1; - } - if (x > y) { - return -1; - } - - return 0; - }); - } + if (order === 'asc') { + collection.sort (function (one, another) { + if (one[property] < another[property]) { + return -1; + } + if (one[property] > another[property]) { + return 1; + } + + return 0; + }); + } else { + collection.sort (function (one, another) { + if (one[property] < another[property]) { + return 1; + } + if (one[property] > another[property]) { + return -1; + } + + return 0; + }); } return collection; From f1b443b0fa761da14e66c42d8101336dd3bc5b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Thu, 10 Nov 2016 13:09:19 +0500 Subject: [PATCH 14/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 62 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/lego.js b/lego.js index 42270d7..9338e26 100644 --- a/lego.js +++ b/lego.js @@ -2,6 +2,7 @@ exports.isStar = false; +// Чем больше число тем ниже приоритет функции var priority = { filterIn: 1, sortBy: 2, @@ -39,45 +40,36 @@ exports.select = function () { var fields = Array.prototype.slice.call(arguments); return function select(collection) { - var result = []; - collection.forEach(function (friend) { + + collection.map(function (friend) { var newFriend = {}; for (var field in friend) { if (fields.indexOf(field) !== -1) { newFriend[field] = friend[field]; } } - result.push(newFriend); - }); - return result; + return newFriend; + }); }; }; exports.sortBy = function (property, order) { return function sortBy(collection) { - if (order === 'asc') { - collection.sort (function (one, another) { - if (one[property] < another[property]) { - return -1; - } - if (one[property] > another[property]) { - return 1; - } + function compare(one, another) { + if (one[property] > another[property]) { - return 0; - }); - } else { - collection.sort (function (one, another) { - if (one[property] < another[property]) { - return 1; - } - if (one[property] > another[property]) { - return -1; - } + return 1; + } else { - return 0; - }); + return -1; + } + } + + if (order === 'asc') { + collection.sort(compare); + } else { + collection.sort(compare).reverse(); } return collection; @@ -92,13 +84,27 @@ exports.limit = function (count) { exports.filterIn = function (property, values) { return function filterIn(collection) { - return collection.filter(function (friend) { - return (friend.hasOwnProperty(property) && - values.indexOf(friend[property]) !== -1); + 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; }; }; +function filter_(property, field, friend) { + if (property === field.toString() && + values.indexOf(friend[field]) !== -1) { + return true; + } +} + exports.format = function (property, formatter) { return function format(collection) { return collection.map(function (friend) { From b0bb7b4d92fa2edfa4d62e9bce65fc11ce0bf761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Thu, 10 Nov 2016 13:15:30 +0500 Subject: [PATCH 15/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/lego.js b/lego.js index 9338e26..ae725ac 100644 --- a/lego.js +++ b/lego.js @@ -41,7 +41,7 @@ exports.select = function () { return function select(collection) { - collection.map(function (friend) { + return collection.map(function (friend) { var newFriend = {}; for (var field in friend) { if (fields.indexOf(field) !== -1) { @@ -57,13 +57,7 @@ exports.select = function () { exports.sortBy = function (property, order) { return function sortBy(collection) { function compare(one, another) { - if (one[property] > another[property]) { - - return 1; - } else { - - return -1; - } + return (one[property] > another[property]) ? 1 : -1; } if (order === 'asc') { @@ -98,13 +92,6 @@ exports.filterIn = function (property, values) { }; }; -function filter_(property, field, friend) { - if (property === field.toString() && - values.indexOf(friend[field]) !== -1) { - return true; - } -} - exports.format = function (property, formatter) { return function format(collection) { return collection.map(function (friend) { From 799aac8abfae0ecbb41a53e5e729238f05b4e73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Thu, 10 Nov 2016 20:56:03 +0500 Subject: [PATCH 16/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lego.js b/lego.js index ae725ac..09c4f62 100644 --- a/lego.js +++ b/lego.js @@ -41,16 +41,18 @@ exports.select = function () { return function select(collection) { - return collection.map(function (friend) { - var newFriend = {}; - for (var field in friend) { + return collection.reduce(function (prev, curr) { + var friend = {}; + for (var field in curr) { if (fields.indexOf(field) !== -1) { - newFriend[field] = friend[field]; + friend[field] = curr[field]; } } + prev.push(friend); + + return prev; + }, []); - return newFriend; - }); }; }; From 80da5101fe30f08895a3fb13fe167eb6e0d3731b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Sat, 12 Nov 2016 16:59:57 +0600 Subject: [PATCH 17/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lego.js b/lego.js index 09c4f62..e02be2e 100644 --- a/lego.js +++ b/lego.js @@ -15,14 +15,11 @@ exports.query = function (collection) { var newCollection = copyCollection(collection); var functions = Array.prototype.slice.call(arguments).slice(1); function compare(one, another) { - var x = priority[one.name]; - var y = priority[another.name]; - - return x - y; + return priority[one.name] - priority[another.name]; } functions.sort(compare); - functions.forEach(function (func) { - newCollection = func(newCollection); + functions.forEach(function (each_query) { + newCollection = each_query(newCollection); }); return newCollection; From f0e810c3e4f04606dbdef4e8fdf1e39867a1944c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Sat, 12 Nov 2016 17:04:26 +0600 Subject: [PATCH 18/18] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=A3=D0=BB=D1=8C=D1=8F=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index e02be2e..845e6c5 100644 --- a/lego.js +++ b/lego.js @@ -18,8 +18,8 @@ exports.query = function (collection) { return priority[one.name] - priority[another.name]; } functions.sort(compare); - functions.forEach(function (each_query) { - newCollection = each_query(newCollection); + functions.forEach(function (eachQuery) { + newCollection = eachQuery(newCollection); }); return newCollection;