From 5a00a9714211cd2e57bdaee3bb32eedf7796898f Mon Sep 17 00:00:00 2001 From: Yuzuki Arai Date: Sun, 20 Nov 2022 16:36:35 +0900 Subject: [PATCH 1/6] feat: add `parseNestedBrackets` --- other/parse_nested_brackets.ts | 45 ++++++++++++++++++++++++ other/test/parse_nested_brackets.test.ts | 32 +++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 other/parse_nested_brackets.ts create mode 100644 other/test/parse_nested_brackets.test.ts diff --git a/other/parse_nested_brackets.ts b/other/parse_nested_brackets.ts new file mode 100644 index 00000000..03de7075 --- /dev/null +++ b/other/parse_nested_brackets.ts @@ -0,0 +1,45 @@ +/** + * @function parseNestedBrackets + * @description Parse nested brackets algorithm (recursive implementation) for a string. + * @param {string} w - string to parse + * @param { openBrackets: string; closingBrackets: string } brackets - object containing the open and closing brackets + * @returns {string[]} - array of the tags + * @example parseNestedBrackets(`
`) => [ '
', '' ] + * @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 = ( + w: string, + brackets: { openBrackets: string; closingBrackets: string } = { + openBrackets: "<", + closingBrackets: ">", + } +) => { + let array: string[] = []; + let prFloor = 0; + let begin = 0, + end = 0; + for (let i = 0; i < w.length; i++) { + if (w[i] === brackets.openBrackets) { + prFloor++; + if (prFloor === 1) begin = i; + } else if (w[i] === brackets.closingBrackets) { + if (prFloor === 1) { + end = i; + const v = w.slice(begin + 1, end); + array.push(`${brackets.openBrackets}${v}${brackets.closingBrackets}`); + array = array.concat(parseNestedBrackets(v, brackets)); + } + prFloor--; + } + } + return array; +}; diff --git a/other/test/parse_nested_brackets.test.ts b/other/test/parse_nested_brackets.test.ts new file mode 100644 index 00000000..45773951 --- /dev/null +++ b/other/test/parse_nested_brackets.test.ts @@ -0,0 +1,32 @@ +import { parseNestedBrackets } from "../parse_nested_brackets"; + +describe("parseNestedBrackets", () => { + it("should be defined", () => { + expect(parseNestedBrackets).toBeDefined(); + }); + it("should return an array", () => { + expect(Array.isArray(parseNestedBrackets(""))).toBeTruthy(); + }); + it("should return an array of strings", () => { + expect(typeof parseNestedBrackets("")[0]).toBe("string"); + }); + it("should return an array of the tags", () => { + expect(parseNestedBrackets("
")).toEqual([ + "
", + "", + ]); + }); + it("should return an array of the tags (recursive)", () => { + expect( + parseNestedBrackets( + `THIS IS SAMPLE TEXT(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))`, + { openBrackets: "(", closingBrackets: ")" } + ) + ).toEqual([ + "(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))", + "(ITEM fuga hoge)", + "(ITEM2 nogami(ABBR))", + "(ABBR)", + ]); + }); +}); From c6e030a12a1a25ca0865899d146a78641da567bc Mon Sep 17 00:00:00 2001 From: Yuzuki Arai Date: Sun, 20 Nov 2022 16:46:46 +0900 Subject: [PATCH 2/6] test: remove obvious cases --- other/test/parse_nested_brackets.test.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/other/test/parse_nested_brackets.test.ts b/other/test/parse_nested_brackets.test.ts index 45773951..5febe60c 100644 --- a/other/test/parse_nested_brackets.test.ts +++ b/other/test/parse_nested_brackets.test.ts @@ -1,15 +1,6 @@ import { parseNestedBrackets } from "../parse_nested_brackets"; describe("parseNestedBrackets", () => { - it("should be defined", () => { - expect(parseNestedBrackets).toBeDefined(); - }); - it("should return an array", () => { - expect(Array.isArray(parseNestedBrackets(""))).toBeTruthy(); - }); - it("should return an array of strings", () => { - expect(typeof parseNestedBrackets("")[0]).toBe("string"); - }); it("should return an array of the tags", () => { expect(parseNestedBrackets("
")).toEqual([ "
", From 614d276f76147b0f70f2cd5fbc57a89f7f56f44e Mon Sep 17 00:00:00 2001 From: Yuzuki Arai Date: Sun, 20 Nov 2022 18:44:22 +0900 Subject: [PATCH 3/6] docs: remove the notation of recursiveness --- other/parse_nested_brackets.ts | 2 +- other/test/parse_nested_brackets.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/other/parse_nested_brackets.ts b/other/parse_nested_brackets.ts index 03de7075..6f15cbe9 100644 --- a/other/parse_nested_brackets.ts +++ b/other/parse_nested_brackets.ts @@ -1,6 +1,6 @@ /** * @function parseNestedBrackets - * @description Parse nested brackets algorithm (recursive implementation) for a string. + * @description Parse nested brackets algorithm for a string. * @param {string} w - string to parse * @param { openBrackets: string; closingBrackets: string } brackets - object containing the open and closing brackets * @returns {string[]} - array of the tags diff --git a/other/test/parse_nested_brackets.test.ts b/other/test/parse_nested_brackets.test.ts index 5febe60c..3e38d1da 100644 --- a/other/test/parse_nested_brackets.test.ts +++ b/other/test/parse_nested_brackets.test.ts @@ -7,7 +7,7 @@ describe("parseNestedBrackets", () => { "", ]); }); - it("should return an array of the tags (recursive)", () => { + 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)))`, From b19cec5e8d2288d093b0d93e74a1aa08e7380d6d Mon Sep 17 00:00:00 2001 From: Yuzuki Arai Date: Sun, 20 Nov 2022 23:56:41 +0900 Subject: [PATCH 4/6] docs: add comments --- other/parse_nested_brackets.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/other/parse_nested_brackets.ts b/other/parse_nested_brackets.ts index 6f15cbe9..1c1a6f70 100644 --- a/other/parse_nested_brackets.ts +++ b/other/parse_nested_brackets.ts @@ -1,7 +1,7 @@ /** * @function parseNestedBrackets * @description Parse nested brackets algorithm for a string. - * @param {string} w - string to parse + * @param {string} text - text to parse * @param { openBrackets: string; closingBrackets: string } brackets - object containing the open and closing brackets * @returns {string[]} - array of the tags * @example parseNestedBrackets(`
`) => [ '
', '' ] @@ -17,25 +17,27 @@ */ export const parseNestedBrackets = ( - w: string, + text: string, brackets: { openBrackets: string; closingBrackets: string } = { openBrackets: "<", closingBrackets: ">", } ) => { - let array: string[] = []; - let prFloor = 0; - let begin = 0, - end = 0; - for (let i = 0; i < w.length; i++) { - if (w[i] === brackets.openBrackets) { + 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] === brackets.openBrackets) { prFloor++; if (prFloor === 1) begin = i; - } else if (w[i] === brackets.closingBrackets) { + } else if (text[i] === brackets.closingBrackets) { if (prFloor === 1) { end = i; - const v = w.slice(begin + 1, end); + const v = text.slice(begin + 1, end); + // push the tag in this present floor. array.push(`${brackets.openBrackets}${v}${brackets.closingBrackets}`); + // push the array of the tags in the next floor. array = array.concat(parseNestedBrackets(v, brackets)); } prFloor--; From d14cee85a6dcc4337d90ebc52e72c89925fe3558 Mon Sep 17 00:00:00 2001 From: Yuzuki Arai Date: Mon, 21 Nov 2022 00:01:20 +0900 Subject: [PATCH 5/6] docs: rename `v` to `tag` --- other/parse_nested_brackets.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/parse_nested_brackets.ts b/other/parse_nested_brackets.ts index 1c1a6f70..7d2d3e87 100644 --- a/other/parse_nested_brackets.ts +++ b/other/parse_nested_brackets.ts @@ -34,11 +34,11 @@ export const parseNestedBrackets = ( } else if (text[i] === brackets.closingBrackets) { if (prFloor === 1) { end = i; - const v = text.slice(begin + 1, end); + const tag = text.slice(begin + 1, end); // push the tag in this present floor. - array.push(`${brackets.openBrackets}${v}${brackets.closingBrackets}`); + array.push(`${brackets.openBrackets}${tag}${brackets.closingBrackets}`); // push the array of the tags in the next floor. - array = array.concat(parseNestedBrackets(v, brackets)); + array = array.concat(parseNestedBrackets(tag, brackets)); } prFloor--; } From beb77348eee5ca3daf5062dd8af1d0b0e1f0e54d Mon Sep 17 00:00:00 2001 From: Yuzuki Arai Date: Mon, 21 Nov 2022 03:23:31 +0900 Subject: [PATCH 6/6] chore: separate parameters for brackets --- other/parse_nested_brackets.ts | 62 ++++++++++++------------ other/test/parse_nested_brackets.test.ts | 3 +- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/other/parse_nested_brackets.ts b/other/parse_nested_brackets.ts index 7d2d3e87..dce1ef37 100644 --- a/other/parse_nested_brackets.ts +++ b/other/parse_nested_brackets.ts @@ -2,7 +2,8 @@ * @function parseNestedBrackets * @description Parse nested brackets algorithm for a string. * @param {string} text - text to parse - * @param { openBrackets: string; closingBrackets: string } brackets - object containing the open and closing brackets + * @param {string} openBrackets - open brackets + * @param {string} closingBrackets - closing brackets * @returns {string[]} - array of the tags * @example parseNestedBrackets(`
`) => [ '
', '' ] * @example parseNestedBrackets( @@ -15,33 +16,32 @@ '(ABBR)' ] */ - -export const parseNestedBrackets = ( - text: string, - brackets: { openBrackets: string; closingBrackets: 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] === brackets.openBrackets) { - prFloor++; - if (prFloor === 1) begin = i; - } else if (text[i] === brackets.closingBrackets) { - if (prFloor === 1) { - end = i; - const tag = text.slice(begin + 1, end); - // push the tag in this present floor. - array.push(`${brackets.openBrackets}${tag}${brackets.closingBrackets}`); - // push the array of the tags in the next floor. - array = array.concat(parseNestedBrackets(tag, brackets)); - } - prFloor--; - } - } - return array; -}; + 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; + }; diff --git a/other/test/parse_nested_brackets.test.ts b/other/test/parse_nested_brackets.test.ts index 3e38d1da..751e8651 100644 --- a/other/test/parse_nested_brackets.test.ts +++ b/other/test/parse_nested_brackets.test.ts @@ -11,7 +11,8 @@ describe("parseNestedBrackets", () => { expect( parseNestedBrackets( `THIS IS SAMPLE TEXT(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))`, - { openBrackets: "(", closingBrackets: ")" } + "(", + ")" ) ).toEqual([ "(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))",