]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix: remove superfluous spaces when normalizing class (#3083)
authorAlbert Kaaman <albert@kaaman.nu>
Thu, 4 Feb 2021 13:41:46 +0000 (14:41 +0100)
committerGitHub <noreply@github.com>
Thu, 4 Feb 2021 13:41:46 +0000 (14:41 +0100)
Co-authored-by: Jacek Karczmarczyk <jkarczm@gmail.com>
packages/shared/__tests__/normalizeProp.spec.ts [new file with mode: 0644]
packages/shared/src/normalizeProp.ts

diff --git a/packages/shared/__tests__/normalizeProp.spec.ts b/packages/shared/__tests__/normalizeProp.spec.ts
new file mode 100644 (file)
index 0000000..f55f7f7
--- /dev/null
@@ -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'
+    )
+  })
+})
index 5e0f84055aaa5eebd36e9962b46af28de8dfc089..1ff825bd8f5ce8c292cb6450306c467f122d9690 100644 (file)
@@ -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) {