From d7abc05a00da2973a9b5e851caceab31dae72339 Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Wed, 2 Nov 2016 22:27:52 +0500 Subject: [PATCH 01/12] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 99 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 34 deletions(-) diff --git a/lego.js b/lego.js index b220745..ac492c7 100644 --- a/lego.js +++ b/lego.js @@ -4,7 +4,15 @@ * Сделано задание на звездочку * Реализованы методы or и and */ -exports.isStar = true; +exports.isStar = false; + +var PRIORITET = ['filterIn', 'sortBy', 'select', 'limit', 'format']; + +function copyCollection(array) { + return array.map(function (element) { + return Object.assign({}, element); + }); +} /** * Запрос к коллекции @@ -13,77 +21,100 @@ exports.isStar = true; * @returns {Array} */ exports.query = function (collection) { - return collection; + var copy = copyCollection(collection); + var func = [].slice.call(arguments, 1); + func.sort(function (a, b) { + return PRIORITET.indexOf(a.name) - PRIORITET.indexOf(b.name); + }); + for (var i = 1; i < func.length; i++) { + copy = func[i](copy); + } + + return copy; }; /** * Выбор полей * @params {...String} + * @returns {Function} */ exports.select = function () { - return; + var fieldsCollection = [].slice.call(arguments); + + return function select(collection) { + return collection.map(function (element) { + return fieldsCollection.reduce(function (newCollection, field) { + if (element.hasOwnProperty(field)) { + newCollection[field] = element[field]; + } + + return newCollection; + }, {}); + }); + }; }; /** * Фильтрация поля по массиву значений * @param {String} property – Свойство для фильтрации * @param {Array} values – Доступные значения + * @returns {Function} */ exports.filterIn = function (property, values) { - console.info(property, values); - - return; + return function filterIn(collection) { + return collection.filter(function (note) { + return values.indexOf(note[property]) >= 0; + }); + }; }; /** * Сортировка коллекции по полю * @param {String} property – Свойство для фильтрации * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) + * @returns {Function} */ exports.sortBy = function (property, order) { - console.info(property, order); + return function sortBy(collection) { + var newCollection = copyCollection(collection); - return; + return newCollection.sort(function (first, second) { + if (first[property] === second[property]) { + return 0; + } + if (first[property] > second[property]) { + return order === 'asc' ? 1 : -1; + } + }); + }; }; /** * Форматирование поля * @param {String} property – Свойство для фильтрации * @param {Function} formatter – Функция для форматирования + * @returns {Function} */ exports.format = function (property, formatter) { - console.info(property, formatter); + return function format(collection) { + return collection.map(function (element) { + var copy = Object.assign({}, element); + if (property in element) { + copy[property] = formatter(copy[property]); + } - return; + return copy; + }); + }; }; /** * Ограничение количества элементов в коллекции * @param {Number} count – Максимальное количество элементов + * @returns {Function} */ exports.limit = function (count) { - console.info(count); - - return; -}; - -if (exports.isStar) { - - /** - * Фильтрация, объединяющая фильтрующие функции - * @star - * @params {...Function} – Фильтрующие функции - */ - exports.or = function () { - return; + return function limit(collection) { + return collection.slice(0, count); }; - - /** - * Фильтрация, пересекающая фильтрующие функции - * @star - * @params {...Function} – Фильтрующие функции - */ - exports.and = function () { - return; - }; -} +}; From 046870b456e1d474bb0d2a49e438af469772e350 Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Wed, 2 Nov 2016 23:14:55 +0500 Subject: [PATCH 02/12] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20sortBy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lego.js b/lego.js index ac492c7..b3ae48e 100644 --- a/lego.js +++ b/lego.js @@ -79,12 +79,11 @@ exports.sortBy = function (property, order) { var newCollection = copyCollection(collection); return newCollection.sort(function (first, second) { - if (first[property] === second[property]) { - return 0; - } - if (first[property] > second[property]) { - return order === 'asc' ? 1 : -1; + if (order === 'asc') { + return first[property] > second[property]; } + + return first[property] < second[property]; }); }; }; From 26a375d4a75d57ff2679fb098633b08116a3605b Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Wed, 2 Nov 2016 23:27:24 +0500 Subject: [PATCH 03/12] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lego.js b/lego.js index b3ae48e..835bd54 100644 --- a/lego.js +++ b/lego.js @@ -25,10 +25,10 @@ exports.query = function (collection) { var func = [].slice.call(arguments, 1); func.sort(function (a, b) { return PRIORITET.indexOf(a.name) - PRIORITET.indexOf(b.name); + }) + .forEach (function (query) { + copy = query(copy); }); - for (var i = 1; i < func.length; i++) { - copy = func[i](copy); - } return copy; }; From 2d47144694094bb5350edc03d3f1e461f91b21d4 Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Sat, 5 Nov 2016 17:05:59 +0500 Subject: [PATCH 04/12] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=D0=B0=20=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=B8=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lego.js b/lego.js index 835bd54..2a3261a 100644 --- a/lego.js +++ b/lego.js @@ -6,10 +6,10 @@ */ exports.isStar = false; -var PRIORITET = ['filterIn', 'sortBy', 'select', 'limit', 'format']; +var priorityFunctions = ['filterIn', 'sortBy', 'select', 'limit', 'format']; -function copyCollection(array) { - return array.map(function (element) { +function getCopyCollection(collection) { + return collection.map(function (element) { return Object.assign({}, element); }); } @@ -21,16 +21,16 @@ function copyCollection(array) { * @returns {Array} */ exports.query = function (collection) { - var copy = copyCollection(collection); - var func = [].slice.call(arguments, 1); - func.sort(function (a, b) { - return PRIORITET.indexOf(a.name) - PRIORITET.indexOf(b.name); + var copyCollection = getCopyCollection(collection); + var functions = [].slice.call(arguments, 1); + functions.sort(function (a, b) { + return priorityFunctions.indexOf(a.name) - priorityFunctions.indexOf(b.name); }) .forEach (function (query) { - copy = query(copy); + copyCollection = query(copyCollection); }); - return copy; + return copyCollection; }; /** @@ -38,6 +38,7 @@ exports.query = function (collection) { * @params {...String} * @returns {Function} */ + exports.select = function () { var fieldsCollection = [].slice.call(arguments); @@ -76,9 +77,9 @@ exports.filterIn = function (property, values) { */ exports.sortBy = function (property, order) { return function sortBy(collection) { - var newCollection = copyCollection(collection); + var copyCollection = getCopyCollection(collection); - return newCollection.sort(function (first, second) { + return copyCollection.sort(function (first, second) { if (order === 'asc') { return first[property] > second[property]; } @@ -97,12 +98,12 @@ exports.sortBy = function (property, order) { exports.format = function (property, formatter) { return function format(collection) { return collection.map(function (element) { - var copy = Object.assign({}, element); + var copyCollection = Object.assign({}, element); if (property in element) { - copy[property] = formatter(copy[property]); + copyCollection[property] = formatter(copyCollection[property]); } - return copy; + return copyCollection; }); }; }; From 331c0dfcc94344a771b2b149e921f3f148c9c4a0 Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Mon, 7 Nov 2016 19:39:21 +0500 Subject: [PATCH 05/12] update getCopyCollection, sortBy, format --- lego.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lego.js b/lego.js index 2a3261a..c5c03ed 100644 --- a/lego.js +++ b/lego.js @@ -9,8 +9,7 @@ exports.isStar = false; var priorityFunctions = ['filterIn', 'sortBy', 'select', 'limit', 'format']; function getCopyCollection(collection) { - return collection.map(function (element) { - return Object.assign({}, element); + return collection.slice(); }); } @@ -80,11 +79,8 @@ exports.sortBy = function (property, order) { var copyCollection = getCopyCollection(collection); return copyCollection.sort(function (first, second) { - if (order === 'asc') { - return first[property] > second[property]; - } - - return first[property] < second[property]; + var result = first[property] > second[property] ? 1: -1; + return order === 'asc'? result : -result; }); }; }; @@ -98,7 +94,7 @@ exports.sortBy = function (property, order) { exports.format = function (property, formatter) { return function format(collection) { return collection.map(function (element) { - var copyCollection = Object.assign({}, element); + var copyCollection = element.slice(); if (property in element) { copyCollection[property] = formatter(copyCollection[property]); } From d984d85d2b14ff2b6bd4aa1a748c0a68b9913f6f Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Mon, 7 Nov 2016 19:43:22 +0500 Subject: [PATCH 06/12] update getCopyCollection --- lego.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lego.js b/lego.js index c5c03ed..7109d4c 100644 --- a/lego.js +++ b/lego.js @@ -10,7 +10,6 @@ var priorityFunctions = ['filterIn', 'sortBy', 'select', 'limit', 'format']; function getCopyCollection(collection) { return collection.slice(); - }); } /** From d5093d7be6b1a355bd47a4818fb8883cd6f3c333 Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Mon, 7 Nov 2016 19:48:46 +0500 Subject: [PATCH 07/12] update sortBy --- lego.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index 7109d4c..99a3c8e 100644 --- a/lego.js +++ b/lego.js @@ -78,8 +78,9 @@ exports.sortBy = function (property, order) { var copyCollection = getCopyCollection(collection); return copyCollection.sort(function (first, second) { - var result = first[property] > second[property] ? 1: -1; - return order === 'asc'? result : -result; + var result = first[property] > second[property] ? 1 : -1; + + return order === 'asc' ? result : -result; }); }; }; From ee4b074681a6a6561efb8ac149f73cf6afe89479 Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Mon, 7 Nov 2016 19:57:04 +0500 Subject: [PATCH 08/12] Update lego.js --- lego.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 99a3c8e..9001bb4 100644 --- a/lego.js +++ b/lego.js @@ -78,7 +78,9 @@ exports.sortBy = function (property, order) { var copyCollection = getCopyCollection(collection); return copyCollection.sort(function (first, second) { - var result = first[property] > second[property] ? 1 : -1; + var oneProperty = first[property]; + var twoProperty = second[property]; + var result = oneProperty > twoProperty ? 1 : -1; return order === 'asc' ? result : -result; }); From d228c8bc08c346ea6edd905e2594d0345f0e8d0e Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Mon, 7 Nov 2016 20:26:42 +0500 Subject: [PATCH 09/12] Update lego.js --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 9001bb4..8a9aa7f 100644 --- a/lego.js +++ b/lego.js @@ -81,7 +81,7 @@ exports.sortBy = function (property, order) { var oneProperty = first[property]; var twoProperty = second[property]; var result = oneProperty > twoProperty ? 1 : -1; - + return order === 'asc' ? result : -result; }); }; From 7be0eba057f3fd8c7823def72232ace87ea4612e Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Mon, 7 Nov 2016 20:40:08 +0500 Subject: [PATCH 10/12] update format --- lego.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lego.js b/lego.js index 8a9aa7f..d536ff8 100644 --- a/lego.js +++ b/lego.js @@ -96,12 +96,11 @@ exports.sortBy = function (property, order) { exports.format = function (property, formatter) { return function format(collection) { return collection.map(function (element) { - var copyCollection = element.slice(); - if (property in element) { - copyCollection[property] = formatter(copyCollection[property]); + if (element.hasOwnProperty(property)) { + element[property] = formatter(element[property]); } - return copyCollection; + return element; }); }; }; From 9d416d274c2c0682592308dcdc64f7b65ae53a2a Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Mon, 7 Nov 2016 20:59:30 +0500 Subject: [PATCH 11/12] update format --- lego.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lego.js b/lego.js index d536ff8..b582633 100644 --- a/lego.js +++ b/lego.js @@ -95,10 +95,8 @@ exports.sortBy = function (property, order) { */ exports.format = function (property, formatter) { return function format(collection) { - return collection.map(function (element) { - if (element.hasOwnProperty(property)) { - element[property] = formatter(element[property]); - } + return collection.slice().map(function (element) { + element[property] = formatter(element[property]); return element; }); From 35e3d2e6a0cc8c7a6df012995917b46a401d00dc Mon Sep 17 00:00:00 2001 From: NovoselovaNatalia Date: Mon, 7 Nov 2016 21:15:34 +0500 Subject: [PATCH 12/12] update sortBy --- lego.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lego.js b/lego.js index b582633..3f5a183 100644 --- a/lego.js +++ b/lego.js @@ -78,11 +78,15 @@ exports.sortBy = function (property, order) { var copyCollection = getCopyCollection(collection); return copyCollection.sort(function (first, second) { - var oneProperty = first[property]; - var twoProperty = second[property]; - var result = oneProperty > twoProperty ? 1 : -1; - - return order === 'asc' ? result : -result; + var result = order === 'asc' ? 1 : -1; + if (first[property] > second[property]) { + return result; + } + if (first[property] > second[property]) { + return -result; + } + + return 0; }); }; };