Skip to content

Commit 7fefe8e

Browse files
committed
feat: new validates method(className), used to handle class names
1 parent a345550 commit 7fefe8e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/utils/validates.ts

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CSSRuleObject } from "tailwindcss/types/config"
2+
import { Strings } from "./strings"
23

34
export class Validates {
45

@@ -29,4 +30,33 @@ export class Validates {
2930
}
3031
return newTokens as Record<string, string>
3132
}
33+
34+
public static className(names: Array<string>, options?: Partial<{ toKebabCase: boolean, singleConnectionSymbol: boolean, preProcessingCallback: (prefix: string) => string, postProcessingCallback: (infix: string) => string }>) {
35+
const processing = {
36+
pre: options?.preProcessingCallback ?? ((e) => e),
37+
post: options?.postProcessingCallback ?? ((e) => e),
38+
}
39+
40+
let className = ''
41+
42+
if (options?.toKebabCase ?? true) {
43+
className = names.map(name => processing.post(Strings.toKebabCase(processing.pre(name)))).reduce((pre: string, cur: string, index) => `${pre}-${cur}`)
44+
} else {
45+
className = names.map(name => processing.post(processing.pre(name))).reduce((pre: string, cur: string, index) => `${pre}-${cur}`)
46+
}
47+
48+
let res = ''
49+
if (options?.singleConnectionSymbol ?? true) {
50+
let lastChar: string | null = null
51+
for (const char of className) {
52+
if (char === '-' && lastChar === '-') {
53+
continue
54+
}
55+
res += char
56+
lastChar = char
57+
}
58+
}
59+
60+
return res
61+
}
3262
}

0 commit comments

Comments
 (0)