-
Notifications
You must be signed in to change notification settings - Fork 70
Квашнина Ульяна #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Квашнина Ульяна #43
Changes from all commits
63da3af
38a3b74
b03ad97
d940bae
4dbb289
bbc6609
5fed378
69b2a4a
e4743a7
e04b042
b139d4d
c980067
319c9fc
f1b443b
b0bb7b4
799aac8
80da510
f0e810c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,70 +1,104 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Сделано задание на звездочку | ||
| * Реализованы методы or и and | ||
| */ | ||
| exports.isStar = true; | ||
|
|
||
| /** | ||
| * Запрос к коллекции | ||
| * @param {Array} collection | ||
| * @params {...Function} – Функции для запроса | ||
| * @returns {Array} | ||
| */ | ||
| exports.isStar = false; | ||
|
|
||
| // Чем больше число тем ниже приоритет функции | ||
| var priority = { | ||
| filterIn: 1, | ||
| sortBy: 2, | ||
| select: 3, | ||
| limit: 4, | ||
| format: 5 | ||
| }; | ||
|
|
||
| exports.query = function (collection) { | ||
| return collection; | ||
| var newCollection = copyCollection(collection); | ||
| var functions = Array.prototype.slice.call(arguments).slice(1); | ||
| function compare(one, another) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Эту функцию можно сильно сократить, возвращая, например, x - y |
||
| return priority[one.name] - priority[another.name]; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем поудаляла все
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. мне с ними было неудобно, тут маленький код и так было понятно( мне их вернуть назад??
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Их же можно сворачивать в IDE :) В данном случае, да маленький, но стоит привыкать с ним работать. Писать |
||
| functions.sort(compare); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не старайся усложнять там, где всё можно сделать проще. Когда функция сортировки достаточно простая, не стоит выносить её в отдельную функцию. К тому же её можно написать сильно короче и проще: functions.sort(function (a, b) {
return priority[a.name] - priority[b.name];
}); |
||
| functions.forEach(function (eachQuery) { | ||
| newCollection = eachQuery(newCollection); | ||
| }); | ||
|
|
||
| return newCollection; | ||
| }; | ||
|
|
||
| /** | ||
| * Выбор полей | ||
| * @params {...String} | ||
| */ | ||
| function copyCollection(collection) { | ||
|
|
||
| return (collection.map(function (friend) { | ||
|
|
||
| return Object.assign({}, friend); | ||
| })); | ||
| } | ||
|
|
||
| exports.select = function () { | ||
| return; | ||
| }; | ||
| var fields = Array.prototype.slice.call(arguments); | ||
|
|
||
| /** | ||
| * Фильтрация поля по массиву значений | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {Array} values – Доступные значения | ||
| */ | ||
| exports.filterIn = function (property, values) { | ||
| console.info(property, values); | ||
| return function select(collection) { | ||
|
|
||
| return; | ||
| return collection.reduce(function (prev, curr) { | ||
| var friend = {}; | ||
| for (var field in curr) { | ||
| if (fields.indexOf(field) !== -1) { | ||
| friend[field] = curr[field]; | ||
| } | ||
| } | ||
| prev.push(friend); | ||
|
|
||
| return prev; | ||
| }, []); | ||
|
|
||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Сортировка коллекции по полю | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {String} order – Порядок сортировки (asc - по возрастанию; desc – по убыванию) | ||
| */ | ||
| exports.sortBy = function (property, order) { | ||
| console.info(property, order); | ||
| return function sortBy(collection) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. у тебя получилась достаточно большая функция сортировки, ее можно сделать проще
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ульяна, у тебя есть идеи как оптимизировать эту функцию?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. есть, сейчас я исправлю всё
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| function compare(one, another) { | ||
| return (one[property] > another[property]) ? 1 : -1; | ||
| } | ||
|
|
||
| return; | ||
| if (order === 'asc') { | ||
| collection.sort(compare); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Метод |
||
| } else { | ||
| collection.sort(compare).reverse(); | ||
| } | ||
|
|
||
| return collection; | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Форматирование поля | ||
| * @param {String} property – Свойство для фильтрации | ||
| * @param {Function} formatter – Функция для форматирования | ||
| */ | ||
| exports.format = function (property, formatter) { | ||
| console.info(property, formatter); | ||
| exports.limit = function (count) { | ||
| return function limit(collection) { | ||
| return collection.slice(0, count); | ||
| }; | ||
| }; | ||
|
|
||
| return; | ||
| exports.filterIn = function (property, values) { | ||
| return function filterIn(collection) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. попробуй метод filter |
||
| var newSorted = []; | ||
| collection.forEach(function (friend) { | ||
| for (var field in friend) { | ||
| if (property === field.toString() && | ||
| values.indexOf(friend[field]) !== -1) { | ||
| newSorted.push(Object.assign({}, friend)); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return newSorted; | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Ограничение количества элементов в коллекции | ||
| * @param {Number} count – Максимальное количество элементов | ||
| */ | ||
| exports.limit = function (count) { | ||
| console.info(count); | ||
| exports.format = function (property, formatter) { | ||
| return function format(collection) { | ||
| return collection.map(function (friend) { | ||
| friend[property] = formatter(friend[property]); | ||
|
|
||
| return; | ||
| return friend; | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| if (exports.isStar) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Стоит написать комментарий, что значат эти цифры – больше число выполнится "быстрее" или в последнюю очередь