Created
June 27, 2017 02:52
-
-
Save vinicius73/38a6a1930559893bc3d3f8782752895e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const { isObject, isFunction } = require('lodash') | |
const { cond, identity, T } = require('ramda') | |
const makeDescriptor = cond([ | |
[isFunction, value => ({ get: value })], | |
[isObject, identity], | |
[T, value => ({ get: () => value })] | |
]) | |
/** | |
* @method getDescriptors | |
* | |
* @param {Object} values | |
* @return {Object} | |
*/ | |
const getDescriptors = values => Object.keys(values) | |
.reduce((acc, key) => { | |
return Object.assign({ [key]: makeDescriptor(values[key]) }, acc) | |
}, {}) | |
/** | |
* @method defineProperties | |
* | |
* @param {Object} obj | |
* @param {Object} values | |
* @return {Object} | |
*/ | |
const defineProperties = (obj, values) => { | |
const descriptors = getDescriptors(values) | |
return Object.defineProperties(obj, descriptors) | |
} | |
module.exports = defineProperties |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment