MCPcopy Create free account
hub / github.com/codemix/graph / compareObjects

Function compareObjects

packages/graph/src/Comparator.ts:118–167  ·  view source on GitHub ↗
(a: object, b: object)

Source from the content-addressed store, hash-verified

116}
117
118export function compareObjects(a: object, b: object): number {
119 if (a === b) return 0;
120 if (Array.isArray(a)) {
121 if (!Array.isArray(b)) {
122 return 1;
123 }
124 return compareArrays(a, b);
125 }
126 if (Array.isArray(b)) {
127 return -1;
128 }
129 if (!isPlainObject(a)) {
130 if (isPlainObject(b)) {
131 return 1;
132 }
133 if (typeof (a as any).toString === "function" && typeof (b as any).toString === "function") {
134 return (a as any).toString().localeCompare((b as any).toString());
135 }
136 if (typeof (a as any).valueOf === "function" && typeof (b as any).valueOf === "function") {
137 return compare((a as any).valueOf(), (b as any).valueOf());
138 }
139 return 1;
140 }
141 if (!isPlainObject(b)) {
142 return -1;
143 }
144 const aKeys = Object.keys(a) as (keyof typeof a & string)[];
145 const bKeys = Object.keys(b) as (keyof typeof b & string)[];
146 if (aKeys.length !== bKeys.length) {
147 return aKeys.length - bKeys.length;
148 }
149 const seen = new Set<string>();
150 for (let i = 0; i < aKeys.length; i++) {
151 const aKey = aKeys[i]!;
152 seen.add(aKey);
153 const aValue = a[aKey];
154 const bValue = (b as any)[aKey];
155 const valueResult = compare(aValue, bValue);
156 if (valueResult !== 0) {
157 return valueResult;
158 }
159 }
160 for (let i = 0; i < bKeys.length; i++) {
161 const bKey = bKeys[i]!;
162 if (!seen.has(bKey)) {
163 return -1;
164 }
165 }
166 return 0;
167}
168
169function compareArrays(a: unknown[], b: unknown[]): number {
170 if (a.length !== b.length) {

Callers 2

compareFunction · 0.85
Comparator.test.tsFile · 0.85

Calls 7

compareArraysFunction · 0.85
isPlainObjectFunction · 0.85
compareFunction · 0.85
valueOfMethod · 0.80
toStringMethod · 0.65
addMethod · 0.65
hasMethod · 0.45

Tested by

no test coverage detected