cleaner-node
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -196,3 +196,3 @@ module.exports = { | ||
"no-proto": "error", | ||
"no-prototype-builtins": "error", | ||
"no-prototype-builtins": "off", | ||
"no-restricted-globals": "error", | ||
@@ -199,0 +199,0 @@ "no-restricted-imports": "error", |
{ | ||
"name": "cleaner-node", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "Helpful utilities and scripts to make Node projects more legible and easier for the next developer to take over.", | ||
@@ -54,2 +54,2 @@ "main": "index.js", | ||
} | ||
} | ||
} |
# cleaner-node | ||
Helpful utilities and scripts to make Node projects more legible and easier for the next developer to take over. | ||
## Changes | ||
| Version | Date | Description | | ||
|-----------|--------|---------------| | ||
| 0.3.5 | 2019/02/17 | Add `objects.getValue`, `getValues`, and `setValue` to get & set properties of an object using fully qualified path names. | |
@@ -1,3 +0,6 @@ | ||
const strings = require('./strings'); | ||
const { isValid: isValidString } = require('./strings'); | ||
const { count } = require('./arrays'); | ||
const isValid = value => (typeof value === 'object' && !(value instanceof Array)); | ||
const getId = item => { | ||
@@ -77,3 +80,3 @@ if (typeof item === 'object' && item instanceof Array) { return undefined; } | ||
const copy = JSON.parse(JSON.stringify(value)); | ||
const all = Object.keys(copy).filter(strings.isValid); | ||
const all = Object.keys(copy).filter(isValidString); | ||
// eslint-disable-next-line no-eq-null | ||
@@ -95,2 +98,47 @@ const toRemove = all.filter(k => (copy[k] == null)); | ||
// --- getValue ... from singular property | ||
function getValue (item, keyOrPath) { | ||
const keys = keyOrPath.split('.'); | ||
const used = []; | ||
while (keys.length > 0) { | ||
const key = keys.shift(); | ||
used.push(key); | ||
if (!isValid(item[key])) { | ||
throw new Error(`path ${used.join('.')} is not an object`); | ||
} | ||
item = item[key]; | ||
} | ||
return item[keys[0]]; | ||
} | ||
function getValues (items, keyOrPath, allowDuplicates) { | ||
const result = []; | ||
[].concat(items).filter(isValid).forEach(item => { | ||
const value = getValue(item, keyOrPath); | ||
if (allowDuplicates || !result.includes(value)) { | ||
result.push(value); | ||
} | ||
}) | ||
return result; | ||
} | ||
// --- setValue | ||
function setValue (obj = {}, path, value) { | ||
const keys = path.split('.'); | ||
const used = []; | ||
while (keys.length > 1) { | ||
const key = keys.shift(); | ||
used.push(key); | ||
if (!(key in obj)) { | ||
obj[key] = {}; | ||
} else if (!isValid(obj[key])) { | ||
throw new Error(`path ${used.join('.')} is set to a primative value`); | ||
} | ||
obj = obj[key]; | ||
} | ||
obj[keys[0]] = value; | ||
return obj; | ||
} | ||
module.exports = { | ||
@@ -102,6 +150,10 @@ findOne, | ||
getUids, | ||
getValue, | ||
getValues, | ||
isDefined, | ||
isValid, | ||
notDefined, | ||
setValue, | ||
toDto, | ||
toDtos | ||
}; |
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
53152
1310
8