Skip to content

Instantly share code, notes, and snippets.

@AntsiferovMaxim
Created March 3, 2020 11:55
Show Gist options
  • Save AntsiferovMaxim/241db72eceedfc1b60cb5fad7dcfc921 to your computer and use it in GitHub Desktop.
Save AntsiferovMaxim/241db72eceedfc1b60cb5fad7dcfc921 to your computer and use it in GitHub Desktop.
type Param = string | number | string[] | {[key: string]: boolean}[] | {[key: string]: boolean};
export function classnames(...args: Param[]) {
if (args.length === 0) {
return '';
} else {
return args
.map(item => rec(item))
.join(' ');
}
}
export const cn = classnames;
function rec(param: Param) {
if (param === null || param === undefined) {
return '';
} else if (typeof param === 'string') {
return param;
} else if (typeof param === "number") {
return param + '';
} else if (Array.isArray(param)) {
return classnames.apply({}, param);
} else if (typeof param === 'object') {
return Object
.entries(param)
.filter(([, condition]) => !!condition)
.map(([name]) => name)
.join(' ');
} else {
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment