core-functions
Advanced tools
Comparing version 2.0.12 to 2.0.13
'use strict'; | ||
const Strings = require('./strings'); | ||
const trim = Strings.trim; | ||
const isNotBlank = Strings.isNotBlank; | ||
/** | ||
@@ -12,3 +16,4 @@ * Module containing utilities for working with objects. | ||
merge: merge, | ||
copy: copy | ||
copy: copy, | ||
getPropertyValue: getPropertyValue | ||
}; | ||
@@ -154,1 +159,21 @@ | ||
} | ||
/** | ||
* Gets the value of the simple or compound named property from the given object. A compound property name is one that | ||
* contains multiple property names separated by fullstops. | ||
* @param {Object} object - the object from which to get the named property's value | ||
* @param {string} propertyName - the simple or compound name of the property | ||
* @returns {*} the value of the named property on the given object | ||
*/ | ||
function getPropertyValue(object, propertyName) { | ||
const propertyNames = propertyName.split(".").map(n => trim(n)).filter(name => isNotBlank(name)); | ||
let value = undefined; | ||
for (let i = 0; i < propertyNames.length; ++i) { | ||
if (!object || typeof object !== 'object') { | ||
return undefined; | ||
} | ||
value = object[propertyNames[i]]; | ||
object = value; | ||
} | ||
return value; | ||
} |
{ | ||
"name": "core-functions", | ||
"version": "2.0.12", | ||
"version": "2.0.13", | ||
"description": "Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including strings, booleans, Promises, base 64, Arrays, Objects, standard AppErrors, etc.", | ||
@@ -5,0 +5,0 @@ "author": "Byron du Preez", |
@@ -1,2 +0,2 @@ | ||
# core-functions v2.0.12 | ||
# core-functions v2.0.13 | ||
@@ -74,3 +74,3 @@ Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including | ||
```js | ||
const appErrors = require('../app-errors'); | ||
const appErrors = require('core-functions/app-errors'); | ||
const AppError = appErrors.AppError; | ||
@@ -107,2 +107,5 @@ | ||
### 2.0.13 | ||
- Added `getPropertyValue` function to `objects.js` module | ||
### 2.0.12 | ||
@@ -109,0 +112,0 @@ - Changed `app-errors.js` module to export `getHttpStatus` function |
@@ -726,1 +726,16 @@ 'use strict'; | ||
}); | ||
test('getPropertyValue', t => { | ||
t.equal(Objects.getPropertyValue(undefined, 'a.b.c'), undefined, `undefined.a.b.c must be undefined`); | ||
t.equal(Objects.getPropertyValue(null, 'a.b.c'), undefined, `null.a.b.c must be undefined`); | ||
t.equal(Objects.getPropertyValue({}, 'a.b.c'), undefined, `{}.a.b.c must be undefined`); | ||
const o = {a: 1, b: {c: 'c', d: {e: 'e'}}}; | ||
t.deepEqual(Objects.getPropertyValue(o, 'a'), 1, 'o.a must be 1'); | ||
t.deepEqual(Objects.getPropertyValue(o, 'b'), {c: 'c', d: {e: 'e'}}, `o.b must be {c: 'c', d: {e: 'e'}}`); | ||
t.deepEqual(Objects.getPropertyValue(o, 'b.c'), 'c', `o.b.c must be 'c'`); | ||
t.deepEqual(Objects.getPropertyValue(o, 'b.d'), {e: 'e'}, `o.b.d must be {e: 'e'}`); | ||
t.deepEqual(Objects.getPropertyValue(o, 'b.d.e'), 'e', `o.b.d.e must be 'e'`); | ||
t.deepEqual(Objects.getPropertyValue(o, 'x.y.z'), undefined, `o.x.y.z must be undefined`); | ||
t.end(); | ||
}); |
{ | ||
"name": "core-functions-tests", | ||
"version": "2.0.12", | ||
"version": "2.0.13", | ||
"author": "Byron du Preez", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
223501
4286
200