Last active
December 10, 2018 12:16
-
-
Save abiodun0/89fc3f5e5a0fe018331556c42dc33ea9 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 R = require('ramda') | |
const initObject = { | |
"gold_bid_usd_toz": "1231.66", | |
"gold_ask_usd_toz": "1232.66", | |
"gold_change_dollar_usd_toz": "9.16", | |
"gold_change_percent_usd_toz": "0.74%", | |
"gold_high_usd_toz": "1232.21", | |
"gold_low_usd_toz": "1221.41", | |
"zara_high_credit": "374" | |
} | |
const getWordBeforeUnderscore = R.compose(R.head, R.split('_')) | |
const getUniqKeys = R.compose(R.uniq, R.map(getWordBeforeUnderscore), R.keys) | |
const modifier = (fn) => (obj) => (acc, val) => { | |
return {...acc, [fn(val)]: obj[val]}; | |
} | |
const filterByStartsWith = R.compose(R.filter, R.startsWith) | |
const bigObjectReduce = (obj) => (acc, key) => { | |
const reduceFn = R.compose(modifier, R.replace(R.__, ''))(`${key}_`) | |
const newobjFn = R.compose(R.objOf(key), | |
R.reduce(reduceFn(obj), {}), | |
filterByStartsWith(key), | |
R.keys) | |
return {...acc, ...newobjFn(obj)} | |
} | |
R.compose(R.reduce(bigObjectReduce(initObject), {}), getUniqKeys)(initObject) | |
/* | |
{ | |
gold: { | |
ask_usd_toz: "1232.66", | |
bid_usd_toz: "1231.66", | |
change_dollar_usd_toz: "9.16", | |
change_percent_usd_toz: "0.74%", | |
high_usd_toz: "1232.21", | |
low_usd_toz: "1221.41" | |
}, | |
zara: { | |
high_credit: "374" | |
} | |
} | |
*/ |
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 initObject = { | |
"gold_bid_usd_toz": "1231.66", | |
"gold_ask_usd_toz": "1232.66", | |
"gold_change_dollar_usd_toz": "9.16", | |
"gold_change_percent_usd_toz": "0.74%", | |
"gold_high_usd_toz": "1232.21", | |
"gold_low_usd_toz": "1221.41", | |
"zara_high_credit": "374" | |
} | |
const getWordBeforeUnderscore = R.compose(R.head, R.split('_')) | |
const getUniqKeys = R.compose(R.uniq, R.map(getWordBeforeUnderscore), R.keys) | |
const modifier = (fn) => (obj) => (acc, val) => R.merge(acc, {[fn(val)]: obj[val]}); | |
const replaceKeyReducer = R.compose(modifier, R.replace(R.__, '')) | |
const filterByStartsWith = R.compose(R.filter, R.startsWith) | |
const buildWithUniqKeys = (obj) => (acc, key) => R.compose(R.merge(acc), | |
R.objOf(key), | |
R.reduce(replaceKeyReducer(`${key}_`)(obj), {}), | |
filterByStartsWith(key), | |
R.keys)(obj) | |
R.compose(R.reduce(buildWithUniqKeys(initObject), {}), getUniqKeys)(initObject) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment