mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-02 03:15:52 +08:00
fix: improve Schema Map/Set equivalence using reusable comparison logic (#619)
This commit is contained in:
@@ -267,40 +267,50 @@ function compareRecords(
|
||||
return true
|
||||
}
|
||||
|
||||
function compareMaps(self: Map<unknown, unknown>, that: Map<unknown, unknown>): boolean {
|
||||
for (const [selfKey, selfValue] of self) {
|
||||
let found = false
|
||||
for (const [thatKey, thatValue] of that) {
|
||||
if (compareBoth(selfKey, thatKey) && compareBoth(selfValue, thatValue)) {
|
||||
found = true
|
||||
break
|
||||
/** @internal */
|
||||
export function makeCompareMap<K, V>(keyEquivalence: Equivalence<K>, valueEquivalence: Equivalence<V>) {
|
||||
return function compareMaps(self: ReadonlyMap<K, V>, that: ReadonlyMap<K, V>): boolean {
|
||||
for (const [selfKey, selfValue] of self) {
|
||||
let found = false
|
||||
for (const [thatKey, thatValue] of that) {
|
||||
if (keyEquivalence(selfKey, thatKey) && valueEquivalence(selfValue, thatValue)) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
function compareSets(self: Set<unknown>, that: Set<unknown>): boolean {
|
||||
for (const selfValue of self) {
|
||||
let found = false
|
||||
for (const thatValue of that) {
|
||||
if (compareBoth(selfValue, thatValue)) {
|
||||
found = true
|
||||
break
|
||||
const compareMaps = makeCompareMap(compareBoth, compareBoth)
|
||||
|
||||
/** @internal */
|
||||
export function makeCompareSet<A>(equivalence: Equivalence<A>) {
|
||||
return function compareSets(self: ReadonlySet<A>, that: ReadonlySet<A>): boolean {
|
||||
for (const selfValue of self) {
|
||||
let found = false
|
||||
for (const thatValue of that) {
|
||||
if (equivalence(selfValue, thatValue)) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
const compareSets = makeCompareSet(compareBoth)
|
||||
|
||||
/**
|
||||
* Determines if a value implements the `Equal` interface.
|
||||
*
|
||||
|
||||
@@ -4,11 +4,9 @@
|
||||
|
||||
import type { StandardSchemaV1 } from "@standard-schema/spec"
|
||||
import * as Cause_ from "../Cause.ts"
|
||||
import * as Arr from "../collections/Array.ts"
|
||||
import type { Brand } from "../data/Brand.ts"
|
||||
import type * as Combiner from "../data/Combiner.ts"
|
||||
import * as Data from "../data/Data.ts"
|
||||
import * as Equivalence from "../data/Equivalence.ts"
|
||||
import * as Option_ from "../data/Option.ts"
|
||||
import * as Predicate from "../data/Predicate.ts"
|
||||
import * as Record_ from "../data/Record.ts"
|
||||
@@ -3834,14 +3832,7 @@ export function ReadonlyMap<Key extends Top, Value extends Top>(key: Key, value:
|
||||
},
|
||||
equivalence: { // TODO: fix this
|
||||
_tag: "Override",
|
||||
override: ([key, value]) => {
|
||||
const entries = Arr.getEquivalence(
|
||||
Equivalence.make<[Key["Type"], Value["Type"]]>(([ka, va], [kb, vb]) => key(ka, kb) && value(va, vb))
|
||||
)
|
||||
return Equivalence.make((a, b) =>
|
||||
entries(globalThis.Array.from(a.entries()).sort(), globalThis.Array.from(b.entries()).sort())
|
||||
)
|
||||
}
|
||||
override: ([key, value]) => Equal.makeCompareMap(key, value)
|
||||
},
|
||||
format: {
|
||||
_tag: "Override",
|
||||
@@ -3924,14 +3915,9 @@ export function ReadonlySet<Value extends Top>(value: Value): ReadonlySet$<Value
|
||||
).map((as) => new globalThis.Set(as))
|
||||
}
|
||||
},
|
||||
equivalence: { // TODO: fix this
|
||||
equivalence: {
|
||||
_tag: "Override",
|
||||
override: ([value]) => {
|
||||
const values = Arr.getEquivalence(value)
|
||||
return Equivalence.make((a, b) =>
|
||||
values(globalThis.Array.from(a.values()).sort(), globalThis.Array.from(b.values()).sort())
|
||||
)
|
||||
}
|
||||
override: ([value]) => Equal.makeCompareSet(value)
|
||||
},
|
||||
format: {
|
||||
_tag: "Override",
|
||||
|
||||
@@ -543,7 +543,9 @@ describe("Equal - Structural Equality Behavior", () => {
|
||||
it("should return true for structurally identical maps", () => {
|
||||
const map1 = new Map([["a", 1], ["b", 2]])
|
||||
const map2 = new Map([["a", 1], ["b", 2]])
|
||||
const map3 = new Map([["b", 2], ["a", 1]])
|
||||
expect(Equal.equals(map1, map2)).toBe(true)
|
||||
expect(Equal.equals(map1, map3)).toBe(true)
|
||||
})
|
||||
|
||||
it("should return true for same reference", () => {
|
||||
|
||||
@@ -4,6 +4,20 @@ import { Check, Schema, ToEquivalence } from "effect/schema"
|
||||
import { describe, it } from "vitest"
|
||||
import { assertFalse, assertTrue } from "../utils/assert.ts"
|
||||
|
||||
const Modulo2 = Schema.Number.annotate({
|
||||
equivalence: {
|
||||
_tag: "Override",
|
||||
override: (): Equivalence.Equivalence<number> => Equivalence.make((a, b) => a % 2 === b % 2)
|
||||
}
|
||||
})
|
||||
|
||||
const Modulo3 = Schema.Number.annotate({
|
||||
equivalence: {
|
||||
_tag: "Override",
|
||||
override: (): Equivalence.Equivalence<number> => Equivalence.make((a, b) => a % 3 === b % 3)
|
||||
}
|
||||
})
|
||||
|
||||
describe("ToEquivalence", () => {
|
||||
it("String", () => {
|
||||
const schema = Schema.String
|
||||
@@ -320,39 +334,61 @@ describe("ToEquivalence", () => {
|
||||
assertFalse(equivalence(Redacted.make("a"), Redacted.make("b")))
|
||||
})
|
||||
|
||||
it("Option(Number)", () => {
|
||||
const schema = Schema.Option(Schema.Number)
|
||||
it("Option(Modulo2)", () => {
|
||||
const schema = Schema.Option(Modulo2)
|
||||
const equivalence = ToEquivalence.make(schema)
|
||||
|
||||
assertTrue(equivalence(Option.none(), Option.none()))
|
||||
assertTrue(equivalence(Option.some(1), Option.some(1)))
|
||||
assertFalse(equivalence(Option.none(), Option.some(1)))
|
||||
assertFalse(equivalence(Option.some(1), Option.none()))
|
||||
assertFalse(equivalence(Option.some(1), Option.some(2)))
|
||||
assertTrue(equivalence(Option.some(0), Option.some(2)))
|
||||
assertTrue(equivalence(Option.some(1), Option.some(3)))
|
||||
|
||||
assertFalse(equivalence(Option.none(), Option.some(0)))
|
||||
assertFalse(equivalence(Option.some(0), Option.none()))
|
||||
assertFalse(equivalence(Option.some(0), Option.some(1)))
|
||||
})
|
||||
|
||||
it("Result(Number, String)", () => {
|
||||
const schema = Schema.Result(Schema.Number, Schema.String)
|
||||
it("Result(Modulo2, Modulo3)", () => {
|
||||
const schema = Schema.Result(Modulo2, Modulo3)
|
||||
const equivalence = ToEquivalence.make(schema)
|
||||
assertTrue(equivalence(Result.succeed(1), Result.succeed(1)))
|
||||
assertFalse(equivalence(Result.succeed(1), Result.succeed(2)))
|
||||
|
||||
assertTrue(equivalence(Result.fail("a"), Result.fail("a")))
|
||||
assertFalse(equivalence(Result.fail("a"), Result.fail("b")))
|
||||
assertTrue(equivalence(Result.succeed(0), Result.succeed(2)))
|
||||
assertTrue(equivalence(Result.succeed(1), Result.succeed(3)))
|
||||
assertTrue(equivalence(Result.fail(0), Result.fail(3)))
|
||||
assertTrue(equivalence(Result.fail(1), Result.fail(4)))
|
||||
assertTrue(equivalence(Result.fail(2), Result.fail(5)))
|
||||
|
||||
assertFalse(equivalence(Result.succeed(1), Result.fail("a")))
|
||||
assertFalse(equivalence(Result.fail("a"), Result.succeed(1)))
|
||||
assertFalse(equivalence(Result.succeed(0), Result.fail(2)))
|
||||
assertFalse(equivalence(Result.fail(0), Result.succeed(3)))
|
||||
})
|
||||
|
||||
it("ReadonlyMap(String, Number)", () => {
|
||||
const schema = Schema.ReadonlyMap(Schema.String, Schema.Number)
|
||||
it("ReadonlySet(Modulo2)", () => {
|
||||
const schema = Schema.ReadonlySet(Modulo2)
|
||||
const equivalence = ToEquivalence.make(schema)
|
||||
|
||||
assertTrue(equivalence(new Set(), new Set()))
|
||||
assertTrue(equivalence(new Set([0]), new Set([0])))
|
||||
assertTrue(equivalence(new Set([0]), new Set([2])))
|
||||
assertTrue(equivalence(new Set([0, 1]), new Set([1, 0])))
|
||||
assertTrue(equivalence(new Set([0, 1]), new Set([2, 3])))
|
||||
|
||||
assertFalse(equivalence(new Set([0]), new Set([1])))
|
||||
assertFalse(equivalence(new Set([0, 1]), new Set([2, 2])))
|
||||
})
|
||||
|
||||
it("ReadonlyMap(Modulo2, Modulo3)", () => {
|
||||
const schema = Schema.ReadonlyMap(Modulo2, Modulo3)
|
||||
const equivalence = ToEquivalence.make(schema)
|
||||
|
||||
assertTrue(equivalence(new Map(), new Map()))
|
||||
assertTrue(equivalence(new Map([["a", 1]]), new Map([["a", 1]])))
|
||||
assertTrue(equivalence(new Map([["a", 1], ["b", 2]]), new Map([["a", 1], ["b", 2]])))
|
||||
assertTrue(equivalence(new Map([["b", 2], ["a", 1]]), new Map([["a", 1], ["b", 2]])))
|
||||
assertFalse(equivalence(new Map([["a", 1]]), new Map([["a", 2]])))
|
||||
assertFalse(equivalence(new Map([["a", 1]]), new Map([["a", 1], ["b", 2]])))
|
||||
assertFalse(equivalence(new Map([["a", 1], ["b", 2]]), new Map([["a", 1]])))
|
||||
assertTrue(equivalence(new Map([[0, 1]]), new Map([[0, 1]])))
|
||||
assertTrue(equivalence(new Map([[0, 1]]), new Map([[2, 4]])))
|
||||
assertTrue(equivalence(new Map([[0, 1], [1, 2]]), new Map([[0, 1], [1, 2]])))
|
||||
assertTrue(equivalence(new Map([[0, 1], [1, 2]]), new Map([[1, 2], [0, 1]])))
|
||||
|
||||
assertFalse(equivalence(new Map([[0, 1]]), new Map([[1, 1]])))
|
||||
assertFalse(equivalence(new Map([[0, 1]]), new Map([[0, 2]])))
|
||||
assertFalse(equivalence(new Map([[0, 1], [1, 2]]), new Map([[0, 1], [1, 3]])))
|
||||
assertFalse(equivalence(new Map([[0, 1], [1, 2]]), new Map([[0, 1], [2, 2]])))
|
||||
})
|
||||
|
||||
it("Duration", () => {
|
||||
|
||||
Reference in New Issue
Block a user