npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

es6-lenses

v0.4.1

Published

Functional lenses for ES6

Downloads

19

Readme

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

  • Metaprogramming in ES6:

    • http://www.2ality.com/2014/12/es6-proxies.html
    • https://ponyfoo.com/articles/es6-proxy-traps-in-depth
    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Meta_programming
  • Proxy docs:

    • https://github.com/tvcutsem/harmony-reflect/blob/master/doc/traps.md
    • https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es2015.proxy.d.ts

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):

    import typescript from 'rollup-plugin-typescript';
    import nodeResolve from 'rollup-plugin-node-resolve';
    
    export default {
      entry: './main.ts',
      format: 'iife',
      dest: 'out.js',
      sourceMap: true,
      plugins: [
        nodeResolve({jsnext: true, main: true}),
        typescript({
          typescript: require('typescript')
        }),
      ]
    }
  • Add the following scripts to your package.json:

    {
      "scripts": {
        "rollup": "rollup -c",
        "rollup:w": "rollup -c -w"
      },
      ...
    }
  • Build with npm run rollup, continuously rebuild with npm run rollup:w

Develop

npm i
npm start