Skip to main content
Module

x/rambda/source/map.js

Faster and smaller alternative to Ramda
Go to Latest
File
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)}