-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsx): normalize class attribute
- Loading branch information
1 parent
d87c180
commit 1681ced
Showing
6 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as assert from 'uvu/assert' | ||
import { normalizeClass } from './utils' | ||
|
||
describe('normalizeClass', () => { | ||
it('handles undefined correctly', () => { | ||
assert.is(normalizeClass(undefined), '') | ||
}) | ||
|
||
it('handles string correctly', () => { | ||
assert.is(normalizeClass('foo'), 'foo') | ||
}) | ||
|
||
it('handles array correctly', () => { | ||
assert.is(normalizeClass(['foo', undefined, true, false, 'bar']), 'foo bar') | ||
}) | ||
|
||
it('handles string containing spaces correctly', () => { | ||
assert.is(normalizeClass('foo1 '), 'foo1') | ||
assert.is(normalizeClass(['foo ', ' baz ']), 'foo baz') | ||
}) | ||
|
||
it('handles empty array correctly', () => { | ||
assert.is(normalizeClass([]), '') | ||
}) | ||
|
||
it('handles nested array correctly', () => { | ||
assert.is(normalizeClass(['foo', ['bar'], [['baz']]]), 'foo bar baz') | ||
}) | ||
|
||
it('handles object correctly', () => { | ||
assert.is(normalizeClass({ foo: true, bar: false, baz: true }), 'foo baz') | ||
}) | ||
|
||
it('handles empty object correctly', () => { | ||
assert.is(normalizeClass({}), '') | ||
}) | ||
|
||
it('handles arrays and objects correctly', () => { | ||
assert.is(normalizeClass(['foo', ['bar'], { baz: true }, [{ qux: true }]]), 'foo bar baz qux') | ||
}) | ||
|
||
it('handles array of objects with falsy values', () => { | ||
assert.is( | ||
normalizeClass([ | ||
{ foo: false }, | ||
{ bar: 0 }, | ||
{ baz: -0 }, | ||
{ qux: '' }, | ||
{ quux: null }, | ||
{ corge: undefined }, | ||
{ grault: NaN }, | ||
]), '') | ||
}) | ||
|
||
it('handles array of objects with truthy values', () => { | ||
assert.is( | ||
normalizeClass([ | ||
{ foo: true }, | ||
{ bar: 'not-empty' }, | ||
{ baz: 1 }, | ||
{ qux: {} }, | ||
{ quux: [] }, | ||
]), 'foo bar baz qux quux') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { isObject } from '@reatom/utils' | ||
|
||
/** | ||
* @see https://github.com/vuejs/core/blob/main/packages/shared/src/normalizeProp.ts | ||
*/ | ||
export const normalizeClass = (value: unknown): string => { | ||
let className = '' | ||
if (typeof value === 'string') { | ||
className = value | ||
} else if (Array.isArray(value)) { | ||
for (let i = 0; i < value.length; i++) { | ||
const normalized = normalizeClass(value[i]) | ||
if (normalized) { | ||
className += normalized + ' ' | ||
} | ||
} | ||
} else if (isObject(value)) { | ||
for (const name in value) { | ||
if (value[name]) { | ||
className += name + ' ' | ||
} | ||
} | ||
} | ||
return className.trim() | ||
} |