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
98 changes: 60 additions & 38 deletions lego.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,111 @@
'use strict';

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

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

/**
* Запрос к коллекции
* @param {Array} collection
* @params {...Function} – Функции для запроса
* @returns {Array}
*/

exports.query = function (collection) {
return collection;
var functions = [].slice.call(arguments, 1);
var changedCollection = collection.map(function (el) {
return Object.assign({}, el);
});
functions.sort(function (first, second) {

Choose a reason for hiding this comment

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

Для функции сортировки можно параметры называть a и b

return FUNCTION_PRIORITY.indexOf(first.name) - FUNCTION_PRIORITY.indexOf(second.name);
})
.forEach (function (query) {
changedCollection = query(changedCollection);
});

return changedCollection;
};

/**
* Выбор полей
* @params {...String}
* @returns {Function}
*/
exports.select = function () {
return;
var fieldsToChoose = [].slice.call(arguments);

return function select(collection) {
return collection.map(function (el) {

Choose a reason for hiding this comment

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

el не несет никакой семантики, лучше подойдет название вроде person

return fieldsToChoose.reduce(function (newCollection, property) {

Choose a reason for hiding this comment

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

Первый аргумент назвал newCollection, но ведь это не коллеция, а объект с информацией о человеке

if (el.hasOwnProperty(property)) {
newCollection[property] = el[property];
}

return newCollection;
}, {});
});
};
};


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

return;
return function filterIn(collection) {

Choose a reason for hiding this comment

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

Снова el никак не отражает того, что лежит внутри (в общем везде el нужно поменять)

return collection.filter(function (el) {
return values.indexOf(el[property]) !== -1;
});
};
};


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

return;
return function sortBy(collection) {
var changedCollection = collection.sort(function (first, second) {

Choose a reason for hiding this comment

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

Зачем еще одну переменную создавать? Можно просто collection.sort()


return (first[property] > second[property]);
Copy link

@ninjagrizzly ninjagrizzly Nov 4, 2016

Choose a reason for hiding this comment

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

Функция сравнения должна возвращать 1 (если first > second по некоторому критерию), 0 (если равны) и -1 (если меньше)

});

return order === 'asc' ? changedCollection : changedCollection.reverse();
};
};

/**
* Форматирование поля
* @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 (el) {
if (el.hasOwnProperty(property)) {
el[property] = formatter(el[property]);
}

return;
return el;
});
};
};

/**
* Ограничение количества элементов в коллекции
* @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;
};
}
};