From 9386baea9235b748744fa904e9ca9d0c05f58527 Mon Sep 17 00:00:00 2001 From: Abdullah Malek <19255312+absorpheus@users.noreply.github.com> Date: Fri, 12 Jul 2024 00:35:46 +0100 Subject: [PATCH] fix: add tests to cover edge cases (#30) * test: add tests to demonstrate that using a combination of preserveNewLineIndentation and removeAllLastNewlines helper functions can handle the edge cases specified in the issue --- src/methods/aggregateDetails.ts | 9 +---- src/utils/index.ts | 2 + src/utils/preserveNewlineIndentation.ts | 6 +++ test/representativeText.spec.ts | 52 +++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/utils/index.ts create mode 100644 src/utils/preserveNewlineIndentation.ts create mode 100644 test/representativeText.spec.ts diff --git a/src/methods/aggregateDetails.ts b/src/methods/aggregateDetails.ts index 713a277..d9f0d77 100644 --- a/src/methods/aggregateDetails.ts +++ b/src/methods/aggregateDetails.ts @@ -1,7 +1,7 @@ import { IBook, IBookAnnotation, ICombinedBooksAndHighlights } from '../types'; import { getBooks } from './getBooks'; import { getAnnotations } from './getAnnotations'; -import { removeAllLastNewlines } from 'src/utils/removeAllLastNewLines'; +import { removeAllLastNewlines, preserveNewlineIndentation } from 'src/utils' export const aggregateBookAndHighlightDetails = async (): Promise => { const books = await getBooks(); @@ -47,10 +47,3 @@ export const aggregateBookAndHighlightDetails = async (): Promise { - const stringWithNewLines = /\n+\s*/g; - - return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock; -} diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..a8f6255 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,2 @@ +export * from './preserveNewlineIndentation' +export * from './removeAllLastNewLines' diff --git a/src/utils/preserveNewlineIndentation.ts b/src/utils/preserveNewlineIndentation.ts new file mode 100644 index 0000000..c0bf6ba --- /dev/null +++ b/src/utils/preserveNewlineIndentation.ts @@ -0,0 +1,6 @@ +// 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; + + return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock; +} diff --git a/test/representativeText.spec.ts b/test/representativeText.spec.ts new file mode 100644 index 0000000..9b0249e --- /dev/null +++ b/test/representativeText.spec.ts @@ -0,0 +1,52 @@ +import { describe, expect, test } from 'vitest' +import { preserveNewlineIndentation, removeAllLastNewlines } from 'src/utils' + +describe('representativeText', () => { + test('Should remove double newline characters at the end of text', () => { + const text = `reboot?\n\n` + const actual = removeAllLastNewlines(preserveNewlineIndentation(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 = removeAllLastNewlines(preserveNewlineIndentation(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 = removeAllLastNewlines(preserveNewlineIndentation(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 = removeAllLastNewlines(preserveNewlineIndentation(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 = removeAllLastNewlines(preserveNewlineIndentation(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 = removeAllLastNewlines(preserveNewlineIndentation(text)) + const expected = "a book" + + expect(actual).toEqual(expected) + }) +})