poor-man-lens

1.0.8 • Public • Published

poor-man-lens

NPM

A simple util to help you keep your objects immutable. Contains two functions:

  • pget(object, path) that will return the value of the object at that path - path could be an array of indeces or a string index in the form of 'a.b.c'
  • pset(object, path, value) that will set the value of the object at that path to value - path is as above, value can be either a value or a function that will be applied to the object's value at that index

Usage with require (node.js)

npm install poor-man-lens
> var pml = require('poor-man-lens')

> console.log(pml.pget({'a':[{'b':2}]}, 'a.0'))
{ b: 2 }

> console.log(pml.pset({'a':[{'b':2}]}, 'a.1', {'c':3}))
{ a: [ { b: 2 }, { c: 3 } ] }

> console.log(pml.pset({'a':[{'b':2}]}, 'a.0.b', function(x) { return x*x } ))
{ a: [ { b: 4 } ] }

Usage within browser

<script type="text/javascript" src='pml.js'></script>
<script>
  var pml = PoorManLens
  console.log(pml.pget({'a':[{'b':2}]}, 'a.0'))
  console.log(pml.pset({'a':[{'b':2}]}, 'a.1', {'c':3}))
  console.log(pml.pset([[[[1]]]], [0,0,0,0], function(x) {return x+11}))
</script>  

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i poor-man-lens

      Weekly Downloads

      1

      Version

      1.0.8

      License

      MIT

      Unpacked Size

      6.48 kB

      Total Files

      6

      Last publish

      Collaborators

      • spapas