Composable, immutable getters and setters for nested data structures in JavaScript.
Fantasy Lenses is a JavaScript library that provides composable, immutable getters and setters for nested data structures. It solves the problem of updating deeply nested properties in a functional way by returning new copies of data instead of mutating them directly. This makes it easier to manage state changes in applications that follow functional programming principles.
JavaScript developers working with functional programming patterns, immutable data structures, or complex state management who need a declarative way to access and update nested properties.
Developers choose Fantasy Lenses for its composable and immutable approach to data manipulation, which integrates seamlessly with functional programming libraries and avoids side effects. Its ability to handle optional fields safely and chain operations makes it a powerful tool for complex data transformations.
Composable, immutable getters and setters.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Lenses can be chained using `andThen()` to access and update deeply nested properties, as shown in the example where `locationLens.andThen(numberLens)` accesses `person.location.number`.
Setters return new copies of the entire data structure without mutating the original, demonstrated by `store.set(1007)` returning a modified copy of the person object.
Partial lenses allow safe access to optional fields using `fold()` to handle missing data, as shown in the filtering example with `configTypeLens` that avoids runtime errors.
The API aligns with Fantasy Land specifications, making it suitable for integration with other functional libraries in JavaScript ecosystems.
The API requires explicit chaining and `.run()` calls for each operation, leading to more code compared to concise alternatives like Lodash's `_.set` or Immer's draft updates.
Immutable updates involve deep copying of objects, which can be inefficient for large or frequently updated data structures, a trade-off inherent to the approach.
Requires understanding of functional concepts like lenses and composability, which may be challenging for developers not versed in functional programming patterns.