Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@fp-ts/optic

Package Overview
Dependencies
Maintainers
3
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fp-ts/optic - npm Package Compare versions

Comparing version 0.0.7 to 0.1.0

_mjs/data/Date.mjs

3

data/Chunk.d.ts

@@ -1,4 +0,1 @@

/**
* @since 1.0.0
*/
import type { Chunk } from "@fp-ts/data/Chunk";

@@ -5,0 +2,0 @@ import type { Optional, PolyPrism, Prism } from "@fp-ts/optic";

@@ -7,6 +7,6 @@ "use strict";

exports.tail = exports.index = exports.head = exports.cons = void 0;
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Either"));
var _Function = /*#__PURE__*/require("@fp-ts/core/Function");
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Option"));
var C = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Chunk"));
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Either"));
var _Function = /*#__PURE__*/require("@fp-ts/data/Function");
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Option"));
var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -16,2 +16,6 @@ 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); }

/**
* @since 1.0.0
*/
/**
* An optic that accesses the specified index of a `Chunk`.

@@ -22,3 +26,3 @@ *

*/
const index = i => Optic.optional(s => (0, _Function.pipe)(s, C.get(i), O.match(() => E.left(new Error(`hasIndex(${i})`)), E.right)), a => s => (0, _Function.pipe)(C.replaceOption(i, a)(s), O.match(() => E.left(new Error(`hasIndex(${i})`)), E.right)));
const index = i => Optic.optional(s => (0, _Function.pipe)(s, C.get(i), O.match(() => E.left(new Error(`Missing index ${i}`)), E.right)), a => s => (0, _Function.pipe)(C.replaceOption(i, a)(s), O.match(() => E.left(new Error(`Missing index ${i}`)), E.right)));
/**

@@ -31,3 +35,3 @@ * An optic that accesses the `Cons` case of a `Chunk`.

exports.index = index;
const cons = () => Optic.prism(s => (0, _Function.pipe)(C.tail(s), O.match(() => E.left(Error("isCons")), tail => E.right([C.unsafeHead(s), tail]))), ([head, tail]) => C.prepend(head)(tail));
const cons = () => Optic.prism(s => (0, _Function.pipe)(C.tail(s), O.match(() => E.left(new Error("Expected a non empty Chunk")), tail => E.right([C.unsafeHead(s), tail]))), ([head, tail]) => C.prepend(head)(tail));
/**

@@ -34,0 +38,0 @@ * @category constructors

/**
* @since 1.0.0
*/
import type { Either } from "@fp-ts/data/Either";
import type { Either } from "@fp-ts/core/Either";
import type { PolyPrism, Prism } from "@fp-ts/optic";

@@ -6,0 +6,0 @@ /**

@@ -7,3 +7,3 @@ "use strict";

exports.right = exports.left = void 0;
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Either"));
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Either"));
var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -17,3 +17,3 @@ 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 right = () => Optic.prism(E.mapLeft(() => Error("isRight")), E.right);
const right = () => Optic.prism(E.mapLeft(() => new Error("Expected a Right")), E.right);
/**

@@ -25,4 +25,4 @@ * An optic that accesses the `Left` case of an `Either`.

exports.right = right;
const left = () => Optic.prism(E.match(E.right, () => E.left(Error("isLeft"))), E.left);
const left = () => Optic.prism(E.match(E.right, () => E.left(new Error("Expected a Left"))), E.left);
exports.left = left;
//# sourceMappingURL=Either.js.map
/**
* @since 1.0.0
*/
import type { Option } from "@fp-ts/core/Option";
import type { HashMap } from "@fp-ts/data/HashMap";
import type { Option } from "@fp-ts/data/Option";
import type { At } from "@fp-ts/optic/typeclass/At";

@@ -7,0 +7,0 @@ import type { Index } from "@fp-ts/optic/typeclass/Index";

@@ -7,5 +7,5 @@ "use strict";

exports.getIndex = exports.getAt = void 0;
var _Function = /*#__PURE__*/require("@fp-ts/data/Function");
var _Function = /*#__PURE__*/require("@fp-ts/core/Function");
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Option"));
var HM = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/HashMap"));
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Option"));
var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -12,0 +12,0 @@ var _Index = /*#__PURE__*/require("@fp-ts/optic/typeclass/Index");

@@ -1,2 +0,2 @@

import type { Option } from "@fp-ts/data/Option";
import type { Option } from "@fp-ts/core/Option";
import type { Prism } from "@fp-ts/optic";

