-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-names.ts
93 lines (86 loc) · 2.27 KB
/
class-names.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* istanbul ignore file */
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2024 Feedzai
*/
interface ClassDictionary {
[id: string]: any;
}
type ClassArray = Array<ClassValue>;
type ClassValue = ClassArray | ClassDictionary | string | number | null | boolean | undefined;
function parseValues<T>(args: T | any): string {
let result = "";
if (typeof args === "string" || typeof args === "number") {
result = result + args;
}
// proceeds to handle objects and arrays.
else if (typeof args === "object") {
if (Array.isArray(args)) {
for (const element of args) {
if (element) {
const parsedValue = parseValues(element);
if (parsedValue) {
result += result ? " " + parsedValue : parsedValue;
}
}
}
} else {
// Iterate over the object's keys
for (const [key, value] of Object.entries(args)) {
if (value) {
result += result ? " " + key : key;
}
}
}
}
return result;
}
/**
* A tiny (228 bytes) utility for constructing className strings conditionally.
*
* Also serves as a faster & smaller drop-in replacement for the classnames module.
*
* @example
*
* // Strings (variadic)
* classNames('foo', true && 'bar', 'baz');
* // 'foo bar baz'
*
* // Objects
* classNames({ foo:true, bar:false, baz:isTrue() });
* // 'foo baz'
*
* // Objects (variadic)
* classNames({ foo:true }, { bar:false }, null, { '--foobar':'hello' });
* // 'foo --foobar'
*
* // Arrays
* classNames(['foo', 0, false, 'bar']);
* // 'foo bar'
*
* // Arrays (variadic)
* classNames(['foo'], ['', 0, false, 'bar'], [['baz', [['hello'], 'there']]]);
* // 'foo bar baz hello there'
*
* // Kitchen sink (with nesting)
* classNames('foo', [1 && 'bar', { baz:false, bat:null }, ['hello', ['world']]], 'cya');
* // 'foo bar hello world cya'
*
* // Dynamic classnames with computed keys
* const btnType = "primary";
* classNames({ [`btn-${btnType}`]: true });
* // 'btn-primary'
*
* @returns {string}
*/
export function classNames(...classes: ClassValue[]): string {
let str = "";
for (const classValue of classes) {
const val = parseValues(classValue);
if (val) {
str += str ? " " + val : val;
}
}
return str;
}