Comparing version 0.60.0 to 0.61.0
@@ -1397,2 +1397,42 @@ /** | ||
/** | ||
* Get, creates an array of values corresponding to paths of the object | ||
* | ||
* @param {object} object input object | ||
* @param {Array|string} path string or an array of strings describing paths to be returned from an object | ||
* @param {*} [defaultValue] value returned when path resolves undefined | ||
* @returns {*} value found by object paths in object, or returns defaultValue if provided and return would otherwise be undefined | ||
* | ||
* @example | ||
* const result = objects.get({ front: [1, 3, 5], back: [37, 39] }); | ||
* console.log(result, 'back[1]'); | ||
* > 39 | ||
* | ||
* @example | ||
* const result = objects.get({ front: [1, 3, 5], back: [37, 39] }); | ||
* console.log(result, ['front', 5], "no value here"); | ||
* > 'no value here' | ||
*/ | ||
function get (object, path, defaultValue) { | ||
if (typeof path === 'undefined') { return undefined; } | ||
let pathArray; | ||
if (Array.isArray(path)) { | ||
pathArray = path; | ||
} else { | ||
pathArray = String(path).replace(/\[(\w+)\]/g, '.$1').split('.'); | ||
} | ||
const result = pathArray.reduce((result, search) => { | ||
if (result === undefined) { return undefined; } | ||
if (Object.prototype.hasOwnProperty.call(result, search)) { | ||
return result[search]; | ||
} else { | ||
return undefined; | ||
} | ||
}, object); | ||
return result !== undefined ? result : defaultValue; | ||
} | ||
/** | ||
* Include filters elements in a new object based on an array of keys to include | ||
@@ -1588,2 +1628,3 @@ * | ||
has: has, | ||
get: get, | ||
include: include, | ||
@@ -1590,0 +1631,0 @@ invert: invert, |
{ | ||
"name": "absurdum", | ||
"version": "0.60.0", | ||
"version": "0.61.0", | ||
"description": "Reductio Ad Absurdum - The Ridiculous Application of Reduce", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -139,2 +139,3 @@ [![GitHub Releases](https://img.shields.io/github/release/vanillaes/absurdum.svg)](https://github.com/vanillaes/absurdum/releases) | ||
- [has][objects.has] | ||
- [get][objects.get] | ||
- [include][objects.include] | ||
@@ -153,2 +154,3 @@ - [invert][objects.invert] | ||
[objects.has]: ./docs/objects/has.md | ||
[objects.get]: ./docs/objects/get.md | ||
[objects.include]: ./docs/objects/include.md | ||
@@ -155,0 +157,0 @@ [objects.invert]: ./docs/objects/invert.md |
@@ -6,2 +6,3 @@ export { at } from "./at.js"; | ||
export { has } from "./has.js"; | ||
export { get } from "./get.js"; | ||
export { include } from "./include.js"; | ||
@@ -8,0 +9,0 @@ export { invert } from "./invert.js"; |
@@ -6,2 +6,3 @@ export { at } from './at.js'; | ||
export { has } from './has.js'; | ||
export { get } from './get.js'; | ||
export { include } from './include.js'; | ||
@@ -8,0 +9,0 @@ export { invert } from './invert.js'; |
Sorry, the diff of this file is not supported yet
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
219630
181
5561
191