changelog
ES6 LensesProxy-powered functional lenses for ECMAScript 2015+ & TypeScript projects (try it in your browser now!)
// Install: `npm i --save es6-lenses`const lens _ = const obj = x: y: 1 z: 2 // Flow / TypeScript alternative:// lens((_: typeof obj) => _.x.y)const xy = // Lenses can be called (== .get): // 1xy // 1// Set deep-clones the modified paths:xy // {x: {y: 10}, z: 2} // Shorthand selectors are also functions:x: 1 x: 2 // [1, 2] // Lenses can create nested structures:const y_z = y_z // ['y', {z: 'z'}]y_z// {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
- Functional Lenses, How Do They Work
- Lenses with Immutable.js
- Great pre-existing lens libraries (see npmjs):
About Proxies
-
Metaprogramming in ES6:
-
Proxy docs:
TODO
- Support filtered / mapped semantics:
lens(_ => _.addresses.map(_ => _.zipCode)).mapped.update(toUpperCase)
- More examples about tuple / object inputs & outputs.
- More tests, especially re/ integration with Immutable (cover interaction w/ Record)
Bundling with rollup
Notes:
- Can't down-compile to ES5 as we're using ES6's Proxy
- UglifyJS might require a special ES6-friendly branch: please report any success :-)
Setup:
-
Prerequisite:
npm --save-dev rollup npm --save-dev rollup-watch npm --save-dev rollup-plugin-typescript npm --save-dev rollup-plugin-node-resolve
-
Config (
rollup.config.js
):;;entry: './main.ts'format: 'iife'dest: 'out.js'sourceMap: trueplugins: -
Add the following scripts to your
package.json
:"scripts":"rollup": "rollup -c""rollup:w": "rollup -c -w"... -
Build with
npm run rollup
, continuously rebuild withnpm run rollup:w
Develop
npm inpm start