util.js 2.6 KB

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