Created
March 3, 2020 11:55
-
-
Save AntsiferovMaxim/241db72eceedfc1b60cb5fad7dcfc921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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