master
svinokot 6 months ago
parent 385b99c604
commit 7c91814d82
  1. 2
      package.json
  2. 7
      readme.md
  3. 8
      src/lens-js.js
  4. 23
      test/random.html

@ -1,6 +1,6 @@
{
"name": "@vovikilelik/lens-js",
"version": "1.6.3",
"version": "1.6.4",
"keywords": [
"util",
"functional",

@ -41,6 +41,13 @@ export const lens = LensUtils.createLens({ /* default data */ });
const deep = lens.go('deep');
const deeper = deep.go('deeper');
```
* Changing
```js
const catName = lens.go('cat').go('name');
catName.set('Tom'); // Set value
catName.get(); / Get value from node
```
* Singleton pattern able to use for each other node
```js
import {lens} from 'store';

@ -26,6 +26,10 @@ const _typeof = (value) => {
: typeof value;
};
const _getKeys = (data) => {
return Array.isArray(data) ? Object.keys(data) : Object.keys(data).sort();
};
const _getDiffs = (prev, next, path = [], diffs = []) => {
const prevType = _typeof(prev);
const nextType = _typeof(next);
@ -38,8 +42,8 @@ const _getDiffs = (prev, next, path = [], diffs = []) => {
switch (prevType) {
case 'object':
const prevKeys = Object.keys(prev);
const nextKeys = Object.keys(next);
const prevKeys = _getKeys(prev);
const nextKeys = _getKeys(next);
if (!_compareKeys(prevKeys, nextKeys)) {
diffs.push(new NodeDiff(path, prev, next));

@ -22,8 +22,29 @@ and open the template in the editor.
lens.go('test').set({ foo: null, moo: undefined });
lens.go('test').set(null);
console.log(lens.get());
const q1 = {
a: 1, b: 2, c: 3
};
const q2 = {
...q1, b: undefined
};
const q3 = {
a: 1, b: 2
};
lens.set(q1);
lens.attach((c) => console.log(c));
lens.go('b').attach((c) => console.log('b', c));
console.log('---q2---')
lens.set(q2);
console.log('---q3---')
lens.set(q3);
</script>
</body>
</html>

Loading…
Cancel
Save