Skip to content

Instantly share code, notes, and snippets.

@mikesmullin
Created July 14, 2018 07:20
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 mikesmullin/ba03cc91fe07b1c92b49db40351007d8 to your computer and use it in GitHub Desktop.
Save mikesmullin/ba03cc91fe07b1c92b49db40351007d8 to your computer and use it in GitHub Desktop.
sort of looks like a regexp parser; will use regexp instead
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