@regle/core 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -266,6 +266,35 @@ function isEmpty(value) {
266
266
  // src/utils/composables.ts
267
267
  import { effectScope, getCurrentScope, onScopeDispose as onScopeDispose2 } from "vue";
268
268
 
269
+ // src/utils/debounce.ts
270
+ function debounce(func, wait, immediate) {
271
+ let timeout;
272
+ const debouncedFn = (...args) => new Promise((resolve) => {
273
+ clearTimeout(timeout);
274
+ timeout = setTimeout(() => {
275
+ timeout = void 0;
276
+ if (!immediate) {
277
+ void Promise.resolve(func.apply(this, [...args])).then(resolve);
278
+ }
279
+ }, wait);
280
+ if (immediate && !timeout) {
281
+ void Promise.resolve(func.apply(this, [...args])).then(resolve);
282
+ }
283
+ });
284
+ debouncedFn.cancel = () => {
285
+ clearTimeout(timeout);
286
+ timeout = void 0;
287
+ };
288
+ debouncedFn.doImmediately = (...args) => new Promise((resolve) => {
289
+ clearTimeout(timeout);
290
+ timeout = setTimeout(() => {
291
+ timeout = void 0;
292
+ void Promise.resolve(func.apply(this, [...args])).then(resolve);
293
+ }, 0);
294
+ });
295
+ return debouncedFn;
296
+ }
297
+
269
298
  // src/core/useRegle/guards/ruleDef.guards.ts
270
299
  function isNestedRulesDef(state, rule) {
271
300
  return isObject(state.value) && isObject(rule.value) && !Object.entries(rule.value).some((rule2) => isRuleDef(rule2));
@@ -571,8 +600,11 @@ function createReactiveFieldStatus({
571
600
  function createReactiveRulesResult() {
572
601
  const declaredRules = rulesDef.value;
573
602
  const storeResult = storage.checkRuleDeclEntry(path, declaredRules);
603
+ $localOptions.value = Object.fromEntries(
604
+ Object.entries(declaredRules).filter(([ruleKey]) => ruleKey.startsWith("$"))
605
+ );
574
606
  $rules.value = Object.fromEntries(
575
- Object.entries(declaredRules).map(([ruleKey, rule]) => {
607
+ Object.entries(declaredRules).filter(([ruleKey]) => !ruleKey.startsWith("$")).map(([ruleKey, rule]) => {
576
608
  if (rule) {
577
609
  const ruleRef = toRef2(() => rule);
578
610
  return [
@@ -605,15 +637,17 @@ function createReactiveFieldStatus({
605
637
  storage.setDirtyEntry(path, $dirty.value);
606
638
  });
607
639
  const $unwatchState = watch2(state, () => {
608
- if (unref2(options.autoDirty)) {
640
+ if (scopeState.$autoDirty.value) {
609
641
  if (!$dirty.value) {
610
642
  $dirty.value = true;
611
643
  }
612
644
  }
613
- if (!unref2(options.lazy)) {
645
+ if (!scopeState.$lazy.value) {
614
646
  $commit();
647
+ if (!scopeState.$rewardEarly.value !== false) {
648
+ $clearExternalErrors();
649
+ }
615
650
  }
616
- $externalErrors.value = [];
617
651
  });
618
652
  function $unwatch() {
619
653
  if ($rules.value) {
@@ -633,11 +667,32 @@ function createReactiveFieldStatus({
633
667
  }
634
668
  function $watch() {
635
669
  scopeState = scope.run(() => {
670
+ const $debounce = computed3(() => {
671
+ return $localOptions.value.$debounce;
672
+ });
673
+ const $lazy = computed3(() => {
674
+ if ($localOptions.value.$lazy) {
675
+ return $localOptions.value.$lazy;
676
+ }
677
+ return unref2(options.lazy);
678
+ });
679
+ const $rewardEarly = computed3(() => {
680
+ if ($localOptions.value.$rewardEarly) {
681
+ return $localOptions.value.$rewardEarly;
682
+ }
683
+ return unref2(options.rewardEarly);
684
+ });
685
+ const $autoDirty = computed3(() => {
686
+ if ($localOptions.value.$autoDirty) {
687
+ return $localOptions.value.$autoDirty;
688
+ }
689
+ return unref2(options.autoDirty);
690
+ });
636
691
  const $error = computed3(() => {
637
692
  return $invalid.value && !$pending.value && $dirty.value;
638
693
  });
639
694
  const $pending = computed3(() => {
640
- if (triggerPunishment.value || !unref2(options.rewardEarly)) {
695
+ if (triggerPunishment.value || !$rewardEarly.value) {
641
696
  return Object.entries($rules.value).some(([key, ruleResult]) => {
642
697
  return ruleResult.$pending;
643
698
  });
@@ -645,7 +700,9 @@ function createReactiveFieldStatus({
645
700
  return false;
646
701
  });
647
702
  const $invalid = computed3(() => {
648
- if (triggerPunishment.value || !unref2(options.rewardEarly)) {
703
+ if ($externalErrors.value?.length) {
704
+ return true;
705
+ } else if (triggerPunishment.value || !$rewardEarly.value) {
649
706
  return Object.entries($rules.value).some(([key, ruleResult]) => {
650
707
  return !ruleResult.$valid;
651
708
  });
@@ -653,7 +710,9 @@ function createReactiveFieldStatus({
653
710
  return false;
654
711
  });
655
712
  const $valid = computed3(() => {
656
- if (unref2(options.rewardEarly)) {
713
+ if ($externalErrors.value?.length) {
714
+ return false;
715
+ } else if ($rewardEarly.value) {
657
716
  return Object.entries($rules.value).every(([key, ruleResult]) => {
658
717
  return ruleResult.$valid;
659
718
  });
@@ -665,14 +724,19 @@ function createReactiveFieldStatus({
665
724
  $error,
666
725
  $pending,
667
726
  $invalid,
668
- $valid
727
+ $valid,
728
+ $debounce,
729
+ $lazy,
730
+ $rewardEarly,
731
+ $autoDirty
669
732
  };
670
733
  });
671
734
  }
672
735
  const $rules = ref2();
736
+ const $localOptions = ref2();
673
737
  createReactiveRulesResult();
674
738
  const $unwatchValid = watch2(scopeState.$valid, (valid) => {
675
- if (unref2(options.rewardEarly) && valid) {
739
+ if (scopeState.$rewardEarly.value && valid) {
676
740
  triggerPunishment.value = false;
677
741
  }
678
742
  });
@@ -683,15 +747,18 @@ function createReactiveFieldStatus({
683
747
  function $touch() {
684
748
  $dirty.value = true;
685
749
  }
686
- function $commit() {
750
+ const $commit = debounce($commitHandler, scopeState.$debounce.value ?? 0);
751
+ function $commitHandler() {
687
752
  Object.entries($rules.value).map(([key, rule]) => {
688
753
  return rule.$validate();
689
754
  });
690
755
  }
691
- async function $validate() {
756
+ const $validate = debounce($validateHandler, scopeState.$debounce.value ?? 0);
757
+ async function $validateHandler() {
692
758
  try {
759
+ $clearExternalErrors();
693
760
  triggerPunishment.value = true;
694
- const results = await Promise.all(
761
+ const results = await Promise.allSettled(
695
762
  Object.entries($rules.value).map(([key, rule]) => {
696
763
  return rule.$validate();
697
764
  })
package/package.json CHANGED
@@ -1,15 +1,9 @@
1
1
  {
2
2
  "name": "@regle/core",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "Vue form validator",
5
- "scripts": {
6
- "lint": "eslint --ext .ts --ext .vue .",
7
- "typecheck": "tsc --noEmit",
8
- "release": "npm publish",
9
- "build": "tsup",
10
- "build:local": "tsup --clean false",
11
- "dev": "tsup --config=tsup.dev.ts --watch",
12
- "test": "echo 'no tests'"
5
+ "peerDependencies": {
6
+ "vue": "> 3.1.0"
13
7
  },
14
8
  "devDependencies": {
15
9
  "@total-typescript/ts-reset": "0.5.1",
@@ -51,5 +45,14 @@
51
45
  "name": "Victor Garcia",
52
46
  "url": "https://github.com/victorgarciaesgi"
53
47
  },
54
- "license": "MIT"
55
- }
48
+ "license": "MIT",
49
+ "scripts": {
50
+ "lint": "eslint --ext .ts --ext .vue .",
51
+ "typecheck": "tsc --noEmit",
52
+ "release": "pnpm publish --report-summary",
53
+ "build": "tsup",
54
+ "build:local": "tsup --clean false",
55
+ "dev": "tsup --config=tsup.dev.ts --watch",
56
+ "test": "echo 'no tests'"
57
+ }
58
+ }