diff --git a/.gitea/workflows/lint.yaml b/.gitea/workflows/lint.yaml index c13cd5d..a104858 100644 --- a/.gitea/workflows/lint.yaml +++ b/.gitea/workflows/lint.yaml @@ -16,3 +16,7 @@ jobs: run: bun lint:tsc - name: Lint Biome run: bun lint:biome + - name: Build + run: bun run build + - name: Test + run: bun test diff --git a/.gitea/workflows/publish.yaml b/.gitea/workflows/publish.yaml index 73a0052..5f16e26 100644 --- a/.gitea/workflows/publish.yaml +++ b/.gitea/workflows/publish.yaml @@ -21,6 +21,8 @@ jobs: run: bun lint:biome - name: Build run: bun run build + - name: Test + run: bun test # - name: Publish effect-lens # uses: JS-DevTools/npm-publish@v4 # with: diff --git a/.gitea/workflows/test-build.yaml b/.gitea/workflows/test-build.yaml index 729eb00..ebafc97 100644 --- a/.gitea/workflows/test-build.yaml +++ b/.gitea/workflows/test-build.yaml @@ -23,5 +23,7 @@ jobs: run: bun lint:biome - name: Build run: bun run build + - name: Test + run: bun test - name: Pack run: bun pack diff --git a/packages/example/src/localstorage.ts b/packages/example/src/localstorage.ts index 679a9f5..56f1d46 100644 --- a/packages/example/src/localstorage.ts +++ b/packages/example/src/localstorage.ts @@ -5,8 +5,8 @@ import { Lens } from "effect-lens" Effect.gen(function*() { - // \/ Lens, PlatformError, PlatformError, never, never> - const lens = Effect.all([ + // \/ Lens, PlatformError, PlatformError, never, never> + Effect.all([ KeyValueStore.KeyValueStore, Effect.succeed("someKey"), ]).pipe( diff --git a/packages/example/src/quickstart.ts b/packages/example/src/quickstart.ts deleted file mode 100644 index c6e5bcd..0000000 --- a/packages/example/src/quickstart.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { KeyValueStore } from "@effect/platform" -import { Array, Console, DateTime, Effect, Stream, SubscriptionRef } from "effect" -import { Lens } from "effect-lens" - -Effect.gen(function*() { - // The ref is the data source - const ref = yield* SubscriptionRef.make([12, 87, 69]) - - // The lens acts as a proxy that allows reading, subscribing from and writing to that - // data source with a similar API to Effect's SubscriptionRef - const lens = Lens.fromSubscriptionRef(ref) - // ^ Lens.Lens - - const value = yield* Lens.get(lens) - yield* Effect.forkScoped(Stream.runForEach(lens.changes, Console.log)) - yield* Lens.update(lens, Array.replace(1, 1664)) -}) - -Effect.gen(function*() { - interface User { - readonly name: string - readonly age: DateTime.Utc - } - - // The state of your app - const ref = yield* SubscriptionRef.make<{ - readonly users: readonly User[] - }>({ - users: [ - { name: "Jean Dupont", age: yield* DateTime.make("03/25/1969") }, - { name: "Juan Joya Borja", age: yield* DateTime.make("04/05/1956") }, - { name: "Benzemonstre", age: yield* DateTime.make("06/12/2000") }, - ] - }) - - // \/ Lens - const jeanDupontLens = ref.pipe( - Lens.fromSubscriptionRef, // Creates a lens that proxies the ref - Lens.focusField("users"), // Creates a focused lens that points to the user field - Lens.focusArrayAt(0), // Creates a focused lens that points to the first entry of the user array - ) - // Reading or writing from this lense can fail with NoSuchElementException - // This is because of Lens.focusArrayAt(0), as reading and writing to an array can be an unsafe operation - - const jeanDupont = yield* Lens.get(jeanDupontLens) - - yield* Lens.set( - // You can focus even further down - Lens.focusField(jeanDupontLens, "age"), - yield* DateTime.make("03/25/1970"), - ) - // Mutations with the parent state are performed immutably by default - // unless you use a specific mutable transform such as 'focusMutableField' -}) - -Effect.gen(function*() { - interface User { - readonly name: string - readonly age: DateTime.Utc - } - - const ref = yield* SubscriptionRef.make([ - { name: "Jean Dupont", age: yield* DateTime.make("03/25/1969") }, - { name: "Juan Joya Borja", age: yield* DateTime.make("04/05/1956") }, - { name: "Benzemonstre", age: yield* DateTime.make("06/12/2000") }, - ]) - - // \/ Lens - const benzemonstreLens = ref.pipe( - Lens.fromSubscriptionRef, - - // Manually focus - Lens.mapEffect( - // Getter: - Array.get(2), - // Setter: - (a, b) => Array.replaceOption(a, 2, b), - // ^ The current Lens value (readonly User[]) - // ^ The new focused value to push (User) - ), - ) - - // Both Array.get and Array.replaceOption return an Option - // When evaluated by the lens, Option becomes Effect - // As you can see, this is automatically tracked by the Lens type -})