forked from urfu-2015/javascript-tasks-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlego.js
94 lines (93 loc) · 2.79 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
'use strict';
module.exports.query = function (collection) {
var argsLength = arguments.length;
for (var i = 1; i < argsLength; i++) {
collection = arguments[i](collection);
}
return collection;
};
module.exports.reverse = function () {
return function (collection) {
var changedCollection = collection.slice().reverse();
return changedCollection;
};
};
module.exports.limit = function (n) {
return function (collection) {
if (n < 0) {
return collection.slice(0, 0);
}
return collection.slice(0, n);
};
};
module.exports.format = function (feature, method) {
return function (collection) {
collection.forEach(function (contact) {
contact[feature] = method(contact[feature]);
});
return collection;
};
};
module.exports.sortBy = function (feature, sortOrder) {
var order = sortOrder === 'asc' ? 1 : -1;
return function (collection) {
return collection.sort(function (a, b) {
var res = a[feature] > b[feature] ? 1 : -1;
return res * order;
});
};
};
module.exports.filterEqual = function (feature, value) {
return function (collection) {
return collection.filter(function (contact) {
return contact[feature] === value;
});
};
};
module.exports.filterIn = function (feature, args) {
return function (collection) {
return collection.filter(function (contact) {
/*return args.some(arg => contact[feature] === arg);*/
return args.indexOf(contact[feature]) != -1;
});
};
};
module.exports.select = function () {
var features = [].slice.call(arguments);
return function (collection) {
collection.forEach(function (contact) {
Object.keys(contact).forEach(function (key) {
if (features.indexOf(key) === -1) {
delete contact[key];
}
});
});
return collection;
};
};
module.exports.and = function () {
var args = [].slice.call(arguments);
return function (collection) {
args.forEach(function (arg) {
collection = arg(collection);
});
return collection;
};
};
module.exports.or = function () {
var args = [].slice.call(arguments);
return function (collection) {
var changedCollection = [];
args.forEach(function (arg) {
var copy = collection.slice();
var res = arg(copy);
/*changedCollection = changedCollection.concat(res);*/
res.forEach(function (contact) {
if (changedCollection.indexOf(contact) === -1) {
changedCollection.push(contact);
}
});
});
return changedCollection;
};
};