-
Notifications
You must be signed in to change notification settings - Fork 87
/
map.js
43 lines (32 loc) · 928 Bytes
/
map.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
import {_isArray} from './_internals/_isArray'
import {_keys} from './_internals/_keys'
export function mapArray(fn, list, isIndexed = false) {
let index = 0
const willReturn = Array(list.length)
while (index < list.length) {
willReturn[index] = isIndexed ? fn(list[index], index) : fn(list[index])
index++
}
return willReturn
}
export function mapObject(fn, obj) {
let index = 0
const keys = _keys(obj)
const len = keys.length
const willReturn = {}
while (index < len) {
const key = keys[index]
willReturn[key] = fn(obj[key], key, obj)
index++
}
return willReturn
}
export const mapObjIndexed = mapObject
export function map(fn, iterable) {
if (arguments.length === 1) return _iterable => map(fn, _iterable)
if (!iterable) {
throw new Error('Incorrect iterable input')
}
if (_isArray(iterable)) return mapArray(fn, iterable)
return mapObject(fn, iterable)
}