index.js 1.4 KB

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