Skip to content

Instantly share code, notes, and snippets.

@apaleslimghost
Last active November 16, 2018 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apaleslimghost/425331338261e703ee8d9874768a545a to your computer and use it in GitHub Desktop.
Save apaleslimghost/425331338261e703ee8d9874768a545a to your computer and use it in GitHub Desktop.
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
@chee
Copy link

chee commented Nov 16, 2018

oh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment