-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathku.js
332 lines (291 loc) · 8.05 KB
/
ku.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* ku 0.3.0-dev <http://github.com/L8D/ku/>
* @license MIT
* @copyright (c) 2014 Tenor Biel
*/
/* global define, exports, module */
/* istanbul ignore next */
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define('ku', factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.ku = factory();
}
})(this, function() {
'use strict';
/**
* @exampleHelpers
* function add(x, y) { return x + y; }
*/
/** @exports ku */
var ku = {
/**
* Creates an array of values by apply the function to each element in the
* collection.
*
* @example
* ku.map(function(num) { return num * 3 }, [1, 2, 3]);
* // => [3, 6, 9]
* @param {function} fn - The function to iterate with
* @param {array} data - The data to iterate over
* @returns {array}
*/
map: function(fn, data) {
var index = data.length;
var res = new Array(index);
while (index--) {
res[index] = fn(data[index], index);
}
return res;
},
/**
* Creates an array of all values that the function returns a truthy value
* for.
*
* @example
* ku.filter(function(num) { return num % 2 === 0; }, [1, 2, 3, 4, 5, 6]);
* // => [2, 4, 6]
* @param {function} fn - The function to iterate with
* @param {array} data - The data to iterate over
* @returns {array}
*/
filter: function(fn, data) {
var index = -1;
var length = data.length;
var res = [];
var rindex = -1;
while (++index < length) {
var value = data[index];
if (fn(value, index)) {
res[++rindex] = value;
}
}
return res;
},
/**
* Aggregates over a collection starting with a value and returning the
* resulting value.
*
* @example
* ku.reduce(add, '', ['foo', 'bar', 'baz']);
* // => (('' + 'foo') + 'bar') + 'baz'
* // => 'foobarbaz'
* @example
* ku.reduce(add, 'some value', []);
* // => 'some value'
* @param {function} fn - The function to aggregate with
* @param {*} value - The initial accumulator value
* @param {array} data - The data to aggregate over
* @returns {*}
*/
reduce: function(fn, value, data) {
var index = -1;
var length = data.length;
while (++index < length) {
value = fn(value, data[index]);
}
return value;
},
/**
* Aggregates over a collection and returns an array of each intermittent
* accumulation.
*
* @example
* ku.scan(add, 0, [1, 2, 3]);
* // => [0, 1, 3, 6]
* @example
* ku.scan(add, 0, []);
* // => [0]
* @param {function} fn - The function to aggregate with
* @param {*} value - The initial accumulator value
* @param {array} data - The data to aggregate over
* @returns {array}
*/
scan: function(fn, value, data) {
var index = -1;
var length = data.length;
var res = new Array(length + 1);
var rindex = 0;
res[0] = value;
while (++index < length) {
value = res[++rindex] = fn(value, data[index]);
}
return res;
},
/**
* Maps over an array and concatenates the result.
*
* @example
* ku.chain(function(x) { return [x, x + 1, x + 2]; }, [1, 2, 3]);
* // => [1, 2, 3, 2, 3, 4, 3, 4, 5]
* @example
* ku.chain(function(x) { return [[x]]; }, [1, 2, 3]);
* // => [[1], [2], [3]]
* @param {function} fn - The function to iterate with
* @param {array} data - The data to iterate over
* @returns {array}
*/
chain: function(fn, data) {
var index = -1;
var length = data.length;
var res = [];
var rindex = -1;
while (++index < length) {
var values = fn(data[index]);
var index2 = -1;
var length2 = values.length;
while (++index2 < length2) {
res[++rindex] = values[index2];
}
}
return res;
},
/**
* Takes the first `count` elements from an array.
*
* @example
* ku.take(5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
* //=> [1, 2, 3, 4, 5]
* @param {number} count
* @param {array} data
* @returns {array}
*/
take: function(count, data) {
var length = data.length;
var index = Math.min(length, count);
var res = new Array(length - index);
while (index--) {
res[index] = data[index];
}
return res;
},
/**
* Drops the first `count` elements from an array.
*
* @example
* (function() {
* return ku.drop(0, arguments);
* })(1, 2, 3, 4);
* //=> [1, 2, 3, 4]
* @param {number} count
* @param {array} data
* @returns {array}
*/
drop: function(count, data) {
var length = data.length;
var index = Math.min(length, count);
var res = new Array(length - index);
index--;
while (++index < length) {
res[index - count] = data[index];
}
return res;
},
/**
* Slices an array at indecies `x` and `y`.
*
* @example
* ku.slice(2, 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
* // => [3, 4, 5]
* @example
* ku.slice(2, 5, []);
* // => []
* @param {number} x
* @param {number} y
* @param {array} data
* @returns {array}
*/
slice: function(x, y, data) {
var length = data.length;
var index = Math.min(x, length);
y = Math.min(y, length);
var res = new Array(y - index);
index--;
while (++index < y) {
res[index - x] = data[index];
}
return res;
},
/**
* Uses `fn` to zip two arrays into one new array.
*
* Due to performance implications and idiomaticism, zip is actually
* zipWith because JavaScript doesn't have a standard for tuple-like
* data structures.
*
* @example
* ku.zip(add, [1, 2, 3], [4, 5, 6]);
* // => [1 + 4, 2 + 5, 3 + 6]
* @example
* ku.zip(add, [], [1, 2, 3, 4]); // => []
* ku.zip(add, [1, 2, 3, 4], []); // => []
* @param {function} fn
* @param {array} left
* @param {array} right
* @returns {array}
*/
zip: function(fn, left, right) {
var index = Math.min(left.length, right.length);
var res = new Array(index);
while (index--) {
res[index] = fn(left[index], right[index]);
}
return res;
},
/**
* Aggregates over a collection from right to left starting with a value
* and returning the resulting value.
*
* @example
* ku.reduceRight(add, '', ['foo', 'bar', 'baz']);
* // => (('' + 'baz') + 'bar') + 'foo'
* // => 'bazbarfoo'
* @example
* ku.reduceRight(add, 'some value', []);
* // => 'some value'
* @param {function} fn - The function to aggregate with
* @param {*} value - The initial accumulator value
* @param {array} data - The data to aggregate over
* @returns {*}
*/
reduceRight: function(fn, value, data) {
var index = data.length;
while (index--) {
value = fn(value, data[index]);
}
return value;
},
/**
* Aggregates over a collection from right to left and returns an array of
* each intermittent accumulation.
*
* @example
* ku.scanRight(add, 0, [1, 2, 3]);
* // => [0, 3, 5, 6]
* @example
* ku.scanRight(add, 0, []);
* // => [0]
* @example
* ku.scanRight(add, '', ['foo', 'bar', 'baz']);
* // => ['', 'baz', 'bazbar', 'bazbarfoo']
* @param {function} fn - The function to aggregate with
* @param {*} value - The initial accumulator value
* @param {array} data - The data to aggregate over
* @returns {array}
*/
scanRight: function(fn, value, data) {
var index = data.length;
var res = new Array(index + 1);
var rindex = 0;
res[0] = value;
while (index--) {
value = res[++rindex] = fn(value, data[index]);
}
return res;
},
};
return ku;
});