Last active
April 25, 2021 20:23
-
-
Save dfkaye/bff43e0c1fa335e52526508346adec18 to your computer and use it in GitHub Desktop.
rAssign library, slightly refactored; see https://github.com/micnic/r-assign/
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
// 21 February 2021 | |
// rAssign library, slightly refactored | |
// r-assign by micnic applies Object.assign() to a series of sources, | |
// then uses a schema against which to extract defined properties by type and name. | |
// see https://github.com/micnic/r-assign/ | |
function rAssign(schema, ...sources) { | |
// Check for valid schema provided | |
if (!schema || typeof schema !== 'object') { | |
throw TypeError(invalidSchema); | |
} | |
var source = Object.assign({}, ...sources); | |
// Loop through schema properties to select them | |
return Object.keys(schema).reduce((result, key) => { | |
// Check for valid schema properties | |
if (typeof schema[key] !== 'function') { | |
throw TypeError(invalidSchemaProperty(key)); | |
} | |
var value = schema[key](source[key], key, source); | |
// Skip values that are undefined | |
if (typeof value !== 'undefined') { | |
result[key] = value; | |
} | |
return result; | |
}, {}) | |
}; | |
var invalidSchema = 'Invalid schema argument type, object expected'; | |
/** | |
* Returns message for invalid schema property error | |
* @param {string} key | |
* @returns {string} | |
*/ | |
function invalidSchemaProperty(key) { | |
return `Invalid property type, "${key}" property expected to be a function`; | |
}; | |
/* full entry test */ | |
function getNumber(value){ | |
return typeof value === 'number' | |
? value | |
: 0 | |
}; | |
function getString(value){ | |
return typeof value === 'string' | |
? value | |
: "" | |
}; | |
var fullEntry = { | |
id: '5cdb44a49539f8cd', | |
index: 0, | |
visible: true, | |
name: 'abc' | |
}; | |
var F = rAssign({ | |
id: getString, | |
index: getNumber, | |
name: getString | |
}, fullEntry); | |
console.log(F) | |
// {id: "5cdb44a49539f8cd", index: 0, name: "abc"} | |
/* partial test */ | |
function getBoolean(value) { | |
return typeof value === 'boolean' | |
? value | |
: false | |
}; | |
var partialEntry = { | |
id: 'da9c8dff96b060a9', | |
index: 1, | |
name: 'def' | |
}; | |
var P = rAssign({ | |
id: getString, | |
visible: getBoolean, | |
name: getString | |
}, fullEntry, partialEntry); | |
console.log(P) | |
// {id: "da9c8dff96b060a9", visible: true, name: "def"} | |
/* invalid entry test */ | |
function validateNumber(value) { | |
if (typeof value !== 'number') { | |
throw new Error(`Value "${value}" expected to be a number`); | |
} | |
return value; | |
}; | |
var invalidEntry = { | |
id: '8a4eebf45748c8a4', | |
index: false, | |
name: 'ghi' | |
}; | |
try { | |
var I = rAssign({ | |
id: getString, | |
index: validateNumber, | |
visible: getBoolean, | |
name: getString | |
}, invalidEntry); | |
} catch(error) { | |
console.error(error) | |
// Uncaught Error: Value expected to be a number | |
} | |
/* | |
Empty schema: returns empty object. | |
This is contrary behavior to schema-ensure which requires the defined properties to exist while accepting any others. | |
See https://gist.github.com/dfkaye/5799d75d8190246a32613ea6e86e3be4 | |
*/ | |
var E = rAssign({}, fullEntry); | |
console.log(E) | |
// {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment