-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathlego.js
More file actions
123 lines (100 loc) · 2.94 KB
/
lego.js
File metadata and controls
123 lines (100 loc) · 2.94 KB
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
'use strict';
exports.isStar = false;
// Чем больше число тем ниже приоритет функции
var priority = {
filterIn: 1,
sortBy: 2,
select: 3,
limit: 4,
format: 5
};
exports.query = function (collection) {
var newCollection = copyCollection(collection);
var functions = Array.prototype.slice.call(arguments).slice(1);
function compare(one, another) {
return priority[one.name] - priority[another.name];
}
functions.sort(compare);
functions.forEach(function (eachQuery) {
newCollection = eachQuery(newCollection);
});
return newCollection;
};
function copyCollection(collection) {
return (collection.map(function (friend) {
return Object.assign({}, friend);
}));
}
exports.select = function () {
var fields = Array.prototype.slice.call(arguments);
return function select(collection) {
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;
}, []);
};
};
exports.sortBy = function (property, order) {
return function sortBy(collection) {
function compare(one, another) {
return (one[property] > another[property]) ? 1 : -1;
}
if (order === 'asc') {
collection.sort(compare);
} else {
collection.sort(compare).reverse();
}
return collection;
};
};
exports.limit = function (count) {
return function limit(collection) {
return collection.slice(0, count);
};
};
exports.filterIn = function (property, values) {
return function filterIn(collection) {
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;
};
};
exports.format = function (property, formatter) {
return function format(collection) {
return collection.map(function (friend) {
friend[property] = formatter(friend[property]);
return friend;
});
};
};
if (exports.isStar) {
/**
* Фильтрация, объединяющая фильтрующие функции
* @star
* @params {...Function} – Фильтрующие функции
*/
exports.or = function () {
return;
};
/**
* Фильтрация, пересекающая фильтрующие функции
* @star
* @params {...Function} – Фильтрующие функции
*/
exports.and = function () {
return;
};
}