diff options
author | Minteck <contact@minteck.org> | 2021-12-21 15:25:09 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2021-12-21 15:25:09 +0100 |
commit | e703e51c9c09b22e3bcda9a1faf1e05897f60616 (patch) | |
tree | 4fd67a209ad6988fbf569d7dff8bc37ba45baf95 /_mint/node_modules/balanced-match/index.js | |
download | mint-e703e51c9c09b22e3bcda9a1faf1e05897f60616.tar.gz mint-e703e51c9c09b22e3bcda9a1faf1e05897f60616.tar.bz2 mint-e703e51c9c09b22e3bcda9a1faf1e05897f60616.zip |
Initial commit
Diffstat (limited to '_mint/node_modules/balanced-match/index.js')
-rw-r--r-- | _mint/node_modules/balanced-match/index.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/_mint/node_modules/balanced-match/index.js b/_mint/node_modules/balanced-match/index.js new file mode 100644 index 0000000..c67a646 --- /dev/null +++ b/_mint/node_modules/balanced-match/index.js @@ -0,0 +1,62 @@ +'use strict'; +module.exports = balanced; +function balanced(a, b, str) { + if (a instanceof RegExp) a = maybeMatch(a, str); + if (b instanceof RegExp) b = maybeMatch(b, str); + + var r = range(a, b, str); + + return r && { + start: r[0], + end: r[1], + pre: str.slice(0, r[0]), + body: str.slice(r[0] + a.length, r[1]), + post: str.slice(r[1] + b.length) + }; +} + +function maybeMatch(reg, str) { + var m = str.match(reg); + return m ? m[0] : null; +} + +balanced.range = range; +function range(a, b, str) { + var begs, beg, left, right, result; + var ai = str.indexOf(a); + var bi = str.indexOf(b, ai + 1); + var i = ai; + + if (ai >= 0 && bi > 0) { + if(a===b) { + return [ai, bi]; + } + begs = []; + left = str.length; + + while (i >= 0 && !result) { + if (i == ai) { + begs.push(i); + ai = str.indexOf(a, i + 1); + } else if (begs.length == 1) { + result = [ begs.pop(), bi ]; + } else { + beg = begs.pop(); + if (beg < left) { + left = beg; + right = bi; + } + + bi = str.indexOf(b, i + 1); + } + + i = ai < bi && ai >= 0 ? ai : bi; + } + + if (begs.length) { + result = [ left, right ]; + } + } + + return result; +} |