diff --git a/.changeset/late-apricots-hunt.md b/.changeset/late-apricots-hunt.md new file mode 100644 index 0000000..093aac4 --- /dev/null +++ b/.changeset/late-apricots-hunt.md @@ -0,0 +1,5 @@ +--- +"@fp-ts/optic": minor +--- + +update effect to 2.4.0 diff --git a/package.json b/package.json index cafbb6f..c8a1e8b 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "@effect-ts/core": "^0.60.5", "@effect/docgen": "^0.2.0", "@effect/dtslint": "^0.0.4", - "@effect/typeclass": "^0.22.0", + "@effect/typeclass": "^0.23.0", "@repo-tooling/eslint-plugin-dprint": "^0.0.4", "@types/benchmark": "^2.1.2", "@types/glob": "^8.1.0", @@ -86,7 +86,7 @@ "babel-plugin-annotate-pure-calls": "^0.4.0", "concurrently": "^8.0.1", "cpx": "^1.5.0", - "effect": "2.0.0", + "effect": "2.4.0", "eslint": "^8.37.0", "eslint-import-resolver-typescript": "^3.5.5", "eslint-plugin-codegen": "0.17.0", @@ -107,7 +107,7 @@ "vitest": "0.30.1" }, "peerDependencies": { - "@effect/typeclass": "^0.22.0", - "effect": "^2.0.0" + "@effect/typeclass": "^0.23.0", + "effect": "^2.4.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a756038..bc2f7a9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -33,8 +33,8 @@ devDependencies: specifier: ^0.0.4 version: 0.0.4(typescript@5.3.2) '@effect/typeclass': - specifier: ^0.22.0 - version: 0.22.0(effect@2.0.0) + specifier: ^0.23.0 + version: 0.23.0(effect@2.4.0) '@repo-tooling/eslint-plugin-dprint': specifier: ^0.0.4 version: 0.0.4(typescript@5.3.2) @@ -75,8 +75,8 @@ devDependencies: specifier: ^1.5.0 version: 1.5.0 effect: - specifier: 2.0.0 - version: 2.0.0 + specifier: 2.4.0 + version: 2.4.0 eslint: specifier: ^8.37.0 version: 8.41.0 @@ -677,12 +677,12 @@ packages: typescript: 5.3.2 dev: true - /@effect/typeclass@0.22.0(effect@2.0.0): - resolution: {integrity: sha512-1++hmUh/ODupG6Xh4GdQ9XlpD8+PB0beWypbb+bmvKLCWrRTfSZt6r4ueqxxa0k+yzyEIs7PXBxUL9FXKaLSDQ==} + /@effect/typeclass@0.23.0(effect@2.4.0): + resolution: {integrity: sha512-AqQh9ZkHEx0RP8PBccXxSAe+yDenXcVivzFcgeboW7dRw9Rp6GCKDssEoNySh3kC0uAXweVh2Ycz8Ynbax22Ug==} peerDependencies: - effect: ^2.0.0 + effect: ^2.4.0 dependencies: - effect: 2.0.0 + effect: 2.4.0 dev: true /@esbuild/android-arm64@0.17.19: @@ -2683,8 +2683,8 @@ packages: resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} dev: true - /effect@2.0.0: - resolution: {integrity: sha512-yYYNf91dlk7dEl+U1QzeSiMHIM9zz1/NqSJ5hNRZ6GrFwctaCQcdxmAhll6mQRCgXFIAMhMEawmATlGLWtU5qQ==} + /effect@2.4.0: + resolution: {integrity: sha512-HAtFVAbAGYDzfGbrSrX1gzMzXym15zk1+ps72X8W1wdZY0vxq+u23w8u9DhAZjiJPHT4W9ZYL7QWQqpP4t+dSg==} dev: true /electron-to-chromium@1.4.402: diff --git a/src/data/Either.ts b/src/data/Either.ts index 0b0e2f1..6083e49 100644 --- a/src/data/Either.ts +++ b/src/data/Either.ts @@ -11,10 +11,10 @@ import * as Either from "effect/Either" * @since 1.0.0 */ export const right: { - (): Prism, A> - (): PolyPrism, Either.Either, A, B> -} = () => - Optic.prism, A>( + (): Prism, A> + (): PolyPrism, Either.Either, A, B> +} = () => + Optic.prism, A>( Either.mapLeft(() => new Error("Expected a Right")), Either.right ) @@ -25,10 +25,10 @@ export const right: { * @since 1.0.0 */ export const left: { - (): Prism, E> - (): PolyPrism, Either.Either, E, B> -} = () => - Optic.prism, E>( + (): Prism, E> + (): PolyPrism, Either.Either, E, B> +} = () => + Optic.prism, E>( Either.match({ onLeft: Either.right, onRight: () => Either.left(new Error("Expected a Left")) diff --git a/src/index.ts b/src/index.ts index 789ce86..6e06dfc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,29 +2,12 @@ * @since 1.0.0 */ import * as Either from "effect/Either" -import { dual, identity, pipe } from "effect/Function" +import { identity, pipe } from "effect/Function" import * as Option from "effect/Option" import type { Predicate, Refinement } from "effect/Predicate" import * as ReadonlyArray from "effect/ReadonlyArray" import * as ReadonlyRecord from "effect/ReadonlyRecord" -import * as S from "effect/Struct" -import type { Simplify } from "effect/Types" - -const orElse: { - ( - that: (e1: E1) => Either.Either - ): (self: Either.Either) => Either.Either - ( - self: Either.Either, - that: (e1: E1) => Either.Either - ): Either.Either -} = dual( - 2, - ( - self: Either.Either, - that: (e1: E1) => Either.Either - ): Either.Either => Either.isLeft(self) ? that(self.left) : Either.right(self.right) -) +import * as Struct from "effect/Struct" /** * @since 1.0.0 @@ -40,12 +23,12 @@ export interface Optic< > { readonly getOptic: ( GetWhole: GetWhole - ) => Either.Either + ) => Either.Either readonly setOptic: ( SetPiece: SetPiece ) => ( SetWholeBefore: SetWholeBefore - ) => Either.Either + ) => Either.Either /** * @since 1.0.0 @@ -177,7 +160,10 @@ export interface Optic< * * @since 1.0.0 */ - key(this: Optional>, key: string): Optional + key( + this: Optional>, + key: string + ): Optional } /** @@ -206,7 +192,7 @@ const prismComposition = pipe( that.getOptic(getPiece), - orElse(([GetError1, SetPiece]) => + Either.orElse(([GetError1, SetPiece]) => pipe( self.setOptic(SetPiece)(getWhole), Either.match({ @@ -271,7 +257,7 @@ const lensComposition = < (a) => pipe( that.getOptic(a), - orElse(([de, b]) => + Either.orElse(([de, b]) => pipe( self.setOptic(b)(s), Either.match({ @@ -317,16 +303,6 @@ const at = (key: Key): Lens]>( - ...keys: Keys -): Lens>> => - lens(S.pick(...keys), (a) => (s) => ({ ...s, ...a as any })) - -const omit = ]>( - ...keys: Keys -): Lens>> => - lens(S.omit(...keys), (a) => (s) => ({ ...s, ...a as any })) - const filter: { ( refinement: Refinement, @@ -364,7 +340,7 @@ const index = (i: number): Optional, A> => ) ) -const key = (key: string): Optional, A> => +const key = (key: string): Optional, A> => optional( (s) => pipe( @@ -396,12 +372,12 @@ class Builder< readonly composition: "prism" | "lens", readonly getOptic: ( GetWhole: GetWhole - ) => Either.Either, + ) => Either.Either, readonly setOptic: ( SetPiece: SetPiece ) => ( SetWholeBefore: SetWholeBefore - ) => Either.Either + ) => Either.Either ) {} compose(that: any): any { @@ -415,11 +391,11 @@ class Builder< } pick(...keys: readonly [PropertyKey, ...Array]) { - return this.compose(pick(...keys)) + return this.compose(lens(Struct.pick(...keys), (a) => (s: any) => ({ ...s, ...a }))) } omit(...keys: readonly [PropertyKey, ...Array]) { - return this.compose(omit(...keys)) + return this.compose(lens(Struct.omit(...keys), (a) => (s: any) => ({ ...s, ...a }))) } filter(predicate: Predicate, message?: string) { @@ -508,7 +484,7 @@ export interface PolyPrism * @since 1.0.0 */ export const polyPrism = ( - polyDecode: (s: S) => Either.Either, + polyDecode: (s: S) => Either.Either, encode: (b: B) => T ): PolyPrism => new Builder("prism", polyDecode, (b) => (_) => Either.right(encode(b))) @@ -522,7 +498,7 @@ export interface Prism extends PolyPrism {} * @since 1.0.0 */ export const prism = ( - decode: (s: S) => Either.Either, + decode: (s: S) => Either.Either, encode: (a: A) => S ): Prism => polyPrism((s) => pipe(decode(s), Either.mapLeft((e) => [e, s])), encode) @@ -561,7 +537,7 @@ export interface PolyReversedPrism */ export const polyReversedPrism = ( get: (s: S) => A, - polyReplaceEither: (b: B) => (s: S) => Either.Either + polyReplaceEither: (b: B) => (s: S) => Either.Either ): PolyReversedPrism => new Builder("prism", (s) => Either.right(get(s)), polyReplaceEither) @@ -575,7 +551,7 @@ export interface ReversedPrism extends PolyReversedPrism( get: (s: S) => A, - replaceEither: (a: A) => Either.Either + replaceEither: (a: A) => Either.Either ): ReversedPrism => polyReversedPrism(get, (a) => (s) => pipe(replaceEither(a), Either.mapLeft((e) => [e, s]))) @@ -606,8 +582,8 @@ export interface PolyOptional * @since 1.0.0 */ export const polyOptional = ( - polyDecode: (s: S) => Either.Either, - polyReplaceEither: (b: B) => (s: S) => Either.Either + polyDecode: (s: S) => Either.Either, + polyReplaceEither: (b: B) => (s: S) => Either.Either ): PolyOptional => new Builder("lens", polyDecode, polyReplaceEither) /** @@ -620,8 +596,8 @@ export interface Optional extends PolyOptional { * @since 1.0.0 */ export const optional = ( - decode: (s: S) => Either.Either, - replaceEither: (a: A) => (s: S) => Either.Either + decode: (s: S) => Either.Either, + replaceEither: (a: A) => (s: S) => Either.Either ): Optional => polyOptional( (s) => pipe(decode(s), Either.mapLeft((e) => [e, s])), @@ -716,8 +692,8 @@ export interface PolyTraversal * @since 1.0.0 */ export const polyTraversal = ( - decode: (s: S) => Either.Either>, - replace: (bs: ReadonlyArray) => (s: S) => Either.Either + decode: (s: S) => Either.Either, readonly [Error, T]>, + replace: (bs: ReadonlyArray) => (s: S) => Either.Either ): PolyTraversal => new Builder("lens", decode, replace) /** @@ -730,8 +706,8 @@ export interface Traversal extends PolyTraversal * @since 1.0.0 */ export const traversal = ( - decode: (s: S) => Either.Either>, - replace: (as: ReadonlyArray) => (s: S) => Either.Either + decode: (s: S) => Either.Either, Error>, + replace: (as: ReadonlyArray) => (s: S) => Either.Either ): Traversal => polyTraversal( (s) => pipe(decode(s), Either.mapLeft((e) => [e, s])), @@ -759,13 +735,13 @@ export const getOption = (optic: Getter) => * @since 1.0.0 */ export const getOrModify = (optic: PolyOptional) => - (s: S): Either.Either => pipe(optic.getOptic(s), Either.mapLeft(([_, t]) => t)) + (s: S): Either.Either => pipe(optic.getOptic(s), Either.mapLeft(([_, t]) => t)) /** * @since 1.0.0 */ export const decode = (optic: PolyPrism) => - (GetWhole: S): Either.Either => + (GetWhole: S): Either.Either => pipe(optic.getOptic(GetWhole), Either.mapLeft(([e, _]) => e)) /** diff --git a/test/data/Either.test.ts b/test/data/Either.test.ts index 5e02654..0d79e9c 100644 --- a/test/data/Either.test.ts +++ b/test/data/Either.test.ts @@ -7,7 +7,7 @@ import { describe, expect, it } from "vitest" describe("prisms", () => { it("right", () => { - const _right = Optic.id>() + const _right = Optic.id>() .compose(EitherOptic.right()) expect(pipe(E.right(1), Optic.getOption(_right))).toEqual(O.some(1)) @@ -19,7 +19,7 @@ describe("prisms", () => { }) it("left", () => { - const _left = Optic.id>() + const _left = Optic.id>() .compose(EitherOptic.left()) expect(pipe(E.left("e"), Optic.getOption(_left))).toEqual(O.some("e"))