From bafc611450a4d345bacb7a0f625cd6972947878f Mon Sep 17 00:00:00 2001 From: Zeace Date: 2016年10月30日 22:21:17 +0500 Subject: [PATCH 1/6] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=B7=D0=B0=D1=85=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 151 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 104 insertions(+), 47 deletions(-) diff --git a/lego.js b/lego.js index b220745..b38320a 100644 --- a/lego.js +++ b/lego.js @@ -6,84 +6,141 @@ */ exports.isStar = true; -/** - * Запрос к коллекции - * @param {Array} collection - * @params {...Function} – Функции для запроса - * @returns {Array} - */ + exports.query = function (collection) { - return collection; + var copy = collection.map(function (entry) { + return Object.assign({}, entry); + }); + var operators = [].slice.call(arguments, 1); + operators.sort(sortByPriority); + for (var i = 0; i < operators.length; i++) { + copy = operators[i](copy); + } + + return copy; }; -/** - * Выбор полей - * @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) { + var copy = collection.slice(); + + return copy.map(function (entry) { + + return selectByParam(params, entry); + }); + }; }; -/** - * Фильтрация поля по массиву значений - * @param {String} property – Свойство для фильтрации - * @param {Array} values – Доступные значения - */ +function selectByParam(params, entry) { + var keys = Object.keys(entry); + var newEntry = {}; + keys.forEach(function (key) { + if (params.indexOf(key) !== -1) { + newEntry[key] = entry[key]; + } + }); + + return newEntry; +} + exports.filterIn = function (property, values) { console.info(property, values); - return; + return function filterIn(collection) { + var copy = collection.slice(); + + return copy.filter(function (entry) { + + return checkPropAndValues(entry, property, values); + }); + }; }; -/** - * Сортировка коллекции по полю - * @param {String} property – Свойство для фильтрации - * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) - */ +function checkPropAndValues(entry, property, values) { + for (var i = 0; i < values.length; i++) { + if (entry[property] === values[i]) { + return true; + } + } + + return false; +} + exports.sortBy = function (property, order) { console.info(property, order); - return; + return function sortBy(collection) { + var copy = collection.slice(); + + return copy.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) { + var copy = collection.slice(); + + return copy.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) { + var copy = collection.slice(); + + return copy.filter(function (item) { + return filters.some(function (filter) { + return filter(copy).indexOf(item) !== -1; + }); + }); + }; }; - /** - * Фильтрация, пересекающая фильтрующие функции - * @star - * @params {...Function} – Фильтрующие функции - */ exports.and = function () { - return; + var filters = [].slice.call(arguments); + + return function and(collection) { + var copy = collection.slice(); + filters.forEach(function (filter) { + copy = filter(copy); + }); + + return copy; + }; }; } From 46ff63bb53201c40064e67264098389ecefa95ff Mon Sep 17 00:00:00 2001 From: Zeace Date: Wed, 2 Nov 2016 14:38:30 +0500 Subject: [PATCH 2/6] =?UTF-8?q?=D1=80=D0=B0=D1=81=D1=81=D1=82=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BF=D0=BE=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=81=D1=82=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 28 +++++++++++++--------------- lego.js | 55 ++++++++++++++++++++++--------------------------------- 2 files changed, 35 insertions(+), 48 deletions(-) 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 b38320a..9d9e2f2 100644 --- a/lego.js +++ b/lego.js @@ -8,16 +8,16 @@ exports.isStar = true; exports.query = function (collection) { - var copy = collection.map(function (entry) { + var copyOfCollection = collection.map(function (entry) { return Object.assign({}, entry); }); var operators = [].slice.call(arguments, 1); operators.sort(sortByPriority); - for (var i = 0; i < operators.length; i++) { - copy = operators[i](copy); - } + copyOfCollection = operators.reduce(function (collect, opepator) { + return opepator(collect); + }, copyOfCollection); - return copy; + return copyOfCollection; }; function sortByPriority(a, b) { @@ -31,10 +31,9 @@ exports.select = function () { var params = [].slice.call(arguments); return function select(collection) { - var copy = collection.slice(); - - return copy.map(function (entry) { + var copyOfCollection = collection.slice(); + return copyOfCollection.map(function (entry) { return selectByParam(params, entry); }); }; @@ -56,34 +55,25 @@ exports.filterIn = function (property, values) { console.info(property, values); return function filterIn(collection) { - var copy = collection.slice(); - - return copy.filter(function (entry) { + var copyOfCollection = collection.slice(); - return checkPropAndValues(entry, property, values); + return copyOfCollection.filter(function (entry) { + return values.some(function (value) { + return entry[property] === value; + }); }); }; }; -function checkPropAndValues(entry, property, values) { - for (var i = 0; i < values.length; i++) { - if (entry[property] === values[i]) { - return true; - } - } - - return false; -} exports.sortBy = function (property, order) { console.info(property, order); return function sortBy(collection) { - var copy = collection.slice(); + var copyOfCollection = collection.slice(); - return copy.sort(function (entryOne, entryTwo) { + return copyOfCollection.sort(function (entryOne, entryTwo) { if (order === 'asc') { - return entryOne[property]> entryTwo[property]; } @@ -96,9 +86,9 @@ exports.format = function (property, formatter) { console.info(property, formatter); return function format(collection) { - var copy = collection.slice(); + var copyOfCollection = collection.slice(); - return copy.map(function (entry) { + return copyOfCollection.map(function (entry) { entry[property] = formatter(entry[property]); return entry; @@ -110,7 +100,6 @@ exports.limit = function (count) { console.info(count); return function limit(collection) { - return collection.slice(0, count); }; }; @@ -121,11 +110,11 @@ if (exports.isStar) { var filters = [].slice.call(arguments); return function or(collection) { - var copy = collection.slice(); + var copyOfCollection = collection.slice(); - return copy.filter(function (item) { + return copyOfCollection.filter(function (item) { return filters.some(function (filter) { - return filter(copy).indexOf(item) !== -1; + return filter(copyOfCollection).indexOf(item) !== -1; }); }); }; @@ -135,12 +124,12 @@ if (exports.isStar) { var filters = [].slice.call(arguments); return function and(collection) { - var copy = collection.slice(); + var copyOfCollection = collection.slice(); filters.forEach(function (filter) { - copy = filter(copy); + copyOfCollection = filter(copyOfCollection); }); - return copy; + return copyOfCollection; }; }; } From 37b96ed2cf7328969d1dd222144d84b5915a797c Mon Sep 17 00:00:00 2001 From: Zeace Date: 2016年11月10日 10:05:40 +0500 Subject: [PATCH 3/6] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B5=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lego.js b/lego.js index 9d9e2f2..3533677 100644 --- a/lego.js +++ b/lego.js @@ -40,12 +40,9 @@ exports.select = function () { }; function selectByParam(params, entry) { - var keys = Object.keys(entry); var newEntry = {}; - keys.forEach(function (key) { - if (params.indexOf(key) !== -1) { - newEntry[key] = entry[key]; - } + params.forEach(function (param) { + newEntry[param] = entry[param]; }); return newEntry; From d3fefa68171b3a36c863a565978e5a5f796d0700 Mon Sep 17 00:00:00 2001 From: Zeace Date: 2016年11月10日 10:10:01 +0500 Subject: [PATCH 4/6] =?UTF-8?q?+=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 3533677..c092495 100644 --- a/lego.js +++ b/lego.js @@ -42,7 +42,9 @@ exports.select = function () { function selectByParam(params, entry) { var newEntry = {}; params.forEach(function (param) { - newEntry[param] = entry[param]; + if (entry[param]) { + newEntry[param] = entry[param]; + } }); return newEntry; From 42880085c0ef2409ae0b0858fae2df8902a8e62d Mon Sep 17 00:00:00 2001 From: Zeace Date: 2016年11月10日 10:13:57 +0500 Subject: [PATCH 5/6] =?UTF-8?q?=D1=83=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BF=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B2=20=D0=BC=D0=B5=D1=81=D1=82=D0=B0=D1=85=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lego.js b/lego.js index c092495..ac34b55 100644 --- a/lego.js +++ b/lego.js @@ -31,9 +31,7 @@ exports.select = function () { var params = [].slice.call(arguments); return function select(collection) { - var copyOfCollection = collection.slice(); - - return copyOfCollection.map(function (entry) { + return collection.slice().map(function (entry) { return selectByParam(params, entry); }); }; @@ -85,9 +83,7 @@ exports.format = function (property, formatter) { console.info(property, formatter); return function format(collection) { - var copyOfCollection = collection.slice(); - - return copyOfCollection.map(function (entry) { + return collection.slice().map(function (entry) { entry[property] = formatter(entry[property]); return entry; From 6ba6d522b7bda616bed26d4441d2c1d72e0e5f37 Mon Sep 17 00:00:00 2001 From: Zeace Date: 2016年11月10日 13:21:23 +0500 Subject: [PATCH 6/6] =?UTF-8?q?=D0=B8=D0=B7=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D1=81=D1=8F=20=D0=BE=D1=82=20=D0=BD=D0=B5=D0=BD=D1=83=D0=B6?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D0=BA=D0=BE=D0=BF=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lego.js b/lego.js index ac34b55..78ed05e 100644 --- a/lego.js +++ b/lego.js @@ -52,9 +52,7 @@ exports.filterIn = function (property, values) { console.info(property, values); return function filterIn(collection) { - var copyOfCollection = collection.slice(); - - return copyOfCollection.filter(function (entry) { + return collection.slice().filter(function (entry) { return values.some(function (value) { return entry[property] === value; }); @@ -67,9 +65,7 @@ exports.sortBy = function (property, order) { console.info(property, order); return function sortBy(collection) { - var copyOfCollection = collection.slice(); - - return copyOfCollection.sort(function (entryOne, entryTwo) { + return collection.slice().sort(function (entryOne, entryTwo) { if (order === 'asc') { return entryOne[property]> entryTwo[property]; } @@ -105,11 +101,9 @@ if (exports.isStar) { var filters = [].slice.call(arguments); return function or(collection) { - var copyOfCollection = collection.slice(); - - return copyOfCollection.filter(function (item) { + return collection.slice().filter(function (item) { return filters.some(function (filter) { - return filter(copyOfCollection).indexOf(item) !== -1; + return filter(collection.slice()).indexOf(item) !== -1; }); }); };

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