@@ -3,0 +3,0 @@ /**

@@ -7,4 +7,4 @@ "use strict";

exports.none = void 0;
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Either"));
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Option"));
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Either"));
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Option"));
var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -22,4 +22,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 none = () => Optic.prism(O.match(() => E.right(undefined), () => E.left(Error("isNone"))), () => O.none);
const none = () => Optic.prism(O.match(() => E.right(undefined), () => E.left(new Error("Expected a None"))), () => O.none());
exports.none = none;
//# sourceMappingURL=Option.js.map
/**
* @since 1.0.0
*/
import type { NonEmptyReadonlyArray } from "@fp-ts/data/ReadonlyArray";
import type { NonEmptyReadonlyArray } from "@fp-ts/core/ReadonlyArray";
import type { Iso, PolyIso } from "@fp-ts/optic";

@@ -6,0 +6,0 @@ /**

@@ -7,3 +7,3 @@ "use strict";

exports.consNonEmpty = void 0;
var RA = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/ReadonlyArray"));
var RA = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/ReadonlyArray"));
var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -10,0 +10,0 @@ 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); }

/**
* @since 1.0.0
*/
import type { Option } from "@fp-ts/data/Option";
import type { Option } from "@fp-ts/core/Option";
import * as SM from "@fp-ts/data/SortedMap";

@@ -6,0 +6,0 @@ import type { SortedMap } from "@fp-ts/data/SortedMap";

@@ -7,4 +7,4 @@ "use strict";

exports.getIndex = exports.getAt = void 0;
var _Function = /*#__PURE__*/require("@fp-ts/data/Function");
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Option"));
var _Function = /*#__PURE__*/require("@fp-ts/core/Function");
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Option"));
var SM = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/SortedMap"));

@@ -11,0 +11,0 @@ var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -10,3 +10,3 @@ /**

*/
export declare const index: (n: number) => Optional<string, string>;
export declare const index: (i: number) => Optional<string, string>;
//# sourceMappingURL=String.d.ts.map

@@ -7,3 +7,4 @@ "use strict";

