forked from urfu-2016/javascript-task-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlego.js
128 lines (102 loc) · 3.13 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
125
126
127
128
'use strict';
var FUNCTION_PRIORITY = {
'and': 1,
'or': 2,
'filterIn': 3,
'sortBy': 4,
'select': 5,
'limit': 6,
'format': 7
};
exports.isStar = true;
function copyCollection(collection) {
if (collection === undefined) {
return [];
}
return collection.map(function (note) {
return Object.assign({}, note);
});
}
exports.query = function (collection) {
var newCollection = copyCollection(collection);
var functions = [].slice.call(arguments, 1);
functions.sort(function (a, b) {
return FUNCTION_PRIORITY[a.name] - FUNCTION_PRIORITY[b.name];
});
functions.forEach(function (func) {
newCollection = func(newCollection);
});
return newCollection;
};
exports.select = function () {
var selectedFields = [].slice.call(arguments);
return function select(collection) {
return collection.map(function (note) {
var newNote = {};
selectedFields.forEach(function (field) {
if (note.hasOwnProperty(field) && note[field] !== undefined) {
newNote[field] = note[field];
}
});
return newNote;
});
};
};
exports.filterIn = function (property, values) {
return function filterIn(collection) {
return collection.filter(function (note) {
return values.indexOf(note[property]) !== -1;
});
};
};
exports.sortBy = function (property, order) {
return function sortBy(collection) {
var newCollection = copyCollection(collection);
return newCollection.sort(function (firstNote, secondNote) {
if (order === 'asc') {
return firstNote[property] > secondNote[property] ? 1 : -1;
}
return firstNote[property] < secondNote[property] ? 1 : -1;
});
};
};
exports.format = function (property, formatter) {
return function format(collection) {
return collection.map(function (note) {
if (note.hasOwnProperty(property)) {
note[property] = formatter(note[property]);
}
return note;
});
};
};
exports.limit = function (count) {
return function limit(collection) {
var newCollection = copyCollection(collection);
return newCollection.slice(0, count);
};
};
if (exports.isStar) {
exports.or = function () {
var filters = [].slice.call(arguments);
return function or(collection) {
var filteredCollections = filters.map(function (filter) {
return filter(collection);
});
return collection.filter(function (note) {
return filteredCollections.some(function (filteredCollection) {
return filteredCollection.indexOf(note) !== -1;
});
});
};
};
exports.and = function () {
var filters = [].slice.call(arguments);
return function and(collection) {
filters.forEach(function (filter) {
collection = filter(collection);
});
return collection;
};
};
}