Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handling null default case #2

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const DEFAULT_VAL = {};

const RE_DELIMITER = /[\[\]|\.]+/g;

const RE_END_BRACKET_LAST_CHAR = /\]$/;
Expand All @@ -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;
Expand Down Expand Up @@ -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 )
};
}

Expand Down
61 changes: 36 additions & 25 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const source = {
year: '2017'
}]
},
nullTester: null,
tags: [
'minim',
'nisi',
Expand All @@ -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 ],
Expand Down Expand Up @@ -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,
Expand Down