Socket
Socket
Sign inDemoInstall

@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.6 to 0.0.7

6

experimental.d.ts

@@ -54,8 +54,2 @@ /**

};
/**
* An optic that accesses a group of keys of a struct.
*
* @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]; }>;
//# sourceMappingURL=experimental.d.ts.map

18

experimental.js

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

exports.path = path;
exports.zoom = exports.pick = void 0;
exports.zoom = void 0;
var E = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@fp-ts/data/Either"));

@@ -55,19 +55,3 @@ var _Function = /*#__PURE__*/require("@fp-ts/data/Function");

};
/**
* An optic that accesses a group of keys of a struct.
*
* @since 1.0.0
*/
exports.zoom = zoom;
const pick = (...keys) => Optic.lens(s => {
const out = {};
for (const k of keys) {
out[k] = s[k];
}
return out;
}, a => s => ({
...s,
...a
}));
exports.pick = pick;
//# sourceMappingURL=experimental.js.map

79

index.d.ts

@@ -32,5 +32,27 @@ /**

*/
at<S, A, Key extends keyof A & (string | symbol)>(this: Lens<S, A>, key: Key): Lens<S, A[Key]>;
at<S, A, Key extends keyof A & (string | symbol)>(this: Optional<S, A>, key: Key): Optional<S, A[Key]>;
at<S, A, Key extends keyof A>(this: Lens<S, A>, key: Key): Lens<S, A[Key]>;
at<S, A, Key extends keyof A>(this: Optional<S, A>, key: Key): Optional<S, A[Key]>;
/**
* An optic that accesses a group of keys of a struct.
*
* @since 1.0.0
*/
pick<S, A, Keys extends readonly [keyof A, ...Array<keyof A>]>(this: Lens<S, A>, ...keys: Keys): Lens<S, {
readonly [K in Keys[number]]: A[K];
}>;
pick<S, A, Keys extends readonly [keyof A, ...Array<keyof A>]>(this: Optional<S, A>, ...keys: Keys): Optional<S, {
readonly [K in Keys[number]]: A[K];
}>;
/**
* An optic that excludes a group of keys of a struct.
*
* @since 1.0.0
*/
omit<S, A, Keys extends readonly [keyof A, ...Array<keyof A>]>(this: Lens<S, A>, ...keys: Keys): Lens<S, {
readonly [K in Exclude<keyof A, Keys[number]>]: A[K];
}>;
omit<S, A, Keys extends readonly [keyof A, ...Array<keyof A>]>(this: Optional<S, A>, ...keys: Keys): Optional<S, {
readonly [K in Exclude<keyof A, Keys[number]>]: A[K];
}>;
/**
* An optic that accesses the case specified by a predicate.

@@ -64,2 +86,16 @@ *

index<S, A>(this: Optional<S, ReadonlyArray<A>>, n: number): Optional<S, A>;
/**
* An optic that accesses the specified key of an index signature.
*
* @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>;
}

@@ -120,4 +156,18 @@ /**

/**
* 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> {

@@ -233,5 +283,29 @@ }

/**
* An optic that accesses all of the elements in a `ReadonlyArray`.
*
* @category constructors
* @since 1.0.0
*/
export declare const indexes: <A>() => Traversal<readonly A[], A>;
/**
* @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>;

@@ -325,2 +399,3 @@ /**

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,3 +6,3 @@ "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.optional = exports.nonNullable = exports.modify = exports.lens = exports.iso = 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;
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"));

@@ -26,2 +26,8 @@ var _Function = /*#__PURE__*/require("@fp-ts/data/Function");

}
pick(...keys) {
return this.compose(pick(...keys));
}
omit(...keys) {
return this.compose(omit(...keys));
}
filter(predicate) {

@@ -39,2 +45,5 @@ return this.compose(filter(predicate));

}
key(k) {
return this.compose(key(k));
}
}

@@ -89,7 +98,48 @@ /**

});
// 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
*/
exports.at = at;
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)));

