Repository navigation
Expand file tree
/
Copy pathunderscore.array.selectors.js
More file actions
117 lines (91 loc) · 3.76 KB
/
Copy pathunderscore.array.selectors.js
File metadata and controls
117 lines (91 loc) · 3.76 KB
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
// Underscore-contrib (underscore.array.selectors.js 0.0.1)
// (c) 2013 Michael Fogus, DocumentCloud and Investigative Reporters & Editors
// Underscore-contrib may be freely distributed under the MIT license.
(function(root) {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `global` on the server.
var _ = root._ || require('underscore');
// Helpers
// -------
// 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)); };
// 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];
},
// Takes all items in an array while a given predicate returns truthy.
takeWhile: function(array, pred) {
if (!isSeq(array)) throw new TypeError;
var sz = _.size(array);
for (var index = 0; index < sz; index++) {
if(!truthy(pred(array[index]))) {
break;
}
}
return _.take(array, index);
},
// Drops all items from an array while a given predicate returns truthy.
dropWhile: function(array, pred) {
if (!isSeq(array)) throw new TypeError;
var sz = _.size(array);
for (var index = 0; index < sz; index++) {
if(!truthy(pred(array[index])))
break;
}
return _.drop(array, index);
},
// 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(pred, array), _.dropWhile(pred, array)];
},
// 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);
}
});
})(this);