Created
June 14, 2018 14:00
-
-
Save fernandocamargo/4fd0fc5ca979d2e257705bcb4c10e099 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
| export const mock = () => ({ | |
| a: () => {}, | |
| b: () => {}, | |
| c: () => {}, | |
| d: { | |
| e: { | |
| f: () => {}, | |
| g: { | |
| h: { | |
| i: () => {} | |
| } | |
| }, | |
| j: [ | |
| () => {}, | |
| { | |
| l: { | |
| m: { | |
| n: () => {} | |
| }, | |
| o: () => {} | |
| } | |
| } | |
| ] | |
| } | |
| }, | |
| p: () => {} | |
| }); | |
| export const last = collection => collection[collection.length - 1]; | |
| export const isFunction = object => typeof object === "function"; | |
| export const ensureIsArray = object => | |
| Array.isArray(object) ? object : [object]; | |
| export const getAttribute = attribute => object => object[attribute]; | |
| export const traverse = (object, reference = []) => ({ | |
| transform: settings => { | |
| return Object.entries(object).reduce((stacks, [key, value]) => { | |
| const path = reference.concat(key); | |
| const isDeep = !!Object.keys(value).length; | |
| return isDeep | |
| ? traverse(value, path).transform(settings) | |
| : stacks.map((stack, index) => { | |
| const { transformation } = settings[index]; | |
| return transformation(stack, path, value); | |
| }); | |
| }, ensureIsArray(settings).map(getAttribute("initial"))); | |
| } | |
| }); | |
| console.log( | |
| traverse(mock()).transform([ | |
| { | |
| initial: {}, | |
| transformation: (stack, path, value) => | |
| Object.assign(stack, { | |
| [last(path)]: function LOL(...params) { | |
| return value(...params); | |
| } | |
| }) | |
| }, | |
| { | |
| initial: new Map(), | |
| transformation: (stack, path, value) => | |
| !isFunction(value) ? stack : stack.set(value, { path }) | |
| } | |
| ]) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment