diff --git a/package.json b/package.json index dc469e5..9791e85 100644 --- a/package.json +++ b/package.json @@ -57,11 +57,11 @@ "url": "git+https://github.com/webKrafters/get-property.git" }, "scripts": { - "build": "eslint --fix && rm -rf dist && babel src -d dist && npx -p typescript tsc", + "build": "eslint --fix && rm -rf dist && babel src -d dist --ignore '**/*.test.js' && npx -p typescript tsc", "test": "eslint --fix && jest --coverage", "test:core": "jest", "test:watch": "eslint --fix && jest --watchAll" }, "types": "dist/index.d.ts", - "version": "1.0.1" + "version": "1.0.2" } diff --git a/src/index.js b/src/index.js index 1550fb6..a314e32 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,5 @@ +const DEFAULT_VAL = {}; + const RE_DELIMITER = /[\[\]|\.]+/g; const RE_END_BRACKET_LAST_CHAR = /\]$/; @@ -18,7 +20,7 @@ const toString = Object.prototype.toString; * * @see lodash.get documentation */ -function getProperty( source, path, defaultValue = undefined ) { +function getProperty( source, path, defaultValue = DEFAULT_VAL ) { switch( toString.call( path ).replace( RE_TYPE, '$1' ) ) { case 'String': path = path.replace( RE_END_BRACKET_LAST_CHAR, '' ).split( RE_DELIMITER ); break; case 'Array': break; @@ -60,7 +62,7 @@ function getProperty( source, path, defaultValue = undefined ) { key: path?.[ path.length - 1 ], source: path.length && path.length - trail.length < 2 ? source : undefined, trail, - value: _value ?? defaultValue + value: _value ?? ( defaultValue === DEFAULT_VAL ? _value : defaultValue ) }; } diff --git a/src/index.test.js b/src/index.test.js index f6cd2ea..8489110 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -66,6 +66,7 @@ const source = { year: '2017' }] }, + nullTester: null, tags: [ 'minim', 'nisi', @@ -83,35 +84,15 @@ const source = { describe( 'getProperty(...)', () => { test( 'obtains info about property located at path in a source data', () => { - expect( getProperty( source, 'classInstance' ) ).toStrictEqual({ - _value: source.classInstance, + expect( getProperty( source, 'nullTester' ) ).toStrictEqual({ + _value: source.nullTester, exists: true, index: NaN, isSelf: false, - key: 'classInstance', + key: 'nullTester', source: source, - trail: [ 'classInstance' ], - value: source.classInstance - }); - expect( getProperty( source, 'classInstance.lastName' ) ).toStrictEqual({ - _value: source.classInstance.lastName, - exists: true, - index: NaN, - isSelf: false, - key: 'lastName', - source: source.classInstance, - trail: [ 'classInstance', 'lastName' ], - value: source.classInstance.lastName - }); - expect( getProperty( source, 'classInstance.range.-1' ) ).toStrictEqual({ - _value: source.classInstance.range[ 6 ], - exists: true, - index: 6, - isSelf: false, - key: '-1', - source: source.classInstance.range, - trail: [ 'classInstance', 'range', 6 ], - value: source.classInstance.range[ 6 ] + trail: [ 'nullTester' ], + value: source.nullTester }); expect( getProperty( source, 'tags.-2' ) ).toStrictEqual({ _value: source.tags[ 5 ], @@ -183,6 +164,36 @@ describe( 'getProperty(...)', () => { trail: [ 'history', 'places', 1 ], value: DEFAULT }); + expect( getProperty( source, 'classInstance' ) ).toStrictEqual({ + _value: source.classInstance, + exists: true, + index: NaN, + isSelf: false, + key: 'classInstance', + source: source, + trail: [ 'classInstance' ], + value: source.classInstance + }); + expect( getProperty( source, 'classInstance.lastName' ) ).toStrictEqual({ + _value: source.classInstance.lastName, + exists: true, + index: NaN, + isSelf: false, + key: 'lastName', + source: source.classInstance, + trail: [ 'classInstance', 'lastName' ], + value: source.classInstance.lastName + }); + expect( getProperty( source, 'classInstance.range.-1' ) ).toStrictEqual({ + _value: source.classInstance.range[ 6 ], + exists: true, + index: 6, + isSelf: false, + key: '-1', + source: source.classInstance.range, + trail: [ 'classInstance', 'range', 6 ], + value: source.classInstance.range[ 6 ] + }); expect( getProperty( source, 'none' ) ).toStrictEqual({ _value: undefined, exists: false,