Created
January 18, 2018 22:24
-
-
Save jaredallard/3d10a6e276a1c4b57717a1919128a7ae to your computer and use it in GitHub Desktop.
Terrible Channel to "branch" code
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
// WEB ENV | |
const fs = require('fs') | |
const data = fs.readFileSync('./workfront2.json') // actually proofhq | |
let channel; | |
try { | |
channel = JSON.parse(data) | |
} catch(e) { | |
throw new Error('Failed to process Channel JSON.') | |
} | |
/** | |
* Find a method's object. | |
* | |
* @param {String} method Method Name | |
* @return {Array} List of it's bricks (zebricks) | |
*/ | |
const findMethod = method => { | |
const filteredMethods = channel.definition.methods.filter(chan => { | |
if(chan.name === method) return true | |
return false | |
}) | |
if(!filteredMethods[0]) throw new Error('Method not found.') | |
return filteredMethods[0].zebricks | |
} | |
/** | |
* Processes and generates a tree of the method's output. | |
* | |
* @param {Array/Object} brick Array of bricks or an object of bricks | |
* @param {Array} [child=[]] Children object to modify | |
* @param {Boolean} isName If true, it's a method name | |
* @param {Boolean} root If true, we're the first method | |
* @return {Object} Tree object | |
*/ | |
const processor = (brick, child = [], isName, root) => { | |
let newPosChild; | |
if(isName) { | |
if(!root) { | |
const newChild = child.push({ | |
name: isName, | |
type: 'method', | |
children: [] | |
}) | |
const newPos = newChild - 1 | |
newPosChild = child[newPos] | |
} else { | |
newPosChild = { | |
name: isName, | |
root: true, | |
children: [] | |
} | |
} | |
brick.forEach(realBrick => { | |
return processor(realBrick, newPosChild.children) | |
}) | |
} | |
if(brick.brick === 'branch') { | |
const variableName = brick.config.conditions[0].expression.A | |
let pushed = child.push({ | |
name: variableName, | |
type: "branch", | |
children: [] | |
}) | |
const pos = child[pushed - 1].children | |
brick.config.conditions.forEach(condition => { | |
const newPushed = pos.push({ | |
name: condition.default ? 'default' : condition.expression.B, | |
type: "condition", | |
children: [] | |
}) | |
let newPos = newPushed-1 | |
const newBrick = findMethod(condition.method) | |
if(condition.method) { | |
processor(newBrick, pos[newPos].children, condition.method) | |
} | |
}) | |
} | |
return newPosChild | |
} | |
const foundMethodBricks = findMethod("New Proof") | |
const tree = processor(foundMethodBricks, null, 'New Proof', true) | |
console.log(JSON.stringify(tree, 0, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment