-
-
Notifications
You must be signed in to change notification settings - Fork 570
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
raklaptudirm
merged 6 commits into
TheAlgorithms:master
from
yudukikun5120:feat/parse-nested-brackets
Nov 21, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a00a97
feat: add `parseNestedBrackets`
yudukikun5120 c6e030a
test: remove obvious cases
yudukikun5120 614d276
docs: remove the notation of recursiveness
yudukikun5120 b19cec5
docs: add comments
yudukikun5120 d14cee8
docs: rename `v` to `tag`
yudukikun5120 beb7734
chore: separate parameters for brackets
yudukikun5120 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)", | ||
| ]); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.