@@ -175,2 +225,4 @@ /**

/**
* An optic that accesses all of the elements in a `ReadonlyArray`.
*
* @category constructors

@@ -180,2 +232,30 @@ * @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");

@@ -182,0 +262,0 @@ /**

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

@@ -11,3 +11,3 @@ "repository": {

"@fp-ts/core": "~0.0.11",
"@fp-ts/data": "~0.0.28"
"@fp-ts/data": "~0.0.39"
},

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

@@ -17,13 +17,2 @@ <h3 align="center">

`@fp-ts/optic` is a library that makes it easy to modify parts of larger data structures based on a single representation of an optic as a combination of a getter and setter.
`@fp-ts/optic` features a unified representation of optics, deep `@fp-ts/data` integration, helpful error messages,
# Features
- **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 `List` and `Chunk`
# Optics
```mermaid

@@ -42,2 +31,36 @@ flowchart TD

# Features
- **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`
## Introduction
`@fp-ts/optic` is a library that makes it easy to modify parts of larger data structures based on a single representation of an optic as a combination of a getter and setter.
`@fp-ts/optic` features a unified representation of optics, deep `@fp-ts/data` integration, helpful error messages,
## Requirements
- TypeScript 4.7 or newer
- The `strict` flag enabled in your `tsconfig.json` file
## Getting started
To get started with `@fp-ts/optic`, you will need to install the library (**alpha** version) using npm or yarn:
```
npm install @fp-ts/optic
```
```
yarn add @fp-ts/optic
```
Once you have installed the library, you can import the necessary types and functions from the `@fp-ts/optic` module.
```ts
import * as Optic from "@fp-ts/optic";
```
# Summary

@@ -51,16 +74,16 @@

interface Street {
num: number;
name: O.Option<string>;
readonly num: number;
readonly name: O.Option<string>;
}
interface Address {
city: string;
street: Street;
readonly city: string;
readonly street: Street;
}
interface Company {
name: string;
address: Address;
readonly name: string;
readonly address: Address;
}
interface Employee {
name: string;
company: Company;
readonly name: string;
readonly company: Company;
}

@@ -224,7 +247,10 @@

| Lens | lens | `S => A`, `A => S => S` | `Lens<S, A>` |
| | at | `Key` | `Iso<S, S[Key]>` |
| | at | `Key` | `Lens<S, S[Key]>` |
| | pick | `Key` | `Lens<S, Pick<S, Key>>` |
| | omit | `Key` | `Lens<S, Omit<S, Key>>` |
| Prism | prism | `S => Either<Error, A>`, `A => S` | `Prism<S, A>` |
| | polyPrism | `S => Either<[Error, T], A>`, `B => T` | `PolyPrism<S, T, A, B>` |
| | cons | | `Prism<A[], [A, A[]]>` |
| | nonNullable | | `Prism<S, NonNullable<S>>` |
| | nonNullable | | `Prism<A, NonNullable<A>>` |
| | some | | `Prism<Option<A>, A>` |
| | filter | `Predicate<S>` | `Prism<S, S>` |

@@ -235,2 +261,3 @@ | | filter | `Refinement<S, A>` | `Prism<S, A>` |

| | index | `number` | `Optional<A[], A>` |
| | key | `string` | `Optional<{ []: A }, A>` |
| | head | | `Optional<A[], A>` |

@@ -254,10 +281,306 @@ | | tail | | `Optional<A[], A[]>` |

# Installation
# Basic usage
To install the **alpha** version:
## id
The `id` optic is a special optic that represents the identity function, which simply returns its input unchanged. It can be thought of as a "base case" for optics, from which more complex optics can be built.
The `id` optic is defined as a singleton type, meaning that there is only one possible value for it. This makes it easy to use as a starting point for building larger optics, as it does not require any arguments or configuration.
```ts
import * as Optic from "@fp-ts/optic";
interface Whole {
readonly a: string;
readonly b: number;
readonly c: boolean;
}
// create an iso that focuses on the 'Whole' data structure
const _a: Optic.Iso<Whole, Whole> = Optic.id<Whole>();
```
npm install @fp-ts/optic
## compose
The `compose` method is a utility function that allows you to combine two or more optics into a single optic.
```ts
import { pipe } from "@fp-ts/data/Function";
import * as Optic from "@fp-ts/optic";
// This is the type of the data structure that the lens will be operating on.
interface Whole {
readonly a: string;
readonly b: number;
readonly c: boolean;
}
// This creates a lens that focuses on the 'a' field within the 'Whole' object.
const _a: Optic.Lens<Whole, string> =
// The 'id' function creates an identity lens for the 'Whole' type.
Optic.id<Whole>()
// The 'compose' method combines the identity lens with an 'at' lens,
// which selects the 'a' field within the 'Whole' object.
.compose(Optic.at("a"));
// Now we can use the '_a' lens to view and modify the 'a' field of a 'Whole' object.
const whole: Whole = {
a: "foo",
b: 42,
c: true,
};
// Use the 'get' function to view the 'a' field of the 'whole' object.
const result: string = pipe(whole, Optic.get(_a)); // returns "foo"
// Use the 'replace' function to update the 'a' field of the 'whole' object.
const updated: Whole = pipe(whole, Optic.replace(_a)("bar")); // returns { a: "bar", b: 42, c: true }
```
## at
The `at` method is a utility function that creates an optic that focuses on a specific field within a data structure.
```ts
import { pipe } from "@fp-ts/data/Function";
import * as Optic from "@fp-ts/optic";
// This is the type of the data structure that the lens will be operating on.
interface Whole {
readonly a: string;
readonly b: number;
readonly c: boolean;
}
// This creates a lens that focuses on the 'a' field within the 'Whole' object.
const _a: Optic.Lens<Whole, string> =
// The 'id' function creates an identity lens for the 'Whole' type.
Optic.id<Whole>()
// The 'at' method selects the 'a' key within the 'Whole' object,
// resulting in a lens that is focused on that field.
.at("a");
// Now we can use the '_a' lens to view and modify the 'a' field of a 'Whole' object.
const whole: Whole = {
a: "foo",
b: 42,
c: true,
};
// Use the 'get' function to view the 'a' field of the 'whole' object.
const result: string = pipe(whole, Optic.get(_a)); // returns "foo"
// Use the 'replace' function to update the 'a' field of the 'whole' object.
const updated: Whole = pipe(whole, Optic.replace(_a)("bar")); // returns { a: "bar", b: 42, c: true }
```
## pick
The `pick` method is a utility function that creates an optic that focuses on a group of keys within a data structure.
```ts
import { pipe } from "@fp-ts/data/Function";
import * as Optic from "@fp-ts/optic";
// This is the type of the data structure that the lens will be operating on.
interface Whole {
readonly a: string;
readonly b: number;
readonly c: boolean;
}
// This creates a lens that focuses on the 'a' and 'b' fields within the 'Whole' object.
const _ab: Optic.Lens<Whole, { readonly a: string; readonly b: number }> =
// The 'id' function creates an identity lens for the 'Whole' type.
Optic.id<Whole>()
// The 'pick' method selects the 'a' and 'b' keys within the 'Whole' object,
// resulting in a lens that is focused on those fields.
.pick("a", "b");
// Now we can use the '_ab' lens to view and modify the 'a' and 'b' fields of a 'Whole' object.
const whole: Whole = {
a: "foo",
b: 42,
c: true,
};
// Use the 'get' function to view the 'a' and 'b' fields of the 'whole' object.
const result: { readonly a: string; readonly b: number } = pipe(
whole,
Optic.get(_ab)
); // returns { a: "foo", b: 42 }
// Use the 'replace' function to update the 'a' and 'b' fields of the 'whole' object.
const updated: Whole = pipe(whole, Optic.replace(_ab)({ a: "bar", b: 23 })); // returns { a: "bar", b: 23, c: true }
```
## omit
The `omit` method is a utility function that creates a lens that excludes a group of keys from a struct. This can be useful when you want to focus on a subset of a data structure and ignore certain fields.
```ts
import { pipe } from "@fp-ts/data/Function";
import * as Optic from "@fp-ts/optic";
interface Whole {
readonly a: string;
readonly b: number;
readonly c: boolean;
}
// This creates a lens that focuses on the 'a' and 'c' fields within the 'Whole' object.
const _ac: Optic.Lens<Whole, { readonly a: string; readonly c: boolean }> =
// The 'id' function creates an identity lens for the 'Whole' type.
Optic.id<Whole>()
// The 'omit' method excludes the 'b' key within the 'Whole' object,
// resulting in a lens that is focused on the 'a' and 'c' fields.
.omit("b");
// Now we can use the '_ac' lens to view and modify the 'a' and 'c' fields of a 'Whole' object.
const whole: Whole = {
a: "foo",
b: 42,
c: true,
};
// Use the 'get' function to view the 'a' and 'c' fields of the 'whole' object.
const result: { readonly a: string; readonly c: boolean } = pipe(
whole,
Optic.get(_ac)
); // returns { a: "foo", c: true }
// Use the 'replace' function to update the 'a' and 'c' fields of the 'whole' object.
const updated: Whole = pipe(whole, Optic.replace(_ac)({ a: "bar", c: false })); // returns { a: "bar", b: 42, c: false }
```
## filter
The `filter` method is a utility function that creates an optic that focuses on the elements of a data structure that match a specified predicate.
```ts
import { pipe } from "@fp-ts/data/Function";
import * as Optic from "@fp-ts/optic";
import type { Option } from "@fp-ts/data/Option";
// This is the type of the data structure that the prism will be operating on.
interface Whole {
readonly a: number;
}
// This creates an `Optional` that focuses on the 'a' field within the 'Whole' object,
// and only includes values that are even numbers.
const _evenA: Optic.Optional<Whole, number> =
// The 'id' function creates an identity prism for the 'Whole' type.
Optic.id<Whole>()
// The 'at' method selects the 'a' key within the 'Whole' object,
// resulting in a `Lens` that is focused on that field.
.at("a")
// The 'filter' method only includes values that are even numbers.
.filter((a) => a % 2 === 0);
// Now we can use the '_evenA' `Optional` to view and modify the 'a' field of a 'Whole' object,
// but only if the value is an even number.
const whole: Whole = {
a: 2,
};
// Use the 'getOption' function to view the 'a' field of the 'whole' object.
const result: Option<number> = pipe(whole, Optic.getOption(_evenA)); // returns some(2)
// Use the 'replace' function to update the 'a' field of the 'whole' object.
const updated: Whole = pipe(whole, Optic.replace(_evenA)(4)); // returns { a: 4 }
```
## nonNullable
The `nonNullable` method is a utility function that creates a `Prism` that focuses on the non-nullable values of a nullable type. This is useful when you want to manipulate or extract the value of a nullable type, but want to ignore the `null` values.
```ts
import { pipe } from "@fp-ts/data/Function";
import * as Optic from "@fp-ts/optic";
import type { Option } from "@fp-ts/data/Option";
const _nonNullString: Optic.Prism<string | null, string> = Optic.id<
string | null
>().nonNullable();
const result1: Option<string> = pipe("foo", Optic.getOption(_nonNullString)); // returns some("foo")
const result2: Option<string> = pipe(null, Optic.getOption(_nonNullString)); // returns none
```
## some
The `some` method is a utility function that creates an optic that focuses on the `Some` case of an `Option` data type. This optic allows you to view and modify the value contained within the `Some` case of an `Option`.
```ts
import { pipe } from "@fp-ts/data/Function";
import * as O from "@fp-ts/data/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>
>().some();
const option: O.Option<number> = O.some(42);
const result: O.Option<number> = pipe(option, Optic.getOption(_some)); // returns some(42)
const updated: O.Option<number> = pipe(option, Optic.replace(_some)(23)); // returns some(23)
```
## index
The `index` method creates an `Optional` optic that focuses on a specific index in a `ReadonlyArray`. The `Optional` optic allows you to view the value at the specified index, or `None` if the index does not exist. You can also use the `Optional` optic to update the value at the specified index, if it exists.
```ts
import { pipe } from "@fp-ts/data/Function";
import * as Optic from "@fp-ts/optic";
import type { Option } from "@fp-ts/data/Option";
const _index2: Optic.Optional<ReadonlyArray<number>, number> = Optic.id<
ReadonlyArray<number>
>().index(2);
const arr: ReadonlyArray<number> = [1, 2, 3, 4];
const result: Option<number> = pipe(arr, Optic.getOption(_index2)); // some(3)
const updated1: ReadonlyArray<number> = pipe(arr, Optic.replace(_index2)(42)); // [1, 2, 42, 4]
const updated2: ReadonlyArray<number> = pipe(arr, Optic.replace(_index2)(10)); // [1, 2, 10, 4]
const updated3: ReadonlyArray<number> = pipe([], Optic.replace(_index2)(10)); // []
```
## key
The `key` method is a utility function that allows you to create an `Optional` optic that focuses on a specific key of an index signature (a type with a string index signature).
```ts
import { pipe } from "@fp-ts/data/Function";
import type { Option } from "@fp-ts/data/Option";
import * as Optic from "@fp-ts/optic";
interface Data {
readonly [key: string]: number;
}
// Create an Optional optic that focuses on the 'foo' key.
const _foo: Optic.Optional<Data, number> = Optic.id<Data>().key("foo");
const data: Data = {
foo: 1,
bar: 2,
};
// Use the 'getOption' function to view the value of the 'foo' key.
const fooValue: Option<number> = pipe(data, Optic.getOption(_foo)); // returns some(1)
// Use the 'replace' function to update the value of the 'foo' key.
const updatedData: Data = pipe(data, Optic.replace(_foo)(10)); // returns { foo: 10, bar: 2 }
```
# Documentation

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

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