UNPKG

2 kBJavaScriptView Raw
1/*!
2 Copyright (c) 2015 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 classNames = (function () {
12 function _parseArray (resultSet, array) {
13 var length = array.length;
14
15 for (var i = 0; i < length; ++i) {
16 _parse(resultSet, array[i]);
17 }
18 }
19
20 var hasOwn = {}.hasOwnProperty;
21
22 function _parseNumber (resultSet, num) {
23 resultSet[num] = true;
24 }
25
26 function _parseObject (resultSet, object) {
27 for (var k in object) {
28 if (hasOwn.call(object, k)) {
29 if (object[k]) {
30 resultSet[k] = true;
31 } else {
32 delete resultSet[k];
33 }
34 }
35 }
36 }
37
38 var SPACE = /\s+/;
39 function _parseString (resultSet, str) {
40 var array = str.split(SPACE);
41 var length = array.length;
42
43 for (var i = 0; i < length; ++i) {
44 resultSet[array[i]] = true;
45 }
46 }
47
48 function _parse (resultSet, arg) {
49 if (!arg) return;
50 var argType = typeof arg;
51
52 // 'foo bar'
53 if (argType === 'string') {
54 _parseString(resultSet, arg);
55
56 // ['foo', 'bar', ...]
57 } else if (Array.isArray(arg)) {
58 _parseArray(resultSet, arg);
59
60 // { 'foo': true, ... }
61 } else if (argType === 'object') {
62 _parseObject(resultSet, arg);
63
64 // '130'
65 } else if (argType === 'number') {
66 _parseNumber(resultSet, arg);
67 }
68 }
69
70 function _classNames () {
71 var classSet = {};
72 _parseArray(classSet, arguments);
73
74 var list = [];
75
76 for (var k in classSet) {
77 if (hasOwn.call(classSet, k) && classSet[k]) {
78 list.push(k)
79 }
80 }
81
82 return list.join(' ');
83 }
84
85 return _classNames;
86 })();
87
88 if (typeof module !== 'undefined' && module.exports) {
89 module.exports = classNames;
90 } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
91 // register as 'classnames', consistent with npm package name
92 define('classnames', [], function () {
93 return classNames;
94 });
95 } else {
96 window.classNames = classNames;
97 }
98}());