exports.index = void 0;
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Either"));
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Either"));
var String = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/String"));
var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -16,3 +17,2 @@ 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 isChar = s => s.length > 0;
/**

@@ -23,12 +23,12 @@ * An optic that accesses the specified index of a `string`.

*/
const index = n => Optic.optional(s => n >= 0 && n < s.length ? E.right(s[n]) : E.left(new Error(`hasIndex(${n})`)), char => s => {
if (!isChar(char)) {
return E.left(new Error("isChar"));
const index = i => Optic.optional(s => i >= 0 && i < s.length ? E.right(s[i]) : E.left(new Error(`Missing index ${i}`)), char => s => {
if (String.isEmpty(char)) {
return E.left(new Error("Expected a non empty string"));
}
if (n >= 0 && n < s.length) {
return E.right(s.substring(0, n) + char.charAt(0) + s.substring(n + 1));
if (i >= 0 && i < s.length) {
return E.right(s.substring(0, i) + char.charAt(0) + s.substring(i + 1));
}
return E.left(new Error(`hasIndex(${n})`));
return E.left(new Error(`Missing index ${i}`));
});
exports.index = index;
//# sourceMappingURL=String.js.map

@@ -1,4 +0,1 @@

/**
* @since 1.0.0
*/
import type { Kind, TypeLambda } from "@fp-ts/core/HKT";

@@ -5,0 +2,0 @@ import type { Applicative } from "@fp-ts/core/typeclass/Applicative";

@@ -9,4 +9,4 @@ "use strict";

exports.zoom = void 0;
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Either"));
var _Function = /*#__PURE__*/require("@fp-ts/data/Function");
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Either"));
var _Function = /*#__PURE__*/require("@fp-ts/core/Function");
var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -18,2 +18,6 @@ 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); }

*/
/**
* @since 1.0.0
*/
const modifyApplicative = optic => F => f => s => (0, _Function.pipe)(optic.getOptic(s), E.match(([_, t]) => F.of(t), a => (0, _Function.pipe)(f(a), F.map(b => (0, _Function.pipe)(optic.setOptic(b)(s), E.match(([_, t]) => t, _Function.identity))))));

@@ -20,0 +24,0 @@ exports.modifyApplicative = modifyApplicative;

/**
* @since 1.0.0
*/
import type { Either } from "@fp-ts/data/Either";
import * as O from "@fp-ts/data/Option";
import type { Option } from "@fp-ts/data/Option";
import type { Predicate, Refinement } from "@fp-ts/data/Predicate";
import type { Either } from "@fp-ts/core/Either";
import * as O from "@fp-ts/core/Option";
import type { Option } from "@fp-ts/core/Option";
import type { Predicate, Refinement } from "@fp-ts/core/Predicate";
import type { ReadonlyRecord } from "@fp-ts/core/ReadonlyRecord";
/**

@@ -33,3 +34,5 @@ * @since 1.0.0

at<S, A, Key extends keyof A>(this: Lens<S, A>, key: Key): Lens<S, A[Key]>;
at<S, T, A, B, Key extends keyof A & keyof B>(this: PolyLens<S, T, A, B>, key: Key): PolyLens<S, T, A[Key], B[Key]>;
at<S, A, Key extends keyof A>(this: Optional<S, A>, key: Key): Optional<S, A[Key]>;
at<S, T, A, B, Key extends keyof A & keyof B>(this: PolyOptional<S, T, A, B>, key: Key): PolyOptional<S, T, A[Key], B[Key]>;
/**

@@ -62,6 +65,6 @@ * An optic that accesses a group of keys of a struct.

*/
filter<S, A extends B, C extends B, B = A>(this: Prism<S, A>, refinement: Refinement<B, C>): Prism<S, C>;
filter<S, A extends B, B = A>(this: Prism<S, A>, predicate: Predicate<B>): Prism<S, A>;
filter<S, A extends B, C extends B, B = A>(this: Optional<S, A>, refinement: Refinement<B, C>): Optional<S, C>;
filter<S, A extends B, B = A>(this: Optional<S, A>, predicate: Predicate<B>): Optional<S, A>;
filter<S, A extends B, C extends B, B = A>(this: Prism<S, A>, refinement: Refinement<B, C>, message?: string): Prism<S, C>;
filter<S, A extends B, B = A>(this: Prism<S, A>, predicate: Predicate<B>, message?: string): Prism<S, A>;
filter<S, A extends B, C extends B, B = A>(this: Optional<S, A>, refinement: Refinement<B, C>, message?: string): Optional<S, C>;
filter<S, A extends B, B = A>(this: Optional<S, A>, predicate: Predicate<B>, message?: string): Optional<S, A>;
/**

@@ -88,15 +91,7 @@ * An optic that accesses the `NonNullable` case of a nullable type.

/**
* An optic that accesses the specified key of an index signature.
* An optic that accesses the specified key of a record.
*
* @since 1.0.0
*/
key<S, A>(this: Optional<S, {
readonly [x: string]: A;
}>, key: string): Optional<S, A>;
key<S, A>(this: Optional<S, {
readonly [x: symbol]: A;
}>, key: symbol): Optional<S, A>;
key<S, A>(this: Optional<S, {
readonly [x: number]: A;
}>, key: number): Optional<S, A>;
key<S, A>(this: Optional<S, ReadonlyRecord<A>>, key: string): Optional<S, A>;
}

@@ -150,25 +145,4 @@ /**

/**
* An optic that accesses the specified key of a struct or a tuple.
*
* @category constructors
* @since 1.0.0
*/
export declare const at: <S, Key extends keyof S & (string | symbol)>(key: Key) => Lens<S, S[Key]>;
/**
* An optic that accesses a group of keys of a struct.
*
* @category constructors
* @since 1.0.0
*/
export declare const pick: <S, Keys extends readonly [keyof S, ...(keyof S)[]]>(...keys: Keys) => Lens<S, { readonly [K in Keys[number]]: S[K]; }>;
/**
* An optic that excludes a group of keys of a struct.
*
* @category constructors
* @since 1.0.0
*/
export declare const omit: <S, Keys extends readonly [keyof S, ...(keyof S)[]]>(...keys: Keys) => Lens<S, { readonly [K in Exclude<keyof S, Keys[number]>]: S[K]; }>;
/**
* @since 1.0.0
*/
export interface PolyPrism<in S, out T, out A, in B> extends Optic<S, unknown, B, Error, never, A, T> {

@@ -202,31 +176,4 @@ }

/**
* An optic that accesses the case specified by a predicate.
*
* @category constructors
* @since 1.0.0
*/
export declare const filter: {
<S extends A, B extends A, A = S>(refinement: Refinement<A, B>): Prism<S, B>;
<S extends A, A = S>(predicate: Predicate<A>): Prism<S, S>;
};
/**
* An optic that accesses the `NonNullable` case of a nullable type.
*
* @category constructors
* @since 1.0.0
*/
export declare const nonNullable: <S>() => Prism<S, NonNullable<S>>;
/**
* An optic that accesses the `Some` case of an `Option`.
*
* @category constructors
* @since 1.0.0
*/
export declare const some: {
<A>(): Prism<Option<A>, A>;
<A, B>(): PolyPrism<Option<A>, Option<B>, A, B>;
};
/**
* @since 1.0.0
*/
export interface PolyReversedPrism<in S, out T, out A, in B> extends Optic<S, S, B, never, Error, A, T> {

@@ -254,4 +201,4 @@ }

export declare const reversedFilter: {
<A, S extends A>(refinement: Refinement<A, S>): ReversedPrism<S, A>;
<S>(predicate: Predicate<S>): ReversedPrism<S, S>;
<A, S extends A>(refinement: Refinement<A, S>, message?: string): ReversedPrism<S, A>;
<S>(predicate: Predicate<S>, message?: string): ReversedPrism<S, S>;
};

@@ -279,9 +226,2 @@ /**

/**
* An optic that accesses the specified index of a `ReadonlyArray`.
*
* @category constructors
* @since 1.0.0
*/
export declare const index: <A>(i: number) => Optional<readonly A[], A>;
/**
* An optic that accesses all of the elements in a `ReadonlyArray`.

@@ -294,22 +234,5 @@ *

/**
* @since 1.0.0
*/
export interface IndexSignature<A> {
readonly [x: PropertyKey]: A;
}
declare const IndexSignature: {
get: (key: PropertyKey) => <A>(is: IndexSignature<A>) => O.Option<A>;
replaceOption: <A_1>(key: PropertyKey, a: A_1) => (is: IndexSignature<A_1>) => O.Option<IndexSignature<A_1>>;
};
/**
* An optic that accesses the specified key of an index signature.
*
* @category constructors
* @since 1.0.0
*/
export declare const key: <A>(key: PropertyKey) => Optional<IndexSignature<A>, A>;
/**
* @category constructors
* @since 1.0.0
*/
export declare const head: <A>() => Optional<readonly A[], A>;

@@ -328,4 +251,4 @@ /**

export declare const findFirst: {
<C extends A, B extends A, A = C>(refinement: Refinement<A, B>): Optional<ReadonlyArray<C>, B>;
<B extends A, A = B>(predicate: Predicate<A>): Optional<ReadonlyArray<B>, B>;
<C extends A, B extends A, A = C>(refinement: Refinement<A, B>, message?: string): Optional<ReadonlyArray<C>, B>;
<B extends A, A = B>(predicate: Predicate<A>, message?: string): Optional<ReadonlyArray<B>, B>;
};

@@ -404,3 +327,2 @@ /**

export declare const modify: <S, T, A, B>(optic: PolyOptional<S, T, A, B>) => (f: (a: A) => B) => (s: S) => T;
export {};
//# sourceMappingURL=index.d.ts.map

@@ -6,9 +6,46 @@ "use strict";

});
exports.traversal = exports.tail = exports.some = exports.reversedPrism = exports.reversedFilter = exports.replaceOption = exports.replace = exports.prism = exports.polyTraversal = exports.polyReversedPrism = exports.polyPrism = exports.polyOptional = exports.pick = exports.optional = exports.omit = exports.nonNullable = exports.modify = exports.lens = exports.key = exports.iso = exports.indexes = exports.index = exports.id = exports.head = exports.getOrModify = exports.getOption = exports.get = exports.findFirst = exports.filter = exports.encode = exports.decode = exports.cons = exports.at = void 0;
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Either"));
var _Function = /*#__PURE__*/require("@fp-ts/data/Function");
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Option"));
var RA = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/ReadonlyArray"));
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 E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Either"));
var _Function = /*#__PURE__*/require("@fp-ts/core/Function");
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Option"));
var RA = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/ReadonlyArray"));
var RR = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/ReadonlyRecord"));
var S = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Struct"));
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); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* Compose two optics when the piece of the whole returned by the get
* operator of the first optic is not needed by the set operator of the
* second optic.
*/
const prismComposition = that => self => new Builder("prism", getWhole => (0, _Function.pipe)(self.getOptic(getWhole), E.flatMap(getPiece => (0, _Function.pipe)(that.getOptic(getPiece), E.catchAll(([GetError1, SetPiece]) => (0, _Function.pipe)(self.setOptic(SetPiece)(getWhole), E.match(([_, SetWholeAfter]) => E.left([GetError1, SetWholeAfter]), SetWholeAfter => E.left([GetError1, SetWholeAfter]))))))), SetPiece1 => SetWholeBefore => (0, _Function.pipe)(that.setOptic(SetPiece1)(undefined), E.match(([SetError1, SetPiece]) => (0, _Function.pipe)(self.setOptic(SetPiece)(SetWholeBefore), E.match(([_, SetWholeAfter]) => E.left([SetError1, SetWholeAfter]), SetWholeAfter => E.left([SetError1, SetWholeAfter]))), SetPiece => self.setOptic(SetPiece)(SetWholeBefore))));
/**
* Compose two optics when the piece of the whole returned by the first
* optic is needed by the set operator of the second optic
*/
const lensComposition = that => self => new Builder("lens", s => (0, _Function.pipe)(self.getOptic(s), E.flatMap(a => (0, _Function.pipe)(that.getOptic(a), E.catchAll(([de, b]) => (0, _Function.pipe)(self.setOptic(b)(s), E.match(([_ee, t]) => E.left([de, t]), t => E.left([de, t]))))))), d => s => (0, _Function.pipe)(self.getOptic(s), E.flatMap(a => (0, _Function.pipe)(that.setOptic(d)(a), E.match(([ee, b]) => (0, _Function.pipe)(self.setOptic(b)(s), E.match(([_, t]) => E.left([ee, t]), t => E.left([ee, t]))), b => self.setOptic(b)(s))))));
const at = key => lens(s => s[key], b => s => {
if (Array.isArray(s)) {
const out = s.slice();
out[key] = b;
return out;
}
return {
...s,
[key]: b
};
});
const pick = (...keys) => lens(S.pick(...keys), a => s => ({
...s,
...a
}));
const omit = (...keys) => lens(S.omit(...keys), a => s => ({
...s,
...a
}));
const filter = (predicate, message) => prism(s => predicate(s) ? E.right(s) : E.left(new Error(message ?? "Expected a value satisfying the specified predicate")), _Function.identity);
const nonNullable = () => filter(s => s != null, "Expected a non nullable value");
const some = () => prism(O.match(() => E.left(new Error("Expected a Some")), E.right), O.some);
const index = i => optional(s => (0, _Function.pipe)(s, RA.get(i), O.match(() => E.left(new Error(`Missing index ${i}`)), E.right)), a => s => (0, _Function.pipe)(RA.replaceOption(i, a)(s), O.match(() => E.left(new Error(`Missing index ${i}`)), E.right)));
const key = key => optional(s => (0, _Function.pipe)(s, RR.get(key), O.match(() => E.left(new Error(`Missing key ${JSON.stringify(key)}`)), E.right)), a => s => (0, _Function.pipe)(s, RR.replaceOption(key, a), O.match(() => E.left(new Error(`Missing key ${JSON.stringify(key)}`)), E.right)));
class Builder {

@@ -32,4 +69,4 @@ constructor(composition, getOptic, setOptic) {

}
filter(predicate) {
return this.compose(filter(predicate));
filter(predicate, message) {
return this.compose(filter(predicate, message));
}

@@ -50,13 +87,2 @@ nonNullable() {

/**
* Compose two optics when the piece of the whole returned by the get
* operator of the first optic is not needed by the set operator of the
* second optic.
*/
const prismComposition = that => self => new Builder("prism", getWhole => (0, _Function.pipe)(self.getOptic(getWhole), E.flatMap(getPiece => (0, _Function.pipe)(that.getOptic(getPiece), E.catchAll(([GetError1, SetPiece]) => (0, _Function.pipe)(self.setOptic(SetPiece)(getWhole), E.match(([_, SetWholeAfter]) => E.left([GetError1, SetWholeAfter]), SetWholeAfter => E.left([GetError1, SetWholeAfter]))))))), SetPiece1 => SetWholeBefore => (0, _Function.pipe)(that.setOptic(SetPiece1)(undefined), E.match(([SetError1, SetPiece]) => (0, _Function.pipe)(self.setOptic(SetPiece)(SetWholeBefore), E.match(([_, SetWholeAfter]) => E.left([SetError1, SetWholeAfter]), SetWholeAfter => E.left([SetError1, SetWholeAfter]))), SetPiece => self.setOptic(SetPiece)(SetWholeBefore))));
/**
* Compose two optics when the piece of the whole returned by the first
* optic is needed by the set operator of the second optic
*/
const lensComposition = that => self => new Builder("lens", s => (0, _Function.pipe)(self.getOptic(s), E.flatMap(a => (0, _Function.pipe)(that.getOptic(a), E.catchAll(([de, b]) => (0, _Function.pipe)(self.setOptic(b)(s), E.match(([_ee, t]) => E.left([de, t]), t => E.left([de, t]))))))), d => s => (0, _Function.pipe)(self.getOptic(s), E.flatMap(a => (0, _Function.pipe)(that.setOptic(d)(a), E.match(([ee, b]) => (0, _Function.pipe)(self.setOptic(b)(s), E.match(([_, t]) => E.left([ee, t]), t => E.left([ee, t]))), b => self.setOptic(b)(s))))));
/**
* @category constructors

@@ -81,4 +107,2 @@ * @since 1.0.0

/**
* An optic that accesses the specified key of a struct or a tuple.
*
* @category constructors

@@ -88,59 +112,2 @@ * @since 1.0.0

exports.lens = lens;
const at = key => lens(s => s[key], b => s => {
if (Array.isArray(s)) {
const out = s.slice();
out[key] = b;
return out;
}
return {
...s,
[key]: b
};
});
// TODO: replace with @fp-ts/data/Struct
exports.at = at;
const Struct = {
pick: (...keys) => s => {
const out = {};
for (const k of keys) {
out[k] = s[k];
}
return out;
},
omit: (...keys) => s => {
const out = {
...s
};
for (const k of keys) {
delete out[k];
}
return out;
}
};
/**
* An optic that accesses a group of keys of a struct.
*
* @category constructors
* @since 1.0.0
*/
const pick = (...keys) => lens(Struct.pick(...keys), a => s => ({
...s,
...a
}));
/**
* An optic that excludes a group of keys of a struct.
*
* @category constructors
* @since 1.0.0
*/
exports.pick = pick;
const omit = (...keys) => lens(Struct.omit(...keys), a => s => ({
...s,
...a
}));
/**
* @category constructors
* @since 1.0.0
*/
exports.omit = omit;
const polyPrism = (polyDecode, encode) => new Builder("prism", polyDecode, b => _ => E.right(encode(b)));

@@ -160,33 +127,7 @@ /**

exports.prism = prism;
const cons = () => prism(s => RA.isNonEmpty(s) ? E.right([s[0], s.slice(1)]) : E.left(new Error("isNonEmpty")), ([head, tail]) => [head, ...tail]);
const cons = () => prism(s => RA.isNonEmpty(s) ? E.right([s[0], s.slice(1)]) : E.left(new Error("Expected a non empty array")), ([head, tail]) => [head, ...tail]);
/**
* An optic that accesses the case specified by a predicate.
*
* @category constructors
* @since 1.0.0
*/
exports.cons = cons;
const filter = predicate => prism(s => predicate(s) ? E.right(s) : E.left(new Error(predicate.name)), _Function.identity);
/**
* An optic that accesses the `NonNullable` case of a nullable type.
*
* @category constructors
* @since 1.0.0
*/
exports.filter = filter;
const nonNullable = () => filter(function isNonNullable(s) {
return s != null;
});
/**
* An optic that accesses the `Some` case of an `Option`.
*
* @category constructors
* @since 1.0.0
*/
exports.nonNullable = nonNullable;
const some = () => prism(O.match(() => E.left(new Error("isSome")), E.right), O.some);
/**
* @since 1.0.0
*/
exports.some = some;
const polyReversedPrism = (get, polyReplaceEither) => new Builder("prism", s => E.right(get(s)), polyReplaceEither);

@@ -205,3 +146,3 @@ /**

exports.reversedPrism = reversedPrism;
const reversedFilter = predicate => reversedPrism(_Function.identity, s => predicate(s) ? E.right(s) : E.left(new Error(predicate.name)));
const reversedFilter = (predicate, message) => reversedPrism(_Function.identity, s => predicate(s) ? E.right(s) : E.left(new Error(message ?? "Expected a value satisfying the specified predicate")));
/**

@@ -220,3 +161,3 @@ * @category constructors

/**
* An optic that accesses the specified index of a `ReadonlyArray`.
* An optic that accesses all of the elements in a `ReadonlyArray`.
*

@@ -227,38 +168,8 @@ * @category constructors

exports.optional = optional;
const index = i => optional(s => (0, _Function.pipe)(s, RA.get(i), O.match(() => E.left(new Error(`hasIndex(${i})`)), E.right)), a => s => (0, _Function.pipe)(RA.replaceOption(i, a)(s), O.match(() => E.left(new Error(`hasIndex(${i})`)), E.right)));
const indexes = () => traversal(E.right, as => s => E.right(as.concat(s.slice(as.length))));
/**
* An optic that accesses all of the elements in a `ReadonlyArray`.
*
* @category constructors
* @since 1.0.0
*/
exports.index = index;
const indexes = () => traversal(E.right, as => s => E.right(as.concat(s.slice(as.length))));
// TODO: replace with @fp-ts/data/ReadonlyRecord
exports.indexes = indexes;
const IndexSignature = {
get: key => is => Object.prototype.hasOwnProperty.call(is, key) ? O.some(is[key]) : O.none,
replaceOption: (key, a) => is => {
if (Object.prototype.hasOwnProperty.call(is, key)) {
const out = {
...is
};
out[key] = a;
return O.some(out);
}
return O.none;
}
};
/**
* An optic that accesses the specified key of an index signature.
*
* @category constructors
* @since 1.0.0
*/
const key = key => optional(s => (0, _Function.pipe)(s, IndexSignature.get(key), O.match(() => E.left(new Error(`hasKey(${String(key)})`)), E.right)), a => s => (0, _Function.pipe)(IndexSignature.replaceOption(key, a)(s), O.match(() => E.left(new Error(`hasKey(${String(key)})`)), E.right)));
/**
* @category constructors
* @since 1.0.0
*/
exports.key = key;
const head = () => cons().at("0");

@@ -278,3 +189,3 @@ /**

exports.tail = tail;
const findFirst = predicate => optional(s => (0, _Function.pipe)(s, RA.findFirst(predicate), E.fromOption(() => new Error(predicate.name))), a => s => (0, _Function.pipe)(s, RA.findFirstIndex(predicate), E.fromOption(() => new Error(predicate.name)), E.map(index => {
const findFirst = (predicate, message) => optional(s => (0, _Function.pipe)(s, RA.findFirst(predicate), E.fromOption(() => new Error(message ?? "Expected a value satisfying the specified predicate"))), a => s => (0, _Function.pipe)(s, RA.findFirstIndex(predicate), E.fromOption(() => new Error(message ?? "Expected a value satisfying the specified predicate")), E.map(index => {
const out = s.slice();

@@ -281,0 +192,0 @@ out[index] = a;

{
"name": "@fp-ts/optic",
"version": "0.0.7",
"version": "0.1.0",
"license": "MIT",

@@ -10,4 +10,4 @@ "repository": {

"dependencies": {
"@fp-ts/core": "~0.0.11",
"@fp-ts/data": "~0.0.39"
"@fp-ts/core": "~0.1.1",
"@fp-ts/data": "~0.1.0"
},

@@ -14,0 +14,0 @@ "main": "./index.js",

@@ -33,3 +33,3 @@ <h3 align="center">

- **Unified Representation Of Optics**. All optics compose the same way because they are all instances of the same data type (`Optic`)
- **Integration**. Built-in optics for `@fp-ts/data` data structures, like `Option`, `Either` and `Chunk`
- **Integration**. Built-in optics for `@fp-ts/core` data structures, like `Option` and `Either`.

@@ -40,4 +40,15 @@ ## Introduction

`@fp-ts/optic` features a unified representation of optics, deep `@fp-ts/data` integration, helpful error messages,
`@fp-ts/optic` features a unified representation of optics, deep `@fp-ts/core` integration, helpful error messages.
# Credits and sponsorship
This library was inspired by the following projects:
- [zio-optics](https://github.com/zio/zio-optics)
- [monocle-ts](https://github.com/gcanti/monocle-ts)
A huge thanks to my sponsors who made the development of `@fp-ts/optic` possible.
If you also want to **become a sponsor** to ensure this library continues to improve and receive maintenance, check out my [GitHub Sponsors profile](https://github.com/sponsors/gcanti?o=sd&sc=t)
## Requirements

@@ -48,13 +59,21 @@

```
{
// ...
"compilerOptions": {
// ...
"strict": true
}
}
```
## Getting started
To get started with `@fp-ts/optic`, you will need to install the library (**alpha** version) using npm or yarn:
To install the **alpha** version:
```
npm install @fp-ts/optic
npm install @fp-ts/schema
```
```
yarn add @fp-ts/optic
```
**Warning**. This package is primarily published to receive early feedback and for contributors, during this development phase we cannot guarantee the stability of the APIs, consider each release to contain breaking changes.

@@ -72,3 +91,3 @@ Once you have installed the library, you can import the necessary types and functions from the `@fp-ts/optic` module.

```ts
import * as O from "@fp-ts/data/Option";
import * as O from "@fp-ts/core/Option";

@@ -126,3 +145,3 @@ interface Street {

import * as StringOptic from "@fp-ts/optic/data/String";
import * as String from "@fp-ts/data/String";
import * as String from "@fp-ts/core/String";

@@ -307,3 +326,3 @@ const _firstChar: Optic.Optional<Employee, string> = Optic.id<Employee>()

```ts
import { pipe } from "@fp-ts/data/Function";
import { pipe } from "@fp-ts/core/Function";
import * as Optic from "@fp-ts/optic";

@@ -346,3 +365,3 @@

```ts
import { pipe } from "@fp-ts/data/Function";
import { pipe } from "@fp-ts/core/Function";
import * as Optic from "@fp-ts/optic";

@@ -385,3 +404,3 @@

```ts
import { pipe } from "@fp-ts/data/Function";
import { pipe } from "@fp-ts/core/Function";
import * as Optic from "@fp-ts/optic";

@@ -427,3 +446,3 @@

```ts
import { pipe } from "@fp-ts/data/Function";
import { pipe } from "@fp-ts/core/Function";
import * as Optic from "@fp-ts/optic";

@@ -468,5 +487,5 @@

```ts
import { pipe } from "@fp-ts/data/Function";
import { pipe } from "@fp-ts/core/Function";
import * as Optic from "@fp-ts/optic";
import type { Option } from "@fp-ts/data/Option";
import type { Option } from "@fp-ts/core/Option";

@@ -508,5 +527,5 @@ // This is the type of the data structure that the prism will be operating on.

```ts
import { pipe } from "@fp-ts/data/Function";
import { pipe } from "@fp-ts/core/Function";
import * as Optic from "@fp-ts/optic";
import type { Option } from "@fp-ts/data/Option";
import type { Option } from "@fp-ts/core/Option";

@@ -526,4 +545,4 @@ const _nonNullString: Optic.Prism<string | null, string> = Optic.id<

```ts
import { pipe } from "@fp-ts/data/Function";
import * as O from "@fp-ts/data/Option";
import { pipe } from "@fp-ts/core/Function";
import * as O from "@fp-ts/core/Option";
import * as Optic from "@fp-ts/optic";

@@ -548,5 +567,5 @@

```ts
import { pipe } from "@fp-ts/data/Function";
import { pipe } from "@fp-ts/core/Function";
import * as Optic from "@fp-ts/optic";
import type { Option } from "@fp-ts/data/Option";
import type { Option } from "@fp-ts/core/Option";

@@ -570,4 +589,4 @@ const _index2: Optic.Optional<ReadonlyArray<number>, number> = Optic.id<

```ts
import { pipe } from "@fp-ts/data/Function";
import type { Option } from "@fp-ts/data/Option";
import { pipe } from "@fp-ts/core/Function";
import type { Option } from "@fp-ts/core/Option";
import * as Optic from "@fp-ts/optic";

@@ -574,0 +593,0 @@

/**
* @since 1.0.0
*/
import type { Option } from "@fp-ts/data/Option";
import type { Option } from "@fp-ts/core/Option";
import type { Lens } from "@fp-ts/optic";

@@ -6,0 +6,0 @@ /**

@@ -7,3 +7,3 @@ "use strict";

exports.remove = void 0;
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Option"));
var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Option"));
var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -21,4 +21,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 remove = F => i => s => Optic.replace(F.at(i))(O.none)(s);
const remove = F => i => s => Optic.replace(F.at(i))(O.none())(s);
exports.remove = remove;
//# sourceMappingURL=At.js.map
/**
* @since 1.0.0
*/
import type { Option } from "@fp-ts/data/Option";
import type { Option } from "@fp-ts/core/Option";
import type { Optional } from "@fp-ts/optic";

@@ -6,0 +6,0 @@ import type { At } from "@fp-ts/optic/typeclass/At";

@@ -7,4 +7,4 @@ "use strict";

exports.fromAt = void 0;
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Either"));
var _Function = /*#__PURE__*/require("@fp-ts/data/Function");
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/core/Either"));
var _Function = /*#__PURE__*/require("@fp-ts/core/Function");
var Optic = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/optic"));

@@ -22,4 +22,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); }

index: i => {
const some = Optic.some();
return F.at(i).compose(Optic.prism(s => (0, _Function.pipe)(some.getOptic(s), E.mapLeft(() => new Error(`hasIndex(${i})`))), Optic.encode(some)));
const some = Optic.id().some();
return F.at(i).compose(Optic.prism(s => (0, _Function.pipe)(some.getOptic(s), E.mapLeft(() => new Error(`Missing key/index ${JSON.stringify(i)}`))), Optic.encode(some)));
}

@@ -26,0 +26,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

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

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

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

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc