Skip to content

Commit

Permalink
fix: add tests to cover edge cases (#30)
Browse files Browse the repository at this point in the history
* test: add tests to demonstrate that using a combination of preserveNewLineIndentation and removeAllLastNewlines helper functions can handle the edge cases specified in the issue
  • Loading branch information
absorpheus committed Jul 11, 2024
1 parent de13e95 commit 9386bae
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/methods/aggregateDetails.ts
Original file line number Diff line number Diff line change
@@ -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<ICombinedBooksAndHighlights[]> => {
const books = await getBooks();
Expand Down Expand Up @@ -47,10 +47,3 @@ export const aggregateBookAndHighlightDetails = async (): Promise<ICombinedBooks

return resultingHighlights;
};

// Handler of double new line characters (\n\n) to preserve proper indentation in text blocks
const preserveNewlineIndentation = (textBlock: string): string => {
const stringWithNewLines = /\n+\s*/g;

return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock;
}
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './preserveNewlineIndentation'
export * from './removeAllLastNewLines'
6 changes: 6 additions & 0 deletions src/utils/preserveNewlineIndentation.ts
Original file line number Diff line number Diff line change
@@ -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;
}
52 changes: 52 additions & 0 deletions test/representativeText.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 9386bae

Please sign in to comment.