Socket
Socket
Sign inDemoInstall

lens.ts

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    lens.ts

TypeScript Lens implementation with object property proxy


Version published
Weekly downloads
1.6K
decreased by-24.07%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

lens.ts travis-ci

TypeScript Lens implementation with object property proxy

Lens?

Lens is composable abstraction of getter and setter. For more detail of Lens, I recommend reading the following documents.

Install

Via npm:

$ npm i lens.ts

Usage

type Person = {
    name: string,
    age: number,
    accounts: Accounts,
};

type Accounts = {
    twitter?: string,
    facebook?: string,
};

const azusa: Person = {
    name: 'Nakano Azusa',
    age: 15,
    accounts: {
        twitter: '@azusa',
    },
};

// create a identity lens for Person
const personL = id<Person>();

// it automatically generates property lenses via Proxy
personL.name; // :: Lens<Person, string>
personL.accounts; // :: Lens<Person, Accounts>
personL.accounts.twitter; // :: Lens<Person, string>

// a key lens can be created manually
key<Person>()('name'); // the same as personL.name

// lenses can be composed via `._()`
key<Person>()('accounts')._(key<Accounts>()('twitter'));

// get, set and update with lens
getl(personL.accounts.twitter)(azusa); // -> '@azusa'

setl(
    personL.name,
    '中野梓',
)(azusa); // -> { name: '中野梓', ... }

updatel(
    personL.age,
    x => x + 1,
)(azusa); // -> { age: 16, ... }

You can find the same code in test/test.ts

API

lens.ts exports the followings:

import {
    Lens,
    LensInternal,
    lens,
    id,
    key,
    getl,
    setl,
    updatel,
} from 'lens.ts';

Lens

The type Lens is just LensInternal + property proxy.

type Lens<T, U> = {
    readonly [K in keyof U]: Lens<T, U[K]>;
} & LensInternal<T, U>;

Example:

let lens1: Lens<T, U>;
lens.prop1; // .prop1 is also a lens from T to prop1 of U

LensInternal

LensInternal provides the compose function _() for two lenses.

class LensInternal<T, U> {
    _<V>(other: Lens<U, V>): Lens<T, V>;
}

Example:

let lens1: Lens<T, U>;
let lens2: Lens<U, V>;

lens1._(lens2); // :: Lens<T, V>

lens

Create a lens with a getter and a setter.

function lens<T, U>(
    get: (from: T) => U,
    set: (val: U) => (from: T) => T,
): Lens<T, U>;

id

Create an identity lens with an object type.

function id<T>(): Lens<T, T>;

Example:

let personL = id<Person>();

key

Create a key lens for an object.

function key<T>(): <K extends keyof T>(k: K): Lens<T, T[K]>;

Example:

let personNameL = key<Person>()('name');

getl

getl is for get lens. It retrives a value from a lens with a target object.

function getl<T, U>(lens: Lens<T, U>): (target: T) => U;

Example:

let p: Person = { name: 'Azusa' };
getl(personNameL)(p); // -> 'Azusa'

setl

setl is for set lens. It sets a value of a lens with a target object.

function setl<T, U>(lens: Lens<T, U>, val: U): (target: T) => T

Example:

setl(personNameL, 'Yui')(p); // -> { name: 'Yui' }

updatel

updatel is for update lens. It updates a value of a lens by a function, with a target object.

function updatel<T, U>(lens: Lens<T, U>, f: (val: U) => U): (target: T) => T

Example:

updatel(personNameL, name => 'Hirasawa ' + name)(p); // -> { name: Hirasawa Yui }

License

MIT

Keywords

FAQs

Last updated on 22 Aug 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc