-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
303 lines (248 loc) · 7.31 KB
/
index.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
;(function (global) {
'use strict'
// This is the hero who's going to save the day!
var reduce = Array.prototype.reduce
// This is his helper, consider it the Robin.
var copier = Array.prototype.slice
// This is a ordinary butler. :P
var toString = Object.prototype.toString
var _ = {
// Here are some utility functions.
// Alone they look like they don't perform anything complex,
// but combined as predicates they do a lot.
//
// Just so you know
util: {
asc: function (a, b) {
return a > b
},
desc: function (a, b) {
return a < b
},
truthy: function (value) {
return value === true
},
falsy: function (value) {
return value === false
},
first: function (collection) {
return collection[0]
},
last: function (collection) {
return collection[collection.length - 1]
},
identity: function (value) {
return value
},
},
chain: function (collection) {
function chainer(collection) {
var self = {
get: function () {
return collection
}
}
var prototype = _.keys(_)
_.each(prototype, function (prop) {
if (typeof _[prop] === 'function') {
self[prop] = function () {
var args = _.toArray(arguments)
args.unshift(collection)
return chainer(_[prop].apply(null, args))
}
} else {
self[prop] = _[prop]
}
})
return self
}
return chainer(collection)
},
isArray: function (value) {
return toString.call(value) === '[object Array]'
},
count: function (collection) {
return _.toArray(collection).length
},
contains: function (collection, value) {
return Boolean(~collection.indexOf(value))
},
reduce: function (collection, predicate, initialValue) {
return arguments.length > 2
? reduce.call(collection, predicate, initialValue)
: reduce.call(collection, predicate)
},
each: function (collection, predicate) {
return _.reduce(collection, function (collection, value, index) {
predicate(value, index, collection)
}, undefined)
},
map: function (collection, predicate) {
return _.reduce(collection, function (collection, value, index) {
collection.push(predicate(value, index, collection))
return collection
}, [])
},
filter: function (collection, predicate) {
return _.reduce(collection, function (collection, value, index) {
if (_.util.truthy(predicate(value, index, collection)))
collection.push(value)
return collection
}, [])
},
some: function (collection, predicate) {
return _.chain(collection)
.map(predicate)
.filter(_.util.truthy)
.get()
.length > 0
},
every: function (collection, predicate) {
return _.chain(collection)
.map(predicate)
.filter(_.util.truthy)
.get()
.length === collection.length
},
none: function (collection, predicate) {
return _.chain(collection)
.map(predicate)
.filter(_.util.falsy)
.get()
.length === collection.length
},
flatten: function (collection) {
return _.reduce(collection, function (collection, value) {
return _.union(collection, value)
}, [])
},
unique: function (collection) {
return _.reduce(collection, function (collection, value) {
if (!_.contains(collection, value))
collection.push(value)
return collection
}, [])
},
toArray: function (arraylike) {
return copier.call(arraylike)
},
groupBy: function (collection, key) {
return _.chain(collection)
.extract(key)
.unique()
.map(function (group) {
return _.filter(collection, function (item) {
return item[key] === group
})
})
.get()
},
extract: function (collection, key) {
return _.map(collection, function (item) {
return item[key]
})
},
head: function (collection) {
return _.util.first(collection)
},
tail: function (collection) {
return _.filter(collection, function (_, i) {
return i > 0
})
},
take: function (collection, until) {
return _.filter(collection, function (_, i) {
return i < until
})
},
takeAt: function (collection, indexes) {
indexes = _.isArray(indexes) ? indexes : [indexes]
return _.filter(collection, function (value, i) {
return _.contains(indexes, i)
})
},
intersection: function (collection1 /*, collection2, collectionN */) {
const collections = _.toArray(arguments)
return _.reduce(collections, function (intersecting, collection) {
return _.filter(intersecting, function (item) {
return _.contains(collection, item)
})
}, collections[0] || [])
},
union: function (collection1 /*, collection2, collectionN */) {
const collections = _.toArray(arguments)
return _.reduce(collections, function (unioning, collection) {
collection = _.isArray(collection) ? collection : [collection]
_.each(collection, function (item) {
unioning.push(item)
})
return unioning
}, [])
},
difference: function (collection1, collection2) {
return _.union(
_.filter(collection1, function (item) {
return !_.contains(collection2, item)
}),
_.filter(collection2, function (item) {
return !_.contains(collection1, item)
})
)
},
find: function (collection, predicate) {
return _.chain(collection)
.filter(predicate)
.head()
.get()
},
fill: function (collection, target) {
return _.map(collection, function (value, index, collection) {
return typeof target === 'function'
? target(value, index, collection)
: target
})
},
reverse: function (collection) {
return _.chain(collection)
.keys()
.sort(_.util.desc)
.map(function (index) {
return collection[index]
})
.get()
},
sort: function (collection, predicate) {
return collection.sort(predicate)
},
max: function (collection) {
return _.reduce(collection, function (current, value) {
return current < value
? value
: current
}, _.util.first(collection) || Infinity)
},
min: function (collection) {
return _.reduce(collection, function (current, value) {
return current > value
? value
: current
}, _.util.first(collection) || -Infinity)
},
keys: function (collection) {
var hasOwnProp = Object.prototype.hasOwnProperty
var keys = []
for (var key in collection) {
if (hasOwnProp.call(collection, key)) {
keys.push(key)
}
}
return keys
},
}
// If it's a nodejs enviroment, export it. Otherwise assume enviroment is a browser.
if (module && module.exports) {
module.exports = _
} else {
global._ = global.reduceIt = _
}
}(this));