forked from urfu-2016/javascript-task-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lego.js
124 lines (96 loc) · 3.05 KB
/
lego.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
/**
* Сделано задание на звездочку
* Реализованы методы or и and
*/
exports.isStar = true;
exports.query = function (collection) {
var copyOfCollection = collection.map(function (entry) {
return Object.assign({}, entry);
});
var operators = [].slice.call(arguments, 1);
operators.sort(sortByPriority);
copyOfCollection = operators.reduce(function (collect, opepator) {
return opepator(collect);
}, copyOfCollection);
return copyOfCollection;
};
function sortByPriority(a, b) {
var priority = ['filterIn', 'and', 'or', 'sortBy', 'select', 'limit', 'format'];
return priority.indexOf(b.name) < priority.indexOf(a.name);
}
exports.select = function () {
var params = [].slice.call(arguments);
return function select(collection) {
return collection.slice().map(function (entry) {
return selectByParam(params, entry);
});
};
};
function selectByParam(params, entry) {
var newEntry = {};
params.forEach(function (param) {
if (entry[param]) {
newEntry[param] = entry[param];
}
});
return newEntry;
}
exports.filterIn = function (property, values) {
console.info(property, values);
return function filterIn(collection) {
return collection.slice().filter(function (entry) {
return values.some(function (value) {
return entry[property] === value;
});
});
};
};
exports.sortBy = function (property, order) {
console.info(property, order);
return function sortBy(collection) {
return collection.slice().sort(function (entryOne, entryTwo) {
if (order === 'asc') {
return entryOne[property] > entryTwo[property];
}
return entryTwo[property] > entryOne[property];
});
};
};
exports.format = function (property, formatter) {
console.info(property, formatter);
return function format(collection) {
return collection.slice().map(function (entry) {
entry[property] = formatter(entry[property]);
return entry;
});
};
};
exports.limit = function (count) {
console.info(count);
return function limit(collection) {
return collection.slice(0, count);
};
};
if (exports.isStar) {
exports.or = function () {
var filters = [].slice.call(arguments);
return function or(collection) {
return collection.slice().filter(function (item) {
return filters.some(function (filter) {
return filter(collection.slice()).indexOf(item) !== -1;
});
});
};
};
exports.and = function () {
var filters = [].slice.call(arguments);
return function and(collection) {
var copyOfCollection = collection.slice();
filters.forEach(function (filter) {
copyOfCollection = filter(copyOfCollection);
});
return copyOfCollection;
};
};
}