From 1ac8d3a9d67356046d8e052fabea608afeed7492 Mon Sep 17 00:00:00 2001 From: Abdullah Malek <19255312+absorpheus@users.noreply.github.com> Date: Mon, 15 Jul 2024 19:28:03 +0100 Subject: [PATCH] refactor: use two separate functions as a cleaner solution. Add tests (#30) * Add removeTrailingSpaces helper * Add tests for removeTrailingSpaces * Add tests for preserveNewlineIndentation --- src/methods/aggregateDetails.ts | 5 +- src/types.ts | 2 - src/utils/createTextBlockHandler.ts | 5 - src/utils/createTextBlockHandlerChain.ts | 9 -- src/utils/handleTextForContext.ts | 9 -- src/utils/index.ts | 5 +- src/utils/preserveNewlineIndentation.ts | 11 +- src/utils/removeAllLastNewLines.ts | 7 - src/utils/removeTrailingSpaces.ts | 6 + test/handleTextForContext.spec.ts | 52 -------- test/preserveNewlineIndentation.spec.ts | 20 +++ test/removeAllLastNewlines.spec.ts | 36 ------ test/removeTrailingSpaces.spec.ts | 156 +++++++++++++++++++++++ 13 files changed, 190 insertions(+), 133 deletions(-) delete mode 100644 src/utils/createTextBlockHandler.ts delete mode 100644 src/utils/createTextBlockHandlerChain.ts delete mode 100644 src/utils/handleTextForContext.ts delete mode 100644 src/utils/removeAllLastNewLines.ts create mode 100644 src/utils/removeTrailingSpaces.ts delete mode 100644 test/handleTextForContext.spec.ts create mode 100644 test/preserveNewlineIndentation.spec.ts delete mode 100644 test/removeAllLastNewlines.spec.ts create mode 100644 test/removeTrailingSpaces.spec.ts diff --git a/src/methods/aggregateDetails.ts b/src/methods/aggregateDetails.ts index 13c0473..083f21a 100644 --- a/src/methods/aggregateDetails.ts +++ b/src/methods/aggregateDetails.ts @@ -1,8 +1,7 @@ import { IBook, IBookAnnotation, ICombinedBooksAndHighlights } from '../types'; import { getBooks } from './getBooks'; import { getAnnotations } from './getAnnotations'; -import { preserveNewlineIndentation } from 'src/utils' -import { handleTextForContext} from 'src/utils/handleTextForContext' +import { preserveNewlineIndentation, removeTrailingSpaces } from 'src/utils' export const aggregateBookAndHighlightDetails = async (): Promise => { const books = await getBooks(); @@ -32,7 +31,7 @@ export const aggregateBookAndHighlightDetails = async (): Promise string diff --git a/src/utils/createTextBlockHandler.ts b/src/utils/createTextBlockHandler.ts deleted file mode 100644 index bdec2fa..0000000 --- a/src/utils/createTextBlockHandler.ts +++ /dev/null @@ -1,5 +0,0 @@ -export const createTextBlockHandler = (regex: RegExp, replaceValue: string) => { - return (textBlock: string) => { - return regex.test(textBlock) ? textBlock.replace(regex, replaceValue) : textBlock - } -} diff --git a/src/utils/createTextBlockHandlerChain.ts b/src/utils/createTextBlockHandlerChain.ts deleted file mode 100644 index 880ee82..0000000 --- a/src/utils/createTextBlockHandlerChain.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { TextBlockHandler } from "src/types" - -export const createTextBlockHandlerChain = (...textBlockHandlers: TextBlockHandler[]) => { - return (textBlock: string) => { - return textBlockHandlers.reduce((text, textBlockHandler) => { - return textBlockHandler(text) - }, textBlock) - } -} \ No newline at end of file diff --git a/src/utils/handleTextForContext.ts b/src/utils/handleTextForContext.ts deleted file mode 100644 index 5010cf8..0000000 --- a/src/utils/handleTextForContext.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { preserveNewlineIndentation} from "./preserveNewlineIndentation"; -import { removeAllLastNewlines } from "./removeAllLastNewLines"; -import { createTextBlockHandlerChain } from "./createTextBlockHandlerChain" - - -export const handleTextForContext = createTextBlockHandlerChain( - preserveNewlineIndentation, - removeAllLastNewlines -) diff --git a/src/utils/index.ts b/src/utils/index.ts index cd88b57..a112e67 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,2 @@ export * from './preserveNewlineIndentation' -export * from './removeAllLastNewLines' -export * from './createTextBlockHandler' -export * from './handleTextForContext' -export * from './createTextBlockHandlerChain' +export * from './removeTrailingSpaces' diff --git a/src/utils/preserveNewlineIndentation.ts b/src/utils/preserveNewlineIndentation.ts index 7e5dd52..c0bf6ba 100644 --- a/src/utils/preserveNewlineIndentation.ts +++ b/src/utils/preserveNewlineIndentation.ts @@ -1,7 +1,6 @@ -import { createTextBlockHandler } from './createTextBlockHandler' - // Handler of double new line characters (\n\n) to preserve proper indentation in text blocks -export const preserveNewlineIndentation = createTextBlockHandler( - /\n+\s*/g, - '\n' -) +export const preserveNewlineIndentation = (textBlock: string): string => { + const stringWithNewLines = /\n+\s*/g; + + return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock; +} diff --git a/src/utils/removeAllLastNewLines.ts b/src/utils/removeAllLastNewLines.ts deleted file mode 100644 index 37535a7..0000000 --- a/src/utils/removeAllLastNewLines.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { createTextBlockHandler } from "./createTextBlockHandler"; - -// Handler of all new line characters (\n) at the end of text blocks to prevent new lines appearing at the end of text blocks -export const removeAllLastNewlines = createTextBlockHandler( - /\n+$/g, - "" -) diff --git a/src/utils/removeTrailingSpaces.ts b/src/utils/removeTrailingSpaces.ts new file mode 100644 index 0000000..cff48c3 --- /dev/null +++ b/src/utils/removeTrailingSpaces.ts @@ -0,0 +1,6 @@ +// Handler of all space, tab or newline characters at the end of text blocks to prevent new lines appearing +export const removeTrailingSpaces = (textBlock: string): string => { + const endLineSpaces = /\s+$/; + + return endLineSpaces.test(textBlock) ? textBlock.replace(endLineSpaces, '') : textBlock; +} diff --git a/test/handleTextForContext.spec.ts b/test/handleTextForContext.spec.ts deleted file mode 100644 index 04081bd..0000000 --- a/test/handleTextForContext.spec.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { describe, expect, test } from 'vitest' -import { handleTextForContext } from 'src/utils/handleTextForContext' - -describe('handleTextForContext', () => { - test('Should remove double newline characters at the end of text', () => { - const text = `reboot?\n\n` - const actual = handleTextForContext(text) - const expected = `reboot?` - - expect(actual).toEqual(expected) - }) - - test('Should remove triple newline characters at the end of text', () => { - const text = `project.\n\n\n` - const actual = handleTextForContext(text) - const expected = "project." - - expect(actual).toEqual(expected) - }) - - test('Should remove a newline character and double tab characters at the end of text', () => { - const text = `simple answers.\n\t\t` - const actual = handleTextForContext(text) - const expected = "simple answers." - - expect(actual).toEqual(expected) - }) - - test('Should remove a newline character and triple tab characters at the end of text', () => { - const text = `success.\n\t\t\t` - const actual = handleTextForContext(text) - const expected = "success." - - expect(actual).toEqual(expected) - }) - - test('Should remove multiple newline characters and tab characters at the end of text', () => { - const text = `instead.\n\t\t\t\t\n\t\t\t\t\n\t\t\t\t\t` - const actual = handleTextForContext(text) - const expected = "instead." - - expect(actual).toEqual(expected) - }) - - test('Should remove multiple newline characters and tab characters at the end of a longer text', () => { - const text = `a book\n\t\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t` - const actual = handleTextForContext(text) - const expected = "a book" - - expect(actual).toEqual(expected) - }) -}) diff --git a/test/preserveNewlineIndentation.spec.ts b/test/preserveNewlineIndentation.spec.ts new file mode 100644 index 0000000..09caf34 --- /dev/null +++ b/test/preserveNewlineIndentation.spec.ts @@ -0,0 +1,20 @@ +import { describe, expect, test } from 'vitest' +import { preserveNewlineIndentation } from 'src/utils' + +describe('preserveNewlineIndentation', () => { + test('Should handle double new line characters to preserve proper indentation in text', () => { + const text = `This is an example text to test the handling of double newline\n\ncharacters in text.` + const actual = preserveNewlineIndentation(text) + const expected = `This is an example text to test the handling of double newline\ncharacters in text.` + + expect(actual).toEqual(expected) + }) + + test('Should handle multiple double new line characters to preserve proper indentation in text', () => { + const text = `This is an example\n\ntext to test\n\nthe handling of multiple double newline\n\ncharacters in text.` + const actual = preserveNewlineIndentation(text) + const expected = `This is an example\ntext to test\nthe handling of multiple double newline\ncharacters in text.` + + expect(actual).toEqual(expected) + }) +}) diff --git a/test/removeAllLastNewlines.spec.ts b/test/removeAllLastNewlines.spec.ts deleted file mode 100644 index 18445a8..0000000 --- a/test/removeAllLastNewlines.spec.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { describe, expect, test } from 'vitest' -import { removeAllLastNewlines } from 'src/utils/removeAllLastNewLines' - -describe('removeAllLastNewlines', () => { - test('Should remove a newline character at the end of text', () => { - const contextualText = "This is an example text to test the removal of a newline character at the end of the text\n" - const actual = removeAllLastNewlines(contextualText) - const expected = "This is an example text to test the removal of a newline character at the end of the text" - - expect(actual).toEqual(expected) - }) - - test('Should remove multiple newline characters at the end of text', () => { - const contextualText = "This is an example text to test the removal of multiple newline characters at the end of the text\n\n" - const actual = removeAllLastNewlines(contextualText) - const expected = "This is an example text to test the removal of multiple newline characters at the end of the text" - - expect(actual).toEqual(expected) - }) - - test('Should preserve all other newline characters', () => { - const contextualText = "This is an example text to test the removal of a newline character at the end of the text\n while preserving other newline characters\n" - const actual = removeAllLastNewlines(contextualText) - const expected = "This is an example text to test the removal of a newline character at the end of the text\n while preserving other newline characters" - - expect(actual).toEqual(expected) - }) - - test('Should return the text when no newline characters exist at the end of the text', () => { - const contextualText = "This is an example text to test that the text is returned when no newline characters exist at the end of the text" - const actual = removeAllLastNewlines(contextualText) - const expected = "This is an example text to test that the text is returned when no newline characters exist at the end of the text" - - expect(actual).toEqual(expected) - }) -}) diff --git a/test/removeTrailingSpaces.spec.ts b/test/removeTrailingSpaces.spec.ts new file mode 100644 index 0000000..87dcffc --- /dev/null +++ b/test/removeTrailingSpaces.spec.ts @@ -0,0 +1,156 @@ +import { describe, expect, test } from 'vitest' +import { removeTrailingSpaces } from 'src/utils' + +describe('removeTrailingSpaces', () => { + test('Should remove a newline character at the end of text', () => { + const text = `This is an example text to test the removal of a newline character at the end of the text.\n` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of a newline character at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove double newline characters at the end of text', () => { + const text = `This is an example text to test the removal of double newline characters at the end of the text.\n\n` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of double newline characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove triple newline characters at the end of text', () => { + const text = `This is an example text to test the removal of triple newline characters at the end of the text.\n\n\n` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of triple newline characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove many newline characters at the end of text', () => { + const text = `This is an example text to test the removal of many newline characters at the end of the text.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of many newline characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove many newline characters at the end of text while preserving space, tab and newline characters within the text', () => { + const text = `This is an example text to test the removal of many newline characters at the end of the text while preserving space , tab\t and newline\n characters within the text.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of many newline characters at the end of the text while preserving space , tab\t and newline\n characters within the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove a tab character at the end of text', () => { + const text = `This is an example text to test the removal of a tab character at the end of the text.\t` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of a tab character at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove double tab characters at the end of text', () => { + const text = `This is an example text to test the removal of double tab characters at the end of the text.\t\t` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of double tab characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove triple tab characters at the end of text', () => { + const text = `This is an example text to test the removal of triple tab characters at the end of the text.\t\t\t` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of triple tab characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove many tab characters at the end of text', () => { + const text = `This is an example text to test the removal of many tab characters at the end of the text.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of many tab characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove many tab characters at the end of text while preserving space, tab and newline characters within the text', () => { + const text = `This is an example text to test the removal of many tab characters at the end of the text while preserving space , tab\t and newline\n characters within the text.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of many tab characters at the end of the text while preserving space , tab\t and newline\n characters within the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove a space character at the end of text', () => { + const text = `This is an example text to test the removal of a space character at the end of the text. ` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of a space character at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove double space characters at the end of text', () => { + const text = `This is an example text to test the removal of double space characters at the end of the text. ` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of double space characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove triple space characters at the end of text', () => { + const text = `This is an example text to test the removal of triple space characters at the end of the text. ` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of triple space characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove many space characters at the end of text', () => { + const text = `This is an example text to test the removal of many space characters at the end of the text. ` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of many space characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove many space characters at the end of text while preserving space, tab and newline characters at the end of the text.', () => { + const text = `This is an example text to test the removal of many space characters at the end of the text preserving space , tab\b and newline\n characters at the end of the text. ` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of many space characters at the end of the text preserving space , tab\b and newline\n characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove multiple space, tab and newline characters at the end of text', () => { + const text = `This is an example text to test the removal of many space, tab and newline characters at the end of the text. \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t ` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of many space, tab and newline characters at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should remove multiple space, tab and newline characters at the end of text while preserving space, tab and newline characters within text', () => { + const text = `This is an example text to test the removal of many space , tab\t and newline\n characters at the end of the text while preserving space, tab and newline characters within text. \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t ` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test the removal of many space , tab\t and newline\n characters at the end of the text while preserving space, tab and newline characters within text.` + + expect(actual).toEqual(expected) + }) + + test('Should return the text when no space, tab or newline characters exist at the end of the text', () => { + const text = `This is an example text to test that the text is returned when no space, tab or newline characters exist at the end of the text.` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test that the text is returned when no space, tab or newline characters exist at the end of the text.` + + expect(actual).toEqual(expected) + }) + + test('Should return the text when no space, tab or newline characters exist at the end of the text while preserving space, tab and newline characters within text', () => { + const text = `This is an example text to test that the text is returned when no space , tab\t or newline\n characters exist at the end of the text while preserving space, tab and newline characters within text.` + const actual = removeTrailingSpaces(text) + const expected = `This is an example text to test that the text is returned when no space , tab\t or newline\n characters exist at the end of the text while preserving space, tab and newline characters within text.` + + expect(actual).toEqual(expected) + }) +})