Permalink
Browse files

maximum depth introduced in deep copy

  • Loading branch information...
1 parent 3c3fc8d commit 2fedc3080897f7dfce65a026881c233212ecbb67 @danstocker committed Jun 4, 2011
Showing with 12 additions and 4 deletions.
  1. +12 −4 src/core/core.js
View
@@ -8,6 +8,8 @@ var jOrder = function (json, options) {
jOrder.core = function () {
var self = {
+ MAX_DEPTH: 10,
+
// delegates all of a module's properties to the jOrder object
delegate: function (module, host, properties) {
host = host || jOrder;
@@ -27,8 +29,14 @@ jOrder.core = function () {
// creates a deep copy of the object recursively
// optionally renumbered
- // WARNING: reference looping is NOT handled!
- deep: function (json, renumber) {
+ deep: function (json, renumber, depth) {
+ depth = depth || 0;
+
+ // checking depth
+ if (depth >= self.MAX_DEPTH) {
+ throw "Deep copying exceeded maximum depth (" + self.MAX_DEPTH + ")";
+ }
+
var result,
isArray = json && typeof json.length !== 'undefined',
i;
@@ -46,9 +54,9 @@ jOrder.core = function () {
for (i in json) {
if (json.hasOwnProperty(i)) {
if (renumber) {
- result.push(self.deep(json[i]));
+ result.push(self.deep(json[i], renumber, depth + 1));
} else {
- result[i] = self.deep(json[i]);
+ result[i] = self.deep(json[i], renumber, depth + 1);
}
}
}

0 comments on commit 2fedc30

Please sign in to comment.