bind.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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) {
  15. classes = appendClass(classes, parseValue.call(this, arg));
  16. }
  17. }
  18. return classes;
  19. }
  20. function parseValue (arg) {
  21. if (typeof arg === 'string' || typeof arg === 'number') {
  22. return this && this[arg] || arg;
  23. }
  24. if (typeof arg !== 'object') {
  25. return '';
  26. }
  27. if (Array.isArray(arg)) {
  28. return classNames.apply(this, arg);
  29. }
  30. if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) {
  31. return arg.toString();
  32. }
  33. var classes = '';
  34. for (var key in arg) {
  35. if (hasOwn.call(arg, key) && arg[key]) {
  36. classes = appendClass(classes, this && this[key] || key);
  37. }
  38. }
  39. return classes;
  40. }
  41. function appendClass (value, newClass) {
  42. if (!newClass) {
  43. return value;
  44. }
  45. if (value) {
  46. return value + ' ' + newClass;
  47. }
  48. return value + newClass;
  49. }
  50. if (typeof module !== 'undefined' && module.exports) {
  51. classNames.default = classNames;
  52. module.exports = classNames;
  53. } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
  54. // register as 'classnames', consistent with npm package name
  55. define('classnames', [], function () {
  56. return classNames;
  57. });
  58. } else {
  59. window.classNames = classNames;
  60. }
  61. }());