From 7fefe8ed06c979d7336c3153368a3666a471259a Mon Sep 17 00:00:00 2001 From: bre97-web Date: Tue, 3 Sep 2024 19:28:19 +0800 Subject: [PATCH] feat: new validates method(className), used to handle class names --- src/utils/validates.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/utils/validates.ts b/src/utils/validates.ts index 191ea6c..c8acd17 100644 --- a/src/utils/validates.ts +++ b/src/utils/validates.ts @@ -1,4 +1,5 @@ import type { CSSRuleObject } from "tailwindcss/types/config" +import { Strings } from "./strings" export class Validates { @@ -29,4 +30,33 @@ export class Validates { } return newTokens as Record } + + public static className(names: Array, 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 + } }