Last active
April 10, 2018 03:05
-
-
Save anabastos/b2e7dda145f469cdb5e5ae4e0a2325b9 to your computer and use it in GitHub Desktop.
generatePrivateMethods.js
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
import R from 'ramda' | |
const generateClass = (config) => R.reduce(generalizeProp(config), {}, R.keys(config)) | |
const generalizeProp = R.curry((config, privated, prop) => { | |
const public = { | |
get: () => config[prop], | |
set: (val) => config[prop] = val, | |
} | |
const capProp = capitalize(prop) | |
const methods = renameKeys({get: `get${capProp}`, set: `set${capProp}`})(public) | |
return {...methods, ...privated} | |
}) | |
const capitalize = (str) => `${str[0].toUpperCase()}${str.substr(1)}` | |
const renameKeys = R.curry((keysMap, obj) => { | |
const mergeKeys = (acc, key) => R.assoc(keysMap[key] || key, obj[key] | |
return R.reduce(mergeKeys, acc), {}, R.keys(obj)) | |
)} | |
const pessoa = generateClass({name: "ana", age: 22}) | |
console.log('pessoa.name: ', pessoa.name) | |
console.log('pessoa.getName: ', pessoa.getName()) | |
console.log('pessoa.setName: ', pessoa.setName('Tetudinho Macio')) | |
console.log('pessoa.getName: ', pessoa.getName()) | |
console.log('pessoa.getAge: ', pessoa.getAge()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment