Created
July 14, 2018 07:20
-
-
Save mikesmullin/ba03cc91fe07b1c92b49db40351007d8 to your computer and use it in GitHub Desktop.
sort of looks like a regexp parser; will use regexp instead
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
var wrap = (list, test, cb) => { | |
const stack = []; | |
let start, end, lastTest, result, lastItem, item, out; | |
for (let i=0,len=list.length; i<len; i++) { | |
item = list.splice(i,1)[0]; | |
result = test(item, lastItem); | |
if ((!result && lastTest !== result) || (i === list.length-1 && lastTest === result)) { | |
out = cb(item, i, start, end, stack, lastItem); | |
if (null != out) { | |
list.splice(i, 0, ...out); | |
i += out.length; | |
} | |
end = start = undefined; | |
stack.splice(0); | |
} | |
else { | |
if (null == start) start = i; | |
end = i; | |
if (0 === stack.length || stack[0].lvl < item.lvl) stack.unshift(item); | |
else while (stack.length > 0 && stack[0].lvl > item.lvl) stack.pop(); | |
} | |
lastTest = result; | |
lastItem = item; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment