-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlodash-contrib.js
1975 lines (1653 loc) · 56.3 KB
/
lodash-contrib.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
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
module.exports = function(_) {
// Create quick reference variables for speed access to core prototypes.
var slice = Array.prototype.slice,
concat = Array.prototype.concat,
sort = Array.prototype.sort;
var existy = function(x) { return x != null; };
// Mixing in the array builders
// ----------------------------
_.mixin({
// Concatenates one or more arrays given as arguments. If given objects and
// scalars as arguments `cat` will plop them down in place in the result
// array. If given an `arguments` object, `cat` will treat it like an array
// and concatenate it likewise.
cat: function() {
return _.reduce(arguments, function(acc, elem) {
if (_.isArguments(elem)) {
return concat.call(acc, slice.call(elem));
}
else {
return concat.call(acc, elem);
}
}, []);
},
// 'Constructs' an array by putting an element at its front
cons: function(head, tail) {
return _.cat([head], tail);
},
// Takes an array and chunks it some number of times into
// sub-arrays of size n. Allows and optional padding array as
// the third argument to fill in the tail chunk when n is
// not sufficient to build chunks of the same size.
chunkContrib: function(array, n, pad) {
var args = arguments;
var p = function(array) {
if (array == null) return [];
var part = _.take(array, n);
if (n === _.size(part)) {
return _.cons(part, p(_.drop(array, n)));
}
else if (args.length === 3) {
pad = _.isArray(pad) ? pad : _.repeatContrib(n, pad);
return [_.take(_.cat(part, pad), n)];
}
else {
return [];
}
};
return p(array);
},
// Takes an array and chunks it some number of times into
// sub-arrays of size n. If the array given cannot fill the size
// needs of the final chunk then a smaller chunk is used
// for the last.
chunkAll: function(array, n, step) {
step = (step != null) ? step : n;
var p = function(array, n, step) {
if (_.isEmpty(array)) return [];
return _.cons(_.take(array, n),
p(_.drop(array, step), n, step));
};
return p(array, n, step);
},
// Maps a function over an array and concatenates all of the results.
mapcat: function(array, fun) {
return _.cat.apply(null, _.map(array, fun));
},
// Returns an array with some item between each element
// of a given array.
interpose: function(array, inter) {
if (!_.isArray(array)) throw new TypeError;
var sz = _.size(array);
if (sz === 0) return array;
if (sz === 1) return array;
return slice.call(_.mapcat(array, function(elem) {
return _.cons(elem, [inter]);
}), 0, -1);
},
// Weaves two or more arrays together
weave: function(/* args */) {
if (!_.some(arguments)) return [];
if (arguments.length == 1) return arguments[0];
return _.filter(_.flatten(_.zip.apply(null, arguments), false), function(elem) {
return elem != null;
});
},
interleave: _.weave,
// Returns an array of a value repeated a certain number of
// times.
repeatContrib: function(t, elem) {
return _.times(t, function() { return elem; });
},
// Returns an array built from the contents of a given array repeated
// a certain number of times.
cycle: function(t, elems) {
return _.flatten(_.times(t, function() { return elems; }), true);
},
// Returns an array with two internal arrays built from
// taking an original array and spliting it at an index.
splitAt: function(array, index) {
return [_.take(array, index), _.drop(array, index)];
},
// Call a function recursively f(f(f(args))) until a second
// given function goes falsey. Expects a seed value to start.
iterateUntil: function(doit, checkit, seed) {
var ret = [];
var result = doit(seed);
while (checkit(result)) {
ret.push(result);
result = doit(result);
}
return ret;
},
// Takes every nth item from an array, returning an array of
// the results.
takeSkipping: function(array, n) {
var ret = [];
var sz = _.size(array);
if (n <= 0) return [];
if (n === 1) return array;
for(var index = 0; index < sz; index += n) {
ret.push(array[index]);
}
return ret;
},
// Returns an array of each intermediate stage of a call to
// a `reduce`-like function.
reductions: function(array, fun, init) {
var ret = [];
var acc = init;
_.each(array, function(v,k) {
acc = fun(acc, array[k]);
ret.push(acc);
});
return ret;
},
// Runs its given function on the index of the elements rather than
// the elements themselves, keeping all of the truthy values in the end.
keepIndexed: function(array, pred) {
return _.filter(_.map(_.range(_.size(array)), function(i) {
return pred(i, array[i]);
}),
existy);
},
// Accepts an array-like object (other than strings) as an argument and
// returns an array whose elements are in the reverse order. Unlike the
// built-in `Array.prototype.reverse` method, this does not mutate the
// original object. Note: attempting to use this method on a string will
// result in a `TypeError`, as it cannot properly reverse unicode strings.
reverseOrder: function(obj) {
if (typeof obj == 'string')
throw new TypeError('Strings cannot be reversed by _.reverseOrder');
return slice.call(obj).reverse();
},
// Returns copy or array sorted according to arbitrary ordering
// order must be an array of values; defines the custom sort
// key must be one of: missing/null, a string, or a function;
collate: function(array, order, key) {
if (!_.isArray(array)) throw new TypeError("expected an array as the first argument");
if (!_.isArray(order)) throw new TypeError("expected an array as the second argument");
return sort.call(array, function (a, b) {
if(_.isFunction(key)) {
valA = key.call(a);
valB = key.call(b);
} else if(existy(key)) {
valA = a[key];
valB = b[key];
} else {
valA = a;
valB = b;
}
var rankA = _.indexOf(order, valA);
var rankB = _.indexOf(order, valB);
if(rankA === -1) return 1;
if(rankB === -1) return -1;
return rankA - rankB;
});
}
});
};
},{}],2:[function(require,module,exports){
module.exports = function(_) {
// Create quick reference variables for speed access to core prototypes.
var slice = Array.prototype.slice,
concat = Array.prototype.concat;
var existy = function(x) { return x != null; };
var truthy = function(x) { return (x !== false) && existy(x); };
var isSeq = function(x) { return (_.isArray(x)) || (_.isArguments(x)); };
function nths(array, indices) {
if (array == null) return void 0;
if (isSeq(indices))
return _(indices).map(function(i){return array[i];}).valueOf();
else
return nths(array, slice.call(arguments, 1));
}
// Mixing in the array selectors
// ----------------------------
_.mixin({
// Returns the second element of an array. Passing **n** will return all but
// the first of the head N values in the array. The **guard** check allows it
// to work with `_.map`.
second: function(array, n, guard) {
if (array == null) return void 0;
return (n != null) && !guard ? slice.call(array, 1, n) : array[1];
},
// Returns the third element of an array. Passing **n** will return all but
// the first two of the head N values in the array. The **guard** check allows it
// to work with `_.map`.
third: function(array, n, guard) {
if (array == null) return void 0;
return (n != null) && !guard ? slice.call(array, 2, n) : array[2];
},
// A function to get at an index into an array
nth: function(array, index, guard) {
if ((index != null) && !guard) return array[index];
},
// A function to get values via indices into an array
nths: nths,
valuesAt: nths,
// A function to get at "truthily" indexed values
// bin refers to "binary" nature of true/false values in binIndices
// but can also be thought of as putting array values into either "take" or "don't" bins
binPick: function binPick(array, binIndices) {
if (array == null) return void 0;
if (isSeq(binIndices))
return _.nths(array, _.range(binIndices.length).filter(function(i){return binIndices[i];}));
else
return binPick(array, slice.call(arguments, 1));
},
// Returns an array with two internal arrays built from
// taking an original array and spliting it at the index
// where a given function goes falsey.
splitWith: function(array, pred) {
return [_.takeWhile(array, pred), _.dropWhile(array, pred)];
},
// Takes an array and partitions it as the given predicate changes
// truth sense.
partitionBy: function(array, fun){
if (_.isEmpty(array) || !existy(array)) return [];
var fst = _.first(array);
var fstVal = fun(fst);
var run = concat.call([fst], _.takeWhile(_.rest(array), function(e) {
return _.isEqual(fstVal, fun(e));
}));
return concat.call([run], _.partitionBy(_.drop(array, _.size(run)), fun));
},
// Returns the 'best' value in an array based on the result of a
// given function.
best: function(array, fun) {
return _.reduce(array, function(x, y) {
return fun(x, y) ? x : y;
});
},
// Returns an array of existy results of a function over an source array.
keep: function(array, fun) {
if (!isSeq(array)) throw new TypeError("expected an array as the first argument");
return _.filter(_.map(array, function(e) {
return fun(e);
}), existy);
}
});
};
},{}],3:[function(require,module,exports){
module.exports = function (_) {
// Helpers
// -------
// An internal object that can be returned from a visitor function to
// prevent a top-down walk from walking subtrees of a node.
var stopRecursion = {};
// An internal object that can be returned from a visitor function to
// cause the walk to immediately stop.
var stopWalk = {};
var notTreeError = 'Not a tree: same object found in two different branches';
// Implements the default traversal strategy: if `obj` is a DOM node, walk
// its DOM children; otherwise, walk all the objects it references.
function defaultTraversal(obj) {
return _.isElement(obj) ? obj.children : obj;
}
// Walk the tree recursively beginning with `root`, calling `beforeFunc`
// before visiting an objects descendents, and `afterFunc` afterwards.
// If `collectResults` is true, the last argument to `afterFunc` will be a
// collection of the results of walking the node's subtrees.
function walkImpl(root, traversalStrategy, beforeFunc, afterFunc, context, collectResults) {
var visited = [];
return (function _walk(value, key, parent) {
// Keep track of objects that have been visited, and throw an exception
// when trying to visit the same object twice.
if (_.isObject(value)) {
if (visited.indexOf(value) >= 0) throw new TypeError(notTreeError);
visited.push(value);
}
if (beforeFunc) {
var result = beforeFunc.call(context, value, key, parent);
if (result === stopWalk) return stopWalk;
if (result === stopRecursion) return;
}
var subResults;
var target = traversalStrategy(value);
if (_.isObject(target) && !_.isEmpty(target)) {
// If collecting results from subtrees, collect them in the same shape
// as the parent node.
if (collectResults) subResults = _.isArray(value) ? [] : {};
var stop = _.any(target, function(obj, key) {
var result = _walk(obj, key, value);
if (result === stopWalk) return true;
if (subResults) subResults[key] = result;
});
if (stop) return stopWalk;
}
if (afterFunc) return afterFunc.call(context, value, key, parent, subResults);
})(root);
}
// Internal helper providing the implementation for `pluck` and `pluckRec`.
function pluck(obj, propertyName, recursive) {
var results = [];
this.preorder(obj, function(value, key) {
if (!recursive && key == propertyName)
return stopRecursion;
if (_.has(value, propertyName))
results[results.length] = value[propertyName];
});
return results;
}
var exports = {
// Performs a preorder traversal of `obj` and returns the first value
// which passes a truth test.
find: function(obj, visitor, context) {
var result;
this.preorder(obj, function(value, key, parent) {
if (visitor.call(context, value, key, parent)) {
result = value;
return stopWalk;
}
}, context);
return result;
},
// Recursively traverses `obj` and returns all the elements that pass a
// truth test. `strategy` is the traversal function to use, e.g. `preorder`
// or `postorder`.
filter: function(obj, strategy, visitor, context) {
var results = [];
if (obj == null) return results;
strategy(obj, function(value, key, parent) {
if (visitor.call(context, value, key, parent)) results.push(value);
}, null, this._traversalStrategy);
return results;
},
// Recursively traverses `obj` and returns all the elements for which a
// truth test fails.
reject: function(obj, strategy, visitor, context) {
return this.filter(obj, strategy, function(value, key, parent) {
return !visitor.call(context, value, key, parent);
});
},
// Produces a new array of values by recursively traversing `obj` and
// mapping each value through the transformation function `visitor`.
// `strategy` is the traversal function to use, e.g. `preorder` or
// `postorder`.
map: function(obj, strategy, visitor, context) {
var results = [];
strategy(obj, function(value, key, parent) {
results[results.length] = visitor.call(context, value, key, parent);
}, null, this._traversalStrategy);
return results;
},
// Return the value of properties named `propertyName` reachable from the
// tree rooted at `obj`. Results are not recursively searched; use
// `pluckRec` for that.
pluck: function(obj, propertyName) {
return pluck.call(this, obj, propertyName, false);
},
// Version of `pluck` which recursively searches results for nested objects
// with a property named `propertyName`.
pluckRec: function(obj, propertyName) {
return pluck.call(this, obj, propertyName, true);
},
// Recursively traverses `obj` in a depth-first fashion, invoking the
// `visitor` function for each object only after traversing its children.
// `traversalStrategy` is intended for internal callers, and is not part
// of the public API.
postorder: function(obj, visitor, context, traversalStrategy) {
traversalStrategy = traversalStrategy || this._traversalStrategy;
walkImpl(obj, traversalStrategy, null, visitor, context);
},
// Recursively traverses `obj` in a depth-first fashion, invoking the
// `visitor` function for each object before traversing its children.
// `traversalStrategy` is intended for internal callers, and is not part
// of the public API.
preorder: function(obj, visitor, context, traversalStrategy) {
traversalStrategy = traversalStrategy || this._traversalStrategy;
walkImpl(obj, traversalStrategy, visitor, null, context);
},
// Builds up a single value by doing a post-order traversal of `obj` and
// calling the `visitor` function on each object in the tree. For leaf
// objects, the `memo` argument to `visitor` is the value of the `leafMemo`
// argument to `reduce`. For non-leaf objects, `memo` is a collection of
// the results of calling `reduce` on the object's children.
reduce: function(obj, visitor, leafMemo, context) {
var reducer = function(value, key, parent, subResults) {
return visitor(subResults || leafMemo, value, key, parent);
};
return walkImpl(obj, this._traversalStrategy, null, reducer, context, true);
}
};
// Set up aliases to match those in lodash.js.
exports.collect = exports.map;
exports.detect = exports.find;
exports.select = exports.filter;
// Returns an object containing the walk functions. If `traversalStrategy`
// is specified, it is a function determining how objects should be
// traversed. Given an object, it returns the object to be recursively
// walked. The default strategy is equivalent to `_.identity` for regular
// objects, and for DOM nodes it returns the node's DOM children.
function walk(traversalStrategy) {
var walker = _.clone(exports);
// Bind all of the public functions in the walker to itself. This allows
// the traversal strategy to be dynamically scoped.
_.bindAll.apply(null, [walker].concat(_.keys(walker)));
walker._traversalStrategy = traversalStrategy || defaultTraversal;
return walker;
}
// Use `_.walk` as a namespace to hold versions of the walk functions which
// use the default traversal strategy.
_.extend(walk, walk());
_.mixin({walk: walk});
};
},{}],4:[function(require,module,exports){
module.exports = function (_) {
// Helpers
// -------
function enforcesUnary(fn) {
return function mustBeUnary() {
if (arguments.length === 1) {
return fn.apply(this, arguments);
}
else throw new RangeError('Only a single argument may be accepted.');
};
}
// Curry
// -------
var curry = (function () {
function collectArgs(func, that, argCount, args, newArg, reverse) {
if (reverse === true) {
args.unshift(newArg);
} else {
args.push(newArg);
}
if (args.length == argCount) {
return func.apply(that, args);
} else {
return enforcesUnary(function () {
return collectArgs(func, that, argCount, args.slice(0), arguments[0], reverse);
});
}
}
return function curry(func, reverse) {
var that = this;
return enforcesUnary(function () {
return collectArgs(func, that, func.length, [], arguments[0], reverse);
});
};
}());
// Enforce Arity
// --------------------
var enforce = (function () {
var CACHE = [];
return function enforce(func) {
if (typeof func !== 'function') {
throw new Error('Argument 1 must be a function.');
}
var funcLength = func.length;
if (CACHE[funcLength] === undefined) {
CACHE[funcLength] = function (enforceFunc) {
return function () {
if (arguments.length !== funcLength) {
throw new RangeError(funcLength + ' arguments must be applied.');
}
return enforceFunc.apply(this, arguments);
};
};
}
return CACHE[funcLength](func);
};
}());
// Right curry variants
// ---------------------
var curryRight = function (func) {
return curry.call(this, func, true);
};
var curryRight2 = function (fun) {
return enforcesUnary(function (last) {
return enforcesUnary(function (first) {
return fun.call(this, first, last);
});
});
};
var curryRight3 = function (fun) {
return enforcesUnary(function (last) {
return enforcesUnary(function (second) {
return enforcesUnary(function (first) {
return fun.call(this, first, second, last);
});
});
});
};
// Mixing in the arity functions
// -----------------------------
_.mixin({
// ### Fixed arguments
// Fixes the arguments to a function based on the parameter template defined by
// the presence of values and the `_` placeholder.
fix: function (fun) {
var fixArgs = _.tail(arguments);
var f = function () {
var args = fixArgs.slice();
var arg = 0;
for (var i = 0; i < (args.length || arg < arguments.length); i++) {
if (args[i] === _) {
args[i] = arguments[arg++];
}
}
return fun.apply(null, args);
};
f._original = fun;
return f;
},
unary: function (fun) {
return function unary(a) {
return fun.call(this, a);
};
},
binary: function (fun) {
return function binary(a, b) {
return fun.call(this, a, b);
};
},
ternary: function (fun) {
return function ternary(a, b, c) {
return fun.call(this, a, b, c);
};
},
quaternary: function (fun) {
return function quaternary(a, b, c, d) {
return fun.call(this, a, b, c, d);
};
},
rCurry: curryRight, // alias for backwards compatibility
curry2: function (fun) {
return enforcesUnary(function curried(first) {
return enforcesUnary(function (last) {
return fun.call(this, first, last);
});
});
},
curry3: function (fun) {
return enforcesUnary(function (first) {
return enforcesUnary(function (second) {
return enforcesUnary(function (last) {
return fun.call(this, first, second, last);
});
});
});
},
// reverse currying for functions taking two arguments.
curryRight2: curryRight2,
rcurry2: curryRight2, // alias for backwards compatibility
curryRight3: curryRight3,
rcurry3: curryRight3, // alias for backwards compatibility
// Dynamic decorator to enforce function arity and defeat varargs.
enforce: enforce,
arity: (function () {
// Allow 'new Function', as that is currently the only reliable way
// to manipulate function.length
/* jshint -W054 */
var FUNCTIONS = {};
return function arity(numberOfArgs, fun) {
if (FUNCTIONS[numberOfArgs] == null) {
var parameters = new Array(numberOfArgs);
for (var i = 0; i < numberOfArgs; ++i) {
parameters[i] = "__" + i;
}
var pstr = parameters.join();
var code = "return function (" + pstr + ") { return fun.apply(this, arguments); };";
FUNCTIONS[numberOfArgs] = new Function(['fun'], code);
}
if (fun == null) {
return function (fun) { return arity(numberOfArgs, fun); };
}
else return FUNCTIONS[numberOfArgs](fun);
};
})()
});
};
},{}],5:[function(require,module,exports){
module.exports = function (_) {
// Helpers
// -------
var existy = function(x) { return x != null; };
var truthy = function(x) { return (x !== false) && existy(x); };
var __reverse = [].reverse;
var __slice = [].slice;
var __map = [].map;
var curry2 = function (fun) {
return function curried (first, optionalLast) {
if (arguments.length === 1) {
return function (last) {
return fun(first, last);
};
}
else return fun(first, optionalLast);
};
};
// n.b. depends on lodash.function.arity.js
// Takes a target function and a mapping function. Returns a function
// that applies the mapper to its arguments before evaluating the body.
function baseMapArgs (fun, mapFun) {
return _.arity(fun.length, function () {
return fun.apply(this, __map.call(arguments, mapFun));
});
}
// Mixing in the combinator functions
// ----------------------------------
_.mixin({
// Provide "always" alias for backwards compatibility
always: _.constant,
// Takes some number of functions, either as an array or variadically
// and returns a function that takes some value as its first argument
// and runs it through a pipeline of the original functions given.
pipeline: function(/*, funs */){
var funs = (_.isArray(arguments[0])) ? arguments[0] : arguments;
return function(seed) {
return _.reduce(funs,
function(l,r) { return r(l); },
seed);
};
},
composeRight: _.pipeline,
// Composes a bunch of predicates into a single predicate that
// checks all elements of an array for conformance to all of the
// original predicates.
conjoin: function(/* preds */) {
var preds = arguments;
return function(array) {
return _.every(array, function(e) {
return _.every(preds, function(p) {
return p(e);
});
});
};
},
// Composes a bunch of predicates into a single predicate that
// checks all elements of an array for conformance to any of the
// original predicates.
disjoin: function(/* preds */) {
var preds = arguments;
return function(array) {
return _.some(array, function(e) {
return _.some(preds, function(p) {
return p(e);
});
});
};
},
// Takes a predicate-like and returns a comparator (-1,0,1).
comparator: function(fun) {
return function(x, y) {
if (truthy(fun(x, y)))
return -1;
else if (truthy(fun(y, x)))
return 1;
else
return 0;
};
},
// Returns a function that reverses the sense of a given predicate-like.
complement: function(pred) {
return function() {
return !pred.apply(this, arguments);
};
},
// Takes a function expecting varargs and
// returns a function that takes an array and
// uses its elements as the args to the original
// function
splat: function(fun) {
return function(array) {
return fun.apply(this, array);
};
},
// Takes a function expecting an array and returns
// a function that takes varargs and wraps all
// in an array that is passed to the original function.
unsplat: function(fun) {
var funLength = fun.length;
if (funLength < 1) {
return fun;
}
else if (funLength === 1) {
return function () {
return fun.call(this, __slice.call(arguments, 0));
};
}
else {
return function () {
var numberOfArgs = arguments.length,
namedArgs = __slice.call(arguments, 0, funLength - 1),
numberOfMissingNamedArgs = Math.max(funLength - numberOfArgs - 1, 0),
argPadding = new Array(numberOfMissingNamedArgs),
variadicArgs = __slice.call(arguments, fun.length - 1);
return fun.apply(this, namedArgs.concat(argPadding).concat([variadicArgs]));
};
}
},
// Same as unsplat, but the rest of the arguments are collected in the
// first parameter, e.g. unsplatl( function (args, callback) { ... ]})
unsplatl: function(fun) {
var funLength = fun.length;
if (funLength < 1) {
return fun;
}
else if (funLength === 1) {
return function () {
return fun.call(this, __slice.call(arguments, 0));
};
}
else {
return function () {
var numberOfArgs = arguments.length,
namedArgs = __slice.call(arguments, Math.max(numberOfArgs - funLength + 1, 0)),
variadicArgs = __slice.call(arguments, 0, Math.max(numberOfArgs - funLength + 1, 0));
return fun.apply(this, [variadicArgs].concat(namedArgs));
};
}
},
// map the arguments of a function
mapArgs: curry2(baseMapArgs),
// Returns a function that returns an array of the calls to each
// given function for some arguments.
juxt: function(/* funs */) {
var funs = arguments;
return function(/* args */) {
var args = arguments;
return _.map(funs, function(f) {
return f.apply(this, args);
}, this);
};
},
// Returns a function that protects a given function from receiving
// non-existy values. Each subsequent value provided to `fnull` acts
// as the default to the original function should a call receive non-existy
// values in the defaulted arg slots.
fnull: function(fun /*, defaults */) {
var defaults = _.rest(arguments);
return function(/*args*/) {
var args = _.toArray(arguments);
var sz = _.size(defaults);
for(var i = 0; i < sz; i++) {
if (!existy(args[i]))
args[i] = defaults[i];
}
return fun.apply(this, args);
};
},
// Flips the first two args of a function
flip2: function(fun) {
return function(/* args */) {
var flipped = __slice.call(arguments);
flipped[0] = arguments[1];
flipped[1] = arguments[0];
return fun.apply(this, flipped);
};
},
// Flips an arbitrary number of args of a function
flip: function(fun) {
return function(/* args */) {
var reversed = __reverse.call(arguments);
return fun.apply(this, reversed);
};
},
// Takes a method-style function (one which uses `this`) and pushes
// `this` into the argument list. The returned function uses its first
// argument as the receiver/context of the original function, and the rest
// of the arguments are used as the original's entire argument list.
functionalize: function(method) {
return function(ctx /*, args */) {
return method.apply(ctx, _.rest(arguments));
};
},
// Takes a function and pulls the first argument out of the argument
// list and into `this` position. The returned function calls the original
// with its receiver (`this`) prepending the argument list. The original
// is called with a receiver of `null`.
methodize: function(func) {
return function(/* args */) {
return func.apply(null, _.cons(this, arguments));
};
},
k: _.always,
t: _.pipeline
});
_.unsplatr = _.unsplat;
// map the arguments of a function, takes the mapping function
// first so it can be used as a combinator
_.mapArgsWith = curry2(_.flip(baseMapArgs));
// Returns function property of object by name, bound to object
_.bound = function(obj, fname) {
var fn = obj[fname];
if (!_.isFunction(fn))
throw new TypeError("Expected property to be a function");
return _.bind(fn, obj);
};
};
},{}],6:[function(require,module,exports){
module.exports = function (_) {
// Helpers
// -------
var HASNTBEENRUN = {};
function unary (fun) {
return function (first) {
return fun.call(this, first);
};
}
function binary (fun) {
return function (first, second) {
return fun.call(this, first, second);
};
}
// Mixing in the iterator functions
// --------------------------------
function foldl (iter, binaryFn, seed) {
var state, element;
if (seed !== void 0) {
state = seed;
}
else {
state = iter();
}
element = iter();