classNames.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. (function() {
  8. 'use strict';
  9. var hasOwn = {}.hasOwnProperty;
  10. function classNames() {
  11. var classes = [];
  12. for (var i = 0; i < arguments.length; i++) {
  13. var arg = arguments[i];
  14. if (!arg) continue;
  15. var argType = typeof arg;
  16. if (argType === 'string' || argType === 'number') {
  17. classes.push(arg);
  18. } else if (Array.isArray(arg) && arg.length) {
  19. var inner = classNames.apply(null, arg);
  20. if (inner) {
  21. classes.push(inner);
  22. }
  23. } else if (argType === 'object') {
  24. for (var key in arg) {
  25. if (hasOwn.call(arg, key) && arg[key]) {
  26. classes.push(key);
  27. }
  28. }
  29. }
  30. }
  31. return classes.join(' ');
  32. }
  33. if (typeof module !== 'undefined' && module.exports) {
  34. classNames.default = classNames;
  35. module.exports = classNames;
  36. } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
  37. // register as 'classnames', consistent with npm package name
  38. define('classnames', [], function() {
  39. return classNames;
  40. });
  41. } else {
  42. window.classNames = classNames;
  43. }
  44. }());