-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathlego.js
More file actions
124 lines (100 loc) · 2.73 KB
/
lego.js
File metadata and controls
124 lines (100 loc) · 2.73 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
124
'use strict';
exports.isStar = false;
function compareFunctions(a, b) {
var obj = {
select: 3,
format: 2,
filterIn: 4,
sortBy: 5,
limit: 1
};
return (obj[a.name] < obj[b.name]) ? 1 : -1;
}
function filterCollection(property, values, record, result) {
for (var j = 0; j < values.length; j++) {
if (record[property] === values[j]) {
result.push(record);
}
}
}
exports.query = function () {
var queryArgs = [].slice.call(arguments);
if (queryArgs.length === 1) {
return queryArgs[0];
}
var changedCollection = queryArgs[0];
queryArgs.splice(0, 1);
queryArgs.sort(compareFunctions);
for (var i = 0; i < queryArgs.length; i++) {
changedCollection = queryArgs[i](changedCollection);
}
return changedCollection;
};
exports.select = function () {
var args = [].slice.call(arguments);
return function select(collection) {
var result = [];
for (var i = 0; i < collection.length; i++) {
result.push({});
for (var j = 0; j < args.length; j++) {
result[i][args[j]] = collection[i][args[j]];
}
}
return result;
};
};
exports.filterIn = function (property, values) {
return function filterIn(collection) {
var result = [];
for (var i = 0; i < collection.length; i++) {
filterCollection(property, values, collection[i], result);
}
return result;
};
};
exports.sortBy = function (property, order) {
return function sortBy(collection) {
collection.sort(function (a, b) {
var coefficient;
if (order === 'asc') {
coefficient = -1;
}
if (order === 'desc') {
coefficient = 1;
}
if (a[property] < b[property]) {
return Number(coefficient) * 1;
}
if (a[property] > b[property]) {
return -1 * Number(coefficient);
}
return 0;
});
return collection;
};
};
exports.format = function (property, formatter) {
return function format(collection) {
var result = [];
for (var i = 0; i < collection.length; i++) {
var a = formatter(collection[i][property]);
collection[i][property] = a;
result.push(collection[i]);
}
return result;
};
};
exports.limit = function (count) {
return function limit(collection) {
var result = collection.splice(0, count);
return result;
};
};
if (exports.isStar) {
exports.or = function () {
return;
};
exports.and = function () {
return;
};
}