Skip to content

Commit

Permalink
Feature: Add text block handlers. Refactor tests. (#30)
Browse files Browse the repository at this point in the history
* add createTextBlockHandler
* add createTextBlockHandlerChain
* add handleTextForContext
* refactor test
  • Loading branch information
absorpheus committed Jul 12, 2024
1 parent 9386bae commit fbdc9be
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/methods/aggregateDetails.ts
Original file line number Diff line number Diff line change
@@ -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<ICombinedBooksAndHighlights[]> => {
const books = await getBooks();
Expand Down Expand Up @@ -31,7 +32,7 @@ export const aggregateBookAndHighlightDetails = async (): Promise<ICombinedBooks

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

export type TextBlockHandler = (textBlock: string) => string

Check warning on line 39 in src/types.ts

View workflow job for this annotation

GitHub Actions / ✅ Linter

'textBlock' is defined but never used
5 changes: 5 additions & 0 deletions src/utils/createTextBlockHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const createTextBlockHandler = (regex: RegExp, replaceValue: string) => {
return (textBlock: string) => {
return regex.test(textBlock) ? textBlock.replace(regex, replaceValue) : textBlock
}
}
9 changes: 9 additions & 0 deletions src/utils/createTextBlockHandlerChain.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
9 changes: 9 additions & 0 deletions src/utils/handleTextForContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { preserveNewlineIndentation} from "./preserveNewlineIndentation";
import { removeAllLastNewlines } from "./removeAllLastNewLines";
import { createTextBlockHandlerChain } from "./createTextBlockHandlerChain"


export const handleTextForContext = createTextBlockHandlerChain(
preserveNewlineIndentation,
removeAllLastNewlines
)
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './preserveNewlineIndentation'
export * from './removeAllLastNewLines'
export * from './createTextBlockHandler'
export * from './handleTextForContext'
export * from './createTextBlockHandlerChain'
11 changes: 6 additions & 5 deletions src/utils/preserveNewlineIndentation.ts
Original file line number Diff line number Diff line change
@@ -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'
)
11 changes: 6 additions & 5 deletions src/utils/removeAllLastNewLines.ts
Original file line number Diff line number Diff line change
@@ -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,
""
)
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
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)
})

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)
})

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)
})

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)
})

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)
})

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)
Expand Down

0 comments on commit fbdc9be

Please sign in to comment.