Home

Awesome

ES6 Lenses Build Status npm version changelog

Proxy-powered functional lenses for ECMAScript 2015+ & TypeScript projects (try it in your browser now!)

// Install: `npm i --save es6-lenses`
const {lens, _} = require("es6-lenses")
const obj = {x: {y: 1}, z: 2}

// Flow / TypeScript alternative:
//   lens((_: typeof obj) => _.x.y)
const xy = lens(_.x.y)
// Lenses can be called (== .get):
xy(obj) // 1
xy.get(obj) // 1
// Set deep-clones the modified paths:
xy.set(obj, 10) // {x: {y: 10}, z: 2}

// Shorthand selectors are also functions:
[{x: 1}, {x: 2}].map(_.x) // [1, 2]

// Lenses can create nested structures:
const y_z = lens([_.x.y, {z: _.z}])
y_z.get(obj) // ['y', {z: 'z'}]
y_z.set(obj, ['yy', {z: 'zz'}])
// {x: {y: 'yy'}, z: 'zz'}

Note: .set deeply clones objects (and is Immutable.Map-aware), while .mutate attempts to modify them in-place.

About lenses

About Proxies

TODO

Bundling with rollup

Notes:

Setup:

Develop

npm i
npm start