Advertisement
AlvinSeville7cf

deepEqual

Aug 22nd, 2021 (edited)
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function deepEqual(first, second) {
  2.   if (first === undefined && second === undefined)
  3.     return true;
  4.   if ([first, second].filter(item => item === undefined).length === 1)
  5.     return false;
  6.  
  7.   let [firstType, secondType] = [typeof first, typeof second];
  8.  
  9.   if (firstType !== "object" && secondType !== "object")
  10.     return first === second;
  11.  
  12.   let firstProps = first === null ? [] : Object.getOwnPropertyNames(first).sort();
  13.   let secondProps = second === null ? [] : Object.getOwnPropertyNames(second).sort();
  14.  
  15.   if (firstProps.length !== secondProps.length)
  16.     return false;
  17.  
  18.   for (i in firstProps)
  19.     if (firstProps[i] !== secondProps[i])
  20.       return false;
  21.  
  22.   for (i in firstProps) {
  23.     let currentFirstProp = firstProps[i];
  24.     if (!secondProps.includes(currentFirstProp) || !deepEqual(first[currentFirstProp], second[currentFirstProp]))
  25.       return false;
  26.   }
  27.  
  28.   return true;
  29. }
  30.  
  31. function main() {
  32.   let a = { location: [1, 1, 1], rotation: [0, 0, 0] }
  33.   let b = { location: [1, 1, 1], rotation: [0, 0, 0] }
  34.  
  35.   console.log(deepEqual(null, a));
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement