From b8d4ef55c33c5b4607c139e2914597e86f0524db Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 13:41:07 +0500 Subject: [PATCH 01/33] initial --- index.html | 247 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 15 ++++ lego.js | 182 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 434 insertions(+), 10 deletions(-) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..8e0bc0e --- /dev/null +++ b/index.html @@ -0,0 +1,247 @@ + + + + + + + \ No newline at end of file diff --git a/index.js b/index.js index 9240afb..80b7f92 100644 --- a/index.js +++ b/index.js @@ -61,6 +61,8 @@ var friends = [ } ]; +var lego = exports; + // Находим настоящих друзей var bestFriends = lego.query( @@ -85,6 +87,19 @@ var bestFriends = lego.query( lego.limit(4) ); +var a = lego.query( + friends, + lego.or( + lego.filterIn('favoriteFruit', ['Яблоко']), + lego.filterIn('favoriteFruit', ['Картофель']) + )); + var b = lego.query( + friends, + lego.and( + lego.filterIn('gender', ['Мужской']), + lego.filterIn('favoriteFruit', ['Яблоко']) + )); + console.info(bestFriends); /* Выведет: diff --git a/lego.js b/lego.js index b220745..bb5ba6d 100644 --- a/lego.js +++ b/lego.js @@ -1,4 +1,11 @@ 'use strict'; +var QUERY_TYPES = { + SELECT: 2, + FILTER: 1, + SORT: 1, + LIMIT: 99, + FORMAT: 99 +}; /** * Сделано задание на звездочку @@ -6,6 +13,61 @@ */ exports.isStar = true; +function cloneCollection(collection) { + var cloned = []; + collection.forEach(function(item) { + var newItem = Object.assign({}, item); + cloned.push(newItem); + }); + + return cloned; +} +function getIntersection(listOfArrays) { + if (listOfArrays.length === 0) { + return []; + } + return listOfArrays[0].filter(function(item) { + var otherContains = true; + listOfArrays.slice(1).forEach(function(otherArray) { + if (otherArray.indexOf(item) === -1) { + otherContains = false; + + return; + } + }); + + return otherContains; + }); +} +function getUnion(listOfArrays) { + if (listOfArrays.length === 0) { + return []; + } + var united = listOfArrays[0]; + listOfArrays.splice(1).forEach(function(arrayItem) { + united.concat(arrayItem.filter(function(item) { + if (united.indexOf(item) === -1) { + united.push(item); + + return true; + } + + return false; + })); + }); + + return united; +} +function applyFiltersToCollection(filters, collection) { + var filteredCollections = []; + filters.forEach(function(filterQuery) { + var filtered = filterQuery.query(collection); + filteredCollections.push(filtered); + }); + + return filteredCollections; +} + /** * Запрос к коллекции * @param {Array} collection @@ -13,7 +75,28 @@ exports.isStar = true; * @returns {Array} */ exports.query = function (collection) { - return collection; + var collectionCopy = cloneCollection(collection); + var args = [].slice.call(arguments, 1); + var selectArguments = []; + args.forEach(function(queryFunction) { + if (queryFunction.queryType === QUERY_TYPES.SELECT) { + args.splice(args.indexOf(queryFunction), 1); + selectArguments.push(queryFunction.queryArguments); + + } + }); + if (selectArguments.length > 0) { + var selectArgumentsIntersection = getIntersection(selectArguments); + args.push(exports.select.apply(this, selectArgumentsIntersection)); + } + args.sort(function(a, b) { + return a.queryType - b.queryType; + }); + args.forEach(function(queryFunction) { + collectionCopy = queryFunction.query(collectionCopy); + }); + + return collectionCopy; }; /** @@ -21,7 +104,24 @@ exports.query = function (collection) { * @params {...String} */ exports.select = function () { - return; + var args = [].slice.call(arguments, 0);; + return { + queryType: QUERY_TYPES.SELECT, + queryArguments: args, + query: function(collection) { + var selected = []; + collection.forEach(function(item) { + var selectedItem = {}; + args.forEach(function(property) { + selectedItem[property] = item[property]; + }); + selected.push(selectedItem); + }); + + return selected; + } + }; + }; /** @@ -31,10 +131,23 @@ exports.select = function () { */ exports.filterIn = function (property, values) { console.info(property, values); - - return; + + return { + queryType: QUERY_TYPES.FILTER, + query: function(collection) { + var filtered = []; + collection.forEach(function(item) { + if (values.indexOf(item[property]) !== -1) { + filtered.push(item); + } + }); + + return filtered; + } + } }; + /** * Сортировка коллекции по полю * @param {String} property – Свойство для фильтрации @@ -42,8 +155,17 @@ exports.filterIn = function (property, values) { */ exports.sortBy = function (property, order) { console.info(property, order); + var ASCENDING = "asc"; + var DESCENDING = "des"; - return; + return { + queryType: QUERY_TYPES.SORT, + query: function(collection) { + return collection.sort(function(a, b) { + return (a[property] + "").localeCompare(b[property] + ""); + }, order === DESCENDING); + } + } }; /** @@ -53,8 +175,19 @@ exports.sortBy = function (property, order) { */ exports.format = function (property, formatter) { console.info(property, formatter); - - return; + return { + queryType: QUERY_TYPES.FORMAT, + query: function(collection) { + var formatted = []; + collection.forEach(function(item) { + var newItem = Object.assign({}, item); + newItem[property] = formatter(item[property]); + formatted.push(newItem); + }) + + return formatted; + } + } }; /** @@ -64,7 +197,17 @@ exports.format = function (property, formatter) { exports.limit = function (count) { console.info(count); - return; + return { + queryType: QUERY_TYPES.LIMIT, + query: function(collection) { + var limited = []; + for (var i = 0; i < count; i += 1) { + limited.push(collection[i]); + } + + return limited; + } + } }; if (exports.isStar) { @@ -75,7 +218,16 @@ if (exports.isStar) { * @params {...Function} – Фильтрующие функции */ exports.or = function () { - return; + var filters = [].slice.call(arguments, 0); + return { + queryType: QUERY_TYPES.FILTER, + query: function(collection) { + var filteredCollections = applyFiltersToCollection(filters, collection); + var united = getUnion(filteredCollections); + + return united; + } + } }; /** @@ -84,6 +236,16 @@ if (exports.isStar) { * @params {...Function} – Фильтрующие функции */ exports.and = function () { - return; + var filters = [].slice.call(arguments, 0); + + return { + queryType: QUERY_TYPES.FILTER, + query: function(collection) { + var filteredCollections = applyFiltersToCollection(filters, collection); + var intersection = getIntersection(filteredCollections); + + return intersection; + } + }; }; } From 4cb855a45acec40e52e6c874d613546fb4992961 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 14:07:51 +0500 Subject: [PATCH 02/33] lint --- index.html | 109 +++++++++++++++++++++++++++++++------------------- index.js | 15 ------- lego.js | 114 ++++++++++++++++++++++++++++++----------------------- 3 files changed, 132 insertions(+), 106 deletions(-) diff --git a/index.html b/index.html index 8e0bc0e..b3836fd 100644 --- a/index.html +++ b/index.html @@ -16,28 +16,31 @@ * Реализованы методы or и and */ exports.isStar = true; - + function cloneCollection(collection) { var cloned = []; - collection.forEach(function(item) { + collection.forEach(function (item) { var newItem = Object.assign({}, item); cloned.push(newItem); }); - + return cloned; } function getIntersection(listOfArrays) { if (listOfArrays.length === 0) { return []; } - return listOfArrays[0].filter(function(item) { + + return listOfArrays[0].filter(function (item) { var otherContains = true; - listOfArrays.slice(1).forEach(function(otherArray) { + listOfArrays.slice(1).forEach(function (otherArray) { if (otherArray.indexOf(item) === -1) { otherContains = false; + return; } }); + return otherContains; }); } @@ -46,23 +49,27 @@ return []; } var united = listOfArrays[0]; - listOfArrays.splice(1).forEach(function(arrayItem) { - united.concat(arrayItem.filter(function(item) { + listOfArrays.splice(1).forEach(function (arrayItem) { + united.concat(arrayItem.filter(function (item) { if (united.indexOf(item) === -1) { united.push(item); + return true; } + return false; })); }); + return united; } function applyFiltersToCollection(filters, collection) { var filteredCollections = []; - filters.forEach(function(filterQuery) { + filters.forEach(function (filterQuery) { var filtered = filterQuery.query(collection); filteredCollections.push(filtered); }); + return filteredCollections; } @@ -76,112 +83,126 @@ var collectionCopy = cloneCollection(collection); var args = [].slice.call(arguments, 1); var selectArguments = []; - args.forEach(function(queryFunction) { + args.forEach(function (queryFunction) { if (queryFunction.queryType === QUERY_TYPES.SELECT) { args.splice(args.indexOf(queryFunction), 1); selectArguments.push(queryFunction.queryArguments); - + } }); if (selectArguments.length > 0) { var selectArgumentsIntersection = getIntersection(selectArguments); args.push(exports.select.apply(this, selectArgumentsIntersection)); } - args.sort(function(a, b) { + args.sort(function (a, b) { return a.queryType - b.queryType; }); - args.forEach(function(queryFunction) { + args.forEach(function (queryFunction) { collectionCopy = queryFunction.query(collectionCopy); }); + return collectionCopy; }; /** * Выбор полей * @params {...String} + * @returns {Object} - query-object */ exports.select = function () { - var args = [].slice.call(arguments, 0);; + var args = [].slice.call(arguments, 0); + return { queryType: QUERY_TYPES.SELECT, queryArguments: args, - query: function(collection) { + query: function (collection) { var selected = []; - collection.forEach(function(item) { + collection.forEach(function (item) { var selectedItem = {}; - args.forEach(function(property) { - selectedItem[property] = item[property]; + args.forEach(function (property) { + if (item.hasOwnProperty(property)) { + selectedItem[property] = item[property]; + } }); selected.push(selectedItem); }); + return selected; } }; - + }; /** * Фильтрация поля по массиву значений * @param {String} property – Свойство для фильтрации * @param {Array} values – Доступные значения + * @returns {Object} - query-object */ exports.filterIn = function (property, values) { console.info(property, values); - + return { - queryType: QUERY_TYPES.FILTER, - query: function(collection) { + queryType: QUERY_TYPES.FILTER, + query: function (collection) { var filtered = []; collection.forEach(function(item) { if (values.indexOf(item[property]) !== -1) { filtered.push(item); } }); + return filtered; } - } + }; }; - + /** * Сортировка коллекции по полю * @param {String} property – Свойство для фильтрации * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) + * @returns {Object} - query-object */ exports.sortBy = function (property, order) { console.info(property, order); - var ASCENDING = "asc"; - var DESCENDING = "des"; - + var SORT_DIRECTION = { + ASCENDING: 'asc', + DESCENDING: 'des' + }; + return { queryType: QUERY_TYPES.SORT, - query: function(collection) { + query: function (collection) { return collection.sort(function(a, b) { - return (a[property] + "").localeCompare(b[property] + ""); - }, order === DESCENDING); + return (String(a[property])).localeCompare(String(b[property])); + }, order === SORT_DIRECTION.DESCENDING); } - } + }; }; /** * Форматирование поля * @param {String} property – Свойство для фильтрации * @param {Function} formatter – Функция для форматирования + * @returns {Object} - query-object */ exports.format = function (property, formatter) { console.info(property, formatter); + return { queryType: QUERY_TYPES.FORMAT, - query: function(collection) { + query: function (collection) { var formatted = []; - collection.forEach(function(item) { + collection.forEach (function(item) { var newItem = Object.assign({}, item); newItem[property] = formatter(item[property]); formatted.push(newItem); - }) + }); + return formatted; } - } + }; }; /** @@ -193,14 +214,15 @@ return { queryType: QUERY_TYPES.LIMIT, - query: function(collection) { + query: function (collection) { var limited = []; for (var i = 0; i < count; i += 1) { - limited.push(collection[i]); + limited.push(collection[i]); } + return limited; } - } + }; }; if (exports.isStar) { @@ -209,32 +231,37 @@ * Фильтрация, объединяющая фильтрующие функции * @star * @params {...Function} – Фильтрующие функции + * @returns {Object} - query-object */ exports.or = function () { var filters = [].slice.call(arguments, 0); + return { queryType: QUERY_TYPES.FILTER, - query: function(collection) { + query: function (collection) { var filteredCollections = applyFiltersToCollection(filters, collection); var united = getUnion(filteredCollections); + return united; } - } + }; }; /** * Фильтрация, пересекающая фильтрующие функции * @star * @params {...Function} – Фильтрующие функции + * @returns {Object} - query-object */ exports.and = function () { var filters = [].slice.call(arguments, 0); - + return { queryType: QUERY_TYPES.FILTER, - query: function(collection) { + query: function (collection) { var filteredCollections = applyFiltersToCollection(filters, collection); var intersection = getIntersection(filteredCollections); + return intersection; } }; diff --git a/index.js b/index.js index 80b7f92..9240afb 100644 --- a/index.js +++ b/index.js @@ -61,8 +61,6 @@ var friends = [ } ]; -var lego = exports; - // Находим настоящих друзей var bestFriends = lego.query( @@ -87,19 +85,6 @@ var bestFriends = lego.query( lego.limit(4) ); -var a = lego.query( - friends, - lego.or( - lego.filterIn('favoriteFruit', ['Яблоко']), - lego.filterIn('favoriteFruit', ['Картофель']) - )); - var b = lego.query( - friends, - lego.and( - lego.filterIn('gender', ['Мужской']), - lego.filterIn('favoriteFruit', ['Яблоко']) - )); - console.info(bestFriends); /* Выведет: diff --git a/lego.js b/lego.js index bb5ba6d..92c9a88 100644 --- a/lego.js +++ b/lego.js @@ -15,27 +15,28 @@ exports.isStar = true; function cloneCollection(collection) { var cloned = []; - collection.forEach(function(item) { + collection.forEach(function (item) { var newItem = Object.assign({}, item); cloned.push(newItem); }); - + return cloned; } function getIntersection(listOfArrays) { if (listOfArrays.length === 0) { return []; } - return listOfArrays[0].filter(function(item) { + + return listOfArrays[0].filter(function (item) { var otherContains = true; - listOfArrays.slice(1).forEach(function(otherArray) { + listOfArrays.slice(1).forEach(function (otherArray) { if (otherArray.indexOf(item) === -1) { otherContains = false; - + return; } }); - + return otherContains; }); } @@ -44,27 +45,27 @@ function getUnion(listOfArrays) { return []; } var united = listOfArrays[0]; - listOfArrays.splice(1).forEach(function(arrayItem) { - united.concat(arrayItem.filter(function(item) { + listOfArrays.splice(1).forEach(function (arrayItem) { + united.concat(arrayItem.filter(function (item) { if (united.indexOf(item) === -1) { united.push(item); - + return true; } - + return false; })); }); - + return united; } function applyFiltersToCollection(filters, collection) { var filteredCollections = []; - filters.forEach(function(filterQuery) { + filters.forEach(function (filterQuery) { var filtered = filterQuery.query(collection); filteredCollections.push(filtered); }); - + return filteredCollections; } @@ -78,73 +79,78 @@ exports.query = function (collection) { var collectionCopy = cloneCollection(collection); var args = [].slice.call(arguments, 1); var selectArguments = []; - args.forEach(function(queryFunction) { + args.forEach(function (queryFunction) { if (queryFunction.queryType === QUERY_TYPES.SELECT) { args.splice(args.indexOf(queryFunction), 1); selectArguments.push(queryFunction.queryArguments); - + } }); if (selectArguments.length > 0) { var selectArgumentsIntersection = getIntersection(selectArguments); args.push(exports.select.apply(this, selectArgumentsIntersection)); } - args.sort(function(a, b) { + args.sort(function (a, b) { return a.queryType - b.queryType; }); - args.forEach(function(queryFunction) { + args.forEach(function (queryFunction) { collectionCopy = queryFunction.query(collectionCopy); }); - + return collectionCopy; }; /** * Выбор полей * @params {...String} + * @returns {Object} - query-object */ exports.select = function () { - var args = [].slice.call(arguments, 0);; + var args = [].slice.call(arguments, 0); + return { queryType: QUERY_TYPES.SELECT, queryArguments: args, - query: function(collection) { + query: function (collection) { var selected = []; - collection.forEach(function(item) { + collection.forEach(function (item) { var selectedItem = {}; - args.forEach(function(property) { - selectedItem[property] = item[property]; + args.forEach(function (property) { + if (item.hasOwnProperty(property)) { + selectedItem[property] = item[property]; + } }); selected.push(selectedItem); }); - + return selected; } }; - + }; /** * Фильтрация поля по массиву значений * @param {String} property – Свойство для фильтрации * @param {Array} values – Доступные значения + * @returns {Object} - query-object */ exports.filterIn = function (property, values) { console.info(property, values); - + return { - queryType: QUERY_TYPES.FILTER, - query: function(collection) { + queryType: QUERY_TYPES.FILTER, + query: function (collection) { var filtered = []; collection.forEach(function(item) { if (values.indexOf(item[property]) !== -1) { filtered.push(item); } }); - + return filtered; } - } + }; }; @@ -152,42 +158,47 @@ exports.filterIn = function (property, values) { * Сортировка коллекции по полю * @param {String} property – Свойство для фильтрации * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) + * @returns {Object} - query-object */ exports.sortBy = function (property, order) { console.info(property, order); - var ASCENDING = "asc"; - var DESCENDING = "des"; + var SORT_DIRECTION = { + ASCENDING: 'asc', + DESCENDING: 'des' + }; return { queryType: QUERY_TYPES.SORT, - query: function(collection) { + query: function (collection) { return collection.sort(function(a, b) { - return (a[property] + "").localeCompare(b[property] + ""); - }, order === DESCENDING); + return (String(a[property])).localeCompare(String(b[property])); + }, order === SORT_DIRECTION.DESCENDING); } - } + }; }; /** * Форматирование поля * @param {String} property – Свойство для фильтрации * @param {Function} formatter – Функция для форматирования + * @returns {Object} - query-object */ exports.format = function (property, formatter) { console.info(property, formatter); + return { queryType: QUERY_TYPES.FORMAT, - query: function(collection) { + query: function (collection) { var formatted = []; - collection.forEach(function(item) { + collection.forEach (function(item) { var newItem = Object.assign({}, item); newItem[property] = formatter(item[property]); formatted.push(newItem); - }) - + }); + return formatted; } - } + }; }; /** @@ -199,15 +210,15 @@ exports.limit = function (count) { return { queryType: QUERY_TYPES.LIMIT, - query: function(collection) { + query: function (collection) { var limited = []; for (var i = 0; i < count; i += 1) { - limited.push(collection[i]); + limited.push(collection[i]); } - + return limited; } - } + }; }; if (exports.isStar) { @@ -216,34 +227,37 @@ if (exports.isStar) { * Фильтрация, объединяющая фильтрующие функции * @star * @params {...Function} – Фильтрующие функции + * @returns {Object} - query-object */ exports.or = function () { var filters = [].slice.call(arguments, 0); + return { queryType: QUERY_TYPES.FILTER, - query: function(collection) { + query: function (collection) { var filteredCollections = applyFiltersToCollection(filters, collection); var united = getUnion(filteredCollections); - + return united; } - } + }; }; /** * Фильтрация, пересекающая фильтрующие функции * @star * @params {...Function} – Фильтрующие функции + * @returns {Object} - query-object */ exports.and = function () { var filters = [].slice.call(arguments, 0); return { queryType: QUERY_TYPES.FILTER, - query: function(collection) { + query: function (collection) { var filteredCollections = applyFiltersToCollection(filters, collection); var intersection = getIntersection(filteredCollections); - + return intersection; } }; From 4400765150f803b6a9da38a1e11bd600de592683 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 14:12:38 +0500 Subject: [PATCH 03/33] linted --- lego.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lego.js b/lego.js index 92c9a88..54ee849 100644 --- a/lego.js +++ b/lego.js @@ -107,7 +107,7 @@ exports.query = function (collection) { */ exports.select = function () { var args = [].slice.call(arguments, 0); - + return { queryType: QUERY_TYPES.SELECT, queryArguments: args, @@ -142,7 +142,7 @@ exports.filterIn = function (property, values) { queryType: QUERY_TYPES.FILTER, query: function (collection) { var filtered = []; - collection.forEach(function(item) { + collection.forEach(function (item) { if (values.indexOf(item[property]) !== -1) { filtered.push(item); } @@ -170,7 +170,7 @@ exports.sortBy = function (property, order) { return { queryType: QUERY_TYPES.SORT, query: function (collection) { - return collection.sort(function(a, b) { + return collection.sort(function (a, b) { return (String(a[property])).localeCompare(String(b[property])); }, order === SORT_DIRECTION.DESCENDING); } @@ -190,7 +190,7 @@ exports.format = function (property, formatter) { queryType: QUERY_TYPES.FORMAT, query: function (collection) { var formatted = []; - collection.forEach (function(item) { + collection.forEach (function (item) { var newItem = Object.assign({}, item); newItem[property] = formatter(item[property]); formatted.push(newItem); @@ -204,6 +204,7 @@ exports.format = function (property, formatter) { /** * Ограничение количества элементов в коллекции * @param {Number} count – Максимальное количество элементов + * @returns {Object} - query-object */ exports.limit = function (count) { console.info(count); @@ -251,7 +252,7 @@ if (exports.isStar) { */ exports.and = function () { var filters = [].slice.call(arguments, 0); - + return { queryType: QUERY_TYPES.FILTER, query: function (collection) { From 03f3e22308c8435cb4c6b0a66b9b1df5873759ce Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 14:59:02 +0500 Subject: [PATCH 04/33] test16 --- index.html | 6 ++++-- lego.js | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index b3836fd..b0f2da2 100644 --- a/index.html +++ b/index.html @@ -83,13 +83,15 @@ var collectionCopy = cloneCollection(collection); var args = [].slice.call(arguments, 1); var selectArguments = []; + var nonSelectArgs = []; args.forEach(function (queryFunction) { if (queryFunction.queryType === QUERY_TYPES.SELECT) { - args.splice(args.indexOf(queryFunction), 1); selectArguments.push(queryFunction.queryArguments); - + } else { + nonSelectArgs.push(queryFunction); } }); + args = nonSelectArgs; if (selectArguments.length > 0) { var selectArgumentsIntersection = getIntersection(selectArguments); args.push(exports.select.apply(this, selectArgumentsIntersection)); diff --git a/lego.js b/lego.js index 54ee849..4cb863f 100644 --- a/lego.js +++ b/lego.js @@ -79,13 +79,15 @@ exports.query = function (collection) { var collectionCopy = cloneCollection(collection); var args = [].slice.call(arguments, 1); var selectArguments = []; + var nonSelectArgs = []; args.forEach(function (queryFunction) { if (queryFunction.queryType === QUERY_TYPES.SELECT) { - args.splice(args.indexOf(queryFunction), 1); selectArguments.push(queryFunction.queryArguments); - + } else { + nonSelectArgs.push(queryFunction); } }); + args = nonSelectArgs; if (selectArguments.length > 0) { var selectArgumentsIntersection = getIntersection(selectArguments); args.push(exports.select.apply(this, selectArgumentsIntersection)); From 6b12e3208f6d940bf3cb966b36b36d33d0351bdb Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 15:11:34 +0500 Subject: [PATCH 05/33] test --- lego.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 4cb863f..dc7273a 100644 --- a/lego.js +++ b/lego.js @@ -68,6 +68,14 @@ function applyFiltersToCollection(filters, collection) { return filteredCollections; } +function getCollectionCopy(old) { + var copy = []; + collection.forEach(function (item) { + copy.push(item); + }); + + return copy; +} /** * Запрос к коллекции @@ -172,7 +180,9 @@ exports.sortBy = function (property, order) { return { queryType: QUERY_TYPES.SORT, query: function (collection) { - return collection.sort(function (a, b) { + var newCollection = getCollectionCopy(collection); + + return newCollection.sort(function (a, b) { return (String(a[property])).localeCompare(String(b[property])); }, order === SORT_DIRECTION.DESCENDING); } From c82ff417644f29c0e70fd734de6c76834b5dbac7 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 15:13:24 +0500 Subject: [PATCH 06/33] lint --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index dc7273a..4d2e850 100644 --- a/lego.js +++ b/lego.js @@ -181,7 +181,7 @@ exports.sortBy = function (property, order) { queryType: QUERY_TYPES.SORT, query: function (collection) { var newCollection = getCollectionCopy(collection); - + return newCollection.sort(function (a, b) { return (String(a[property])).localeCompare(String(b[property])); }, order === SORT_DIRECTION.DESCENDING); From f522f4f36c11ba2beb40883bcecc0c6950a597f1 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 15:14:52 +0500 Subject: [PATCH 07/33] test --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 4d2e850..3a3ce87 100644 --- a/lego.js +++ b/lego.js @@ -68,7 +68,7 @@ function applyFiltersToCollection(filters, collection) { return filteredCollections; } -function getCollectionCopy(old) { +function getCollectionCopy(collection) { var copy = []; collection.forEach(function (item) { copy.push(item); From be7faabec5d1ae38a666353f7b109fb1d611e8fc Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 15:43:47 +0500 Subject: [PATCH 08/33] test16 --- index.html | 18 +++++++++++++++--- lego.js | 4 +++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index b0f2da2..f19a64e 100644 --- a/index.html +++ b/index.html @@ -72,6 +72,14 @@ return filteredCollections; } + function getCollectionCopy(collection) { + var copy = []; + collection.forEach(function (item) { + copy.push(item); + }); + + return copy; + } /** * Запрос к коллекции @@ -176,7 +184,9 @@ return { queryType: QUERY_TYPES.SORT, query: function (collection) { - return collection.sort(function(a, b) { + var newCollection = getCollectionCopy(collection); + + return newCollection.sort(function (a, b) { return (String(a[property])).localeCompare(String(b[property])); }, order === SORT_DIRECTION.DESCENDING); } @@ -197,8 +207,10 @@ query: function (collection) { var formatted = []; collection.forEach (function(item) { - var newItem = Object.assign({}, item); - newItem[property] = formatter(item[property]); + var newItem = Object.assign({}, item) + if (newItem.hasOwnProperty(property)) { + newItem[property] = formatter(item[property]); + } formatted.push(newItem); }); diff --git a/lego.js b/lego.js index 3a3ce87..bd6678f 100644 --- a/lego.js +++ b/lego.js @@ -204,7 +204,9 @@ exports.format = function (property, formatter) { var formatted = []; collection.forEach (function (item) { var newItem = Object.assign({}, item); - newItem[property] = formatter(item[property]); + if (newItem.hasOwnProperty(property)) { + newItem[property] = formatter(item[property]); + } formatted.push(newItem); }); From 8e1d77b876063b5c774cb5e53d0a924a49c065d6 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 16:06:17 +0500 Subject: [PATCH 09/33] tested --- index.html | 21 ++++++++++++++++++++- lego.js | 21 ++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index f19a64e..5fc48e7 100644 --- a/index.html +++ b/index.html @@ -80,6 +80,22 @@ return copy; } + function getCompatibleSelectArguments(collection, queryArguments) { + var compatibleArguments = []; + queryArguments.forEach(function (argument) { + var allHaveProperty = true; + collection.forEach(function (item) { + if (!item.hasOwnProperty(argument)) { + allHaveProperty = false; + } + }); + if (allHaveProperty) { + compatibleArguments.push(argument); + } + }); + + return compatibleArguments; + } /** * Запрос к коллекции @@ -94,7 +110,10 @@ var nonSelectArgs = []; args.forEach(function (queryFunction) { if (queryFunction.queryType === QUERY_TYPES.SELECT) { - selectArguments.push(queryFunction.queryArguments); + var compatibleArguments = getCompatibleSelectArguments(collection, queryFunction.queryArguments); + if (compatibleArguments.length > 0) { + selectArguments.push(compatibleArguments); + } } else { nonSelectArgs.push(queryFunction); } diff --git a/lego.js b/lego.js index bd6678f..b5eb6be 100644 --- a/lego.js +++ b/lego.js @@ -76,6 +76,22 @@ function getCollectionCopy(collection) { return copy; } +function getCompatibleSelectArguments(collection, queryArguments) { + var compatibleArguments = []; + queryArguments.forEach(function (argument) { + var allHaveProperty = true; + collection.forEach(function (item) { + if (!item.hasOwnProperty(argument)) { + allHaveProperty = false; + } + }); + if (allHaveProperty) { + compatibleArguments.push(argument); + } + }); + + return compatibleArguments; +} /** * Запрос к коллекции @@ -90,7 +106,10 @@ exports.query = function (collection) { var nonSelectArgs = []; args.forEach(function (queryFunction) { if (queryFunction.queryType === QUERY_TYPES.SELECT) { - selectArguments.push(queryFunction.queryArguments); + var compatibleArguments = getCompatibleSelectArguments(collection, queryFunction.queryArguments); + if (compatibleArguments.length > 0) { + selectArguments.push(compatibleArguments); + } } else { nonSelectArgs.push(queryFunction); } From c7c89977f6aaff223bafaab249323054099205d5 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 16:09:33 +0500 Subject: [PATCH 10/33] lint --- lego.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lego.js b/lego.js index b5eb6be..2d26756 100644 --- a/lego.js +++ b/lego.js @@ -106,10 +106,11 @@ exports.query = function (collection) { var nonSelectArgs = []; args.forEach(function (queryFunction) { if (queryFunction.queryType === QUERY_TYPES.SELECT) { - var compatibleArguments = getCompatibleSelectArguments(collection, queryFunction.queryArguments); - if (compatibleArguments.length > 0) { - selectArguments.push(compatibleArguments); - } + var compatibleArguments = getCompatibleSelectArguments( + collection, queryFunction.queryArguments); + if (compatibleArguments.length > 0) { + selectArguments.push(compatibleArguments); + } } else { nonSelectArgs.push(queryFunction); } From 10e325f05032a7e747e09a56eb1c2adbd34b770f Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 16:20:21 +0500 Subject: [PATCH 11/33] test16 --- index.html | 2 +- lego.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 5fc48e7..e931be6 100644 --- a/index.html +++ b/index.html @@ -176,7 +176,7 @@ query: function (collection) { var filtered = []; collection.forEach(function(item) { - if (values.indexOf(item[property]) !== -1) { + if (!item.hasOwnProperty(property) || values.indexOf(item[property]) !== -1) { filtered.push(item); } }); diff --git a/lego.js b/lego.js index 2d26756..1400999 100644 --- a/lego.js +++ b/lego.js @@ -173,7 +173,7 @@ exports.filterIn = function (property, values) { query: function (collection) { var filtered = []; collection.forEach(function (item) { - if (values.indexOf(item[property]) !== -1) { + if (!item.hasOwnProperty(property) || values.indexOf(item[property]) !== -1) { filtered.push(item); } }); From c7ed3c2712dc03db720353da3973507743ab52a7 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 16:29:58 +0500 Subject: [PATCH 12/33] test16 --- index.html | 3 +++ lego.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/index.html b/index.html index e931be6..ffc91e9 100644 --- a/index.html +++ b/index.html @@ -65,6 +65,9 @@ } function applyFiltersToCollection(filters, collection) { var filteredCollections = []; + if (filters.length === 0) { + return [getCollectionCopy(collection)]; + } filters.forEach(function (filterQuery) { var filtered = filterQuery.query(collection); filteredCollections.push(filtered); diff --git a/lego.js b/lego.js index 1400999..4039a07 100644 --- a/lego.js +++ b/lego.js @@ -61,6 +61,9 @@ function getUnion(listOfArrays) { } function applyFiltersToCollection(filters, collection) { var filteredCollections = []; + if (filters.length === 0) { + return [getCollectionCopy(collection)]; + } filters.forEach(function (filterQuery) { var filtered = filterQuery.query(collection); filteredCollections.push(filtered); From 09022578f07602752a852251df68ba51efcc810a Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 17:16:32 +0500 Subject: [PATCH 13/33] typo --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 4039a07..facf91a 100644 --- a/lego.js +++ b/lego.js @@ -197,7 +197,7 @@ exports.sortBy = function (property, order) { console.info(property, order); var SORT_DIRECTION = { ASCENDING: 'asc', - DESCENDING: 'des' + DESCENDING: 'desc' }; return { From cb72bebddd77a95672f4838551d9e8397747c172 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Tue, 1 Nov 2016 17:23:42 +0500 Subject: [PATCH 14/33] desc --- index.html | 7 ++++--- lego.js | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index ffc91e9..bc0b62f 100644 --- a/index.html +++ b/index.html @@ -200,17 +200,18 @@ console.info(property, order); var SORT_DIRECTION = { ASCENDING: 'asc', - DESCENDING: 'des' + DESCENDING: 'desc' }; return { queryType: QUERY_TYPES.SORT, query: function (collection) { var newCollection = getCollectionCopy(collection); + var orderMultiplyer = order === SORT_DIRECTION.DESCENDING ? -1 : 1; return newCollection.sort(function (a, b) { - return (String(a[property])).localeCompare(String(b[property])); - }, order === SORT_DIRECTION.DESCENDING); + return orderMultiplyer * (String(a[property])).localeCompare(String(b[property])); + }); } }; }; diff --git a/lego.js b/lego.js index facf91a..dc77259 100644 --- a/lego.js +++ b/lego.js @@ -204,10 +204,11 @@ exports.sortBy = function (property, order) { queryType: QUERY_TYPES.SORT, query: function (collection) { var newCollection = getCollectionCopy(collection); + var orderMultiplyer = order === SORT_DIRECTION.DESCENDING ? -1 : 1; return newCollection.sort(function (a, b) { - return (String(a[property])).localeCompare(String(b[property])); - }, order === SORT_DIRECTION.DESCENDING); + return orderMultiplyer * (String(a[property])).localeCompare(String(b[property])); + }); } }; }; From e6630c63ff0bad23854ceb989b1f3fdb1440204e Mon Sep 17 00:00:00 2001 From: ilearnf Date: Wed, 2 Nov 2016 11:54:46 +0500 Subject: [PATCH 15/33] filter --- lego.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index dc77259..83987a6 100644 --- a/lego.js +++ b/lego.js @@ -176,8 +176,9 @@ exports.filterIn = function (property, values) { query: function (collection) { var filtered = []; collection.forEach(function (item) { - if (!item.hasOwnProperty(property) || values.indexOf(item[property]) !== -1) { - filtered.push(item); + if (values.length === 0 || !item.hasOwnProperty(property) || + values.indexOf(item[property]) !== -1) { + filtered.push(item); } }); From f47577666ed2b8f027606813ac76e09a256c1d1e Mon Sep 17 00:00:00 2001 From: ilearnf Date: Wed, 2 Nov 2016 11:56:11 +0500 Subject: [PATCH 16/33] linted --- index.html | 2 +- lego.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index bc0b62f..9fb3759 100644 --- a/index.html +++ b/index.html @@ -179,7 +179,7 @@ query: function (collection) { var filtered = []; collection.forEach(function(item) { - if (!item.hasOwnProperty(property) || values.indexOf(item[property]) !== -1) { + if (values.length === 0 || !item.hasOwnProperty(property) || values.indexOf(item[property]) !== -1) { filtered.push(item); } }); diff --git a/lego.js b/lego.js index 83987a6..0540267 100644 --- a/lego.js +++ b/lego.js @@ -178,7 +178,7 @@ exports.filterIn = function (property, values) { collection.forEach(function (item) { if (values.length === 0 || !item.hasOwnProperty(property) || values.indexOf(item[property]) !== -1) { - filtered.push(item); + filtered.push(item); } }); From def9af3f426865537fddd467b2d2350ff1a12638 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Wed, 2 Nov 2016 12:26:10 +0500 Subject: [PATCH 17/33] limit --- index.html | 1 + lego.js | 1 + 2 files changed, 2 insertions(+) diff --git a/index.html b/index.html index 9fb3759..6501fcc 100644 --- a/index.html +++ b/index.html @@ -253,6 +253,7 @@ queryType: QUERY_TYPES.LIMIT, query: function (collection) { var limited = []; + count = count > collection.length ? collection.length : count; for (var i = 0; i < count; i += 1) { limited.push(collection[i]); } diff --git a/lego.js b/lego.js index 0540267..dddc39a 100644 --- a/lego.js +++ b/lego.js @@ -252,6 +252,7 @@ exports.limit = function (count) { queryType: QUERY_TYPES.LIMIT, query: function (collection) { var limited = []; + count = count > collection.length ? collection.length : count; for (var i = 0; i < count; i += 1) { limited.push(collection[i]); } From 1a95e1aa085254553b0eb8c8c9d52496973f2e72 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Wed, 2 Nov 2016 12:35:41 +0500 Subject: [PATCH 18/33] sort --- lego.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index dddc39a..d0786b6 100644 --- a/lego.js +++ b/lego.js @@ -205,11 +205,16 @@ exports.sortBy = function (property, order) { queryType: QUERY_TYPES.SORT, query: function (collection) { var newCollection = getCollectionCopy(collection); - var orderMultiplyer = order === SORT_DIRECTION.DESCENDING ? -1 : 1; + var orderMultiplyer = order === SORT_DIRECTION.DESCENDING ? 1 : 1; - return newCollection.sort(function (a, b) { + newCollection.sort(function (a, b) { return orderMultiplyer * (String(a[property])).localeCompare(String(b[property])); }); + if (order === SORT_DIRECTION.DESCENDING) { + newCollection.reverse(); + } + + return newCollection; } }; }; From f80eb72385f748df2b478561988b76010c276053 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Wed, 2 Nov 2016 20:19:47 +0500 Subject: [PATCH 19/33] test17 --- index.html | 17 +++++++++++++++++ lego.js | 26 +++++++++++++++++++------- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 6501fcc..98e2ba7 100644 --- a/index.html +++ b/index.html @@ -44,6 +44,21 @@ return otherContains; }); } + function orderByDefault(defaultCollection, unorderedCollection) { + var orderedCollection = []; + unorderedCollection = unorderedCollection.map(function (item) { + var idx = defaultCollection.indexOf(item); + return Object.assign({ position: idx }, item); + }); + orderedCollection = unorderedCollection.sort(function (a, b) { + return a.position - b.position; + }).map(function (item) { + delete item.position; + return item; + }); + + return orderedCollection; + } function getUnion(listOfArrays) { if (listOfArrays.length === 0) { return []; @@ -279,6 +294,7 @@ query: function (collection) { var filteredCollections = applyFiltersToCollection(filters, collection); var united = getUnion(filteredCollections); + united = orderByDefault(collection, united); return united; } @@ -299,6 +315,7 @@ query: function (collection) { var filteredCollections = applyFiltersToCollection(filters, collection); var intersection = getIntersection(filteredCollections); + intersection = orderByDefault(collection, intersection); return intersection; } diff --git a/lego.js b/lego.js index d0786b6..9547e3e 100644 --- a/lego.js +++ b/lego.js @@ -95,6 +95,21 @@ function getCompatibleSelectArguments(collection, queryArguments) { return compatibleArguments; } +function orderByDefault(defaultCollection, unorderedCollection) { + var orderedCollection = []; + unorderedCollection = unorderedCollection.map(function (item) { + var idx = defaultCollection.indexOf(item); + return Object.assign({ position: idx }, item); + }); + orderedCollection = unorderedCollection.sort(function (a, b) { + return a.position - b.position; + }).map(function (item) { + delete item.position; + return item; + }); + + return orderedCollection; +} /** * Запрос к коллекции @@ -205,16 +220,11 @@ exports.sortBy = function (property, order) { queryType: QUERY_TYPES.SORT, query: function (collection) { var newCollection = getCollectionCopy(collection); - var orderMultiplyer = order === SORT_DIRECTION.DESCENDING ? 1 : 1; + var orderMultiplyer = order === SORT_DIRECTION.DESCENDING ? -1 : 1; - newCollection.sort(function (a, b) { + return newCollection.sort(function (a, b) { return orderMultiplyer * (String(a[property])).localeCompare(String(b[property])); }); - if (order === SORT_DIRECTION.DESCENDING) { - newCollection.reverse(); - } - - return newCollection; } }; }; @@ -283,6 +293,7 @@ if (exports.isStar) { query: function (collection) { var filteredCollections = applyFiltersToCollection(filters, collection); var united = getUnion(filteredCollections); + united = orderByDefault(collection, united); return united; } @@ -303,6 +314,7 @@ if (exports.isStar) { query: function (collection) { var filteredCollections = applyFiltersToCollection(filters, collection); var intersection = getIntersection(filteredCollections); + intersection = orderByDefault(collection, intersection); return intersection; } From 77569527d78c71ba9483a7ec1edad08489824990 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Wed, 2 Nov 2016 20:22:56 +0500 Subject: [PATCH 20/33] lint --- lego.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lego.js b/lego.js index 9547e3e..da0341d 100644 --- a/lego.js +++ b/lego.js @@ -99,12 +99,14 @@ function orderByDefault(defaultCollection, unorderedCollection) { var orderedCollection = []; unorderedCollection = unorderedCollection.map(function (item) { var idx = defaultCollection.indexOf(item); + return Object.assign({ position: idx }, item); }); orderedCollection = unorderedCollection.sort(function (a, b) { return a.position - b.position; }).map(function (item) { delete item.position; + return item; }); From 28205ebd95f49f0345d4aaaef6d2592de337de20 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Wed, 2 Nov 2016 20:50:41 +0500 Subject: [PATCH 21/33] test17 --- index.html | 55 +++++++++++++++++++++++++++++------------------------- lego.js | 4 ++-- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/index.html b/index.html index 98e2ba7..6fbe1c0 100644 --- a/index.html +++ b/index.html @@ -44,21 +44,6 @@ return otherContains; }); } - function orderByDefault(defaultCollection, unorderedCollection) { - var orderedCollection = []; - unorderedCollection = unorderedCollection.map(function (item) { - var idx = defaultCollection.indexOf(item); - return Object.assign({ position: idx }, item); - }); - orderedCollection = unorderedCollection.sort(function (a, b) { - return a.position - b.position; - }).map(function (item) { - delete item.position; - return item; - }); - - return orderedCollection; - } function getUnion(listOfArrays) { if (listOfArrays.length === 0) { return []; @@ -114,6 +99,23 @@ return compatibleArguments; } + function orderByDefault(defaultCollection, unorderedCollection) { + var orderedCollection = []; + unorderedCollection = unorderedCollection.map(function (item) { + var idx = defaultCollection.indexOf(item); + + return Object.assign({ position: idx }, item); + }); + orderedCollection = unorderedCollection.sort(function (a, b) { + return a.position - b.position; + }).map(function (item) { + delete item.position; + + return item; + }); + + return orderedCollection; + } /** * Запрос к коллекции @@ -128,10 +130,11 @@ var nonSelectArgs = []; args.forEach(function (queryFunction) { if (queryFunction.queryType === QUERY_TYPES.SELECT) { - var compatibleArguments = getCompatibleSelectArguments(collection, queryFunction.queryArguments); - if (compatibleArguments.length > 0) { - selectArguments.push(compatibleArguments); - } + var compatibleArguments = getCompatibleSelectArguments( + collection, queryFunction.queryArguments); + if (compatibleArguments.length > 0) { + selectArguments.push(compatibleArguments); + } } else { nonSelectArgs.push(queryFunction); } @@ -158,7 +161,7 @@ */ exports.select = function () { var args = [].slice.call(arguments, 0); - + return { queryType: QUERY_TYPES.SELECT, queryArguments: args, @@ -193,8 +196,9 @@ queryType: QUERY_TYPES.FILTER, query: function (collection) { var filtered = []; - collection.forEach(function(item) { - if (values.length === 0 || !item.hasOwnProperty(property) || values.indexOf(item[property]) !== -1) { + collection.forEach(function (item) { + if (values.length === 0 || !item.hasOwnProperty(property) || + values.indexOf(item[property]) !== -1) { filtered.push(item); } }); @@ -244,8 +248,8 @@ queryType: QUERY_TYPES.FORMAT, query: function (collection) { var formatted = []; - collection.forEach (function(item) { - var newItem = Object.assign({}, item) + collection.forEach (function (item) { + var newItem = Object.assign({}, item); if (newItem.hasOwnProperty(property)) { newItem[property] = formatter(item[property]); } @@ -260,6 +264,7 @@ /** * Ограничение количества элементов в коллекции * @param {Number} count – Максимальное количество элементов + * @returns {Object} - query-object */ exports.limit = function (count) { console.info(count); @@ -309,7 +314,7 @@ */ exports.and = function () { var filters = [].slice.call(arguments, 0); - + return { queryType: QUERY_TYPES.FILTER, query: function (collection) { diff --git a/lego.js b/lego.js index da0341d..50ce562 100644 --- a/lego.js +++ b/lego.js @@ -1,8 +1,8 @@ 'use strict'; var QUERY_TYPES = { - SELECT: 2, + SELECT: 3, FILTER: 1, - SORT: 1, + SORT: 2, LIMIT: 99, FORMAT: 99 }; From 3307310a1b29aff45a4d1e2fd27e9302ca2844b6 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Wed, 2 Nov 2016 22:50:19 +0500 Subject: [PATCH 22/33] without star --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 50ce562..5147f91 100644 --- a/lego.js +++ b/lego.js @@ -11,7 +11,7 @@ var QUERY_TYPES = { * Сделано задание на звездочку * Реализованы методы or и and */ -exports.isStar = true; +exports.isStar = false; function cloneCollection(collection) { var cloned = []; From 302dfce7db4f9e45cbd63a0e46f1f595e6c3ccd0 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Thu, 10 Nov 2016 13:36:22 +0500 Subject: [PATCH 23/33] corrections --- index.html | 334 ----------------------------------------------------- lego.js | 69 ++++------- 2 files changed, 23 insertions(+), 380 deletions(-) delete mode 100644 index.html diff --git a/index.html b/index.html deleted file mode 100644 index 6fbe1c0..0000000 --- a/index.html +++ /dev/null @@ -1,334 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/lego.js b/lego.js index 5147f91..28d1dc6 100644 --- a/lego.js +++ b/lego.js @@ -11,16 +11,16 @@ var QUERY_TYPES = { * Сделано задание на звездочку * Реализованы методы or и and */ -exports.isStar = false; +exports.isStar = true; function cloneCollection(collection) { - var cloned = []; + var clonedCollection = []; collection.forEach(function (item) { var newItem = Object.assign({}, item); - cloned.push(newItem); + clonedCollection.push(newItem); }); - return cloned; + return clonedCollection; } function getIntersection(listOfArrays) { if (listOfArrays.length === 0) { @@ -28,28 +28,19 @@ function getIntersection(listOfArrays) { } return listOfArrays[0].filter(function (item) { - var otherContains = true; - listOfArrays.slice(1).forEach(function (otherArray) { - if (otherArray.indexOf(item) === -1) { - otherContains = false; - - return; - } + return listOfArrays.slice(1).some(function (otherArray) { + return otherArray.indexOf(item) !== -1 }); - - return otherContains; }); } function getUnion(listOfArrays) { if (listOfArrays.length === 0) { return []; } - var united = listOfArrays[0]; + var union = listOfArrays[0]; listOfArrays.splice(1).forEach(function (arrayItem) { - united.concat(arrayItem.filter(function (item) { - if (united.indexOf(item) === -1) { - united.push(item); - + union.concat(arrayItem.filter(function (item) { + if (union.indexOf(item) === -1) { return true; } @@ -62,7 +53,7 @@ function getUnion(listOfArrays) { function applyFiltersToCollection(filters, collection) { var filteredCollections = []; if (filters.length === 0) { - return [getCollectionCopy(collection)]; + return [cloneCollection(collection)]; } filters.forEach(function (filterQuery) { var filtered = filterQuery.query(collection); @@ -71,24 +62,16 @@ function applyFiltersToCollection(filters, collection) { return filteredCollections; } -function getCollectionCopy(collection) { - var copy = []; - collection.forEach(function (item) { - copy.push(item); - }); - - return copy; -} -function getCompatibleSelectArguments(collection, queryArguments) { +function getExistingSelectArguments(collection, queryArguments) { var compatibleArguments = []; queryArguments.forEach(function (argument) { - var allHaveProperty = true; + var allCollectionItemsHaveProperty = true; collection.forEach(function (item) { if (!item.hasOwnProperty(argument)) { - allHaveProperty = false; + allCollectionItemsHaveProperty = false; } }); - if (allHaveProperty) { + if (allCollectionItemsHaveProperty) { compatibleArguments.push(argument); } }); @@ -126,7 +109,7 @@ exports.query = function (collection) { var nonSelectArgs = []; args.forEach(function (queryFunction) { if (queryFunction.queryType === QUERY_TYPES.SELECT) { - var compatibleArguments = getCompatibleSelectArguments( + var compatibleArguments = getExistingSelectArguments( collection, queryFunction.queryArguments); if (compatibleArguments.length > 0) { selectArguments.push(compatibleArguments); @@ -162,7 +145,7 @@ exports.select = function () { queryType: QUERY_TYPES.SELECT, queryArguments: args, query: function (collection) { - var selected = []; + var selectedFromCollection = []; collection.forEach(function (item) { var selectedItem = {}; args.forEach(function (property) { @@ -170,10 +153,10 @@ exports.select = function () { selectedItem[property] = item[property]; } }); - selected.push(selectedItem); + selectedFromCollection.push(selectedItem); }); - return selected; + return selectedFromCollection; } }; @@ -191,15 +174,12 @@ exports.filterIn = function (property, values) { return { queryType: QUERY_TYPES.FILTER, query: function (collection) { - var filtered = []; - collection.forEach(function (item) { + return collection.filter(function (item) { if (values.length === 0 || !item.hasOwnProperty(property) || values.indexOf(item[property]) !== -1) { - filtered.push(item); + return true; } }); - - return filtered; } }; }; @@ -221,7 +201,7 @@ exports.sortBy = function (property, order) { return { queryType: QUERY_TYPES.SORT, query: function (collection) { - var newCollection = getCollectionCopy(collection); + var newCollection = cloneCollection(collection); var orderMultiplyer = order === SORT_DIRECTION.DESCENDING ? -1 : 1; return newCollection.sort(function (a, b) { @@ -269,12 +249,9 @@ exports.limit = function (count) { queryType: QUERY_TYPES.LIMIT, query: function (collection) { var limited = []; - count = count > collection.length ? collection.length : count; - for (var i = 0; i < count; i += 1) { - limited.push(collection[i]); - } + count = Math.min(count, collection.length); - return limited; + return limited.slice(0, count); } }; }; From 03e7ea2f2058060e80036995a4f78bc3da5fc273 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Thu, 10 Nov 2016 13:40:13 +0500 Subject: [PATCH 24/33] lint --- lego.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index 28d1dc6..bf30979 100644 --- a/lego.js +++ b/lego.js @@ -29,7 +29,7 @@ function getIntersection(listOfArrays) { return listOfArrays[0].filter(function (item) { return listOfArrays.slice(1).some(function (otherArray) { - return otherArray.indexOf(item) !== -1 + return otherArray.indexOf(item) !== -1; }); }); } @@ -48,7 +48,7 @@ function getUnion(listOfArrays) { })); }); - return united; + return union; } function applyFiltersToCollection(filters, collection) { var filteredCollections = []; @@ -179,6 +179,8 @@ exports.filterIn = function (property, values) { values.indexOf(item[property]) !== -1) { return true; } + + return false; }); } }; From 96e8d624407bdb5f7a05f4f15f3d792db329a54d Mon Sep 17 00:00:00 2001 From: ilearnf Date: Thu, 10 Nov 2016 14:00:59 +0500 Subject: [PATCH 25/33] lint --- index.html | 313 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lego.js | 5 +- 2 files changed, 315 insertions(+), 3 deletions(-) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..b60c417 --- /dev/null +++ b/index.html @@ -0,0 +1,313 @@ + + + + + + + \ No newline at end of file diff --git a/lego.js b/lego.js index bf30979..b247b2b 100644 --- a/lego.js +++ b/lego.js @@ -30,7 +30,7 @@ function getIntersection(listOfArrays) { return listOfArrays[0].filter(function (item) { return listOfArrays.slice(1).some(function (otherArray) { return otherArray.indexOf(item) !== -1; - }); + }) || listOfArrays.length === 1; }); } function getUnion(listOfArrays) { @@ -250,10 +250,9 @@ exports.limit = function (count) { return { queryType: QUERY_TYPES.LIMIT, query: function (collection) { - var limited = []; count = Math.min(count, collection.length); - return limited.slice(0, count); + return collection.slice(0, count); } }; }; From 43bd9a49fd788a3f975b58ba9a54ec3330d1e977 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Thu, 10 Nov 2016 14:03:12 +0500 Subject: [PATCH 26/33] star --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index b247b2b..aa5bb9d 100644 --- a/lego.js +++ b/lego.js @@ -11,7 +11,7 @@ var QUERY_TYPES = { * Сделано задание на звездочку * Реализованы методы or и and */ -exports.isStar = true; +exports.isStar = false; function cloneCollection(collection) { var clonedCollection = []; From f6c5e4b3c107dd761360bd1963245f18520d4c0c Mon Sep 17 00:00:00 2001 From: ilearnf Date: Thu, 10 Nov 2016 14:31:19 +0500 Subject: [PATCH 27/33] exports call --- lego.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lego.js b/lego.js index aa5bb9d..80c68c3 100644 --- a/lego.js +++ b/lego.js @@ -121,7 +121,7 @@ exports.query = function (collection) { args = nonSelectArgs; if (selectArguments.length > 0) { var selectArgumentsIntersection = getIntersection(selectArguments); - args.push(exports.select.apply(this, selectArgumentsIntersection)); + args.push(select.apply(this, selectArgumentsIntersection)); } args.sort(function (a, b) { return a.queryType - b.queryType; @@ -138,7 +138,7 @@ exports.query = function (collection) { * @params {...String} * @returns {Object} - query-object */ -exports.select = function () { +var select = function () { var args = [].slice.call(arguments, 0); return { @@ -161,6 +161,7 @@ exports.select = function () { }; }; +exports.select = select; /** * Фильтрация поля по массиву значений From 5a7064cc8a5f1f54dd7523d95a658bcc7d684ff5 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Thu, 10 Nov 2016 14:35:19 +0500 Subject: [PATCH 28/33] lint; --- lego.js | 58 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/lego.js b/lego.js index 80c68c3..d4d7e03 100644 --- a/lego.js +++ b/lego.js @@ -96,6 +96,35 @@ function orderByDefault(defaultCollection, unorderedCollection) { return orderedCollection; } +/** + * Выбор полей + * @params {...String} + * @returns {Object} - query-object + */ +var select = function () { + var args = [].slice.call(arguments, 0); + + return { + queryType: QUERY_TYPES.SELECT, + queryArguments: args, + query: function (collection) { + var selectedFromCollection = []; + collection.forEach(function (item) { + var selectedItem = {}; + args.forEach(function (property) { + if (item.hasOwnProperty(property)) { + selectedItem[property] = item[property]; + } + }); + selectedFromCollection.push(selectedItem); + }); + + return selectedFromCollection; + } + }; + +}; + /** * Запрос к коллекции * @param {Array} collection @@ -132,35 +161,6 @@ exports.query = function (collection) { return collectionCopy; }; - -/** - * Выбор полей - * @params {...String} - * @returns {Object} - query-object - */ -var select = function () { - var args = [].slice.call(arguments, 0); - - return { - queryType: QUERY_TYPES.SELECT, - queryArguments: args, - query: function (collection) { - var selectedFromCollection = []; - collection.forEach(function (item) { - var selectedItem = {}; - args.forEach(function (property) { - if (item.hasOwnProperty(property)) { - selectedItem[property] = item[property]; - } - }); - selectedFromCollection.push(selectedItem); - }); - - return selectedFromCollection; - } - }; - -}; exports.select = select; /** From 97dcb26d9c9a954528635003c8d63af277aa91b9 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Sat, 19 Nov 2016 14:46:31 +0500 Subject: [PATCH 29/33] corrections by mentor --- index.html | 2 +- lego.js | 38 +++++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/index.html b/index.html index b60c417..3129140 100644 --- a/index.html +++ b/index.html @@ -142,7 +142,7 @@ * @params {...String} * @returns {Object} - query-object */ - exports.select = function () { + var select = function () { var args = [].slice.call(arguments, 0); return { diff --git a/lego.js b/lego.js index d4d7e03..f9a9fd4 100644 --- a/lego.js +++ b/lego.js @@ -14,27 +14,39 @@ var QUERY_TYPES = { exports.isStar = false; function cloneCollection(collection) { - var clonedCollection = []; - collection.forEach(function (item) { - var newItem = Object.assign({}, item); - clonedCollection.push(newItem); + return collection.reduce(function (previous, current) { + current = [cloneObject(current)]; + return previous.concat(current); + }); +} +function cloneObject(obj) { + var clonedObject = {}; + Object.keys(obj).forEach(function (key) { + clonedObject[key] = obj[key]; }); - return clonedCollection; + return clonedObj; } function getIntersection(listOfArrays) { if (listOfArrays.length === 0) { return []; } + var firstArray = listOfArrays[0] + var anyOtherArraysContains = function(item) { + var otherArrays = listOfArrays.splice(1); + if (!otherArrays.length) { + return true; + } - return listOfArrays[0].filter(function (item) { - return listOfArrays.slice(1).some(function (otherArray) { + return otherArrays.some(function (otherArray) { return otherArray.indexOf(item) !== -1; - }) || listOfArrays.length === 1; - }); + }); + }; + + return firstArray.filter(anyOtherArrayContains); } function getUnion(listOfArrays) { - if (listOfArrays.length === 0) { + if (!listOfArrays.length) { return []; } var union = listOfArrays[0]; @@ -82,8 +94,12 @@ function orderByDefault(defaultCollection, unorderedCollection) { var orderedCollection = []; unorderedCollection = unorderedCollection.map(function (item) { var idx = defaultCollection.indexOf(item); + var clonedObject = cloneObject(item); + clonedObject.idx = idx; + var objectWithIdx = clonedObject; + - return Object.assign({ position: idx }, item); + return objectWithIdx; }); orderedCollection = unorderedCollection.sort(function (a, b) { return a.position - b.position; From 336d2f5ad7779f5ef31ee82ea0ecf47a59d93f5f Mon Sep 17 00:00:00 2001 From: ilearnf Date: Sat, 19 Nov 2016 14:46:59 +0500 Subject: [PATCH 30/33] deleted index --- index.html | 313 ----------------------------------------------------- 1 file changed, 313 deletions(-) delete mode 100644 index.html diff --git a/index.html b/index.html deleted file mode 100644 index 3129140..0000000 --- a/index.html +++ /dev/null @@ -1,313 +0,0 @@ - - - - - - - \ No newline at end of file From 4d6a9b65710c8f79b949cfb6ab474c2507dba18b Mon Sep 17 00:00:00 2001 From: ilearnf Date: Sat, 19 Nov 2016 14:49:37 +0500 Subject: [PATCH 31/33] linted --- lego.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lego.js b/lego.js index f9a9fd4..8a32667 100644 --- a/lego.js +++ b/lego.js @@ -16,6 +16,7 @@ exports.isStar = false; function cloneCollection(collection) { return collection.reduce(function (previous, current) { current = [cloneObject(current)]; + return previous.concat(current); }); } @@ -25,14 +26,14 @@ function cloneObject(obj) { clonedObject[key] = obj[key]; }); - return clonedObj; + return clonedObject; } function getIntersection(listOfArrays) { if (listOfArrays.length === 0) { return []; } - var firstArray = listOfArrays[0] - var anyOtherArraysContains = function(item) { + var firstArray = listOfArrays[0]; + var anyOtherArrayContains = function(item) { var otherArrays = listOfArrays.splice(1); if (!otherArrays.length) { return true; @@ -97,7 +98,7 @@ function orderByDefault(defaultCollection, unorderedCollection) { var clonedObject = cloneObject(item); clonedObject.idx = idx; var objectWithIdx = clonedObject; - + return objectWithIdx; }); From efa38527c4e3e2a5c7aac2e31557f4ad95ebf250 Mon Sep 17 00:00:00 2001 From: ilearnf Date: Sat, 19 Nov 2016 14:54:07 +0500 Subject: [PATCH 32/33] lint --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index 8a32667..e9efeb3 100644 --- a/lego.js +++ b/lego.js @@ -33,7 +33,7 @@ function getIntersection(listOfArrays) { return []; } var firstArray = listOfArrays[0]; - var anyOtherArrayContains = function(item) { + var anyOtherArrayContains = function (item) { var otherArrays = listOfArrays.splice(1); if (!otherArrays.length) { return true; From 9e2b4e10b18fd64765db895361282a96e9280afc Mon Sep 17 00:00:00 2001 From: ilearnf Date: Sat, 19 Nov 2016 14:58:25 +0500 Subject: [PATCH 33/33] reduce --- lego.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lego.js b/lego.js index e9efeb3..95371c3 100644 --- a/lego.js +++ b/lego.js @@ -18,7 +18,7 @@ function cloneCollection(collection) { current = [cloneObject(current)]; return previous.concat(current); - }); + }, []); } function cloneObject(obj) { var clonedObject = {};