Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- // JSON object comparizon without key order: o1 == o2
- function equals(o1, o2) {
- if (typeof o1 !== "object" || typeof o2 !== "object") return o1 === o2;
- if (o1 === null || o2 === null) return o1 === o2;
- if (Array.isArray(o1)) return Array.isArray(o2) &&
- o1.length === o2.length && o1.every((v, i) => equals(v, o2[i]));
- const k1 = Reflect.ownKeys(o1).sort(), k2 = Reflect.ownKeys(o2).sort();
- if (k1.length !== k2.length) return false;
- return k1.every((k, i) => k === k2[i] && equals(o1[k], o2[k]));
- }
- // JSON satisfiability: o1 <= o2
- function satisfy(o1, o2) {
- if (typeof o1 !== "object" || typeof o2 !== "object") return o1 === o2;
- if (o1 === null || o2 === null) return o1 === o2;
- if (Array.isArray(o1)) return Array.isArray(o2) &&
- o1.every((v, i) => satisfy(v, o2[i]));
- const k1 = Reflect.ownKeys(o1), k2 = Reflect.ownKeys(o2);
- return k1.every(k => k2.includes(k) && satisfy(o1[k], o2[k]));
- }
- // case
- console.assert(equals(
- {foo: true, bar: {buzz: [1, 2, 3], quux: "abc"}},
- {bar: {quux: "abc", buzz: [1, 2, 3]}, foo: true}));
- console.assert(satisfy(
- {foo: true, bar: {buzz: [1, 2, 3], quux: "abc"}},
- {bar: {quux: "abc", buzz: [1, 2, 3, 4], $$: false}, foo: true, 1: null}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement