-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayManip.js
203 lines (171 loc) · 4.9 KB
/
arrayManip.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// arrayManip.js
// v1.0
// Dependency-free library for turning common array manipulations into a readable single line of code with minimal nested parentheses.
// Created By: Gavin Ovsak. www.workbygavin.com
// ---- Code ----
var a = {
version: '1.0'
};
var paramTransforms = ['prop', 'from', 'in', 'is', 'op', 'toArr', 'toObj'];
var noParamTransforms = ['not', 'index'];
var addTransforms = function(f, paramOnly) {
// Allows chaining of wrapped functions
paramTransforms.map(function(t) {
f[t] = function() {
var func = a[t].apply(this, arguments);
return addTransforms(function(item, i, origItem) {
return func(f(item, i, origItem), i, item);
});
};
});
if (paramOnly) return f;
noParamTransforms.map(function(t) {
f[t] = addTransforms(function(item, i, origItem) {
return a[t](f(item, i, origItem), i, item);
}, true);
});
return f;
};
// Transforms
a.prop = function(prop) {
if (prop === '' || prop == null) return a.item;
else return addTransforms(a.use(a.get, [null, prop]));
};
a.from = function(set) {
return addTransforms(a.use(a.get, [set, null]));
};
a.in = function(set) {
return addTransforms(function(item) {
return Array.isArray(set) ? set.indexOf(item) >= 0 : set[item] != null;
});
};
a.is = function(val) {
return addTransforms(function(item) {
return item == val;
});
};
a.op = function(operator, num) {
// In reductions (when num == null), item2 is the new item. In normal arrays, item2 is the index.
return addTransforms(function(item, item2, origItem) {
if (typeof num === 'function') var other = num(origItem);
else if (num == null) var other = item2;
else var other = num;
if (operator == '>') {
return item > other;
} else if (operator == '>=') {
return item >= other;
} else if (operator == '<') {
return item < other;
} else if (operator == '<=') {
return item <= other;
} else if (operator == '==') {
return item == other;
} else if (operator == '!=') {
return item != other;
} else if (operator == '+') {
return item + other;
} else if (operator == '-') {
return item - other;
} else if (operator == '*') {
return item * other;
} else if (operator == '^') {
return Math.pow(item, other);
} else if (operator == '/') {
return item / other;
} else if (operator == 'abs') {
return Math.abs(item);
} else if (operator == 'min') {
return Math.min(item, other);
} else if (operator == 'max') {
return Math.max(item, other);
} else if (typeof operator === 'function') {
return operator(item);
}
});
};
a.index = addTransforms(function(item, i) {
return i;
});
a.not = addTransforms(function(item) {
return !item;
});
a.toArr = function(fList) {
return addTransforms(function(item, i) {
return fList.map(function(func) {
if (typeof func === 'function') return func(item, i);
else if (func == null) return item;
else return func;
});
});
};
a.toObj = function(map) {
return addTransforms(function(item, i) {
var res = {};
for (var key in map) {
if (typeof map[key] === 'function') res[key] = map[key](item, i);
else if (map[key] == null) res[key] = item;
else res[key] = map[key];
}
return res;
});
};
// Sort
a.by = function(func, direction) {
if (typeof func !== 'function') func = a.prop(func);
func = func || a.item;
direction = direction || 'inc';
return function(a, b) {
return (func == 'alpha' ? a.localeCompare(b) : (func(a) - func(b))) * (direction == 'inc' ? 1 : -1);
};
}
// Reduce
a.minIndex = function(compare) {
if (typeof compare === 'function') {
return function(old_i, item, i, list) {
if (i == 1) old_i = 0; // Old_i should always be 0 when i == 1
if (list[old_i] == null || compare(item) < compare(list[old_i])) return i;
return old_i;
};
} else {
return a.minIndex(a.item).apply(this, arguments);
}
};
// Conditionals
a.and = function() {
var fList = [].slice.call(arguments);
return addTransforms(function(item, i) {
return fList.every(function(func) {
return func(item, i);
});
});
};
a.or = function() {
var fList = [].slice.call(arguments);
return addTransforms(function(item, i) {
return fList.some(function(func) {
return func(item, i);
});
});
};
// Trivial function
a.item = function(item) {
return item;
};
// Internal utility based on lodash.js _.get
a.get = function(obj, prop) {
return (prop + '').split('.').reduce(function(start, nextPath) {
return start[nextPath];
}, obj);
};
// Extend other functions
a.use = function(func, args) {
var itemIndex = args.indexOf(null);
if (itemIndex == -1) itemIndex = args.length;
return function(item) {
var list = args.map(function(arg, i) {
if (i == itemIndex) return item;
return arg;
});
return func.apply(this, list);
}
};