Clojure's immutable data API for JavaScript's own data structures, using the function bind operator.

import { inc, update } from 'zaphod' ; const state = { count: 0 }; state::update( 'count' , inc)

However, you don't need to know Clojure to make the most of Zaphod. This library is just a set of JavaScript functions that make working with immutable data more fun!

Try the tutorial, check out the cheatsheet, join the chat or explore the code.

Rationale

Working with JavaScript's data structures as though they were immutable is a challenge. Although arrays have some methods which return new array instances, the only way to update objects without mutating them is to use unwieldy functions like Object.assign .

Some libraries reimplement these data structures with persistent interfaces, but they don't really integrate well with existing code.

The new structures need to be converted back into native structures at the edges of the project, or the code outside needs to be rewritten to work with them.

Zaphod takes the tried and tested functions from the core of Clojure's immutable data API and rewrites them so that they work with JavaScript's own data structures.

Function Bind

The :: (function-bind) operator is still at the proposal stage for the language, which means Babel is required to use it. Many JavaScript projects are already using Babel, but compiling this syntax will also require either the plugin or the stage-0 preset.

Not everyone is ready to adopt experimental syntax, but it's completely possible to use Zaphod's functions without function-bind. Check out the compatibility interface.

Usage

Zaphod is available through npm.

npm install --save zaphod

Or you can get it from a CDN.

https://unpkg.com/zaphod https://unpkg.com/zaphod/compat

Check out the getting started guide for an in-depth explanation on configuring Babel to work with the function-bind operator.