Skip to content

Commit 088b311

Browse files
committed
Release 0.11.0
1 parent fd67b28 commit 088b311

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Build status](https://travis-ci.org/Hexagon/klon.svg)](https://travis-ci.org/Hexagon/klon) [![npm version](https://badge.fury.io/js/klon.svg)](https://badge.fury.io/js/klon)
55
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://img.shields.io/badge/license-MIT-blue.svg)
66

7-
JavaScript helper for deep cloning and deep merging objects as efficiently as possible. Supports and clones complex native objects as well as circular references.
7+
JavaScript helper for deep cloning and deep merging objects as efficiently as possible. Supports and clones complex native objects as well as circular references. Is also capable of replacing circular references with a string containing "[Circular]".
88

99
# Installation
1010

@@ -13,15 +13,15 @@ JavaScript helper for deep cloning and deep merging objects as efficiently as po
1313
# Usage
1414

1515
```javascript
16-
clone(source[, destination]);
16+
clone(source[, destination][, breakCircular]);
1717
```
1818

1919
**source** is the object to be copied.
2020

2121
**destination** (optional) is the new object, where all properties from source will be copied to.
2222

2323
```javascript
24-
var clone = require('klon'),
24+
var klon = require('klon'),
2525
myObject1 = {
2626
a: 1,
2727
b: 2,
@@ -38,13 +38,16 @@ var clone = require('klon'),
3838
};
3939

4040
// Clone
41-
var newObject1 = clone(myObject1);
41+
var newObject1 = klon(myObject1);
4242

4343
// Extend newObject1 with myObject2
44-
clone(myObject2, newObject1)
44+
klon(myObject2, newObject1)
4545

4646
// Merge two objects into a new one
47-
var newObject3 = clone(newObject2, clone(newObject1, {}) );
47+
var newObject3 = klon(newObject2, klon(newObject1, {}) );
48+
49+
// Clone and break circular references
50+
var newObject4 = klon(myObject1, {}, true);
4851

4952
```
5053

index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SOFTWARE.
2626

2727
"use strict";
2828

29-
function klon (source, target, trace) {
29+
function klon (source, target, breakCircular, trace) {
3030

3131
// No need to copy null, undefined or primitive values
3232
// Shortest way first
@@ -68,8 +68,13 @@ function klon (source, target, trace) {
6868

6969
// Circular reference? Store the circular reference and move on to next property
7070
if ( circularReference = trace.get(prop) ) {
71-
target[key] = circularReference;
72-
continue;
71+
if ( !breakCircular ) {
72+
target[key] = circularReference;
73+
continue;
74+
} else {
75+
target[key] = "[Circular]"
76+
continue;
77+
}
7378
}
7479

7580
// Pure objects and arrays need tracing
@@ -78,7 +83,7 @@ function klon (source, target, trace) {
7883
}
7984

8085
// Ok, this seem to be a simple value, assign it
81-
target[key] = klon(prop, target[key], trace);
86+
target[key] = klon(prop, target[key], breakCircular, trace);
8287

8388
}
8489

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "klon",
3-
"version": "0.9.0",
3+
"version": "0.11.0",
44
"description": "JavaScript helper for deep cloning and deep merging objects as efficiently as possible. Supports and clones complex native objects as well as circular references.",
55
"author": "Hexagon <github.com/hexagon>",
66
"contributors": [{

test/test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ describe("Deep Extend / Clone / Merge", function () {
8181

8282
});
8383

84+
it("Break shallow circular reference", function () {
85+
86+
var simpleObject = {
87+
a: {
88+
b: {
89+
90+
}
91+
}
92+
},
93+
clonedObject;
94+
95+
// Create a circular reference
96+
simpleObject.a.b = simpleObject.a;
97+
98+
clonedObject = clone(simpleObject, undefined, true);
99+
100+
clonedObject.a.b.should.equal("[Circular]");
101+
102+
});
103+
84104
it("Circular reference", function () {
85105

86106
var simpleObject = {
@@ -112,6 +132,31 @@ describe("Deep Extend / Clone / Merge", function () {
112132

113133
});
114134

135+
it("Breaking circular reference", function () {
136+
137+
var simpleObject = {
138+
a: 1,
139+
b: {
140+
c: {
141+
d: {
142+
val: 2
143+
}
144+
}
145+
},
146+
d: 3
147+
},
148+
clonedObject;
149+
150+
// Create a circular reference
151+
simpleObject.b.c.d.e = simpleObject.b;
152+
153+
clonedObject = clone(simpleObject, undefined, true);
154+
155+
// Test that the circular reference works
156+
clonedObject.b.c.d.e.should.equal("[Circular]");
157+
158+
});
159+
115160
it("Plain string", function () {
116161

117162
var

0 commit comments

Comments
 (0)