Effect-TS / effect

An ecosystem of tools to build robust applications in TypeScript
https://effect.website
MIT License
7.61k stars 242 forks source link

Add combinators to shape up equality of the produced data value #1822

Open parischap opened 11 months ago

parischap commented 11 months ago

What is the problem this feature would solve?

Schema interoperates with effect/Data. The Schema.data(schema) combinator can be used to build a schema from an existing schema that can decode a value A to a value Data. However, the equality operator of the produced Data is not always appropiate. It often happens that we need the equality operator to bear only on a subgroup of A's properties. For instance:

export class Person extends Data.Class<{
    readonly id: string;
    readonly firstName: string;
    readonly lastName: string;
    readonly address: string;
}> {
    [Equal.symbol] = (that: Equal.Equal): boolean =>
        that instanceof Person
            ? this.id=== that.id
            : false;
    [Hash.symbol] = (): number => Hash.hash(this.id);
}

What is the feature you are proposing to solve the problem?

Add a .notInEqual combinator:

const person= S.data(
  S.struct({
    id: S.string,
    firstName: pipe(S.string,S.notInEqual),
    lastName: pipe(S.string,S.notInEqual),
    address: pipe(S.string,S.notInEqual),
  })
);

What alternatives have you considered?

mikearnaldi commented 9 months ago

could be an underlying feature of Data and an option to S.data()