forked from documentcloud/underscore-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
underscore.array.selectors.js
142 lines (112 loc) · 4.65 KB
/
underscore.array.selectors.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
// Underscore-contrib (underscore.array.selectors.js 0.3.0)
// (c) 2013 Michael Fogus, DocumentCloud and Investigative Reporters & Editors
// Underscore-contrib may be freely distributed under the MIT license.
(function() {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `require` it on the server.
if (typeof exports === 'object') {
_ = module.exports = 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];
},
// A function to get values via indices into an array
nths: nths = function(array, indices) {
if (array == null) return void 0;
if (isSeq(indices))
return _(indices).map(function(i){return array[i];});
else
return nths(array, slice.call(arguments, 1));
},
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));
},
// 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(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);
}
});
}).call(this);