Skip to content

Instantly share code, notes, and snippets.

@joshuaedwardcrowe
Last active September 20, 2018 10:52
Show Gist options
  • Save joshuaedwardcrowe/2edcf9c06c4f4375da9fe072f4d64d66 to your computer and use it in GitHub Desktop.
Save joshuaedwardcrowe/2edcf9c06c4f4375da9fe072f4d64d66 to your computer and use it in GitHub Desktop.
Spread an Object
/**
* 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