Skip to content

Commit

Permalink
fix(utils): handle undefined in html util functions
Browse files Browse the repository at this point in the history
this prevents displaying abstract 'undefined' as string in record-preview (although abstract should usually not be undefined)
  • Loading branch information
tkohr committed Nov 17, 2023
1 parent 0c56fb5 commit 0867625
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions libs/util/shared/src/lib/utils/remove-whitespace.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { removeWhitespace } from './remove-whitespace'

describe('#removeWhitespace', () => {
it('returns undefined input is not defined', () => {
const html = null
expect(removeWhitespace(html)).toBe(undefined)
})
it('removes superfluent whitespace for a single word', () => {
const html = ' hello '
expect(removeWhitespace(html)).toBe('hello')
Expand Down
2 changes: 1 addition & 1 deletion libs/util/shared/src/lib/utils/remove-whitespace.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const removeWhitespace = function (str: string): string {
return str.replace(/\s+/g, ' ').trim()
return str ? str.replace(/\s+/g, ' ').trim() : undefined
}
6 changes: 6 additions & 0 deletions libs/util/shared/src/lib/utils/strip-html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ describe('strip HTML', () => {
expect(stripHtml(html)).toBe('hello')
})
})
describe('when HTML not defined', () => {
it('returns undefined', () => {
const html = null
expect(stripHtml(html)).toBe(undefined)
})
})
describe('when no HTML tags', () => {
it('return same string', () => {
const html = 'hello'
Expand Down
1 change: 1 addition & 0 deletions libs/util/shared/src/lib/utils/strip-html.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const stripHtml = function (html: string): string {
if (!html) return undefined
const doc = new DOMParser().parseFromString(html, 'text/html')
return doc.body.textContent || ''
}

0 comments on commit 0867625

Please sign in to comment.