1
|
|
|
import { EMPTY_ARRAY } from "./consts" |
2
|
|
|
import type { ClassNamed, ClassValue, Falsy } from "./defs" |
3
|
|
|
import { stringifyClassNamed } from "./utils" |
4
|
|
|
|
5
|
|
|
const classNameKey = "className" as const |
6
|
|
|
|
7
|
|
|
const {keys: $keys} = Object |
8
|
|
|
|
9
|
|
|
export { |
10
|
|
|
wrapper, |
11
|
|
|
// replacer of `dehash` and `truthyKeys` |
12
|
|
|
resolver, |
13
|
|
|
joinWithLead |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
function wrapper<T>( |
17
|
|
|
destination: T, |
18
|
|
|
className: undefined | string |
19
|
|
|
) { |
20
|
|
|
//@ts-expect-error |
21
|
|
|
destination[classNameKey] = className |
22
|
|
|
|
23
|
|
|
return stringifyClassNamed(destination as T & ClassNamed) |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
function resolver( |
27
|
|
|
vocabulary: undefined | Record<string, ClassValue>, |
28
|
|
|
actions: Record<string, ClassValue | boolean> |
29
|
|
|
// actions: Record<string, ClassValue> | Record<string, boolean> |
30
|
|
|
) { |
31
|
|
|
const keys = $keys(actions) |
32
|
|
|
|
33
|
|
|
for (let i = keys.length; i--;) { |
34
|
|
|
const key = keys[i] |
35
|
|
|
, act = actions[key] |
36
|
|
|
|
37
|
|
|
//TODO clarify what behaviour to implement |
38
|
|
|
|
39
|
|
|
if (act !== undefined && !act) { |
40
|
|
|
delete keys[i] |
41
|
|
|
continue |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
const hash = vocabulary?.[key] |
45
|
|
|
if (hash !== undefined) |
46
|
|
|
keys[i] = hash |
47
|
|
|
else if (typeof act === "string") |
48
|
|
|
keys[i] = act |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
//TODO Compare `.flat()`, `.filter(Boolean)` or `.filter(idfn)` |
52
|
|
|
const filtered = keys.filter(Boolean) |
53
|
|
|
|
54
|
|
|
return filtered.length === 0 ? EMPTY_ARRAY : filtered |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
//TODO Consider returning `undefined` on empty string |
58
|
|
|
function joinWithLead(value: Falsy|ClassValue, arr: undefined | string | readonly string[]) : string { |
59
|
|
|
const str1 = value || "" |
60
|
|
|
if (!(arr && arr.length)) |
61
|
|
|
return str1 |
62
|
|
|
|
63
|
|
|
const str2 = typeof arr === "string" ? arr : arr.join(" ") |
64
|
|
|
if (!str1) |
65
|
|
|
return str2 |
66
|
|
|
|
67
|
|
|
return `${str1} ${str2}` |
68
|
|
|
} |