Skip to main content
Module

x/rambda/paths.spec.js

Faster and smaller alternative to Ramda
Go to Latest
File
import { paths } from './paths'
const obj = { a : { b : { c : 1, d : 2, }, }, p : [ { q : 3 } ], x : { y : 'FOO', z : [ [ {} ] ], },}
test('with string path + curry', () => { const pathsInput = [ 'a.b.d', 'p.q' ] const expected = [ 2, undefined ] const result = paths(pathsInput, obj) const curriedResult = paths(pathsInput)(obj)
expect(result).toEqual(expected) expect(curriedResult).toEqual(expected)})
test('with array path', () => { const result = paths([ [ 'a', 'b', 'c' ], [ 'x', 'y' ], ], obj)
expect(result).toEqual([ 1, 'FOO' ])})
test('takes a paths that contains indices into arrays', () => { expect(paths([ [ 'p', 0, 'q' ], [ 'x', 'z', 0, 0 ], ], obj)).toEqual([ 3, {} ]) expect(paths([ [ 'p', 0, 'q' ], [ 'x', 'z', 2, 1 ], ], obj)).toEqual([ 3, undefined ])})
test('gets a deep property\'s value from objects', () => { expect(paths([ [ 'a', 'b' ] ], obj)).toEqual([ obj.a.b ]) expect(paths([ [ 'p', 0 ] ], obj)).toEqual([ obj.p[ 0 ] ])})
test('returns undefined for items not found', () => { expect(paths([ [ 'a', 'x', 'y' ] ], obj)).toEqual([ undefined ]) expect(paths([ [ 'p', 2 ] ], obj)).toEqual([ undefined ])})