@fp-ts/optic
Advanced tools
Comparing version 0.23.0 to 0.24.0
@@ -1,9 +0,5 @@ | ||
/** | ||
* @since 1.0.0 | ||
*/ | ||
import * as Either from "effect/Either"; | ||
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 Record from "effect/Record"; | ||
/** | ||
@@ -93,3 +89,3 @@ * @since 1.0.0 | ||
*/ | ||
key<S, A>(this: Optional<S, ReadonlyRecord.ReadonlyRecord<string, A>>, key: string): Optional<S, A>; | ||
key<S, A>(this: Optional<S, Record.ReadonlyRecord<string, A>>, key: string): Optional<S, A>; | ||
} | ||
@@ -226,3 +222,3 @@ /** | ||
*/ | ||
export declare const indexes: <A>() => Traversal<readonly A[], A>; | ||
export declare const indexes: <A>() => Traversal<ReadonlyArray<A>, A>; | ||
/** | ||
@@ -232,3 +228,3 @@ * @category constructors | ||
*/ | ||
export declare const head: <A>() => Optional<readonly A[], A>; | ||
export declare const head: <A>() => Optional<ReadonlyArray<A>, A>; | ||
/** | ||
@@ -238,3 +234,3 @@ * @category constructors | ||
*/ | ||
export declare const tail: <A>() => Optional<readonly A[], readonly A[]>; | ||
export declare const tail: <A>() => Optional<ReadonlyArray<A>, ReadonlyArray<A>>; | ||
/** | ||
@@ -274,3 +270,3 @@ * An optic that accesses the first case specified by a predicate. | ||
*/ | ||
export declare const polyTraversal: <S, T, A, B>(decode: (s: S) => Either.Either<readonly A[], readonly [Error, T]>, replace: (bs: readonly B[]) => (s: S) => Either.Either<T, readonly [Error, T]>) => PolyTraversal<S, T, A, B>; | ||
export declare const polyTraversal: <S, T, A, B>(decode: (s: S) => Either.Either<ReadonlyArray<A>, readonly [Error, T]>, replace: (bs: ReadonlyArray<B>) => (s: S) => Either.Either<T, readonly [Error, T]>) => PolyTraversal<S, T, A, B>; | ||
/** | ||
@@ -285,3 +281,3 @@ * @since 1.0.0 | ||
*/ | ||
export declare const traversal: <S, A>(decode: (s: S) => Either.Either<readonly A[], Error>, replace: (as: readonly A[]) => (s: S) => Either.Either<S, Error>) => Traversal<S, A>; | ||
export declare const traversal: <S, A>(decode: (s: S) => Either.Either<ReadonlyArray<A>, Error>, replace: (as: ReadonlyArray<A>) => (s: S) => Either.Either<S, Error>) => Traversal<S, A>; | ||
/** | ||
@@ -288,0 +284,0 @@ * @since 1.0.0 |
12
index.js
@@ -7,7 +7,7 @@ "use strict"; | ||
exports.traversal = exports.tail = exports.reversedPrism = exports.reversedFilter = exports.replaceOption = exports.replace = exports.prism = exports.polyTraversal = exports.polyReversedPrism = exports.polyPrism = exports.polyOptional = exports.optional = exports.modify = exports.lens = exports.iso = exports.indexes = exports.id = exports.head = exports.getOrModify = exports.getOption = exports.get = exports.findFirst = exports.encode = exports.decode = exports.cons = void 0; | ||
var Array = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Array")); | ||
var Either = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Either")); | ||
var _Function = /*#__PURE__*/require("effect/Function"); | ||
var Option = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Option")); | ||
var ReadonlyArray = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/ReadonlyArray")); | ||
var ReadonlyRecord = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/ReadonlyRecord")); | ||
var Record = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Record")); | ||
var Struct = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Struct")); | ||
@@ -63,4 +63,4 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
const some = () => prism(Either.fromOption(() => new Error("Expected a Some")), Option.some); | ||
const index = i => optional(s => (0, _Function.pipe)(s, ReadonlyArray.get(i), Either.fromOption(() => new Error(`Missing index ${i}`))), a => s => (0, _Function.pipe)(ReadonlyArray.replaceOption(i, a)(s), Either.fromOption(() => new Error(`Missing index ${i}`)))); | ||
const key = key => optional(s => (0, _Function.pipe)(s, ReadonlyRecord.get(key), Either.fromOption(() => new Error(`Missing key ${JSON.stringify(key)}`))), a => s => (0, _Function.pipe)(s, ReadonlyRecord.replaceOption(key, a), Either.fromOption(() => new Error(`Missing key ${JSON.stringify(key)}`)))); | ||
const index = i => optional(s => (0, _Function.pipe)(s, Array.get(i), Either.fromOption(() => new Error(`Missing index ${i}`))), a => s => (0, _Function.pipe)(Array.replaceOption(i, a)(s), Either.fromOption(() => new Error(`Missing index ${i}`)))); | ||
const key = key => optional(s => (0, _Function.pipe)(s, Record.get(key), Either.fromOption(() => new Error(`Missing key ${JSON.stringify(key)}`))), a => s => (0, _Function.pipe)(s, Record.replaceOption(key, a), Either.fromOption(() => new Error(`Missing key ${JSON.stringify(key)}`)))); | ||
class Builder { | ||
@@ -144,3 +144,3 @@ constructor(composition, getOptic, setOptic) { | ||
exports.prism = prism; | ||
const cons = () => prism(s => ReadonlyArray.isNonEmptyReadonlyArray(s) ? Either.right([s[0], s.slice(1)]) : Either.left(new Error("Expected a non empty array")), ([head, tail]) => [head, ...tail]); | ||
const cons = () => prism(s => Array.isNonEmptyReadonlyArray(s) ? Either.right([s[0], s.slice(1)]) : Either.left(new Error("Expected a non empty array")), ([head, tail]) => [head, ...tail]); | ||
/** | ||
@@ -203,3 +203,3 @@ * @since 1.0.0 | ||
exports.tail = tail; | ||
const findFirst = (predicate, message) => optional(s => (0, _Function.pipe)(s, ReadonlyArray.findFirst(predicate), Either.fromOption(() => new Error(message ?? "Expected a value satisfying the specified predicate"))), a => s => (0, _Function.pipe)(s, ReadonlyArray.findFirstIndex(predicate), Either.fromOption(() => new Error(message ?? "Expected a value satisfying the specified predicate")), Either.map(index => { | ||
const findFirst = (predicate, message) => optional(s => (0, _Function.pipe)(s, Array.findFirst(predicate), Either.fromOption(() => new Error(message ?? "Expected a value satisfying the specified predicate"))), a => s => (0, _Function.pipe)(s, Array.findFirstIndex(predicate), Either.fromOption(() => new Error(message ?? "Expected a value satisfying the specified predicate")), Either.map(index => { | ||
const out = s.slice(); | ||
@@ -206,0 +206,0 @@ out[index] = a; |
{ | ||
"name": "@fp-ts/optic", | ||
"version": "0.23.0", | ||
"version": "0.24.0", | ||
"license": "MIT", | ||
@@ -10,4 +10,4 @@ "repository": { | ||
"peerDependencies": { | ||
"@effect/typeclass": "^0.23.0", | ||
"effect": "^2.4.0" | ||
"@effect/typeclass": "^0.24.0", | ||
"effect": "^3.0.0" | ||
}, | ||
@@ -14,0 +14,0 @@ "main": "./index.js", |
@@ -54,3 +54,3 @@ <h3 align="center"> | ||
- TypeScript 5.0 or newer | ||
- TypeScript 5.4 or newer | ||
- The `strict` flag enabled in your `tsconfig.json` file | ||
@@ -89,7 +89,7 @@ | ||
```ts | ||
import * as O from "effect/Option"; | ||
import * as Option from "effect/Option"; | ||
interface Street { | ||
readonly num: number; | ||
readonly name: O.Option<string>; | ||
readonly name: Option.Option<string>; | ||
} | ||
@@ -117,3 +117,3 @@ interface Address { | ||
num: 23, | ||
name: O.some("high street"), | ||
name: Option.some("high street"), | ||
}, | ||
@@ -132,3 +132,3 @@ }, | ||
num: 23, | ||
name: O.some("High street"), | ||
name: Option.some("High street"), | ||
}, | ||
@@ -144,4 +144,5 @@ }, | ||
import * as Optic from "@fp-ts/optic"; | ||
import * as StringOptic from "@fp-ts/optic/data/String"; | ||
import * as StringOptic from "@fp-ts/optic/String"; | ||
import * as String from "effect/String"; | ||
import * as assert from "node:assert"; | ||
@@ -158,3 +159,3 @@ const _firstChar: Optic.Optional<Employee, string> = Optic.id<Employee>() | ||
expect(capitalizeName(from)).toEqual(to); | ||
assert.deepStrictEqual(capitalizeName(from), to); | ||
``` | ||
@@ -174,3 +175,3 @@ | ||
out GetPiece, | ||
out SetWholeAfter | ||
out SetWholeAfter, | ||
> { | ||
@@ -542,15 +543,15 @@ readonly getOptic: ( | ||
import { pipe } from "effect"; | ||
import * as O from "effect/Option"; | ||
import * as Option from "effect/Option"; | ||
import * as Optic from "@fp-ts/optic"; | ||
// This creates a prism that focuses on the 'Some' case of the 'Option<number>' object. | ||
const _some: Optic.Prism<O.Option<number>, number> = Optic.id< | ||
O.Option<number> | ||
const _some: Optic.Prism<Option.Option<number>, number> = Optic.id< | ||
Option.Option<number> | ||
>().some(); | ||
const option: O.Option<number> = O.some(42); | ||
const option: Option.Option<number> = Option.some(42); | ||
const result: O.Option<number> = pipe(option, Optic.getOption(_some)); // returns some(42) | ||
const result: Option.Option<number> = pipe(option, Optic.getOption(_some)); // returns some(42) | ||
const updated: O.Option<number> = pipe(option, Optic.replace(_some)(23)); // returns some(23) | ||
const updated: Option.Option<number> = pipe(option, Optic.replace(_some)(23)); // returns some(23) | ||
``` | ||
@@ -557,0 +558,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
607
0
173951
74
1569