Last active
September 20, 2018 10:52
-
-
Save joshuaedwardcrowe/2edcf9c06c4f4375da9fe072f4d64d66 to your computer and use it in GitHub Desktop.
Spread an Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Spread an object into a Map of properties and their values, so that | |
* you can call Map.values or Map.keys to spread as needed. | |
* | |
* @name Spreader | |
* @example Spreader({userName: 'soas'}, '*'); | |
* @param {Object} object Object to spread. | |
* @param {String|Array.<String>} [allowed='*'] Properties to allow. | |
* @returns {Map<String, Object>} Values in the object by properties. | |
*/ | |
function Spreader (object, allowed = '*') { | |
if (Array.isArray(allowed) && !allowed.length) allowed = '*' | |
const schema = new Map() | |
for (let prop in object) { | |
if (object.hasOwnProperty(prop)) { | |
const spreaderValue = object[prop] | |
const spreaderIs = Object.is(allowed, '*') | |
const spreaderIncludes = allowed.includes(prop) | |
if (spreaderIs || spreaderIncludes) schema.set(prop, spreaderValue); | |
} | |
} | |
return schema; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment