Coder Social home page Coder Social logo

pokato's Introduction

Pokato

npm version

A poor man's lens library for TypeScript.

Overview

This is a simple library with functional pseudo-lenses that you may know from languages like Haskell.

Note that there are many great lens-like libraries for JavaScript. If you use JavaScript - use them, not this one! They have much nicer API and are more general. This library was designed specifically to work nice with the TypeScript's type system.

The main issue with making Haskell-like functional lenses work in TypeScript is that its type inference algorithm is not strong enough. And while we can have working lens implementation, we would have to provide generic type annotations virtually everywhere which completely destroys the purpose of lenses - making our lives easier.

Therefore, we end up with this pseudo-lens library. The problem with these pseudo-lenses is that they are not very composable. However, it works just fine for my everyday use case - updating deeply nested values in a Redux store.

Usage

Let us assume you have a JSON like this:

let json = { foo: { bar: { baz: 42 }, quux: "norf" } }

Just import the focus function from this library:

import { focus } from "pokato";

and then use it like this:

focus(json).at("foo").at("bar").at("baz").get()
// => 42

focus(json).at("foo").at("quux").get()
// => "norf"

focus(json).at("foo").at("bar").at("baz").modify((x: number) => x * 2)
// => { foo: { bar: { baz: 84 }, quux: "norf" } }

focus(json).at("foo").at("quux").set("thud")
// => { foo: { bar: { baz: 42 }, quux: "thud" } }

For more usage samples and methods refer to the specification files.

Note that everything is type safe. That is, you can not do something like this:

focus(json).at("bar").get()
// compilation error (missing property "bar")

focus(json).at("foo").at("baz").get()
// compilation error (missing property "baz")

focus(json).at("foo").at("bar").at("baz").set("plugh")
// compilation error (incompatible types `number` and `string`)

You can also update multiple fields of a given object with the then operator:

focus({ foo: 1, bar: { baz: 2, quux: 3 } })
    .then($ => $.at("foo").modify(x => x + 10))
    .then($ => $.at("bar").at("baz").set(5))
    .unfocus()
// => { foo: 11, bar: { baz: 5, quux: 3 } }

Building

Simply use the build script (it should work both with npm and with yarn). There is also a simple Mocha specification that you can check using test script.

yarn build
yarn test
yarn lint

pokato's People

Contributors

panhania avatar

Stargazers

 avatar

Watchers

 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.