util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.controlDefaultValue = exports.capitalize = exports.camelize = exports.cacheStringFunction = void 0;
  7. exports.getDataAndAriaProps = getDataAndAriaProps;
  8. exports.isSymbol = exports.isString = exports.isOn = exports.isObject = exports.isFunction = exports.isArray = exports.hyphenate = void 0;
  9. exports.renderHelper = renderHelper;
  10. exports.resolvePropValue = resolvePropValue;
  11. exports.toPx = toPx;
  12. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  13. var isFunction = function isFunction(val) {
  14. return typeof val === 'function';
  15. };
  16. exports.isFunction = isFunction;
  17. var controlDefaultValue = Symbol('controlDefaultValue');
  18. exports.controlDefaultValue = controlDefaultValue;
  19. var isArray = Array.isArray;
  20. exports.isArray = isArray;
  21. var isString = function isString(val) {
  22. return typeof val === 'string';
  23. };
  24. exports.isString = isString;
  25. var isSymbol = function isSymbol(val) {
  26. return (0, _typeof2.default)(val) === 'symbol';
  27. };
  28. exports.isSymbol = isSymbol;
  29. var isObject = function isObject(val) {
  30. return val !== null && (0, _typeof2.default)(val) === 'object';
  31. };
  32. exports.isObject = isObject;
  33. var onRE = /^on[^a-z]/;
  34. var isOn = function isOn(key) {
  35. return onRE.test(key);
  36. };
  37. exports.isOn = isOn;
  38. var cacheStringFunction = function cacheStringFunction(fn) {
  39. var cache = Object.create(null);
  40. return function (str) {
  41. var hit = cache[str];
  42. return hit || (cache[str] = fn(str));
  43. };
  44. };
  45. exports.cacheStringFunction = cacheStringFunction;
  46. var camelizeRE = /-(\w)/g;
  47. var camelize = cacheStringFunction(function (str) {
  48. return str.replace(camelizeRE, function (_, c) {
  49. return c ? c.toUpperCase() : '';
  50. });
  51. });
  52. exports.camelize = camelize;
  53. var hyphenateRE = /\B([A-Z])/g;
  54. var hyphenate = cacheStringFunction(function (str) {
  55. return str.replace(hyphenateRE, '-$1').toLowerCase();
  56. });
  57. exports.hyphenate = hyphenate;
  58. var capitalize = cacheStringFunction(function (str) {
  59. return str.charAt(0).toUpperCase() + str.slice(1);
  60. });
  61. exports.capitalize = capitalize;
  62. var hasOwnProperty = Object.prototype.hasOwnProperty;
  63. var hasOwn = function hasOwn(val, key) {
  64. return hasOwnProperty.call(val, key);
  65. };
  66. // change from vue sourcecode
  67. function resolvePropValue(options, props, key, value) {
  68. var opt = options[key];
  69. if (opt != null) {
  70. var hasDefault = hasOwn(opt, 'default');
  71. // default values
  72. if (hasDefault && value === undefined) {
  73. var defaultValue = opt.default;
  74. value = opt.type !== Function && isFunction(defaultValue) ? defaultValue() : defaultValue;
  75. }
  76. // boolean casting
  77. if (opt.type === Boolean) {
  78. if (!hasOwn(props, key) && !hasDefault) {
  79. value = false;
  80. } else if (value === '') {
  81. value = true;
  82. }
  83. }
  84. }
  85. return value;
  86. }
  87. function getDataAndAriaProps(props) {
  88. return Object.keys(props).reduce(function (memo, key) {
  89. if (key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-') {
  90. memo[key] = props[key];
  91. }
  92. return memo;
  93. }, {});
  94. }
  95. function toPx(val) {
  96. if (typeof val === 'number') return "".concat(val, "px");
  97. return val;
  98. }
  99. function renderHelper(v) {
  100. var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  101. var defaultV = arguments.length > 2 ? arguments[2] : undefined;
  102. if (typeof v === 'function') {
  103. return v(props);
  104. }
  105. return v !== null && v !== void 0 ? v : defaultV;
  106. }