From 086762582e2521f44dcd6ccd4cba8d013490f85d Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Fri, 17 Nov 2023 11:06:15 +0100 Subject: [PATCH] fix(utils): handle undefined in html util functions this prevents displaying abstract 'undefined' as string in record-preview (although abstract should usually not be undefined) --- libs/util/shared/src/lib/utils/remove-whitespace.spec.ts | 4 ++++ libs/util/shared/src/lib/utils/remove-whitespace.ts | 2 +- libs/util/shared/src/lib/utils/strip-html.spec.ts | 6 ++++++ libs/util/shared/src/lib/utils/strip-html.ts | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) 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 || '' }