util.js 3.4 KB

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