From 4b551420fc058c4683219db5d75893f9fc69aa04 Mon Sep 17 00:00:00 2001 From: Albert Kaaman Date: Thu, 4 Feb 2021 14:41:46 +0100 Subject: [PATCH] fix: remove superfluous spaces when normalizing class (#3083) Co-authored-by: Jacek Karczmarczyk --- packages/shared/__tests__/normalizeProp.spec.ts | 17 +++++++++++++++++ packages/shared/src/normalizeProp.ts | 5 ++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 packages/shared/__tests__/normalizeProp.spec.ts diff --git a/packages/shared/__tests__/normalizeProp.spec.ts b/packages/shared/__tests__/normalizeProp.spec.ts new file mode 100644 index 00000000..f55f7f7b --- /dev/null +++ b/packages/shared/__tests__/normalizeProp.spec.ts @@ -0,0 +1,17 @@ +import { normalizeClass } from '../src' + +describe('normalizeClass', () => { + test('handles string correctly', () => { + expect(normalizeClass('foo')).toEqual('foo') + }) + + test('handles array correctly', () => { + expect(normalizeClass(['foo', undefined, true, false, 'bar'])).toEqual('foo bar') + }) + + test('handles object correctly', () => { + expect(normalizeClass({ foo: true, bar: false, baz: true })).toEqual( + 'foo baz' + ) + }) +}) diff --git a/packages/shared/src/normalizeProp.ts b/packages/shared/src/normalizeProp.ts index 5e0f8405..1ff825bd 100644 --- a/packages/shared/src/normalizeProp.ts +++ b/packages/shared/src/normalizeProp.ts @@ -62,7 +62,10 @@ export function normalizeClass(value: unknown): string { res = value } else if (isArray(value)) { for (let i = 0; i < value.length; i++) { - res += normalizeClass(value[i]) + ' ' + const normalized = normalizeClass(value[i]) + if (normalized) { + res += normalized + ' ' + } } } else if (isObject(value)) { for (const name in value) {