Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Created June 27, 2017 02:52
Show Gist options
  • Save vinicius73/38a6a1930559893bc3d3f8782752895e to your computer and use it in GitHub Desktop.
Save vinicius73/38a6a1930559893bc3d3f8782752895e to your computer and use it in GitHub Desktop.
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