Git Product home page Git Product logo

ts-lenses's Introduction

A simple utility library that implements functional Lenses - composable setter and getter pairs that focuses on particular nested properties of an object.


Usage

// Import lenses library
import * as lens from 'lenses'

// Define your lens
const myLens = lens.of("a.b.c")

// Use the lens to get or set a nested value
myLens.get( {a: {b: {c: 42}}} )     // returns 42
myLens.set( {a: {b: {c: 42}}}, 777) // returns {a: {b: {c: 777}}}

// missing properties are created
myLens.set({}, 777) // still returns {a: {b: {c: 777}}}

set() method is a pure function. It creates shallow copies of objects along the path.

Lens compositions

const fooBarBazLens = lens.composition(lens.of("foo"), lens.of("bar"), lens.of("baz"))  // or...
const fooBarBazLens = lens.of("foo.bar").compose(lens.of("baz"))

// lens compositions are associative:
const fooBarBazLens = lens.of("foo").compose(lens.of("bar.baz"))                        // is the same as...
const fooBarBazLens = lens.of("foo").compose(lens.of("bar").compose(lens.of("baz")))    // is the same as...
const fooBarBazLens = (lens.of("foo").compose(lens.of("bar"))).compose(lens.of("baz"))  // is the same as...
const fooBarBazLens = lens.of("foo").focus("bar").focus("baz")                          // is the same as...
const fooBarBazLens = lens.of("foo").focus("bar.baz")                                   // is the same as...
const fooBarBazLens = lens.of("foo.bar.baz")

Lens projections

Projections are lenses with arity > 1, defined as complex lenses. Projections point to multiple properties of an object at any levels of nesting. Projections can be nested.

const shortContactInfoProjection = 
    lens.projection(
        lens.of("name"),
        lens.of("surname"),
        lens.of("contacts").project(
            lens.of("tel"),
            lens.of("email")))

const personInfo = {
    name: "John",
    surname: "Smith",
    age: 30,
    sex: "male",
    otherPrivateInfo: {...},
    contacts: {
        tel: 111222333444,
        email: "[email protected]",
        address: "Earth"
    },
    otherInfo: {...}
}

shortContactInfoProjection.get(personInfo) // returns ["John", "Smith", 111222333444, "[email protected]"]
shortContactInfoProjection.set(personInfo, "John", "Smith", 111222333444, "[email protected]") // returns updated copy

Todo

  1. Associative projection composition. Currently a Projection can only be a terminal element in a Lens composition chain.

  2. [Optimization] Do not copy an object if the current value is equal to the updating value.

  3. Performance testing

  4. Typing

ts-lenses's People

Contributors

dependabot[bot] avatar wajda avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.