Skip to content

Commit

Permalink
refactor: use two separate functions as a cleaner solution. Add tests (
Browse files Browse the repository at this point in the history
…#30)

* Add removeTrailingSpaces helper
* Add tests for removeTrailingSpaces
* Add tests for preserveNewlineIndentation
  • Loading branch information
absorpheus committed Jul 15, 2024
1 parent fbdc9be commit 1ac8d3a
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 133 deletions.
5 changes: 2 additions & 3 deletions src/methods/aggregateDetails.ts
Original file line number Diff line number Diff line change
@@ -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<ICombinedBooksAndHighlights[]> => {
const books = await getBooks();
Expand Down Expand Up @@ -32,7 +31,7 @@ export const aggregateBookAndHighlightDetails = async (): Promise<ICombinedBooks

return {
chapter: annotation.ZFUTUREPROOFING5,
contextualText: textForContext ? handleTextForContext(textForContext) : textForContext,
contextualText: textForContext ? removeTrailingSpaces(preserveNewlineIndentation(textForContext)) : textForContext,
highlight: preserveNewlineIndentation(annotation.ZANNOTATIONSELECTEDTEXT),
note: userNote ? preserveNewlineIndentation(userNote) : userNote,
highlightStyle: annotation.ZANNOTATIONSTYLE,
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,3 @@ export interface ICombinedBooksAndHighlights {
bookCoverUrl: string;
annotations: IHighlight[];
}

export type TextBlockHandler = (textBlock: string) => string
5 changes: 0 additions & 5 deletions src/utils/createTextBlockHandler.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/utils/createTextBlockHandlerChain.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/utils/handleTextForContext.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
export * from './preserveNewlineIndentation'
export * from './removeAllLastNewLines'
export * from './createTextBlockHandler'
export * from './handleTextForContext'
export * from './createTextBlockHandlerChain'
export * from './removeTrailingSpaces'
11 changes: 5 additions & 6 deletions src/utils/preserveNewlineIndentation.ts
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 0 additions & 7 deletions src/utils/removeAllLastNewLines.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/utils/removeTrailingSpaces.ts
Original file line number Diff line number Diff line change
@@ -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;
}
52 changes: 0 additions & 52 deletions test/handleTextForContext.spec.ts

This file was deleted.

20 changes: 20 additions & 0 deletions test/preserveNewlineIndentation.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
36 changes: 0 additions & 36 deletions test/removeAllLastNewlines.spec.ts

This file was deleted.

156 changes: 156 additions & 0 deletions test/removeTrailingSpaces.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 1ac8d3a

Please sign in to comment.