UNPKG

537 BJavaScriptView Raw
1function map (collection, predicate) {
2 if (!collection) return []
3 if (!predicate) {
4 predicate = function (a) {
5 return a
6 }
7 }
8
9 var out = []
10 if (Array.isArray(collection)) {
11 for (var i = 0; i < collection.length; i++) {
12 out.push(predicate(collection[i], i))
13 }
14 return out
15 }
16
17 if (typeof collection === 'object') {
18 var keys = Object.keys(collection)
19 for (var i = 0; i < keys.length; i++) {
20 var k = keys[i]
21 out.push(predicate(collection[k], k))
22 }
23 return out
24 }
25
26 return out
27}
28
29module.exports = { map: map }