Skip to main content
Module

x/rambda/source/indexBy.spec.js

Faster and smaller alternative to Ramda
Go to Latest
File
import {indexBy} from './indexBy'import {prop} from './prop'
test('happy', () => { const list = [ {id: 1}, { id: 1, a: 2, }, {id: 2}, {id: 10}, {id: 'a'}, ]
expect(indexBy(prop('id'))(list)).toEqual({ 1: { id: 1, a: 2, }, 2: {id: 2}, 10: {id: 10}, a: {id: 'a'}, })})
test('with string as condition', () => { const list = [{id: 1}, {id: 2}, {id: 10}, {id: 'a'}] const standardResult = indexBy(obj => obj.id, list) const suggestionResult = indexBy('id', list)
expect(standardResult).toEqual(suggestionResult)})
test('with string - bad path', () => { const list = [ { a: { b: 1, c: 2, }, }, {a: {c: 4}}, {}, { a: { b: 10, c: 20, }, }, ]
const result = indexBy('a.b', list) const expected = { 1: { a: { b: 1, c: 2, }, }, 10: { a: { b: 10, c: 20, }, }, undefined: {}, }
expect(result).toEqual(expected)})