util.js 2.7 KB

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