Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 99 additions & 11 deletions lego.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
'use strict';

var KEY = ['filterIn', 'sortBy', 'limit', 'format', 'select'];
function copy(collection) {
return collection.map(function (entry) {
return Object.assign({}, entry);
});
}

/**
* Сделано задание на звездочку
* Реализованы методы or и and
*/
exports.isStar = true;
exports.isStar = false;

/**
* Запрос к коллекции
Expand All @@ -13,58 +20,139 @@ exports.isStar = true;
* @returns {Array}
*/
exports.query = function (collection) {
return collection;
var arg = [].slice.call(arguments, 1);
var newCollection = copy(collection);
arg = arg.sort(function (a, b) {

return KEY.indexOf(a.name) - KEY.indexOf(b.name);
});
for (var i = 0; i < arg.length; i++) {
newCollection = arg[i](newCollection);
}

return newCollection;
};
function helpSelect(arg, collection, i) {
var dict = {};
for (var j = 0; j < arg.length; j++) {
if (!collection[i][arg[j]] !== undefined) {
dict[arg[j]] = collection[i][arg[j]];
}
}

return dict;
}

/**
* Выбор полей
* @params {...String}
* @returns {Array}
*/
exports.select = function () {
return;
var arg = arguments;

return function select(collection) {

var newCollection = [];
for (var i = 0; i < collection.length; i++) {
newCollection.push(helpSelect(arg, collection, i));
}

return newCollection;
};

};
function filter(value, collection, property) {
var newCollection = [];
var fn = new RegExp(value, 'i');
for (var j = 0; j < collection.length; j++) {
if (fn.exec(collection[j][property])) {
newCollection.push(collection[j]);
}
}

return newCollection;
}

/**
* Фильтрация поля по массиву значений
* @param {String} property – Свойство для фильтрации
* @param {Array} values – Доступные значения
* @returns {Array}
*/
exports.filterIn = function (property, values) {
console.info(property, values);

return;
return function filterIn(collection) {
var newCollection = [];
for (var i = 0; i < values.length; i++) {
newCollection = newCollection.concat(filter(values[i], collection, property));
}

return newCollection;
};
};

/**
* Сортировка коллекции по полю
* @param {String} property – Свойство для фильтрации
* @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию)
* @returns {Array}
*/
exports.sortBy = function (property, order) {
console.info(property, order);

return;
var type;
if (order === 'asc') {
type = 1;
} else {
type = -1;
}

return function sortBy(collection) {

return collection.sort(function (cur, next) {
if (cur[property] > next[property]) {

return type;
}
if (cur[property] < next[property]) {

return -type;
}

return 0;
});
};
};

/**
* Форматирование поля
* @param {String} property – Свойство для фильтрации
* @param {Function} formatter – Функция для форматирования
* @returns {Array}
*/
exports.format = function (property, formatter) {
console.info(property, formatter);

return;
return function format(collection) {

for (var i = 0; i < collection.length; i++) {
collection[i][property] = formatter(collection[i][property]);
}

return collection;
};
};

/**
* Ограничение количества элементов в коллекции
* @param {Number} count – Максимальное количество элементов
* @returns {Array}
*/
exports.limit = function (count) {
console.info(count);

return;
return function limit(collection) {

return collection.slice(0, count);
};
};

if (exports.isStar) {
Expand Down