WIP This library will expose a function that accepts an object and a callback that also accepts that object type and returns void
. Within the scope of this callback, any mutation to the object will generate a JSON which is added to a list of such and returned to the consumer when the callback scope returns. The originally provided object and all references created within the callback scope are not altered, but proxied.
This is very similar to the Immer library's produceWithes
function except here we do not return an immutable generation of the provided object, just the list of es to get from state A to state B. Additionally, Immer does not support a number of expected access patterns, e.g. dereffing objects and mutating them outside the initial object tree; this library does the right thing with detached references.
import { generatees } from 'json--proxy'
import t from 'tap'
const root = {
i: {
j: 1,
k: 2,
l: 3,
}
}
const es = generatees(root, proxy => {
p.i = {
j: 3,
k: 2,
l: 1
}
p.i.j = 4
})
t.equal(es.length, 2)
t.equal(es[0].op, "replace")
t.equal(es[0].path, "/i")
t.equal(es[0].value.j, 3)
t.equal(es[0].value.k, 2)
t.equal(es[0].value.l, 1)
t.equal(es[1].op, "replace")
t.equal(es[1].path, "/i/j")
t.equal(es[1].value, 4)
t.end()