0.0.1 • Published 2 years ago

obj.getter v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

obj.getter

Utility function to get a value corresponding to path of object.

Install

npm i obj.getter

Usage

import getter from 'obj.getter';

const value1 = getter('a.b[0].c', { a: { b: [{ c: 1 }] } });
const value2 = getter('a.b[0]["c"]', { a: { b: [{ c: 1 }] } });
const value3 = getter('a.b["0"].c', { a: { b: [{ c: 1 }] } });

const value4 = getter('a.b[0].c')({ a: { b: [{ c: 1 }] } });

console.log(value1); // 1
console.log(value2); // 1
console.log(value3); // 1
console.log(value4); // 1

API

function getter(path: string, obj?: Record<string, any>): any;
function getter(path: string): (obj: Record<string, any>) => any;

Return a value corresponding to path of object.

License

MIT