Skip to content

feat: add parseNestedBrackets #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions other/parse_nested_brackets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @function parseNestedBrackets
* @description Parse nested brackets algorithm for a string.
* @param {string} text - text to parse
* @param {string} openBrackets - open brackets
* @param {string} closingBrackets - closing brackets
* @returns {string[]} - array of the tags
* @example parseNestedBrackets(`<MAIN hoge><MAIN2 fuga>`) => [ '<MAIN hoge>', '<MAIN2 fuga>' ]
* @example parseNestedBrackets(
* `THIS IS SAMPLE TEXT(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))`,
* { openBrackets: '(', closingBrackets: ')' }) =>
* [
'(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))',
'(ITEM fuga hoge)',
'(ITEM2 nogami(ABBR))',
'(ABBR)'
]
*/
export const parseNestedBrackets = (
text: string,
openBrackets = "<",
closingBrackets = ">"
) => {
let array: string[] = []; // The array of the tags in this present floor.
let prFloor = 0; // The present floor.
let begin = 0, // The begin index of the tag.
end = 0; // The end index of the tag.
for (let i = 0; i < text.length; i++) {
if (text[i] === openBrackets) {
prFloor++;
if (prFloor === 1) begin = i;
} else if (text[i] === closingBrackets) {
if (prFloor === 1) {
end = i;
const tag = text.slice(begin + 1, end);
// push the tag in this present floor.
array.push(`${openBrackets}${tag}${closingBrackets}`);
// push the array of the tags in the next floor.
array = array.concat(
parseNestedBrackets(tag, openBrackets, closingBrackets)
);
}
prFloor--;
}
}
return array;
};
24 changes: 24 additions & 0 deletions other/test/parse_nested_brackets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { parseNestedBrackets } from "../parse_nested_brackets";

describe("parseNestedBrackets", () => {
it("should return an array of the tags", () => {
expect(parseNestedBrackets("<MAIN hoge><MAIN2 fuga>")).toEqual([
"<MAIN hoge>",
"<MAIN2 fuga>",
]);
});
it("should return an array of the tags (nested)", () => {
expect(
parseNestedBrackets(
`THIS IS SAMPLE TEXT(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))`,
"(",
")"
)
).toEqual([
"(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))",
"(ITEM fuga hoge)",
"(ITEM2 nogami(ABBR))",
"(ABBR)",
]);
});
});