https://github.com/spapas/poor-man-lens
An easy way to keep your objects immutable
https://github.com/spapas/poor-man-lens
lens object-drill-down
Last synced: about 2 months ago
JSON representation
An easy way to keep your objects immutable
- Host: GitHub
- URL: https://github.com/spapas/poor-man-lens
- Owner: spapas
- License: mit
- Created: 2018-04-04T19:55:28.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-05T09:10:54.000Z (over 7 years ago)
- Last Synced: 2025-06-04T04:28:55.471Z (5 months ago)
- Topics: lens, object-drill-down
- Language: JavaScript
- Homepage: https://spapas.github.io/2018/04/05/easy-immutable-objects/
- Size: 9.77 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# poor-man-lens
[](https://www.npmjs.org/package/poor-man-lens)
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
```
``` javascript
> 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
``` html
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}))
```