Shallowclone

stdtraits~ Shallowclone

Source:

Shal­lowly clone an ob­ject.

const { stric­tEqual: as­sertIs, not­Stric­tEqual: as­sertIs­Not } = re­quire('assert');
const { Shal­low­clone, shal­low­clone, as­ser­tEquals, Equals, eq } = re­quire('fer­rum');

class Bar {
  con­structor(foo, bang) {
    this.foo = foo;
    this.bang = bang;
  }

  [Shal­low­clone.sym]() {
    re­turn new Bar(this.foo, this.bang);
  }

  [Equals.sym](otr) {
    re­turn eq(this.foo, otr.foo) && eq(this.bar, otr.bar);
  }
}

const a = new Bar({foo: 42}, {bar: 5});
const b = shal­low­clone(a);

as­ser­tEquals(a, b);
as­sertIs­Not(a, b);

a.foo.foo = 5;
as­sertIs(b.foo.foo, 5);

In­ter­face

(x: The­Value) => r: The­Value

Laws

  • x !== r
  • get(r, k) === get(x, k) for any k.

Im­ple­ment­a­tion Notes

No-Op im­ple­ment­a­tions are provided for read only prim­it­ive types.

See: shal­low­clone