Skip to content

Commit

Permalink
Equivalence: return Equal.equals instead of Equivalence.strict() … (
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored and mikearnaldi committed Feb 21, 2024
1 parent 4ed1d92 commit e9d272d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-chicken-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

Equivalence: return `Equal.equals` instead of `Equivalence.strict()` as default
2 changes: 1 addition & 1 deletion packages/effect/src/Equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ export const isEqual = (u: unknown): u is Equal => hasProperty(u, symbol)
* @since 2.0.0
* @category instances
*/
export const equivalence: <A>() => Equivalence<A> = () => (self, that) => equals(self, that)
export const equivalence: <A>() => Equivalence<A> = () => equals
5 changes: 3 additions & 2 deletions packages/schema/src/Equivalence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @since 1.0.0
*/

import * as Equal from "effect/Equal"
import * as Equivalence from "effect/Equivalence"
import * as Option from "effect/Option"
import * as Predicate from "effect/Predicate"
Expand Down Expand Up @@ -78,7 +79,7 @@ const go = (ast: AST.AST): Equivalence.Equivalence<any> => {
case "VoidKeyword":
case "Enums":
case "ObjectKeyword":
return Equivalence.strict()
return Equal.equals
case "Refinement":
return go(ast.from)
case "Suspend": {
Expand Down Expand Up @@ -127,7 +128,7 @@ const go = (ast: AST.AST): Equivalence.Equivalence<any> => {
}
case "TypeLiteral": {
if (ast.propertySignatures.length === 0 && ast.indexSignatures.length === 0) {
return Equivalence.strict()
return Equal.equals
}
const propertySignatures = ast.propertySignatures.map((ps) => go(ps.type))
const indexSignatures = ast.indexSignatures.map((is) => go(is.type))
Expand Down
14 changes: 5 additions & 9 deletions packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3520,7 +3520,7 @@ export const Uint8ArrayFromSelf: Schema<Uint8Array> = declare(
identifier: "Uint8ArrayFromSelf",
pretty: (): Pretty.Pretty<Uint8Array> => (u8arr) => `new Uint8Array(${JSON.stringify(Array.from(u8arr))})`,
arbitrary: (): Arbitrary<Uint8Array> => (fc) => fc.uint8Array(),
equivalence: (): Equivalence.Equivalence<Uint8Array> => ReadonlyArray.getEquivalence(Equivalence.strict()) as any
equivalence: (): Equivalence.Equivalence<Uint8Array> => ReadonlyArray.getEquivalence(Equal.equals) as any
}
)

Expand Down Expand Up @@ -4644,8 +4644,7 @@ export const dataFromSelf = <
{
description: `Data<${format(item)}>`,
pretty: dataPretty,
arbitrary: dataArbitrary,
equivalence: () => Equal.equals
arbitrary: dataArbitrary
}
)
}
Expand Down Expand Up @@ -5076,8 +5075,7 @@ export const FiberIdFromSelf: Schema<FiberId.FiberId> = declare(
{
identifier: "FiberIdFromSelf",
pretty: () => fiberIdPretty,
arbitrary: () => fiberIdArbitrary,
equivalence: () => Equal.equals
arbitrary: () => fiberIdArbitrary
}
)
Expand Down Expand Up @@ -5255,8 +5253,7 @@ export const causeFromSelf = <A, I, R1, R2 = never>({ defect = unknown, error }:
{
description: `Cause<${format(error)}>`,
pretty: causePretty,
arbitrary: causeArbitrary,
equivalence: () => Equal.equals
arbitrary: causeArbitrary
}
)
}
Expand Down Expand Up @@ -5432,8 +5429,7 @@ export const exitFromSelf = <E, IE, RE, A, IA, RA, RD = never>({ defect = unknow
{
description: `Exit<${format(failure)}, ${format(success)}>`,
pretty: exitPretty,
arbitrary: exitArbitrary,
equivalence: () => Equal.equals
arbitrary: exitArbitrary
}
)
Expand Down
27 changes: 21 additions & 6 deletions packages/schema/test/Equivalence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import * as S from "@effect/schema/Schema"
import * as Chunk from "effect/Chunk"
import * as Data from "effect/Data"
import * as Either from "effect/Either"
import * as Equal from "effect/Equal"
import * as Equivalence from "effect/Equivalence"
import * as Hash from "effect/Hash"
import * as Option from "effect/Option"
import { isUnknown } from "effect/Predicate"
import * as fc from "fast-check"
Expand Down Expand Up @@ -113,14 +115,27 @@ describe("Equivalence", () => {
})

describe("declaration", () => {
it("should return Equivalence.strict() when an annotation exists", () => {
const schema = S.declare(isUnknown, {
arbitrary: (): A.Arbitrary<string> => (fc) => fc.string()
})
it("should return Equal.equals when an annotation doesn't exist", () => {
const schema = S.declare(isUnknown)
const equivalence = E.make(schema)
expect(equivalence).toStrictEqual(Equivalence.strict())
expect(equivalence).toStrictEqual(Equal.equals)

const make = (id: number, s: string) => {
return {
[Hash.symbol]() {
return 0
},
[Equal.symbol](that: any) {
return that.id === id
},
id,
s
}
}

// propertyTo(schema)
expect(equivalence(make(1, "a"), make(1, "a"))).toBe(true)
expect(equivalence(make(1, "a"), make(1, "b"))).toBe(true)
expect(equivalence(make(1, "a"), make(2, "a"))).toBe(false)
})

it("Chunk", () => {
Expand Down

0 comments on commit e9d272d

Please sign in to comment.