From 4d387b5aa3459f530aaeccdb291832db13ad8e47 Mon Sep 17 00:00:00 2001 From: Aaron Dancer Date: Tue, 14 May 2019 11:40:29 -0500 Subject: [PATCH 1/4] Add array support --- index.js | 2 +- test.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f9ef4d0..b83356a 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ export default function dlv(obj, key, def, p) { p = 0; - key = key.split ? key.split('.') : key; + key = key.split ? key.replace(/\[([\w\d]+)\]/g, '.$1').split('.') : key; while (obj && p With Defaults"); From 052acc5e6fa17130c7a74836c5f261b283e40e76 Mon Sep 17 00:00:00 2001 From: Aaron Dancer Date: Tue, 14 May 2019 11:42:24 -0500 Subject: [PATCH 2/4] Add square-bracket notation to README Huge thanks to @bwendt-mylo for writing this previously in #24 --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bcbc73d..619e2b7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # `dlv(obj, keypath)` [![NPM](https://img.shields.io/npm/v/dlv.svg)](https://npmjs.com/package/dlv) [![Build](https://travis-ci.org/developit/dlv.svg?branch=master)](https://travis-ci.org/developit/dlv) -> Safely get a dot-notated path within a nested object, with ability to return a default if the full key path does not exist or the value is undefined +> Safely get a path-specified value within a nested object, with ability to return a default if the full key path does not exist or the value is undefined ### Why? @@ -29,7 +29,11 @@ let obj = { b: { c: 1, d: undefined, - e: null + e: null, + x: [ + { y: 8 }, + { z: 9 } + ] } } }; @@ -42,11 +46,15 @@ delve(obj, ['a', 'b', 'c']) === 1; delve(obj, 'a.b') === obj.a.b; +//or access nested objects within an array +delve(obj, 'a.b.x[1].z') === 9; + //returns undefined if the full key path does not exist and no default is specified delve(obj, 'a.b.c.f') === undefined; //optional third parameter for default if the full key in path is missing delve(obj, 'a.b.c.f', 'foo') === 'foo'; +delve(obj, 'a.b.x[2].f', 'foo') === 'foo'; //or if the key exists but the value is undefined delve(obj, 'a.b.c.d', 'foo') === 'foo'; From 0802471b0d93c81f4fbb0f20fe6cdca657d5f5e9 Mon Sep 17 00:00:00 2001 From: Aaron Dancer Date: Tue, 14 May 2019 11:48:10 -0500 Subject: [PATCH 3/4] Update byte size in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 619e2b7..a8438b5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ### Why? -Smallest possible implementation: only **130 bytes.** +Smallest possible implementation: only **153 bytes.** You could write this yourself, but then you'd have to write [tests]. From d5d665770cc98d88febbf2ca35243c49c03d4a6f Mon Sep 17 00:00:00 2001 From: Aaron Dancer Date: Tue, 14 May 2019 13:12:51 -0500 Subject: [PATCH 4/4] Handle square bracket syntax edge-cases This should make dlv API-compatible with lodash.get --- README.md | 2 +- index.js | 2 +- test.js | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a8438b5..7fbe0c5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ### Why? -Smallest possible implementation: only **153 bytes.** +Smallest possible implementation: only **160 bytes.** You could write this yourself, but then you'd have to write [tests]. diff --git a/index.js b/index.js index b83356a..e4c0762 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ export default function dlv(obj, key, def, p) { p = 0; - key = key.split ? key.replace(/\[([\w\d]+)\]/g, '.$1').split('.') : key; + key = key.split ? key.replace(/\[("|')?([^\[\]]+)\1\]/g, '.$2').split('.') : key; while (obj && p