PNW-TechPros / js-natural-lenses

A JavaScript-native lens (and ancillary optics) system loosely based on Kmett-style lenses.
MIT License
3 stars 0 forks source link

JavaScript-Native Lenses

Automated tests status

While retrieving or setting a property of an Object in JavaScript is trivially easy to code, when the data structure is more complex than a single Object or single Array, the code gets trickier: each level of retrieval has to check that the container it is accessing actually exists in the subject data and, when setting, has to ensure that a container exists at each level on the way down to the slot to be set. Further complications arise when treating the data as immutable: each container to be "changed" must be cloned and the appropriate change made in the fresh clone returned.

Lenses address these problems by codifying the concept of "slots" within a data structure as objects separate from the data structure, but knowing how to operate upon it. They have a strong theoretical background and there are many ways to modify, combine, and use them. Additionally, datum plans (please see the tutorial in the documentation) offer a concise and intuitive way to build sets of useful lenses that maximize consistency when accessing structured data.

Thorough documentation is available.