-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from vigan-abd/chore/merge-cleanup
Chore/merge cleanup
- Loading branch information
Showing
9 changed files
with
187 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
### Description: | ||
... | ||
|
||
### Breaking changes: | ||
- [ ] | ||
|
||
### New features: | ||
- [ ] | ||
|
||
### Fixes: | ||
- [ ] | ||
|
||
### PR status: | ||
- [ ] Version bumped | ||
- [ ] Change-log updated | ||
- [ ] Tests added or updated | ||
- [ ] PR keeps 100% test coverage | ||
- [ ] Type definition updated | ||
- [ ] Readme updated | ||
- [ ] Linter is applied | ||
- [ ] Const arrow functions are used instead of pure function definitions where applicable | ||
- [ ] Functions are listed in alphabetic order in index.js, index.d.ts and readme |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
# 1.2.0 | ||
- feat: get | ||
- feat: isEmpty | ||
- feat: merge | ||
|
||
# 1.1.0 | ||
- feat: cloneDeep | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
export function camelize(str: string): string | ||
export function cloneDeep (obj: Object): Object | ||
export function get(obj: Object, path: string | Array<string | number>, defaultValue: any) | ||
export function getArrayHasIntersect (arr1: Array<any>, arr2: Array<any>): boolean | ||
export function getArrayUniq (arr: Array<any>): Array<any> | ||
export function isEmpty(val: any): boolean | ||
export function isNil(val: any): boolean | ||
export function isPlainObject(val: any): boolean | ||
export function pick(obj: Object, keys: Array<string>): Object | ||
export function pickBy(obj: Object, predicate: (val: any, key: string) => boolean): Object | ||
export function camelize(str: string): string | ||
export function merge(obj: Object, ...sources: Object[]): Object | ||
export function omit(obj: Object, keys: Array<string>): Object | ||
export function omitBy(obj: Object, predicate: (val: any, key: string) => boolean): Object | ||
export function merge(obj: Object, ...sources: Object): Object | ||
export function pick(obj: Object, keys: Array<string>): Object | ||
export function pickBy(obj: Object, predicate: (val: any, key: string) => boolean): Object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
'use strict' | ||
|
||
const camelize = require('./src/camelize') | ||
const cloneDeep = require('./src/cloneDeep') | ||
const get = require('./src/get') | ||
const getArrayHasIntersect = require('./src/getArrayHasIntersect') | ||
const getArrayUniq = require('./src/getArrayUniq') | ||
const isEmpty = require('./src/isEmpty') | ||
const isNil = require('./src/isNil') | ||
const isPlainObject = require('./src/isPlainObject') | ||
const pick = require('./src/pick') | ||
const pickBy = require('./src/pickBy') | ||
const merge = require('./src/merge') | ||
const omit = require('./src/omit') | ||
const omitBy = require('./src/omitBy') | ||
const camelize = require('./src/camelize') | ||
const merge = require('./src/merge') | ||
const pick = require('./src/pick') | ||
const pickBy = require('./src/pickBy') | ||
|
||
module.exports = { | ||
camelize, | ||
cloneDeep, | ||
get, | ||
getArrayHasIntersect, | ||
getArrayUniq, | ||
isEmpty, | ||
isNil, | ||
isPlainObject, | ||
pick, | ||
pickBy, | ||
camelize, | ||
merge, | ||
omit, | ||
omitBy, | ||
merge | ||
pick, | ||
pickBy | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict' | ||
|
||
/** | ||
* | ||
* @param {string} path | ||
* @returns {Array<string>} | ||
*/ | ||
const pathToArray = (path) => { | ||
if (Array.isArray(path)) return path | ||
return path.split(/\[|\]\.|\]\[|\.|\]/gm).filter((key) => key !== '') | ||
} | ||
|
||
/** | ||
* | ||
* @param {any} obj | ||
* @param {Array<string>} nextPath | ||
* @returns {any} | ||
*/ | ||
const getDeeperProp = (obj, nextPath, defaults) => { | ||
const key = nextPath.shift() | ||
if (obj[key] !== undefined) { | ||
if (nextPath.length > 0) return getDeeperProp(obj[key], nextPath, defaults) | ||
|
||
return obj[key] | ||
} | ||
|
||
return defaults | ||
} | ||
|
||
/** | ||
* | ||
* @param {any} object | ||
* @param {string | Array} path | ||
* @param {any} defaults | ||
* @returns {any} | ||
*/ | ||
const get = (object, path, defaults) => { | ||
if (typeof object !== 'object' || object === null) { | ||
return defaults | ||
} | ||
|
||
if (typeof path === 'string' && object[path] !== undefined) { | ||
return object[path] | ||
} | ||
|
||
return getDeeperProp(object, pathToArray(path), defaults) | ||
} | ||
|
||
module.exports = get |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
|
||
const assert = require('assert') | ||
const get = require('../src/get') | ||
|
||
describe('get', () => { | ||
it('should get property from object', () => { | ||
const object = { a: 'foo' } | ||
const res = get(object, 'a') | ||
assert.strictEqual(res, 'foo') | ||
}) | ||
|
||
it('should get property from nested object', () => { | ||
const object = { a: { foo: { bar: 'baz' } } } | ||
const res = get(object, 'a.foo.bar') | ||
assert.strictEqual(res, 'baz') | ||
}) | ||
|
||
it('should get property by name passed in square brackets', () => { | ||
const object = { a: { foo: { bar: 'baz' } } } | ||
const res = get(object, 'a.foo[bar]') | ||
assert.strictEqual(res, 'baz') | ||
}) | ||
|
||
it('should get property in array', () => { | ||
const object = { a: [1, 2, { foo: { bar: 'baz' } }] } | ||
const res = get(object, 'a[2].foo[bar]') | ||
assert.strictEqual(res, 'baz') | ||
}) | ||
|
||
it('should get property in array at the lowest level', () => { | ||
const object = { a: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] } | ||
const res = get(object, 'a[2].foo.bar.[3]') | ||
assert.strictEqual(res, 'baz') | ||
}) | ||
|
||
it('should get property if the first level is array', () => { | ||
const object = [{ a: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] }] | ||
const res = get(object, '[0]a[2].foo.bar.[3]') | ||
assert.strictEqual(res, 'baz') | ||
}) | ||
|
||
it('should also works if path passed as array', () => { | ||
const object = [{ a: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] }] | ||
const res = get(object, [0, 'a', '2', 'foo', 'bar', 3]) | ||
assert.strictEqual(res, 'baz') | ||
}) | ||
|
||
it('should work with "false" values as part of path', () => { | ||
const object = [{ false: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] }] | ||
const res = get(object, '[0]false[2].foo.bar.[3]') | ||
assert.strictEqual(res, 'baz') | ||
}) | ||
|
||
it('should work with nullable values', () => { | ||
const object = [{ false: [1, 2, { foo: { bar: [0, 1, 2, { null: 'baz' }] } }] }] | ||
const res = get(object, '[0]false[2].foo.bar.[3]null') | ||
assert.strictEqual(res, 'baz') | ||
}) | ||
|
||
it('should return default value if item is not found by path', () => { | ||
const object = [{ false: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] }] | ||
const res = get(object, '[0]false[4].foo.bar.[5]', 'some default value') | ||
assert.strictEqual(res, 'some default value') | ||
}) | ||
|
||
it('should return default value if result item is not defined', () => { | ||
const object = [{ false: [1, 2, { foo: { bar: [0, 1] } }] }] | ||
const res = get(object, '[0]false[2].foo.bar.[2]', 'some default value') | ||
assert.strictEqual(res, 'some default value') | ||
}) | ||
|
||
it('should return default value if input is null', () => { | ||
const res = get(null, 'test', 'some default value') | ||
assert.strictEqual(res, 'some default value') | ||
}) | ||
|
||
it('should return default value if input is not object', () => { | ||
const res = get(true, 'test', 'some default value') | ||
assert.strictEqual(res, 'some default value') | ||
}) | ||
}) |