From ff932e5f512787b69ec79f429310adf1f4844837 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Tue, 11 May 2021 10:25:36 -0700 Subject: [PATCH] 0.1.2 getKeys now supports one level nested array of keys, will flatten wrote corresponding tests --- package.json | 2 +- src/obj-nested-prop.js | 37 +++++++++++++++++++++++++++++------- test/obj-nested-prop.test.js | 6 ++++-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index ae11539..ee583d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci-utils/obj-nested-prop", - "version": "0.1.1", + "version": "0.1.2", "description": "Functions to get set, delete, and search nested properties of an object", "main": "src/obj-nested-prop.js", "type": "module", diff --git a/src/obj-nested-prop.js b/src/obj-nested-prop.js index 61c91df..b190775 100644 --- a/src/obj-nested-prop.js +++ b/src/obj-nested-prop.js @@ -71,19 +71,42 @@ function getKeys (path, opts = {}) { return false } if (check.isFunction(opts.split)) return opts.split(path, opts) + const sep = opts.separator || opts.delimiter || '.' - if (typeof path === 'symbol') { + if (check.isSymbol(path)) { return [path] } - // custom get keys - if (typeof opts.split === 'function') { - return opts.split(path, opts) - } const regx = new RegExp(`(? i.split(regx).map(e => e.replace(regxr, '')) - const keys = Array.isArray(path) ? path.filter(Boolean).map(e => typeof e === 'string' && opts.elSplit !== false ? splitr(e) : e).flat() : splitr(path) + const splitr = str => { + return str.split(regx).map(e => e.replace(regxr, '')) + } + + const keys = check.isArray(path) + ? path.filter(Boolean) + .map(e => { + if (check.isArray(e)) { + e = e.filter(Boolean) + const splits = new Map() + let ne = [] + e.forEach((en, i) => { + if (!check.isArray(en) && check.isString(en) && opts.elSplit !== false) { + splits.set(i, splitr(en)) + } + }) + e.forEach((el, j) => { + ne = splits.has(j) ? [...ne, ...splits.get(j)] : [...ne, el] + }) + e = ne + } + if (check.isString(e) && opts.elSplit !== false) e = splitr(e) + return e + }) + .flat() + : splitr(path) + + if (keys.find(el => check.isArray(el))) return false // can have more than one nested array for (let i = 0; i < keys.length; i++) { if (!check.isString(keys[i])) break diff --git a/test/obj-nested-prop.test.js b/test/obj-nested-prop.test.js index 409373e..2987fc1 100644 --- a/test/obj-nested-prop.test.js +++ b/test/obj-nested-prop.test.js @@ -296,9 +296,11 @@ describe('helpers', () => { it('getKeys: will split path as string or array into array of single keys', () => { const test = 'atest' const key = Symbol('akey') + assert.deepStrictEqual(getKeys(['one', ['two', 'three', 'four']]), ['one', 'two', 'three', 'four']) + assert(!getKeys(['one', ['two', ['three']]])) assert.deepStrictEqual(getKeys('this.is\\.a.string'), ['this', 'is.a', 'string']) - assert.deepStrictEqual(getKeys(['this.is.a.string', key, test]), - ['this', 'is', 'a', 'string', key, 'atest']) + assert.deepStrictEqual(getKeys(['this.is.a.string', ['some\\.nested.keys', key], key, test]), + ['this', 'is', 'a', 'string', 'some.nested', 'keys', key, key, 'atest']) }) it('pathToString: converts path with getKeys and returns joined string', () => {