UNPKG

1.2 kBJavaScriptView Raw
1/*!
2 Copyright (c) 2017 Jed Watson.
3 Licensed under the MIT License (MIT), see
4 http://jedwatson.github.io/classnames
5*/
6/* global define */
7
8(function () {
9 'use strict';
10
11 var hasOwn = {}.hasOwnProperty;
12
13 function classNames () {
14 var classes = [];
15
16 for (var i = 0; i < arguments.length; i++) {
17 var arg = arguments[i];
18 if (!arg) continue;
19
20 var argType = typeof arg;
21
22 if (argType === 'string' || argType === 'number') {
23 classes.push(arg);
24 } else if (Array.isArray(arg) && arg.length) {
25 var inner = classNames.apply(null, arg);
26 if (inner) {
27 classes.push(inner);
28 }
29 } else if (argType === 'object') {
30 for (var key in arg) {
31 if (hasOwn.call(arg, key) && arg[key]) {
32 classes.push(key);
33 }
34 }
35 }
36 }
37
38 return classes.join(' ');
39 }
40
41 if (typeof module !== 'undefined' && module.exports) {
42 classNames.default = classNames;
43 module.exports = classNames;
44 } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
45 // register as 'classnames', consistent with npm package name
46 define('classnames', [], function () {
47 return classNames;
48 });
49 } else {
50 window.classNames = classNames;
51 }
52}());