riot
Version:
Simple and elegant component-based UI library
52 lines (45 loc) • 1.69 kB
JavaScript
/* Riot WIP, @license MIT */
/**
* Helper function to set an immutable property
* @param {object} source - object where the new property will be set
* @param {string} key - object key where the new property will be stored
* @param {*} value - value of the new property
* @param {object} options - set the property overriding the default options
* @returns {object} - the original object modified
*/
function defineProperty(source, key, value, options = {}) {
Object.defineProperty(source, key, {
value,
enumerable: false,
writable: false,
configurable: true,
...options,
});
return source
}
/**
* Define multiple properties on a target object
* @param {object} source - object where the new properties will be set
* @param {object} properties - object containing as key pair the key + value properties
* @param {object} options - set the property overriding the default options
* @returns {object} the original object modified
*/
function defineProperties(source, properties, options) {
Object.entries(properties).forEach(([key, value]) => {
defineProperty(source, key, value, options);
});
return source
}
/**
* Define default properties if they don't exist on the source object
* @param {object} source - object that will receive the default properties
* @param {object} defaults - object containing additional optional keys
* @returns {object} the original object received enhanced
*/
function defineDefaults(source, defaults) {
Object.entries(defaults).forEach(([key, value]) => {
if (!source[key]) source[key] = value;
});
return source
}
export { defineDefaults, defineProperties, defineProperty };