forked from stephband/dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.rolled.js
7102 lines (5592 loc) · 174 KB
/
module.rolled.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
cache(fn)
Returns a function that caches the output values of `fn(input)`
against input values in a map, such that for each input value
`fn` is only ever called once.
*/
function cache(fn) {
var map = new Map();
return function cache(object) {
if (map.has(object)) {
return map.get(object);
}
var value = fn(object);
map.set(object, value);
return value;
};
}
/**
curry(fn [, muteable, arity])
Returns a function that wraps `fn` and makes it partially applicable.
*/
const A = Array.prototype;
function applyFn(fn, args) {
return typeof fn === 'function' ? fn.apply(null, args) : fn ;
}
function curry(fn, muteable, arity) {
arity = arity || fn.length;
var memo = arity === 1 ?
// Don't cache if `muteable` flag is true
muteable ? fn : cache(fn) :
// It's ok to always cache intermediate memos, though
cache(function(object) {
return curry(function() {
var args = [object];
args.push.apply(args, arguments);
return fn.apply(null, args);
}, muteable, arity - 1) ;
}) ;
return function partial(object) {
return arguments.length === 0 ?
partial :
arguments.length === 1 ?
memo(object) :
arguments.length === arity ?
fn.apply(null, arguments) :
arguments.length > arity ?
applyFn(fn.apply(null, A.splice.call(arguments, 0, arity)), arguments) :
applyFn(memo(object), A.slice.call(arguments, 1)) ;
};
}
//function curry(fn, muteable, arity) {
// arity = arity || fn.length;
// return function curried() {
// return arguments.length >= arity ?
// fn.apply(null, arguments) :
// curried.bind(null, ...arguments) ;
// };
//}
{
const _curry = curry;
// Feature test
const isFunctionLengthDefineable = (function() {
var fn = function() {};
try {
// Can't do this on Safari - length non configurable :(
Object.defineProperty(fn, 'length', { value: 2 });
}
catch(e) {
return false;
}
return fn.length === 2;
})();
const setFunctionProperties = function setFunctionProperties(text, parity, fn1, fn2) {
// Make the string representation of fn2 display parameters of fn1
fn2.toString = function() {
return /function\s*[\w\d]*\s*\([,\w\d\s]*\)/.exec(fn1.toString()) + ' { [' + text + '] }';
};
// Where possible, define length so that curried functions show how
// many arguments they are yet expecting
if (isFunctionLengthDefineable) {
Object.defineProperty(fn2, 'length', { value: parity });
}
return fn2;
};
// Make curried functions log a pretty version of their partials
curry = function curry(fn, muteable, arity) {
arity = arity || fn.length;
return setFunctionProperties('curried', arity, fn, _curry(fn, muteable, arity));
};
}
var curry$1 = curry;
/**
ready(fn)
Calls `fn` on DOM content load, or if later than content load, immediately
(on the next tick).
*/
const ready = new Promise(function(accept, reject) {
function handle(e) {
document.removeEventListener('DOMContentLoaded', handle);
window.removeEventListener('load', handle);
accept(e);
}
document.addEventListener('DOMContentLoaded', handle);
window.addEventListener('load', handle);
});
var ready$1 = ready.then.bind(ready);
function now() {
// Return DOM time in seconds
return window.performance.now() / 1000;
}
/**
rest(n, array)
**/
function rest(i, object) {
if (object.slice) { return object.slice(i); }
if (object.rest) { return object.rest(i); }
var a = [];
var n = object.length - i;
while (n--) { a[n] = object[n + i]; }
return a;
}
/**
choose(fn, map)
Returns a function that takes its first argument as a key and uses it
to select a function in `map` which is invoked with the remaining arguments.
Where `map` has a function `default`, that function is run when a key
is not found, otherwise unfound keys will error.
```
var fn = choose({
'fish': function fn1(a, b) {...},
'chips': function fn2(a, b) {...}
});
fn('fish', a, b); // Calls fn1(a, b)
```
*/
function choose(map) {
return function choose(key) {
var fn = map[key] || map.default;
return fn && fn.apply(this, rest(1, arguments)) ;
};
}
/**
noop()
Returns undefined.
*/
function noop() {}
/**
requestTick(fn)
Call `fn` on the next tick.
*/
const resolved = Promise.resolve();
function requestTick(fn) {
resolved.then(fn);
return fn;
}
/**
toArray(object)
*/
function toArray(object) {
if (object.toArray) { return object.toArray(); }
// Speed test for array conversion:
// https://jsperf.com/nodelist-to-array/27
var array = [];
var l = object.length;
var i;
if (typeof object.length !== 'number') { return array; }
array.length = l;
for (i = 0; i < l; i++) {
array[i] = object[i];
}
return array;
}
const A$1 = Array.prototype;
const S = String.prototype;
/**
by(fn, a, b)
Compares `fn(a)` against `fn(b)` and returns `-1`, `0` or `1`. Useful for sorting
objects by property:
```
[{id: '2'}, {id: '1'}].sort(by(get('id'))); // [{id: '1'}, {id: '2'}]
```
**/
function by(fn, a, b) {
const fna = fn(a);
const fnb = fn(b);
return fnb === fna ? 0 : fna > fnb ? 1 : -1 ;
}
/**
byAlphabet(a, b)
Compares `a` against `b` alphabetically using the current locale alphabet.
**/
function byAlphabet(a, b) {
return S.localeCompare.call(a, b);
}
/**
each(fn, array)
Calls `fn` for each member in `array`.
**/
function each(fn, object) {
// A stricter version of .forEach, where the callback fn
// gets a single argument and no context.
var l, n;
if (typeof object.each === 'function') {
object.each(fn);
}
else {
l = object.length;
n = -1;
while (++n < l) { fn(object[n]); }
}
return object;
}
/**
map(fn, object)
Delegates to `object.map` or `Array.map` to return a new collection of mapped
values.
**/
function map(fn, object) {
return object && object.map ? object.map(fn) : A$1.map.call(object, fn) ;
}
/**
filter(fn, object)
Delegates to `object.filter` or `Array.filter` to return a new collection of
filtered objects.
**/
function filter(fn, object) {
return object.filter ?
object.filter(fn) :
A$1.filter.call(object, fn) ;
}
/**
reduce(fn, seed, object)
Delegates to `object.reduce` or `Array.reduce` to return a reduced value.
**/
function reduce(fn, seed, object) {
return object.reduce ?
object.reduce(fn, seed) :
A$1.reduce.call(object, fn, seed);
}
function sort(fn, object) {
return object.sort ? object.sort(fn) : A$1.sort.call(object, fn);
}
/**
concat(array2, array1)
Where JavaScript's Array.concat only works reliably on arrays, `concat`
will glue together any old array-like object.
**/
function concat(array2, array1) {
// A.concat only works with arrays - it does not flatten array-like
// objects. We need a robust concat that will glue any old thing
// together.
return Array.isArray(array1) ?
// 1 is an array. Convert 2 to an array if necessary
array1.concat(Array.isArray(array2) ? array2 : toArray(array2)) :
array1.concat ?
// It has it's own concat method. Lets assume it's robust
array1.concat(array2) :
// 1 is not an array, but 2 is
toArray(array1).concat(Array.isArray(array2) ? array2 : toArray(array2)) ;
}
function contains(value, object) {
return object.includes ?
object.includes(value) :
object.contains ?
object.contains(value) :
A$1.includes ?
A$1.includes.call(object, value) :
A$1.indexOf.call(object, value) !== -1 ;
}
function find(fn, object) {
return A$1.find.call(object, fn);
}
function slice(n, m, object) {
return object.slice ?
object.slice(n, m) :
A$1.slice.call(object, n, m) ;
}
/**
exec(regex, fn, string)
Calls `fn` with the result of `regex.exec(string)` if that result is not null,
and returns the resulting value.
*/
function exec(regex, fn, string) {
let data;
// If string looks like a regex result, get rest of string
// from latest index
if (typeof string !== 'string' && string.input !== undefined && string.index !== undefined) {
data = string;
string = data.input.slice(
string.index
+ string[0].length
+ (string.consumed || 0)
);
}
// Look for tokens
const tokens = regex.exec(string);
if (!tokens) { return; }
const output = fn(tokens);
// If we have a parent tokens object update its consumed count
if (data) {
data.consumed = (data.consumed || 0)
+ tokens.index
+ tokens[0].length
+ (tokens.consumed || 0) ;
}
return output;
}
var exec$1 = curry$1(exec, true);
function error(regex, reducers, string) {
if (string.input !== undefined && string.index !== undefined) {
string = string.input;
}
throw new Error('Cannot parse string "' + string + '"');
}
function reduce$1(reducers, acc, tokens) {
let n = -1;
while (++n < tokens.length) {
acc = (tokens[n] !== undefined && reducers[n]) ? reducers[n](acc, tokens) : acc ;
}
// Call the optional close fn
return reducers.close ?
reducers.close(acc, tokens) :
acc ;
}
/**
capture(regex, reducers, accumulator, string)
Parse `string` with `regex`, calling functions in `reducers` to modify
and return `accumulator`.
Reducers is an object of functions keyed by the index of their capturing
group in the regexp result (`0` corresponding to the entire regex match,
the first capturing group being at index `1`). Reducer functions are
called in capture order for all capturing groups that captured something.
Reducers may also define the function 'close', which is called at the end
of every capture. All reducer functions are passed the paremeters
`(accumulator, tokens)`, where `tokens` is the regexp result, and are expected
to return a value that is passed as an accumulator to the next reducer function.
Reducers may also define a function `'catch'`, which is called when a match
has not been made (where `'catch'` is not defined an error is thrown).
```js
const parseValue = capture(/^\s*(-?\d*\.?\d+)(\w+)?\s*$/, {
// Create a new accumulator object each call
0: () => ({}),
1: (acc, tokens) => {
acc.number = parseFloat(tokens[1]);
return acc;
},
2: (acc, tokens) => {
acc.unit = tokens[2];
return acc;
}
}, null);
const value = parseValue('36rem'); // { number: 36, unit: 'rem' }
```
*/
function capture(regex, reducers, acc, string) {
const output = exec(regex, (tokens) => reduce$1(reducers, acc, tokens), string);
// If tokens is undefined exec has failed apply regex to string
return output === undefined ?
// If there is a catch function, call it, otherwise error out
reducers.catch ?
reducers.catch(acc, string) :
error(regex, reducers, string) :
// Return the accumulator
output ;
}
curry$1(capture, true);
/**
choke(fn, time)
Returns a function that waits for `time` seconds without being invoked
before calling `fn` using the context and arguments from the latest
invocation.
*/
function choke(fn, time) {
var timer, context, args;
var cue = function cue() {
if (timer) { clearTimeout(timer); }
timer = setTimeout(update, (time || 0) * 1000);
};
function update() {
timer = false;
fn.apply(context, args);
}
function cancel() {
// Don't permit further changes to be queued
cue = noop;
// If there is an update queued apply it now
if (timer) { clearTimeout(timer); }
}
function wait() {
// Store the latest context and arguments
context = this;
args = arguments;
// Cue the update
cue();
}
wait.cancel = cancel;
return wait;
}
// Choke or wait? A simpler implementation without cancel(), I leave this here for reference...
// function choke(seconds, fn) {
// var timeout;
//
// function update(context, args) {
// fn.apply(context, args);
// }
//
// return function choke() {
// clearTimeout(timeout);
// timeout = setTimeout(update, seconds * 1000, this, arguments);
// };
// }
/**
compose(fn2, fn1)
Calls `fn1`, passes the result to `fn2`, and returns that result.
*/
function compose(fn2, fn1) {
return function compose() {
return fn2(fn1.apply(null, arguments));
};
}
function equals(a, b) {
// Fast out if references are for the same object
if (a === b) { return true; }
// If either of the values is null, or not an object, we already know
// they're not equal so get out of here
if (a === null ||
b === null ||
typeof a !== 'object' ||
typeof b !== 'object') {
return false;
}
// Compare their enumerable keys
const akeys = Object.keys(a);
let n = akeys.length;
while (n--) {
// Has the property been set to undefined on a?
if (a[akeys[n]] === undefined) {
// We don't want to test if it is an own property of b, as
// undefined represents an absence of value
if (b[akeys[n]] === undefined) {
return true;
}
}
else {
//
if (b.hasOwnProperty(akeys[n]) && !equals(a[akeys[n]], b[akeys[n]])) {
return false;
}
}
}
return true;
}
curry$1(equals, true);
function get(key, object) {
// Todo? Support WeakMaps and Maps and other map-like objects with a
// get method - but not by detecting the get method
return object[key];
}
var get$1 = curry$1(get, true);
var rpath = /\[?([-\w]+)(?:=(['"])([^\2]+)\2|(true|false)|((?:\d*\.)?\d+))?\]?\.?/g;
function findByProperty(key, value, array) {
var l = array.length;
var n = -1;
while (++n < l) {
if (array[n][key] === value) {
return array[n];
}
}
}
// Get path
function getRegexPathThing(regex, path, object, fn) {
var tokens = regex.exec(path);
if (!tokens) {
throw new Error('Fn.getPath(path, object): invalid path "' + path + '"');
}
var key = tokens[1];
var property = tokens[3] ?
findByProperty(key,
tokens[2] ? tokens[3] :
tokens[4] ? Boolean(tokens[4]) :
parseFloat(tokens[5]),
object) :
object[key] ;
return fn(regex, path, property);
}
function getRegexPath(regex, path, object) {
return regex.lastIndex === path.length ?
object :
!(object && typeof object === 'object') ?
undefined :
getRegexPathThing(regex, path, object, getRegexPath) ;
}
function getPath(path, object) {
rpath.lastIndex = 0;
return getRegexPath(rpath, path, object) ;
}
curry$1(getPath, true);
/**
has(key, value, object)
Returns `true` if `object[key]` is strictly equal to `value`.
*/
function has(key, value, object) {
return object[key] === value;
}
curry$1(has, true);
/**
id(value)
Returns `value`.
*/
function id(value) { return value; }
/**
invoke(name, parameters, object)
Invokes `object.name()` with `parameters` as arguments. For example:
```
models.forEach(invoke('save', [version]));
```
*/
function invoke(name, values, object) {
return object[name].apply(object, values);
}
curry$1(invoke, true);
const is = Object.is || function is(a, b) { return a === b; };
curry$1(is, true);
function latest(source) {
var value = source.shift();
return value === undefined ? arguments[1] : latest(source, value) ;
}
/**
matches(selector, object)
Where `selector` is an object containing properties to be compared against
properties of `object`. If they are all strictly equal, returns `true`,
otherwise `false`.
```
const vegeFoods = menu.filter(matches({ vegetarian: true }));
```
*/
function matches(object, item) {
let property;
for (property in object) {
if (object[property] !== item[property]) { return false; }
}
return true;
}
curry$1(matches, true);
const done = { done: true };
const iterator = { next: () => done };
var nothing = Object.freeze({
// Standard array methods
shift: noop,
push: noop,
join: function() { return ''; },
// Stream methods
start: noop,
stop: noop,
// Make it look like an empty array
length: 0,
// Make it an iterable with nothing in it
[Symbol.iterator]: () => iterator
});
function now$1() {
// Return time in seconds
return +new Date() / 1000;
}
/**
overload(fn, map)
Returns a function that calls a function at the property of `object` that
matches the result of calling `fn` with all arguments.</p>
```
var fn = overload(toType, {
string: function a(name, n) {...},
number: function b(n, m) {...}
});
fn('pie', 4); // Returns a('pie', 4)
fn(1, 2); // Returns b(1, 2)
```
*/
function overload(fn, map) {
return typeof map.get === 'function' ?
function overload() {
var key = fn.apply(null, arguments);
return map.get(key).apply(this, arguments);
} :
function overload() {
const key = fn.apply(null, arguments);
const handler = (map[key] || map.default);
if (!handler) { throw new Error('overload() no handler for "' + key + '"'); }
return handler.apply(this, arguments);
} ;
}
/**
parseValue(units, string)
Parse `string` as a value with a unit (such as `"3px"`). Parameter `units` is an
object of functions keyed by the unit postfix. It may also have a `catch`
function.
```js=
const value = parseValue({
px: function(n) {
return n;
},
catch: function(string) {
if (typeof string === 'number') {
return string;
}
throw new Error('Cannot parse px value');
}
}, '36px');
```
**/
// Be generous in what we accept, space-wise
const runit = /^\s*(-?\d*\.?\d+)(\w+|%)?\s*$/;
function parseValue(units, string) {
var entry = runit.exec(string);
if (!entry || !units[entry[2]]) {
if (!units.catch) {
throw new Error('Cannot parse value "' + string + '"');
}
return units.catch(string);
}
return units[entry[2]](parseFloat(entry[1]));
}
var parseVal = curry$1(parseValue);
function apply(value, fn) {
return fn(value);
}
/**
pipe(fn1, fn2, ...)
Returns a function that calls `fn1`, `fn2`, etc., passing the result of
calling one function to the next and returning the the last result.
*/
const A$2 = Array.prototype;
function pipe() {
const fns = arguments;
return fns.length ?
(value) => A$2.reduce.call(fns, apply, value) :
id ;
}
const $private = Symbol('private');
function privates(object) {
return object[$private] ?
object[$private] :
Object.defineProperty(object, $private, {
value: {}
})[$private] ;
}
/**
set(key, object, value)
```
// Set `input.value` whenever a value is pushed into a stream:
stream.scan(set('value'), input);
```
*/
function set(key, object, value) {
return typeof object.set === "function" ?
object.set(key, value) :
(object[key] = value) ;
}
var set$1 = curry$1(set, true);
var rpath$1 = /\[?([-\w]+)(?:=(['"])([^\2]+)\2|(true|false)|((?:\d*\.)?\d+))?\]?\.?/g;
function findByProperty$1(key, value, array) {
var l = array.length;
var n = -1;
while (++n < l) {
if (array[n][key] === value) {
return array[n];
}
}
}
function setRegexPath(regex, path, object, thing) {
var tokens = regex.exec(path);
if (!tokens) {
throw new Error('Fn.getPath(path, object): invalid path "' + path + '"');
}
var key = tokens[1];
if (regex.lastIndex === path.length) {
// Cannot set to [prop=value] selector
if (tokens[3]) {
throw new Error('Fn.setPath(path, object): invalid path "' + path + '"');
}
return object[key] = thing;
}
var value = tokens[3] ?
findByProperty$1(key,
tokens[2] ? tokens[3] :
tokens[4] ? Boolean(tokens[4]) :
parseFloat(tokens[5])
) :
object[key] ;
if (!(value && typeof value === 'object')) {
value = {};
if (tokens[3]) {
if (object.push) {
value[key] = tokens[2] ?
tokens[3] :
parseFloat(tokens[3]) ;
object.push(value);
}
else {
throw new Error('Not supported');
}
}
set(key, object, value);
}
return setRegexPath(regex, path, value, thing);
}
function setPath(path, object, value) {
rpath$1.lastIndex = 0;
return setRegexPath(rpath$1, path, object, value);
}
curry$1(setPath, true);
/**
toClass(object)
*/
const O = Object.prototype;
function toClass(object) {
return O.toString.apply(object).slice(8, -1);
}
/**
toFixed(number)
*/
const N = Number.prototype;
const isNaN = Number.isNaN;
function toFixed(n, value) {
if (isNaN(value)) {
return '';
// throw new Error('Fn.toFixed does not accept NaN.');
}
return N.toFixed.call(value, n);
}
curry$1(toFixed, true);
/**
toType(object)
Returns `typeof object`.
*/
function toType(object) {
return typeof object;
}
/**
prepend(string1, string2)
Returns `str1 + str2`.
**/
function prepend(string1, string2) {
return '' + string1 + string2;
}
var prepend$1 = curry$1(prepend);
const assign = Object.assign;
function isDone(source) {
return source.length === 0 || source.status === 'done' ;
}
function create(object, fn) {
var functor = Object.create(object);
functor.shift = fn;
return functor;
}
function arrayReducer(array, value) {
array.push(value);
return array;
}
function shiftTap(shift, fn) {
return function tap() {
var value = shift();
value !== undefined && fn(value);
return value;
};
}
function sortedSplice(array, fn, value) {
// Splices value into array at position determined by result of fn,
// where result is either in the range [-1, 0, 1] or [true, false]
var n = sortIndex(array, function(n) {
return fn(value, n);
});
array.splice(n, 0, value);
}
function sortIndex(array, fn) {
var l = array.length;
var n = l + l % 2;
var i = 0;
while ((n = Math.floor(n / 2)) && (i + n <= l)) {
if (fn(array[i + n - 1]) >= 0) {
i += n;
n += n % 2;
}
}
return i;
}