Skip to content

Instantly share code, notes, and snippets.

@afreeorange
Created September 21, 2024 18:50
Show Gist options
  • Select an option

  • Save afreeorange/64f0c0642e298622c36c4e1565483571 to your computer and use it in GitHub Desktop.

Select an option

Save afreeorange/64f0c0642e298622c36c4e1565483571 to your computer and use it in GitHub Desktop.
clsx
/**
* Copy of:
* https://github.com/lukeed/clsx/blob/master/src/index.js
*/
export type ClassValue =
| ClassArray
| ClassDictionary
| string
| number
| bigint
| null
| boolean
| undefined;
export type ClassDictionary = Record<string, any>;
export type ClassArray = ClassValue[];
function toVal(mix: any) {
var k,
y,
str = "";
if (typeof mix === "string" || typeof mix === "number") {
str += mix;
} else if (typeof mix === "object") {
if (Array.isArray(mix)) {
var len = mix.length;
for (k = 0; k < len; k++) {
if (mix[k]) {
if ((y = toVal(mix[k]))) {
str && (str += " ");
str += y;
}
}
}
} else {
for (y in mix) {
if (mix[y]) {
str && (str += " ");
str += y;
}
}
}
}
return str;
}
export function clsx(...inputs: ClassValue[]): string {
var i = 0,
tmp,
x,
str = "",
len = inputs.length;
for (; i < len; i++) {
if ((tmp = inputs[i])) {
if ((x = toVal(tmp))) {
str && (str += " ");
str += x;
}
}
}
return str;
}
export default clsx;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment