Created
January 2, 2019 16:18
-
-
Save ratbeard/be09182f5590898a994378eb934e871e to your computer and use it in GitHub Desktop.
Typescript version of the classNames util w/ es6 export, so typescript is able to import it without confusion.
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
// Based on: https://github.com/JedWatson/classnames/blob/master/index.js | |
/*! | |
Copyright (c) 2018 Jed Watson. | |
Licensed under the MIT License (MIT), see | |
http://jedwatson.github.io/classnames | |
*/ | |
var hasOwn = {}.hasOwnProperty; | |
export function classNames(...args: any[]) { | |
var classes = []; | |
for (var i = 0; i < args.length; i++) { | |
var arg = args[i]; | |
if (!arg) continue; | |
var argType = typeof arg; | |
if (argType === 'string' || argType === 'number') { | |
classes.push(arg); | |
} else if (Array.isArray(arg) && arg.length) { | |
var inner = classNames.apply(null, arg); | |
if (inner) { | |
classes.push(inner); | |
} | |
} else if (argType === 'object') { | |
for (var key in arg) { | |
if (hasOwn.call(arg, key) && arg[key]) { | |
classes.push(key); | |
} | |
} | |
} | |
} | |
return classes.join(' '); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment