Skip to content

Instantly share code, notes, and snippets.

@ratbeard
Created January 2, 2019 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ratbeard/be09182f5590898a994378eb934e871e to your computer and use it in GitHub Desktop.
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.
// 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