Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
import { sameFactory, same } from "./same.ts";import { assert } from "https://deno.land/x/tsafe@v1.1.1/assert.ts";import { copy } from "./copy.ts";import * as util from "https://deno.land/std@0.158.0/node/util.ts";
{
const { same } = sameFactory({ "takeIntoAccountArraysOrdering": false });
assert( same( new Set<string[]>([ ["a", "b"], ["c", "d"] ]), new Set<string[]>([ ["a", "b"], ["c", "d"] ]) ) );
assert( same( new Set<string[]>([ ["a", "b"], ["c", "d"] ]), new Set<string[]>([ ["c", "d"], ["a", "b"] ]) ) );
assert( same( new Set<string[]>([ ["a", "b"], ["c", "d"] ]), new Set<string[]>([ ["b", "a"], ["c", "d"] ]) ) );
assert( same( new Set<string[]>([ ["a", "b"], ["c", "d"] ]), new Set<string[]>([ ["c", "d"], ["b", "a"] ]) ) );
assert( !same( new Set<string[]>([ ["a", "b", "c"], ["c", "d"] ]), new Set<string[]>([ ["c", "d"], ["b", "a"] ]) ) );
}
{
const { same } = sameFactory({ "takeIntoAccountArraysOrdering": true });
assert( same( new Set<string[]>([ ["a", "b"], ["c", "d"] ]), new Set<string[]>([ ["a", "b"], ["c", "d"] ]) ) );
assert( same( new Set<string[]>([ ["a", "b"], ["c", "d"] ]), new Set<string[]>([ ["c", "d"], ["a", "b"] ]) ) );
assert( !same( new Set<string[]>([ ["a", "b"], ["c", "d"] ]), new Set<string[]>([ ["b", "a"], ["c", "d"] ]) ) );
assert( !same( new Set<string[]>([ ["a", "b"], ["c", "d"] ]), new Set<string[]>([ ["c", "d"], ["b", "a"] ]) ) );
assert( !same( new Set<string[]>([ ["a", "b", "c"], ["c", "d"] ]), new Set<string[]>([ ["c", "d"], ["b", "a"] ]) ) );
}
{
const { same } = sameFactory({ "takeIntoAccountArraysOrdering": false });
assert( same( new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
assert( same( new Map<string[], string[]>([ [["b", "a"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
assert( same( new Map<string[], string[]>([ [["c", "d"], ["3", "4"]], [["a", "b"], ["1", "2"]] ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
assert( !same( new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]], [["e", "f"], ["5", "6"]], ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
assert( !same( new Map<string[], string[]>([ [["1", "2"], ["a", "b"]], [["c", "d"], ["3", "4"]] ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
}
{
const { same } = sameFactory({ "takeIntoAccountArraysOrdering": true });
assert( same( new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
assert( !same( new Map<string[], string[]>([ [["b", "a"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
assert( same( new Map<string[], string[]>([ [["c", "d"], ["3", "4"]], [["a", "b"], ["1", "2"]] ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
assert( !same( new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]], [["e", "f"], ["5", "6"]], ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
assert( !same( new Map<string[], string[]>([ [["1", "2"], ["a", "b"]], [["c", "d"], ["3", "4"]] ]), new Map<string[], string[]>([ [["a", "b"], ["1", "2"]], [["c", "d"], ["3", "4"]] ]), ) );
}
{
const obj: any = {};
Object.assign(obj, { "p": obj });
const clone: any = {};
Object.assign(clone, { "p": clone });
assert(same(obj, clone));
}
{
const map1 = new Map<any, any>(); const map2 = new Map<any, any>();
map1.set({ "p": "foo" }, { "q": "bar" }); map2.set({ "p": "foo" }, { "q": "bar" });
map1.set({ "p": "baz" }, { "q": 44 }); map2.set({ "p": "baz" }, { "q": 44 });
map1.set(33, ["a", 33, new Date(12), new Set<any>(["a", [map1], 55])]); map2.set(33, ["a", 33, new Date(12), new Set<any>(["a", [map2], 55])]);
assert(same(map1, map2));
}
{
const map1 = new Map<any, any>(); const map2 = new Map<any, any>();
map1.set({ "p": "foo" }, { "q": "bar" }); map2.set({ "p": "foo" }, { "q": "bar" });
map1.set({ "p": "baz" }, { "q": 44 }); map2.set({ "p": "baz" }, { "q": 44 });
map1.set(33, ["a", 33, new Date(12), new Set<any>(["a", [map1], -1])]); map2.set(33, ["a", 33, new Date(12), new Set<any>(["a", [map2], 55])]);
assert(!same(map1, map2));
}
{
const map1 = new Map<any, any>(); const map2 = new Map<any, any>();
map1.set({ "p": "foo" }, { "q": "bar" }); map2.set({ "p": "foo" }, { "q": "bar" });
map1.set({ "p": "baz" }, { "q": 44 }); map2.set({ "p": "baz" }, { "q": 44 });
map1.set(33, ["a", 33, new Date(12), new Set<any>(["a", [map1], -1])]); map2.set(33, ["a", 33, new Date(12), new Set<any>(["a", [map2], 55])]);
assert(!same(map1, map2));
}
{
const map1 = new Map<any, any>(); const map2 = new Map<any, any>();
map1.set({ "p": "foo" }, { "q": "x" }); map2.set({ "p": "foo" }, { "q": "bar" });
map1.set({ "p": "baz" }, { "q": 44 }); map2.set({ "p": "baz" }, { "q": 44 });
map1.set(33, ["a", 33, new Date(12), new Set<any>(["a", [map1], 55])]); map2.set(33, ["a", 33, new Date(12), new Set<any>(["a", [map2], 55])]);
assert(!same(map1, map2));
}
{
const map1 = new Map<any, any>(); const map2 = new Map<any, any>();
map1.set({ "p": "foo" }, { "q": "bar" }); map2.set({ "p": "foo" }, { "q": "bar" });
map1.set({ "p": "baz" }, { "q": 44 }); map2.set({ "p": "baz" }, { "q": 44 });
map1.set(33, ["a", 33, new Date(13), new Set<any>(["a", [map1], 55])]); map2.set(33, ["a", 33, new Date(12), new Set<any>(["a", [map2], 55])]);
assert(!same(map1, map2));
}
const obj: any = { "p1": "FOO", "p2": new Set(["a", "b", "c"]), "p3": ["a", "b"], "p4": undefined, "p5": null };
Object.assign(obj, { obj });
const s1 = util.inspect(obj);
const clone = copy(obj, { "freeze": true })
assert(same(obj, clone));
obj["p1"] = "changed";obj["p2"].add("changed");obj["p3"].push("changed");obj["p4"] = "changed";obj["p5"] = "changed";
assert(util.inspect(clone) === s1);
try {
clone.p1 = "BAR";
assert(false, "should have throw");
} catch {}
console.log("PASS in depth");