Skip to content

Commit

Permalink
feat: new validates method(className), used to handle class names
Browse files Browse the repository at this point in the history
  • Loading branch information
bre97-web committed Sep 3, 2024
1 parent a345550 commit 7fefe8e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/utils/validates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CSSRuleObject } from "tailwindcss/types/config"
import { Strings } from "./strings"

export class Validates {

Expand Down Expand Up @@ -29,4 +30,33 @@ export class Validates {
}
return newTokens as Record<string, string>
}

public static className(names: Array<string>, options?: Partial<{ toKebabCase: boolean, singleConnectionSymbol: boolean, preProcessingCallback: (prefix: string) => string, postProcessingCallback: (infix: string) => string }>) {
const processing = {
pre: options?.preProcessingCallback ?? ((e) => e),
post: options?.postProcessingCallback ?? ((e) => e),
}

let className = ''

if (options?.toKebabCase ?? true) {
className = names.map(name => processing.post(Strings.toKebabCase(processing.pre(name)))).reduce((pre: string, cur: string, index) => `${pre}-${cur}`)
} else {
className = names.map(name => processing.post(processing.pre(name))).reduce((pre: string, cur: string, index) => `${pre}-${cur}`)
}

let res = ''
if (options?.singleConnectionSymbol ?? true) {
let lastChar: string | null = null
for (const char of className) {
if (char === '-' && lastChar === '-') {
continue
}
res += char
lastChar = char
}
}

return res
}
}

0 comments on commit 7fefe8e

Please sign in to comment.