cleaner-node
Advanced tools
Comparing version 0.8.0 to 0.8.1
{ | ||
"name": "cleaner-node", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"description": "Helpful utilities and scripts to make Node projects more legible and easier for the next developer to take over.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -29,1 +29,2 @@ # cleaner-node | ||
| 0.8.0 | 2020/01/16 | Add `env` methods. | | ||
| 0.8.1 | 2020/01/17 | Add `.print` and `.toPrintable` to `object`. | |
@@ -213,2 +213,52 @@ const { isValid: isValidString, toCamelCase } = require('./strings'); | ||
const _toPrintable = (value, valuePath, results) => { | ||
if (typeof value === 'undefined') { | ||
results.items.push({ | ||
key: valuePath, | ||
value: undefined | ||
}); | ||
} else if (value === null) { | ||
results.items.push({ | ||
key: valuePath, | ||
value: null | ||
}); | ||
} else if (['string', 'number', 'boolean'].includes(typeof value)) { | ||
results.items.push({ | ||
key: valuePath, | ||
value | ||
}); | ||
} else if (typeof value === 'object' && value instanceof Array) { | ||
for (let i = 0; i < value.length; i += 1) { | ||
if (results.cache.includes(value[i])) { return; } | ||
results.cache.push(value[i]); | ||
_toPrintable(value[i], `${valuePath}[${i}]`, results); | ||
} | ||
} else if (isValid(value)) { | ||
if (results.cache.includes(value)) { return; } | ||
results.cache.push(value); | ||
const keys = Object.keys(value).filter(isValidString); | ||
keys.sort(); | ||
keys.forEach(key => { | ||
const keyPath = [].concat(valuePath.split('.'), key).filter(isValidString).join('.'); | ||
_toPrintable(value[key], keyPath, results); | ||
}); | ||
} | ||
}; | ||
const toPrintable = (value) => { | ||
if (!isValid(value)) { throw new Error('Value passed to toPrintable is not an object.'); } | ||
const results = { | ||
items: [], | ||
cache: [] | ||
}; | ||
_toPrintable(value, '', results); | ||
return results.items; | ||
}; | ||
const print = value => { | ||
if (!isValid(value)) { throw new Error('Value passed to print is not an object.'); } | ||
toPrintable(value).filter(isValid).forEach(item => { | ||
console.log(`${item.key} : ${item.value}`); | ||
}); | ||
}; | ||
module.exports = { | ||
@@ -233,3 +283,5 @@ findOne, | ||
toDtos, | ||
toPrintable, | ||
print, | ||
reduce | ||
}; |
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
66179
1685
30