From be938fd21f6118774873e0987a9c58cbf07dd7ef Mon Sep 17 00:00:00 2001 From: Nikolay Kostyurin Date: Wed, 25 Sep 2019 09:29:21 +0200 Subject: [PATCH] fix(parser): try to hack terser minifier that removes working code (#49) * fix(parser): try to hack terser minifier that removes working code Fixes #48 --- packages/bbob-parser/src/utils.js | 45 ++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/bbob-parser/src/utils.js b/packages/bbob-parser/src/utils.js index 683a7156..305fd31c 100644 --- a/packages/bbob-parser/src/utils.js +++ b/packages/bbob-parser/src/utils.js @@ -14,38 +14,51 @@ import { /** * Creates a grabber wrapper for source string, that helps to iterate over string char by char * @param {String} source - * @param {Function} onSkip + * @param {Object} options + * @param {Function} options.onSkip * @returns */ -export const createCharGrabber = (source, { onSkip } = {}) => { - let idx = 0; +export const createCharGrabber = (source, options) => { + // let idx = 0; + const cursor = { + pos: 0, + length: source.length, + }; const skip = () => { - idx += 1; + cursor.pos += 1; - if (onSkip) { - onSkip(); + if (options && options.onSkip) { + options.onSkip(); } }; - const hasNext = () => source.length > idx; - const getRest = () => source.substr(idx); - const getCurr = () => source[idx]; + const hasNext = () => cursor.length > cursor.pos; + const getRest = () => source.substr(cursor.pos); + const getCurr = () => source[cursor.pos]; return { skip, hasNext, - isLast: () => (idx === source.length), + isLast: () => (cursor.pos === cursor.length), + /** + * @param {Function} cond + * @returns {string} + */ grabWhile: (cond) => { - const start = idx; + let start = 0; + + if (hasNext()) { + start = cursor.pos; - while (hasNext() && cond(getCurr())) { - skip(); + while (hasNext() && cond(getCurr())) { + skip(); + } } - return source.substr(start, idx - start); + return source.substr(start, cursor.pos - start); }, - getNext: () => source[idx + 1], - getPrev: () => source[idx - 1], + getNext: () => source[cursor.pos + 1], + getPrev: () => source[cursor.pos - 1], getCurr, getRest, /**