bind.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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(this && this[arg] || arg);
  18. } else if (Array.isArray(arg)) {
  19. classes.push(classNames.apply(this, arg));
  20. } else if (argType === 'object') {
  21. if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) {
  22. classes.push(arg.toString());
  23. continue;
  24. }
  25. for (var key in arg) {
  26. if (hasOwn.call(arg, key) && arg[key]) {
  27. classes.push(this && this[key] || key);
  28. }
  29. }
  30. }
  31. }
  32. return classes.join(' ');
  33. }
  34. if (typeof module !== 'undefined' && module.exports) {
  35. classNames.default = classNames;
  36. module.exports = classNames;
  37. } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
  38. // register as 'classnames', consistent with npm package name
  39. define('classnames', [], function () {
  40. return classNames;
  41. });
  42. } else {
  43. window.classNames = classNames;
  44. }
  45. }());