stevekane / graph-clone

Playground for conditional cloning techniques in JS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Graph Clone

This module offers a traversal mechanic for circular data JSON structures that allows you to clone or skip cloning on nodes and also apply optional transforms to encountered nodes.

Example usage

See test.js for best illustrating example. Below is a dead simple version but the test is more exhaustive

var w = require('graph-clone')
var stringify = require('json-stringify-safe')
var obj = {
  foo: {
    name: 'boris',
    bar: null 
  }
}
obj.foo.bar = obj

var copy = w(obj, () => true, (obj, key, val) => key === 'name' ? val + '!!!' : val)

console.log(stringify(copy))

// copy = {
//   foo: {
//      name: 'boris!!!',
//      bar: [ circular ]
//   }
// }

About

Playground for conditional cloning techniques in JS


Languages

Language:JavaScript 100.0%