Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/iagobelo/ts-loupe

Composable getters and setters.
https://github.com/iagobelo/ts-loupe

functional-programming lens typescript

Last synced: about 4 hours ago
JSON representation

Composable getters and setters.

Awesome Lists containing this project

README

        

# TS Loupe

[![Build Status](https://travis-ci.org/iagobelo/ts-loupe.svg?branch=master)](https://travis-ci.org/iagobelo/ts-loupe)
[![License](https://badgen.net/github/license/iagobelo/ts-loupe)](./LICENSE)
[![Library minified size](https://badgen.net/bundlephobia/min/ts-loupe)](https://bundlephobia.com/result?p=ts-loupe)
[![Library minified + gzipped size](https://badgen.net/bundlephobia/minzip/ts-loupe)](https://bundlephobia.com/result?p=ts-loupe)

## About

Lenses is a pattern used to read and update properties within an object.

## Installation

This library is published in the NPM registry and can be installed using any compatible package manager.

```sh
npm install ts-loupe --save

# For Yarn, use the command below.
yarn add ts-loupe
```

### Installation from CDN

This module has an UMD bundle available through JSDelivr and Unpkg CDNs.

```html

```

## API

- [Lens](#lens)
- [View](#view)
- [Set](#set)
- [Over](#over)
- [Prop](#prop)
- [Compose](#compose)

### Lens

Is a pair of two functions that abstracts the way of we access the requested field. In other words is a focus into some data structure.

Signature:

```typescript
type Getter = (data: A) => B;

type Setter = (value: B) => (data: A) => A;

interface Lens {
get: Getter
;
set: Setter
;
}
```

Example:

```typescript
type User = { name: string; age: number };

const user: User = { name: 'Jerry Lee', age: 18 };

const getName = (user: User) => user.name;
const setName = (name: User['name']) => (data: User) => ({ ...data, name });

const nameLens = lens(getName, setName);

nameLens.get(user); // returns: "Jerry Lee".
nameLens.set('Leon Lan')(user); // returns: A new user with Leon Lan as the name.
```

### View

Returns the data structure pointed to by the lens getter function.

Signature:

```typescript
type LensView = (lens: Pick, 'get'>) => (data: O) => V;
```

Example:

```typescript
type User = { name: string };

const user: User = { name: 'Len Lon' };

const getName = (user: User) => user.name;
const setName = (name: User['name']) => (data: User) => ({ ...data, name });

const nameLens = lens(getName, setName);

view(nameLens)(user); //returns: "Len Lon".
```

### Set

Set is used to 'set' a value into the data structure pointed to by the lens setter function.

Signature:

```typescript
type LensSet = (
lens: Pick, 'set'>
) => (value: V) => (data: O) => O;
```

Example:

```typescript
type User = { name: string };

const user: User = { name: 'Jackie Chan' };

const getName = (user: User) => user.name;
const setName = (name: User['name']) => (data: User) => ({ ...data, name });
const nameLens = lens(getName, setName);

lensSet(nameLens)('John Wick')(user); // returns: A new user with "John Wick" as the name.
```

### Over

Applies the function to the given lens property and returns the result.

Signature:

```typescript
type LensOver = (
lens: Lens
) => (fn: (value: V) => V) => (data: O) => O;
```

Example:

```typescript
type User = { name: string };

const user: User = { name: 'Santino' };
const nameLens = prop('name');

over(nameLens)(name => `${name} D'Antonio`)(user); // returns: A new user with "Santino D'Antonio" as the name.
```

### Prop

Creates a lens focused on a given property.

Signature:

```typescript
type LensProp = (key: K) => Lens;
```

Example:

```typescript
type User = { age: string; name: string };
lensProp('age'); // returns: A lens instance focused on the age propertie.
```

### Compose

Compose two lenses `Lens` and `Lens`, to produce a new lens `Lens`.

Signature:

```typescript
interface LensCompose {
(...lenses: [Lens, Lens]): Lens;
}
```

Example:

```typescript
type User = {
pocket: {
money: number;
};
};

const user: User = {
pocket: {
money: 3213
}
};

const pocketLens = lensProp('pocket');
const moneyLens = lensProp('money');

compose(
pocketLens,
moneyLens
); // returns: A new lens focused on money propertie.
```

## License

Released under [MIT License](./LICENSE).