array-comparisoncomparisoncomparison-tooldeep-diffdiffjson-diffnodejsobject-comparisonobject-diffobjectdiffobjectdifferencereactstreamingstreaming-datatypescript
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.2 KiB
41 lines
1.2 KiB
7 months ago
|
import { isEqualOptions } from "@models/utils";
|
||
2 years ago
|
|
||
1 year ago
|
/**
|
||
|
* Returns true if two data are equal
|
||
7 months ago
|
* @param {unknown} a - The original data.
|
||
|
* @param {unknown} b - The data to compare.
|
||
10 months ago
|
* @param {isEqualOptions} options - The options to compare the data.
|
||
1 year ago
|
* @returns boolean
|
||
|
*/
|
||
2 years ago
|
export function isEqual(
|
||
7 months ago
|
a: unknown,
|
||
|
b: unknown,
|
||
7 months ago
|
options: isEqualOptions = { ignoreArrayOrder: false },
|
||
2 years ago
|
): boolean {
|
||
2 years ago
|
if (typeof a !== typeof b) return false;
|
||
10 months ago
|
if (Array.isArray(a) && Array.isArray(b)) {
|
||
2 years ago
|
if (a.length !== b.length) {
|
||
|
return false;
|
||
|
}
|
||
2 years ago
|
if (options.ignoreArrayOrder) {
|
||
2 years ago
|
return a.every((v) =>
|
||
7 months ago
|
b.some((nextV) => JSON.stringify(nextV) === JSON.stringify(v)),
|
||
2 years ago
|
);
|
||
|
}
|
||
2 years ago
|
return a.every((v, i) => JSON.stringify(v) === JSON.stringify(b[i]));
|
||
2 years ago
|
}
|
||
|
if (typeof a === "object") {
|
||
|
return JSON.stringify(a) === JSON.stringify(b);
|
||
|
}
|
||
|
return a === b;
|
||
|
}
|
||
|
|
||
1 year ago
|
/**
|
||
|
* Returns true if the provided value is an object
|
||
7 months ago
|
* @param {unknown} value - The data to check.
|
||
|
* @returns value is Record<string, unknown>
|
||
1 year ago
|
*/
|
||
7 months ago
|
export function isObject(value: unknown): value is Record<string, unknown> {
|
||
2 years ago
|
return !!value && typeof value === "object" && !Array.isArray(value);
|
||
2 years ago
|
}
|