@n3okill/utils
Version:
Many javascript helpers
40 lines • 1.16 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBalanced = getBalanced;
const balancedCounter_1 = require("./balancedCounter");
/**
* @internal
* @param input
* @param open
* @param close
* @returns
*/
function getBalanced(input, open = "{", close = "}") {
const openLength = open.length;
const closeLength = close.length;
if (!(0, balancedCounter_1.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
;