@goodie-forms/core 1.2.6-alpha → 1.3.0

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/package.json CHANGED
@@ -1,6 +1,16 @@
1
1
  {
2
2
  "name": "@goodie-forms/core",
3
- "version": "1.2.6-alpha",
3
+ "version": "1.3.0",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/iGoodie/goodie-forms"
7
+ },
8
+ "author": {
9
+ "name": "Taha Anılcan Metinyurt",
10
+ "email": "igoodie@programmer.net",
11
+ "url": "https://github.com/iGoodie"
12
+ },
13
+ "license": "CC BY-SA 4.0",
4
14
  "type": "module",
5
15
  "main": "dist/index.js",
6
16
  "types": "dist/index.d.ts",
@@ -95,6 +95,7 @@ export namespace FieldPath {
95
95
  }
96
96
  return true;
97
97
  }
98
+
98
99
  export function isDescendant(parentPath: Segments, childPath: Segments) {
99
100
  if (parentPath.length >= childPath.length) return false;
100
101
 
@@ -73,24 +73,29 @@ export namespace Reconcile {
73
73
  export function arrayDiff<T>(
74
74
  prev: readonly T[],
75
75
  next: readonly T[],
76
- equals: (a: T, b: T) => boolean,
77
- filter?: (a: T) => boolean,
76
+ opts?: {
77
+ equals?: (a: T, b: T) => boolean;
78
+ include?: (a: T) => boolean;
79
+ },
78
80
  ) {
79
81
  const added: T[] = [];
80
82
  const removed: T[] = [];
81
83
  const unchanged: T[] = [];
82
84
 
85
+ const equals = (a: T, b: T) => (opts?.equals ? opts.equals(a, b) : a === b);
86
+ const include = (a: T) => (opts?.include ? opts.include(a) : true);
87
+
83
88
  for (const n of next) {
84
89
  if (prev.some((p) => equals(p, n))) {
85
- if (filter?.(n) ?? true) unchanged.push(n);
90
+ if (include(n)) unchanged.push(n);
86
91
  } else {
87
- if (filter?.(n) ?? true) added.push(n);
92
+ if (include(n)) added.push(n);
88
93
  }
89
94
  }
90
95
 
91
96
  for (const p of prev) {
92
97
  if (!next.some((n) => equals(p, n))) {
93
- if (filter?.(p) ?? true) removed.push(p);
98
+ if (include(p)) removed.push(p);
94
99
  }
95
100
  }
96
101
 
@@ -116,8 +116,7 @@ export class FormController<TOutput extends object> {
116
116
  }
117
117
 
118
118
  get isValid() {
119
- // TODO: Does it still count valid while validating?
120
- return this._issues.length === 0;
119
+ return this.isValidating || this._issues.length === 0;
121
120
  }
122
121
 
123
122
  get isValidating() {
@@ -273,11 +272,9 @@ export class FormController<TOutput extends object> {
273
272
  _result: StandardSchemaV1.Result<TOutput>,
274
273
  path: TPath,
275
274
  ) {
276
- const diff = Reconcile.arrayDiff(
277
- this._issues,
278
- _result.issues ?? [],
279
- Reconcile.deepEqual,
280
- (issue) => {
275
+ const diff = Reconcile.arrayDiff(this._issues, _result.issues ?? [], {
276
+ equals: Reconcile.deepEqual,
277
+ include: (issue) => {
281
278
  if (issue.path == null) return false;
282
279
  const issuePath = FieldPath.normalize(issue.path);
283
280
  return (
@@ -285,7 +282,7 @@ export class FormController<TOutput extends object> {
285
282
  FieldPath.isDescendant(path, issuePath)
286
283
  );
287
284
  },
288
- );
285
+ });
289
286
 
290
287
  removeBy(this._issues, (issue) => diff.removed.includes(issue));
291
288
 
@@ -314,11 +311,9 @@ export class FormController<TOutput extends object> {
314
311
  }
315
312
 
316
313
  // Append non-registered issues too
317
- const diff = Reconcile.arrayDiff(
318
- this._issues,
319
- result.issues ?? [],
320
- Reconcile.deepEqual,
321
- );
314
+ const diff = Reconcile.arrayDiff(this._issues, result.issues ?? [], {
315
+ equals: Reconcile.deepEqual,
316
+ });
322
317
  diff.added.forEach((issue) => this._issues.push(issue));
323
318
 
324
319
  this.setValidating(false);
@@ -182,6 +182,9 @@ export class FormField<TOutput extends object, TValue> {
182
182
  }
183
183
  }
184
184
 
185
+ // TODO: impl
186
+ // private modifyInitialData() {}
187
+
185
188
  setValue(value: TValue, opts?: Parameters<typeof this.modifyData>[1]) {
186
189
  return this.modifyData((data) => {
187
190
  FieldPath.setValue(data as TOutput, this.path, value as never);