Last active
November 16, 2018 12:45
-
-
Save apaleslimghost/425331338261e703ee8d9874768a545a to your computer and use it in GitHub Desktop.
This file contains 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 ruleRegex = /^([^:\s]+):(?!=):?([^:\n]*)$/ | |
module.exports = string => string.split('\n').concat('').reduce(({state, results}, line, index, lines) => ( | |
state.parsingRule? | |
line.startsWith('\t')? { | |
state: Object.assign( | |
{}, | |
state, | |
{ | |
ruleBody: state.ruleBody.concat( | |
line.trim() | |
) | |
} | |
), | |
results | |
} | |
: /* otherwise */ { | |
state: { | |
parsingRule: false, | |
ruleBody: [] | |
}, | |
results: Object.assign( | |
{}, | |
results, | |
{ | |
[state.parsingRule]: state.ruleBody.join('\n') | |
} | |
) | |
} | |
: line.match(ruleRegex)? { | |
state: { | |
parsingRule: RegExp.$1, | |
ruleBody: [] | |
}, | |
results | |
} | |
: /* otherwise */ {state, results, line} | |
), {state: {parsingRule: false, ruleBody: []}, results: {}}).results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oh