UNPKG

@squiz/json-schema-library

Version:

Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation

46 lines (45 loc) 1.18 kB
/** * deep comparison of javascript types. Better handling than using * JSON.stringify, where property order is accounted for * * @returns true, if data types are deep equal */ export default function deepCompareStrict(a: any, b: any): boolean { const typeofa = typeof a; if (typeofa !== typeof b) { return false; } if (Array.isArray(a)) { if (!Array.isArray(b)) { return false; } const length = a.length; if (length !== b.length) { return false; } for (let i = 0; i < length; i++) { if (!deepCompareStrict(a[i], b[i])) { return false; } } return true; } if (typeofa === "object") { if (!a || !b) { return a === b; } const aKeys = Object.keys(a); const bKeys = Object.keys(b); const length = aKeys.length; if (length !== bKeys.length) { return false; } for (const k of aKeys) { if (!deepCompareStrict(a[k], b[k])) { return false; } } return true; } return a === b; }