diff --git a/libs/util/shared/src/lib/utils/remove-whitespace.spec.ts b/libs/util/shared/src/lib/utils/remove-whitespace.spec.ts index 20b5459045..4f21470524 100644 --- a/libs/util/shared/src/lib/utils/remove-whitespace.spec.ts +++ b/libs/util/shared/src/lib/utils/remove-whitespace.spec.ts @@ -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') diff --git a/libs/util/shared/src/lib/utils/remove-whitespace.ts b/libs/util/shared/src/lib/utils/remove-whitespace.ts index c9dffa3aa5..696d88f457 100644 --- a/libs/util/shared/src/lib/utils/remove-whitespace.ts +++ b/libs/util/shared/src/lib/utils/remove-whitespace.ts @@ -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 } diff --git a/libs/util/shared/src/lib/utils/strip-html.spec.ts b/libs/util/shared/src/lib/utils/strip-html.spec.ts index 1e0a44cf15..c72b4ee071 100644 --- a/libs/util/shared/src/lib/utils/strip-html.spec.ts +++ b/libs/util/shared/src/lib/utils/strip-html.spec.ts @@ -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' diff --git a/libs/util/shared/src/lib/utils/strip-html.ts b/libs/util/shared/src/lib/utils/strip-html.ts index 5e5b8bf527..14170efd71 100644 --- a/libs/util/shared/src/lib/utils/strip-html.ts +++ b/libs/util/shared/src/lib/utils/strip-html.ts @@ -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 || '' }