Skip to content
98 changes: 64 additions & 34 deletions lego.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
* Сделано задание на звездочку
* Реализованы методы or и and
*/
exports.isStar = true;
exports.isStar = false;

var priorityFunctions = ['filterIn', 'sortBy', 'select', 'limit', 'format'];

function getCopyCollection(collection) {
return collection.slice();
}

/**
* Запрос к коллекции
Expand All @@ -13,77 +19,101 @@ exports.isStar = true;
* @returns {Array}
*/
exports.query = function (collection) {
return collection;
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) {
copyCollection = query(copyCollection);
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В качестве упражнения можете этот кусочек переделать на reduce, тогда можно будет переменную copyCollection не изменять. Но тут я не настаиваю.


return copyCollection;
};

/**
* Выбор полей
* @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 copyCollection = getCopyCollection(collection);

return copyCollection.sort(function (first, second) {
var result = order === 'asc' ? 1 : -1;
if (first[property] > second[property]) {
return result;
}
if (first[property] > second[property]) {
return -result;
}

return;
return 0;
});
};
};

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

return;
return element;
});
};
};

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

return;
};

if (exports.isStar) {

/**
* Фильтрация, объединяющая фильтрующие функции
* @star
* @params {...Function} – Фильтрующие функции
*/
exports.or = function () {
return;
};

/**
* Фильтрация, пересекающая фильтрующие функции
* @star
* @params {...Function} – Фильтрующие функции
*/
exports.and = function () {
return;
return function limit(collection) {
return collection.slice(0, count);
};
}
};