Skip to content

Commit

Permalink
Merge pull request #20 from dictor93/feature/extend-pick-functionality
Browse files Browse the repository at this point in the history
Feature/extend pick functionality
  • Loading branch information
prdn authored Dec 11, 2023
2 parents bff7d38 + 7dc420f commit 4e56dae
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.8.1
- feat: extended pick functionality by using multiple path parameters and nested pick

# 1.8.0
- feat: max
- feat: min
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitfinexcom/lib-js-util-base",
"version": "1.8.0",
"version": "1.8.1",
"description": "general utils",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 1 addition & 9 deletions src/get.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
'use strict'

/**
*
* @param {string} path
* @returns {Array<string>}
*/
const pathToArray = (path) => {
if (Array.isArray(path)) return path
return path.split(/\[|\]\.|\]\[|\.|\]/gm).filter((key) => key !== '')
}
const pathToArray = require('./util/pathToArray')

/**
*
Expand Down
59 changes: 55 additions & 4 deletions src/pick.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
'use strict'

const pick = (obj, keys) => {
const get = require('./get')
const pathToArray = require('./util/pathToArray')

/**
*
* @param {any} nested
* @param {Array<string>} path
* @param {any} value
* @returns
*/
const _setObjByPath = (nested, path, value) => {
if (path.length === 1) {
return Object.assign(nested, { [path[0]]: value })
}

const currentProp = path.shift()
if (nested[currentProp] === undefined) {
nested[currentProp] = {}
}

return _setObjByPath(nested[currentProp], path, value)
}

/**
*
* @param {object} src
* @param {object} dest
* @param {string | Array<string>} path
* @returns
*/
const _pickByPath = (src, dest, path) => {
const pathArr = pathToArray(path)

const value = get(src, [...pathArr])
if (value !== undefined) {
_setObjByPath(dest, pathArr, value)
}
return dest
}

/**
*
* @param {any} obj
* @param {...(string | Array<string | Array<strig>>)} pathes
* @returns
*/
const pick = (obj, ...pathes) => {
if (obj === null) {
return {}
}

const newObj = {}
for (const key in obj) {
if (keys.includes(key)) {
newObj[key] = obj[key]
for (const path of pathes) {
if (typeof path === 'string') {
_pickByPath(obj, newObj, path)
}
if (Array.isArray(path)) {
for (const chunk of path) {
_pickByPath(obj, newObj, chunk)
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/util/pathToArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

/**
*
* @param {string} path
* @returns {Array<string>}
*/
const pathToArray = (path) => {
if (Array.isArray(path)) return path
return path.split(/\[|\]\.|\]\[|\.|\]/gm).filter((key) => key !== '')
}

module.exports = pathToArray
45 changes: 45 additions & 0 deletions test/pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,49 @@ describe('pick', () => {
it('should handle keys that are not strings', () => {
assert.deepStrictEqual(pick({ 1: 'one', 2: 'two' }, ['1', '2']), { 1: 'one', 2: 'two' })
})

it('should pick properties by few path parameters', () => {
const src = { a: 1, b: 2, c: 3, d: 4 }
const expected = { a: 1, b: 2, d: 4 }
assert.deepStrictEqual(pick(src, ['a', 'b'], 'd'), expected)
})

it('should pick nested properties by string or array path', () => {
const src = {
a: 1,
b: {
c: { d: 5, g: 2, h: 7 },
f: 6
},
e: 4
}
const expected = {
a: 1,
b: {
c: { d: 5, h: 7 }
}
}

assert.deepStrictEqual(
pick(
src,
['a', 'b.c.d', 'b.c.h'],
'd'
),
expected
)
assert.deepStrictEqual(
pick(
src,
['a', ['b', 'c', 'd'], ['b', 'c', 'h']],
'd'
),
expected
)
})

it('should return empty object if path is not specified', () => {
const src = { a: 1, b: { c: { d: 5, g: 2, h: 7 }, f: 6 }, e: 4 }
assert.deepStrictEqual(pick(src, ''), {})
})
})

0 comments on commit 4e56dae

Please sign in to comment.