common
common copied to clipboard
Implement Haskell lens package functions
Working with GlobalStorage means working with nested objects with deeply nested structures. Haskell solution for that is lenses. There is rambda-lens implementation for javascript. It's fully functional and so performance is only okay using with immutable-js. But we can rewrite functions using functionality and mutability for best results.
There are many functions in the lens so I give only a few examples in this issue. There is also an article about using rambda-lens.
Data:
const users = [{
id: 3,
name: 'Charles Bronson',
addresses: [{
street: '99 Walnut Dr.',
zip: '04821',
}, {
street: '2321 Crane Way',
zip: '08082',
}],
}, {
id: 5,
name: 'Mr. Ambassador',
addresses: [{
street: '2050 Bamako Place',
zip: '20521-2050',
}, {
street: '7100 Athens Place',
zip: '20521-7100',
}],
}];
Standard way:
users.forEach((_, i, arr) =>
arr[i]['addresses'].forEach((_, i, arr) =>
arr[i]['street'] = '*****'
)
);
Using lens:
const { set, lensProp, mapped } = require('metarhia-common');
set([mapped, lensProp('addresses'), mapped, lensProp('street')], '*****', users);