Skip to content

Commit c7c806c

Browse files
authored
fix: extend is equal with file comparison logic #3911 (#3932)
1 parent 82d05db commit c7c806c

6 files changed

Lines changed: 82 additions & 7 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"eslint-plugin-prettier": "^4.0.0",
4141
"eslint-plugin-promise": "^5.1.0",
4242
"eslint-plugin-standard": "^5.0.0",
43-
"fast-deep-equal": "^3.1.3",
4443
"filesize": "^8.0.7",
4544
"flush-promises": "^1.0.2",
4645
"fs-extra": "^10.1.0",

packages/vee-validate/src/useField.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import {
1313
ComponentInternalInstance,
1414
} from 'vue';
1515
import { klona as deepCopy } from 'klona/full';
16-
import isEqual from 'fast-deep-equal/es6';
1716
import { validate as validateValue } from './validate';
1817
import {
19-
ValidationResult,
2018
MaybeRef,
2119
GenericValidateFunction,
2220
YupValidator,
@@ -37,6 +35,7 @@ import {
3735
isYupValidator,
3836
applyModelModifiers,
3937
withLatest,
38+
isEqual,
4039
} from './utils';
4140
import { isCallable } from '../../shared';
4241
import { FieldContextKey, FormContextKey, IS_ABSENT } from './symbols';

packages/vee-validate/src/useFieldState.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { computed, reactive, ref, Ref, unref, watch } from 'vue';
2-
import isEqual from 'fast-deep-equal/es6';
32
import { FormContextKey } from './symbols';
43
import { FieldMeta, FieldState, MaybeRef } from './types';
5-
import { getFromPath, injectWithSelf } from './utils';
4+
import { getFromPath, injectWithSelf, isEqual } from './utils';
65

76
export interface StateSetterInit<TValue = unknown> extends FieldState<TValue> {
87
initialValue: TValue;

packages/vee-validate/src/useForm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
markRaw,
1414
watchEffect,
1515
} from 'vue';
16-
import isEqual from 'fast-deep-equal/es6';
1716
import { klona as deepCopy } from 'klona/full';
1817
import {
1918
FieldMeta,
@@ -47,6 +46,7 @@ import {
4746
debounceAsync,
4847
isEmptyContainer,
4948
withLatest,
49+
isEqual,
5050
} from './utils';
5151
import { FormContextKey } from './symbols';
5252
import { validateYupSchema, validateObjectSchema } from './validate';

packages/vee-validate/src/utils/assertions.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,81 @@ export function isEvent(evt: unknown): evt is Event {
111111
export function isPropPresent(obj: Record<string, unknown>, prop: string) {
112112
return prop in obj && obj[prop] !== IS_ABSENT;
113113
}
114+
115+
/**
116+
* Compares if two values are the same borrowed from:
117+
* https://github.com/epoberezkin/fast-deep-equal
118+
* We added a case for file matching since `Object.keys` doesn't work with Files.
119+
* */
120+
export function isEqual(a: any, b: any) {
121+
if (a === b) return true;
122+
123+
if (a && b && typeof a === 'object' && typeof b === 'object') {
124+
if (a.constructor !== b.constructor) return false;
125+
126+
// eslint-disable-next-line no-var
127+
var length, i, keys;
128+
if (Array.isArray(a)) {
129+
length = a.length;
130+
// eslint-disable-next-line eqeqeq
131+
if (length != b.length) return false;
132+
for (i = length; i-- !== 0; ) if (!isEqual(a[i], b[i])) return false;
133+
return true;
134+
}
135+
136+
if (a instanceof Map && b instanceof Map) {
137+
if (a.size !== b.size) return false;
138+
for (i of a.entries()) if (!b.has(i[0])) return false;
139+
for (i of a.entries()) if (!isEqual(i[1], b.get(i[0]))) return false;
140+
return true;
141+
}
142+
143+
// We added this part for file comparison, arguably a little naive but should work for most cases.
144+
// #3911
145+
if (a instanceof File && b instanceof File) {
146+
if (a.size !== b.size) return false;
147+
if (a.name !== b.name) return false;
148+
if (a.lastModified !== b.lastModified) return false;
149+
if (a.type !== b.type) return false;
150+
151+
return true;
152+
}
153+
154+
if (a instanceof Set && b instanceof Set) {
155+
if (a.size !== b.size) return false;
156+
for (i of a.entries()) if (!b.has(i[0])) return false;
157+
return true;
158+
}
159+
160+
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
161+
length = (a as any).length;
162+
// eslint-disable-next-line eqeqeq
163+
if (length != (b as any).length) return false;
164+
for (i = length; i-- !== 0; ) if ((a as any)[i] !== (b as any)[i]) return false;
165+
return true;
166+
}
167+
168+
if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
169+
if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
170+
if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
171+
172+
keys = Object.keys(a);
173+
length = keys.length;
174+
if (length !== Object.keys(b).length) return false;
175+
176+
for (i = length; i-- !== 0; ) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
177+
178+
for (i = length; i-- !== 0; ) {
179+
// eslint-disable-next-line no-var
180+
var key = keys[i];
181+
182+
if (!isEqual(a[key], b[key])) return false;
183+
}
184+
185+
return true;
186+
}
187+
188+
// true if both NaN, false otherwise
189+
// eslint-disable-next-line no-self-compare
190+
return a !== a && b !== b;
191+
}

scripts/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function createConfig(pkg, format) {
5050
}),
5151
tsPlugin,
5252
resolve({
53-
dedupe: ['fast-deep-equal/es6', 'fast-deep-equal', 'klona', 'klona/full'],
53+
dedupe: ['klona', 'klona/full'],
5454
}),
5555
commonjs(),
5656
],

0 commit comments

Comments
 (0)