Skip to content

Commit 02f5b26

Browse files
authored
Merge pull request #9 from CeeblueTV/dev
feat(util): add a function to compare any object simple or complexe
2 parents cb1df64 + 22cedfe commit 02f5b26

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/Util.ts

+29
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,35 @@ export function sleep(ms: number) {
227227
});
228228
}
229229

230+
/**
231+
* Test equality between two value whatever their type, array included
232+
*/
233+
export function equal(a: any, b: any) {
234+
if (Object(a) !== a) {
235+
if (Object(b) === b) {
236+
return false;
237+
}
238+
// both primitive (null and undefined included)
239+
return a === b;
240+
}
241+
// complexe object
242+
if (a[Symbol.iterator]) {
243+
if (!b[Symbol.iterator]) {
244+
return false;
245+
}
246+
if (a.length !== b.length) {
247+
return false;
248+
}
249+
for (let i = 0; i !== a.length; ++i) {
250+
if (a[i] !== b[i]) {
251+
return false;
252+
}
253+
}
254+
return true;
255+
}
256+
return a === b;
257+
}
258+
230259
/**
231260
* fetch help method with few usefull fix:
232261
* - throw an string exception if response code is not 200 with the text of the response or uses statusText

0 commit comments

Comments
 (0)