utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. cutString: () => cutString,
  21. endComposing: () => endComposing,
  22. getRuleMessage: () => getRuleMessage,
  23. getStringLength: () => getStringLength,
  24. isEmptyValue: () => isEmptyValue,
  25. mapInputType: () => mapInputType,
  26. resizeTextarea: () => resizeTextarea,
  27. runRuleValidator: () => runRuleValidator,
  28. runSyncRule: () => runSyncRule,
  29. startComposing: () => startComposing
  30. });
  31. module.exports = __toCommonJS(stdin_exports);
  32. var import_utils = require("../utils");
  33. function isEmptyValue(value) {
  34. if (Array.isArray(value)) {
  35. return !value.length;
  36. }
  37. if (value === 0) {
  38. return false;
  39. }
  40. return !value;
  41. }
  42. function runSyncRule(value, rule) {
  43. if (isEmptyValue(value)) {
  44. if (rule.required) {
  45. return false;
  46. }
  47. if (rule.validateEmpty === false) {
  48. return true;
  49. }
  50. }
  51. if (rule.pattern && !rule.pattern.test(String(value))) {
  52. return false;
  53. }
  54. return true;
  55. }
  56. function runRuleValidator(value, rule) {
  57. return new Promise((resolve) => {
  58. const returnVal = rule.validator(value, rule);
  59. if ((0, import_utils.isPromise)(returnVal)) {
  60. returnVal.then(resolve);
  61. return;
  62. }
  63. resolve(returnVal);
  64. });
  65. }
  66. function getRuleMessage(value, rule) {
  67. const { message } = rule;
  68. if ((0, import_utils.isFunction)(message)) {
  69. return message(value, rule);
  70. }
  71. return message || "";
  72. }
  73. function startComposing({ target }) {
  74. target.composing = true;
  75. }
  76. function endComposing({ target }) {
  77. if (target.composing) {
  78. target.composing = false;
  79. target.dispatchEvent(new Event("input"));
  80. }
  81. }
  82. function resizeTextarea(input, autosize) {
  83. const scrollTop = (0, import_utils.getRootScrollTop)();
  84. input.style.height = "auto";
  85. let height = input.scrollHeight;
  86. if ((0, import_utils.isObject)(autosize)) {
  87. const { maxHeight, minHeight } = autosize;
  88. if (maxHeight !== void 0) {
  89. height = Math.min(height, maxHeight);
  90. }
  91. if (minHeight !== void 0) {
  92. height = Math.max(height, minHeight);
  93. }
  94. }
  95. if (height) {
  96. input.style.height = `${height}px`;
  97. (0, import_utils.setRootScrollTop)(scrollTop);
  98. }
  99. }
  100. function mapInputType(type, inputmode) {
  101. if (type === "number") {
  102. type = "text";
  103. inputmode != null ? inputmode : inputmode = "decimal";
  104. }
  105. if (type === "digit") {
  106. type = "tel";
  107. inputmode != null ? inputmode : inputmode = "numeric";
  108. }
  109. return { type, inputmode };
  110. }
  111. function getStringLength(str) {
  112. return [...str].length;
  113. }
  114. function cutString(str, maxlength) {
  115. return [...str].slice(0, maxlength).join("");
  116. }