is-circular
Advanced tools
Comparing version 1.0.0 to 1.0.1
23
index.js
@@ -18,23 +18,24 @@ module.exports = isCircular | ||
this.obj = obj | ||
this.seen = [] | ||
} | ||
/** | ||
* checks whether this.obj is circular | ||
* @param {object} _obj do not pass. this param is used for recursive calls. defaults to this.obj | ||
* @param {object} obj do not pass. this param is used for recursive calls. defaults to this.obj | ||
* @param {array} seen a list of descendants from the root object to obj | ||
* @return {Boolean} true if obj is circular, false if it is not | ||
*/ | ||
CircularChecker.prototype.isCircular = function (_obj) { | ||
_obj = _obj || this.obj | ||
if (!(_obj instanceof Object)) { | ||
CircularChecker.prototype.isCircular = function (obj, seen) { | ||
obj = obj || this.obj | ||
seen = seen || [] | ||
if (!(obj instanceof Object)) { | ||
throw new TypeError('"obj" must be an object (or inherit from it)') | ||
} | ||
var self = this | ||
this.seen.push(_obj) | ||
seen.push(obj) | ||
for (var key in _obj) { | ||
var val = _obj[key] | ||
for (var key in obj) { | ||
var val = obj[key] | ||
if (val instanceof Object) { | ||
return (~self.seen.indexOf(val)) | ||
? true // object is circular | ||
: self.isCircular(val) | ||
if (~seen.indexOf(val) || self.isCircular(val, seen.slice())) { | ||
return true | ||
} | ||
} | ||
@@ -41,0 +42,0 @@ } |
{ | ||
"name": "is-circular", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "checks an object (function or array) for circular references", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -25,2 +25,18 @@ var Code = require('code') | ||
it('should return true for circular objects', function (done) { | ||
var x = {} | ||
x.cyclic = { a: {}, x: x } | ||
expect(isCircular(x)).to.equal(true) | ||
done() | ||
}) | ||
it('should return true for circular objects', function (done) { | ||
var x = {} | ||
x.cyclic = { a: {}, indirect: { x: x } } | ||
expect(isCircular(x)).to.equal(true) | ||
done() | ||
}) | ||
it('should return false for non-circular objects', function (done) { | ||
@@ -33,2 +49,11 @@ var x = {} | ||
}) | ||
it('should return false for non-circular objects', function (done) { | ||
var x = {} | ||
var y = {} | ||
x.cyclic = { a: y, b: y } | ||
expect(isCircular(x)).to.equal(false) | ||
done() | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5624
84