Miss any of our Open RFC calls?Watch the recordings here! »

get-valu

1.3.2 • Public • Published

get-valu

travis-ci codecov

Why you should consider using this project:

  • VanillaJS
  • no dependencies
  • lightweight and fast
  • supports all browsers and systems
  • simplicity
  • returns only 2 options: either undefined or desired value
  • second parameter (path) works as simple as copy-paste

How to use

  1. Write the path to desired value: a['b.c'][0].d.e
  2. Cut path: ['b.c'][0].d.e
  3. Paste as a second argument: get(a, "['b.c'][0].d.e")

Install

Install with npm:

npm install get-valu

or with yarn:

yarn add get-valu

Usage

Supports object nesting

const get = require('get-valu');
 
const obj = { a: { b: { c: { d: 'e' } } } };
 
console.log(get(obj));            //=> { a: { b: { c: { d: 'e' } } } }
console.log(get(obj, 'a'));       //=> { b: { c: { d: 'e' } } }
console.log(get(obj, 'a.b'));     //=> { c: { d: 'e' } }
console.log(get(obj, 'a.b.c'));   //=> { d: 'e' }
console.log(get(obj, 'a.b.c.d')); //=> 'e'

Supports complex keys with dots

console.log(get({ "a.b": "c" }, "['a.b']")); //=> 'c'

Supports array

console.log(get([{ a: "b" }], "[0]"));           //=> { a: "b" }
console.log(get({ a: [{ b: "c" }] }, "a[0]"));   //=> { b: "c" }
console.log(get({ a: [{ b: "c" }] }, "a[0].b")); //=> "c"
console.log(get({ a: { b: ["c"] } }, "a.b[0]")); //=> "c"

Supports functions

const a = () => {};
a.b = { c: "d" };
 
console.log(get(a, ""));    //=> [Function a]
console.log(get(a, "b"));   //=> { c: "d" }
console.log(get(a, "b.c")); //=> "d"

For more examples look unit tests.

Benchmarks

JS Benchmark

LICENSE

GitHub

Install

npm i get-valu

DownloadsWeekly Downloads

7

Version

1.3.2

License

MIT

Unpacked Size

7.01 kB

Total Files

7

Last publish

Collaborators

  • avatar