UNPKG

rambdax

Version:

Extended version of Rambda - a lightweight, faster alternative to Ramda

30 lines (23 loc) 592 B
import { path } from './path' function indexByPath(pathInput, list){ const toReturn = {} for (let i = 0; i < list.length; i++){ const item = list[ i ] toReturn[ path(pathInput, item) ] = item } return toReturn } export function indexBy(condition, list){ if (arguments.length === 1){ return _list => indexBy(condition, _list) } if (typeof condition === 'string'){ return indexByPath(condition, list) } const toReturn = {} for (let i = 0; i < list.length; i++){ const item = list[ i ] toReturn[ condition(item) ] = item } return toReturn }