UNPKG

rambda

Version:

Lightweight faster alternative to Ramda

39 lines (29 loc) 669 B
function filterObject(fn, obj){ const willReturn = {} for (const prop in obj){ if (fn( obj[ prop ], prop, obj )){ willReturn[ prop ] = obj[ prop ] } } return willReturn } export function filter(predicate, list){ if (arguments.length === 1) return _list => filter(predicate, _list) if (!list) return [] if (!Array.isArray(list)){ return filterObject(predicate, list) } let index = -1 let resIndex = 0 const len = list.length const willReturn = [] while (++index < len){ const value = list[ index ] if (predicate(value, index)){ willReturn[ resIndex++ ] = value } } return willReturn }