From fbdc9be1b7a96be00b156c2fd6af780cdde0fc1c Mon Sep 17 00:00:00 2001 From: Abdullah Malek <19255312+absorpheus@users.noreply.github.com> Date: Fri, 12 Jul 2024 21:36:57 +0100 Subject: [PATCH] Feature: Add text block handlers. Refactor tests. (#30) * add createTextBlockHandler * add createTextBlockHandlerChain * add handleTextForContext * refactor test --- 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 | 3 +++ src/utils/preserveNewlineIndentation.ts | 11 ++++++----- src/utils/removeAllLastNewLines.ts | 11 ++++++----- ...Text.spec.ts => handleTextForContext.spec.ts} | 16 ++++++++-------- 9 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 src/utils/createTextBlockHandler.ts create mode 100644 src/utils/createTextBlockHandlerChain.ts create mode 100644 src/utils/handleTextForContext.ts rename test/{representativeText.spec.ts => handleTextForContext.spec.ts} (71%) diff --git a/src/methods/aggregateDetails.ts b/src/methods/aggregateDetails.ts index d9f0d77..13c0473 100644 --- a/src/methods/aggregateDetails.ts +++ b/src/methods/aggregateDetails.ts @@ -1,7 +1,8 @@ import { IBook, IBookAnnotation, ICombinedBooksAndHighlights } from '../types'; import { getBooks } from './getBooks'; import { getAnnotations } from './getAnnotations'; -import { removeAllLastNewlines, preserveNewlineIndentation } from 'src/utils' +import { preserveNewlineIndentation } from 'src/utils' +import { handleTextForContext} from 'src/utils/handleTextForContext' export const aggregateBookAndHighlightDetails = async (): Promise => { const books = await getBooks(); @@ -31,7 +32,7 @@ export const aggregateBookAndHighlightDetails = async (): Promise string diff --git a/src/utils/createTextBlockHandler.ts b/src/utils/createTextBlockHandler.ts new file mode 100644 index 0000000..bdec2fa --- /dev/null +++ b/src/utils/createTextBlockHandler.ts @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..880ee82 --- /dev/null +++ b/src/utils/createTextBlockHandlerChain.ts @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000..5010cf8 --- /dev/null +++ b/src/utils/handleTextForContext.ts @@ -0,0 +1,9 @@ +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 a8f6255..cd88b57 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,2 +1,5 @@ export * from './preserveNewlineIndentation' export * from './removeAllLastNewLines' +export * from './createTextBlockHandler' +export * from './handleTextForContext' +export * from './createTextBlockHandlerChain' diff --git a/src/utils/preserveNewlineIndentation.ts b/src/utils/preserveNewlineIndentation.ts index c0bf6ba..7e5dd52 100644 --- a/src/utils/preserveNewlineIndentation.ts +++ b/src/utils/preserveNewlineIndentation.ts @@ -1,6 +1,7 @@ -// Handler of double new line characters (\n\n) to preserve proper indentation in text blocks -export const preserveNewlineIndentation = (textBlock: string): string => { - const stringWithNewLines = /\n+\s*/g; +import { createTextBlockHandler } from './createTextBlockHandler' - return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock; -} +// Handler of double new line characters (\n\n) to preserve proper indentation in text blocks +export const preserveNewlineIndentation = createTextBlockHandler( + /\n+\s*/g, + '\n' +) diff --git a/src/utils/removeAllLastNewLines.ts b/src/utils/removeAllLastNewLines.ts index 0e09e7f..37535a7 100644 --- a/src/utils/removeAllLastNewLines.ts +++ b/src/utils/removeAllLastNewLines.ts @@ -1,6 +1,7 @@ -// 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 = (textBlock: string): string => { - const stringAllLastNewLines = /\n+$/; +import { createTextBlockHandler } from "./createTextBlockHandler"; - return stringAllLastNewLines.test(textBlock) ? textBlock.replace(stringAllLastNewLines, "") : textBlock; -} +// 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/test/representativeText.spec.ts b/test/handleTextForContext.spec.ts similarity index 71% rename from test/representativeText.spec.ts rename to test/handleTextForContext.spec.ts index 9b0249e..04081bd 100644 --- a/test/representativeText.spec.ts +++ b/test/handleTextForContext.spec.ts @@ -1,10 +1,10 @@ import { describe, expect, test } from 'vitest' -import { preserveNewlineIndentation, removeAllLastNewlines } from 'src/utils' +import { handleTextForContext } from 'src/utils/handleTextForContext' -describe('representativeText', () => { +describe('handleTextForContext', () => { test('Should remove double newline characters at the end of text', () => { const text = `reboot?\n\n` - const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) + const actual = handleTextForContext(text) const expected = `reboot?` expect(actual).toEqual(expected) @@ -12,7 +12,7 @@ describe('representativeText', () => { test('Should remove triple newline characters at the end of text', () => { const text = `project.\n\n\n` - const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) + const actual = handleTextForContext(text) const expected = "project." expect(actual).toEqual(expected) @@ -20,7 +20,7 @@ describe('representativeText', () => { test('Should remove a newline character and double tab characters at the end of text', () => { const text = `simple answers.\n\t\t` - const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) + const actual = handleTextForContext(text) const expected = "simple answers." expect(actual).toEqual(expected) @@ -28,7 +28,7 @@ describe('representativeText', () => { test('Should remove a newline character and triple tab characters at the end of text', () => { const text = `success.\n\t\t\t` - const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) + const actual = handleTextForContext(text) const expected = "success." expect(actual).toEqual(expected) @@ -36,7 +36,7 @@ describe('representativeText', () => { 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 = removeAllLastNewlines(preserveNewlineIndentation(text)) + const actual = handleTextForContext(text) const expected = "instead." expect(actual).toEqual(expected) @@ -44,7 +44,7 @@ describe('representativeText', () => { 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 = removeAllLastNewlines(preserveNewlineIndentation(text)) + const actual = handleTextForContext(text) const expected = "a book" expect(actual).toEqual(expected)