/
class-utils.ts
164 lines (133 loc) · 4.79 KB
/
class-utils.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { ClassGroupId, Config, ClassGroup, ClassValidator, ThemeObject, ThemeGetter } from './types'
export interface ClassPartObject {
nextPart: Record<string, ClassPartObject>
validators: ClassValidatorObject[]
classGroupId?: ClassGroupId
}
interface ClassValidatorObject {
classGroupId: ClassGroupId
validator: ClassValidator
}
const CLASS_PART_SEPARATOR = '-'
export function createClassUtils(config: Config) {
const classMap = createClassMap(config)
function getClassGroupId(className: string) {
const classParts = className.split(CLASS_PART_SEPARATOR)
// Classes like `-inset-1` produce an empty string as first classPart. We assume that classes for negative values are used correctly and remove it from classParts.
if (classParts[0] === '' && classParts.length !== 1) {
classParts.shift()
}
return getGroupRecursive(classParts, classMap) || getGroupIdForArbitraryProperty(className)
}
function getConflictingClassGroupIds(classGroupId: ClassGroupId) {
return config.conflictingClassGroups[classGroupId] || []
}
return {
getClassGroupId,
getConflictingClassGroupIds,
}
}
function getGroupRecursive(
classParts: string[],
classPartObject: ClassPartObject
): ClassGroupId | undefined {
if (classParts.length === 0) {
return classPartObject.classGroupId
}
const currentClassPart = classParts[0]!
const nextClassPartObject = classPartObject.nextPart[currentClassPart]
const classGroupFromNextClassPart = nextClassPartObject
? getGroupRecursive(classParts.slice(1), nextClassPartObject)
: undefined
if (classGroupFromNextClassPart) {
return classGroupFromNextClassPart
}
if (classPartObject.validators.length === 0) {
return undefined
}
const classRest = classParts.join(CLASS_PART_SEPARATOR)
return classPartObject.validators.find(({ validator }) => validator(classRest))?.classGroupId
}
const arbitraryPropertyRegex = /^\[(.+)\]$/
function getGroupIdForArbitraryProperty(className: string) {
if (arbitraryPropertyRegex.test(className)) {
const arbitraryPropertyClassName = arbitraryPropertyRegex.exec(className)![1]
const property = arbitraryPropertyClassName?.substring(
0,
arbitraryPropertyClassName.indexOf(':')
)
if (property) {
// I use two dots here because one dot is used as prefix for class groups in plugins
return 'arbitrary..' + property
}
}
}
/**
* Exported for testing only
*/
export function createClassMap(config: Config) {
const { theme } = config
const classMap: ClassPartObject = {
nextPart: {},
validators: [],
}
Object.entries(config.classGroups).forEach(([classGroupId, classGroup]) => {
processClassesRecursively(classGroup, classMap, classGroupId, theme)
})
return classMap
}
function processClassesRecursively(
classGroup: ClassGroup,
classPartObject: ClassPartObject,
classGroupId: ClassGroupId,
theme: ThemeObject
) {
classGroup.forEach((classDefinition) => {
if (typeof classDefinition === 'string') {
const classPartObjectToEdit =
classDefinition === '' ? classPartObject : getPart(classPartObject, classDefinition)
classPartObjectToEdit.classGroupId = classGroupId
return
}
if (typeof classDefinition === 'function') {
if (isThemeGetter(classDefinition)) {
processClassesRecursively(
classDefinition(theme),
classPartObject,
classGroupId,
theme
)
return
}
classPartObject.validators.push({
validator: classDefinition,
classGroupId,
})
return
}
Object.entries(classDefinition).forEach(([key, classGroup]) => {
processClassesRecursively(
classGroup,
getPart(classPartObject, key),
classGroupId,
theme
)
})
})
}
function getPart(classPartObject: ClassPartObject, path: string) {
let currentClassPartObject = classPartObject
path.split(CLASS_PART_SEPARATOR).forEach((pathPart) => {
if (currentClassPartObject.nextPart[pathPart] === undefined) {
currentClassPartObject.nextPart[pathPart] = {
nextPart: {},
validators: [],
}
}
currentClassPartObject = currentClassPartObject.nextPart[pathPart]!
})
return currentClassPartObject
}
function isThemeGetter(func: ClassValidator | ThemeGetter): func is ThemeGetter {
return (func as ThemeGetter).isThemeGetter
}