0.1.2 getKeys now supports one level nested array of keys, will flatten

wrote corresponding tests
master
Kebler Network System Administrator 2021-05-11 10:25:36 -07:00
parent 1681f56326
commit ff932e5f51
3 changed files with 35 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@uci-utils/obj-nested-prop", "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", "description": "Functions to get set, delete, and search nested properties of an object",
"main": "src/obj-nested-prop.js", "main": "src/obj-nested-prop.js",
"type": "module", "type": "module",

View File

@ -71,19 +71,42 @@ function getKeys (path, opts = {}) {
return false return false
} }
if (check.isFunction(opts.split)) return opts.split(path, opts) if (check.isFunction(opts.split)) return opts.split(path, opts)
const sep = opts.separator || opts.delimiter || '.' const sep = opts.separator || opts.delimiter || '.'
if (typeof path === 'symbol') { if (check.isSymbol(path)) {
return [path] return [path]
} }
// custom get keys
if (typeof opts.split === 'function') {
return opts.split(path, opts)
}
const regx = new RegExp(`(?<!\\\\)[${sep}]`, 'g') const regx = new RegExp(`(?<!\\\\)[${sep}]`, 'g')
const regxr = /\\/ig const regxr = /\\/ig
const splitr = i => i.split(regx).map(e => e.replace(regxr, '')) const splitr = str => {
const keys = Array.isArray(path) ? path.filter(Boolean).map(e => typeof e === 'string' && opts.elSplit !== false ? splitr(e) : e).flat() : splitr(path) 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++) { for (let i = 0; i < keys.length; i++) {
if (!check.isString(keys[i])) break if (!check.isString(keys[i])) break

View File

@ -296,9 +296,11 @@ describe('helpers', () => {
it('getKeys: will split path as string or array into array of single keys', () => { it('getKeys: will split path as string or array into array of single keys', () => {
const test = 'atest' const test = 'atest'
const key = Symbol('akey') 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'), ['this', 'is.a', 'string'])
assert.deepStrictEqual(getKeys(['this.is.a.string', key, test]), assert.deepStrictEqual(getKeys(['this.is.a.string', ['some\\.nested.keys', key], key, test]),
['this', 'is', 'a', 'string', key, 'atest']) ['this', 'is', 'a', 'string', 'some.nested', 'keys', key, key, 'atest'])
}) })
it('pathToString: converts path with getKeys and returns joined string', () => { it('pathToString: converts path with getKeys and returns joined string', () => {