UNPKG

1.53 kBJavaScriptView Raw
1/*!
2 Copyright (c) 2018 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) {
19 classes = appendClass(classes, parseValue(arg));
20 }
21 }
22
23 return classes;
24 }
25
26 function parseValue (arg) {
27 if (typeof arg === 'string' || typeof arg === 'number') {
28 return arg;
29 }
30
31 if (typeof arg !== 'object') {
32 return '';
33 }
34
35 if (Array.isArray(arg)) {
36 return classNames.apply(null, arg);
37 }
38
39 if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) {
40 return arg.toString();
41 }
42
43 var classes = '';
44
45 for (var key in arg) {
46 if (hasOwn.call(arg, key) && arg[key]) {
47 classes = appendClass(classes, key);
48 }
49 }
50
51 return classes;
52 }
53
54 function appendClass (value, newClass) {
55 if (!newClass) {
56 return value;
57 }
58
59 if (value) {
60 return value + ' ' + newClass;
61 }
62
63 return value + newClass;
64 }
65
66 if (typeof module !== 'undefined' && module.exports) {
67 classNames.default = classNames;
68 module.exports = classNames;
69 } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
70 // register as 'classnames', consistent with npm package name
71 define('classnames', [], function () {
72 return classNames;
73 });
74 } else {
75 window.classNames = classNames;
76 }
77}());