Deep Diff Error

node v10.24.1
version: master
endpointsharetweet
Import the relevant libraries – redux-deep-diff library uses deep-diff to create diffs and clone-deep to clone the state when traveling through history to prevent mutations; the ngrx-store-freeze library uses deep-freeze to recursively freeze the state.
const diff = require('deep-diff') const clone = require('clone-deep') const freeze = require('deep-freeze') let lhs, rhs, diffs, target
This is a succesful test where freezing, diffing, and cloning the state works as expected.
lhs = freeze({ data: { foo: 'bar', arr: [1, 2, 3] } }) rhs = freeze({ data: { foo: 'baz', arr: [1, 2, 3, 4] } }) diffs = diff(lhs, rhs) target = clone(rhs) console.log(target) diff.revertChange(target, target, diffs[1]) console.log(target)
This is a failing test when part of the state contains a non-pojo. A limitation of deep-clone returns a reference and revertChange attempts to mutate the target.
class Obj { constructor(props) { Object.assign(this, props) } } lhs = freeze({ data: new Obj({ foo: 'bar', arr: [1, 2, 3] }) }) rhs = freeze({ data: new Obj({ foo: 'baz', arr: [1, 2, 3, 4] }) }) diffs = diff(lhs, rhs) target = clone(rhs) console.log(target) diff.revertChange(target, target, diffs[1]) console.log(target)
Loading…

no comments

    sign in to comment