UNPKG

@n3okill/utils

Version:
37 lines 1.03 kB
import { balancedCounter } from "./balancedCounter"; /** * @internal * @param input * @param open * @param close * @returns */ export function getBalanced(input, open = "{", close = "}") { const openLength = open.length; const closeLength = close.length; if (!balancedCounter(input, open, close)) { return false; } const opens = []; const closes = []; const matches = []; let openI = input.indexOf(open); while (openI !== -1) { opens.push(openI); openI = input.indexOf(open, openI + openLength); } while (opens.length) { const openI = opens.pop(); let closeI = input.indexOf(close, openI + openLength); while (closes.indexOf(closeI) !== -1) { closeI = input.indexOf(close, closeI + closeLength); } if (closeI === -1) { return false; } closes.push(closeI); matches.push([openI, closeI]); } return matches.reverse(); } //# sourceMappingURL=getBalanced.js.map