phadej / optika

Optics library for JavaScript
MIT License
141 stars 9 forks source link

Optika

Optics: modular data access

Build Status NPM version Dependency Status devDependency Status

Getting Started

Install the module with: npm install optika

Synopsis

var o = require("optika");

var value = {
  foo: [
    { bar: 2, baz: 3 },
    { bar: 4, baz: 5 },
  ]
};

// [2, 4]
o.key("foo").traversed().key("bar").arrayOf(value);

// { foo: [ { bar: 9, baz: 3 }, { bar: 11, baz: 5 } ] }
o.key("foo").traversed().key("bar").over(value, function (x) {
  return x + 7;
});

Motivation

Immutable.js is great! But working with immutable containers & nested records is un-easy:

return data.update("months", months =>
  months.map(month =>
    month.update("days", days =>
      days.map(day => injectAuxData(day, auxiliaryData))
    )
  )
);

The updateIn isn't powerful enough to make the drilling less boilerplaty (and less error-prone).

If you are a Haskell programmer, you might know that lenses are a solution to this problem:

data_ & months . traversed . days . traversed %~ \day ->
  injectAuxData day auxData

-- or without operators:
over
  (months . traversed . days . traversed)
  (\day -> injectAuxData day auxData)
  data_

And with this library you can write similar code in JavaScript:

o.imkey("months").traversed().imkey("days").traversed().over(data, day =>
  injectAuxData(day, auxData)
);

Documentation

There is small amount of run-time validation. For example, if you try to set over Getter, the exception will be thrown:

o.key("foo").to(function (x) { return x[0]; }).set(value, 42);
// throws: Trying to run Traversal operation via Getter

Operations

Constructors

Convenient optics

Note: The predicate is checked when the value is injected (via Traversal) or constructed via Prism.

Like filtered but predicate is checked on construction.

Internals

Functions which you probably never need to use directly.

.o(this: Optic<S,T,A,B>, other: Optic<A,B,U,V>): Optic<S,T,U,V>

Compose two optics.

.run(this: Optic<S,T,A,B>, p: Profunctor<A,B>): Profunctor<S,T>

Contributing

Release History

Implementation details

Related work

JavaScript

The MIT License (MIT)

Copyright (c) 2017 Oleg Grenrus

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.