Skip to content

Commit

Permalink
feat: add equal
Browse files Browse the repository at this point in the history
  • Loading branch information
fupengl committed Oct 23, 2021
1 parent c29f56a commit 61ca0b3
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 219 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"devDependencies": {
"@commitlint/cli": "^9.1.2",
"@commitlint/config-conventional": "^11.0.0",
"@planjs/fabric": "^0.0.81",
"@planjs/fabric": "^0.0.85",
"@types/jest": "^26.0.23",
"babel-jest": "^27.0.5",
"commitizen": "^4.2.4",
Expand All @@ -99,7 +99,7 @@
"husky": "^4.2.5",
"jest": "^26.4.2",
"lint-staged": "^10.2.13",
"stan-builder": "^0.11.0",
"stan-builder": "^0.11.3",
"standard-version": "^9.3.1",
"ts-jest": "^26.3.0",
"typedoc": "^0.20.36",
Expand Down
66 changes: 66 additions & 0 deletions src/function/equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 检查两个对象是否相等
* @param a
* @param b
*/
function equal(a: any, b: any): boolean {
if (a === b) return true;

if (a && b && typeof a == 'object' && typeof b == 'object') {
if (a.constructor !== b.constructor) return false;

let length, i, keys;
if (Array.isArray(a)) {
length = a.length;
if (length !== b.length) return false;
for (i = length; i-- !== 0; ) if (!equal(a[i], b[i])) return false;
return true;
}

if (a instanceof Map && b instanceof Map) {
if (a.size !== b.size) return false;
for (i of a.entries()) if (!b.has(i[0])) return false;
for (i of a.entries()) if (!equal(i[1], b.get(i[0]))) return false;
return true;
}

if (a instanceof Set && b instanceof Set) {
if (a.size !== b.size) return false;
for (i of a.entries()) if (!b.has(i[0])) return false;
return true;
}

if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
// @ts-ignore
length = a.length;
// @ts-ignore
if (length != b.length) return false;
for (i = length; i-- !== 0; ) if (a[i] !== b[i]) return false;
return true;
}

if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();

// eslint-disable-next-line prefer-const
keys = Object.keys(a);
length = keys.length;
if (length !== Object.keys(b).length) return false;

for (i = length; i-- !== 0; )
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;

for (i = length; i-- !== 0; ) {
const key = keys[i];

if (!equal(a[key], b[key])) return false;
}

return true;
}

return a !== a && b !== b;
}

export default equal;
1 change: 1 addition & 0 deletions src/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { default as nextTick } from './next-tick';
export { default as toggle } from './toogle';
export { default as singleton, SINGLETON_KEY } from './singleton';
export { default as shallowEqual } from './shallow-equal';
export { default as equal } from './equal';
export { default as incrementId } from './increment-id';
export { default as after } from './after';
export { default as before } from './before';
Expand Down
9 changes: 4 additions & 5 deletions src/object/to-object-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ function toObjectArray<T extends Record<string, any> = { label: any; value: stri
const fn: ToObjectArrayOptions<T>['callbackfn'] = (k, v) => {
if (opts?.callbackfn) {
return opts.callbackfn(k, v);
} else {
return {
[keyPropName]: k,
[valuePropName]: dict[k],
} as T;
}
return {
[keyPropName]: k,
[valuePropName]: dict[k],
} as T;
};
for (const [k, v] of isMap(dict) ? dict : Object.entries(dict)) {
const item = fn(k, v);
Expand Down
Loading

0 comments on commit 61ca0b3

Please sign in to comment.