1.0.8 • Published 6 years ago

poor-man-lens v1.0.8

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

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>  
1.0.8

6 years ago

1.0.7

6 years ago

1.0.5

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago