is-circular
Advanced tools
Comparing version 1.0.1 to 1.0.2
38
index.js
@@ -0,38 +1,32 @@ | ||
var Node = require('./lib/node') | ||
module.exports = isCircular | ||
/** | ||
* is circular utility | ||
* @param {object} obj object or array to be checked for circular references | ||
* checks whether the object is circular | ||
* @param {object} obj - object to check circularity for | ||
* @return {Boolean} true if obj is circular, false if it is not | ||
*/ | ||
function isCircular (obj) { | ||
return new CircularChecker(obj).isCircular() | ||
if (!(obj instanceof Object)) { | ||
throw new TypeError('"obj" must be an object (or inherit from it)') | ||
} | ||
return _isCircular(obj) | ||
} | ||
/** | ||
* Circular checker helper class | ||
* @param {object} obj object or array to be checked for circular references | ||
*/ | ||
function CircularChecker (obj) { | ||
this.obj = obj | ||
} | ||
/** | ||
* checks whether this.obj is circular | ||
* @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 | ||
* @private | ||
* checks whether the object is circular | ||
* @param {object} obj - object to check circularity for | ||
* @param {Node} parentList - linked-list that contains all the object's parents | ||
* @return {Boolean} true if obj is circular, false if it is not | ||
*/ | ||
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 | ||
seen.push(obj) | ||
function _isCircular (obj, parentList) { | ||
parentList = new Node(obj, parentList) | ||
// breadth-first search for circular object | ||
for (var key in obj) { | ||
var val = obj[key] | ||
if (val instanceof Object) { | ||
if (~seen.indexOf(val) || self.isCircular(val, seen.slice())) { | ||
if (parentList.contains(val) || _isCircular(val, parentList)) { | ||
return true | ||
@@ -39,0 +33,0 @@ } |
{ | ||
"name": "is-circular", | ||
"version": "1.0.1", | ||
"description": "checks an object (function or array) for circular references", | ||
"version": "1.0.2", | ||
"description": "high-performance, zero-dependency circular reference check for objects (or arrays)", | ||
"main": "index.js", | ||
@@ -10,11 +10,13 @@ "directories": { | ||
"devDependencies": { | ||
"code": "^1.5.0", | ||
"lab": "^6.2.0", | ||
"standard": "^5.4.1" | ||
"is-circular": "1.0.1", | ||
"jest": "^23.2.0", | ||
"matcha": "^0.7.0", | ||
"standard": "^11.0.1" | ||
}, | ||
"scripts": { | ||
"test": "lab -a code -t 100", | ||
"benchmark": "matcha benchmark/*", | ||
"test": "jest --coverage", | ||
"test-watch": "nodemon -x npm test", | ||
"lint": "standard", | ||
"format": "standard --format" | ||
"format": "standard --fix" | ||
}, | ||
@@ -21,0 +23,0 @@ "repository": { |
@@ -1,5 +0,4 @@ | ||
# is-circular [![Build Status](https://travis-ci.org/tjmehta/is-circular.svg)](https://travis-ci.org/tjmehta/is-circular) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) | ||
Split an array into multiple arrays using filters | ||
# is-circular [![Build Status](https://travis-ci.org/tjmehta/is-circular.svg?branch=master)](https://travis-ci.org/tjmehta/is-circular) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) | ||
determines if an object (or array) is circular | ||
High-performance circular reference check for objects (or arrays) w/ no dependencies | ||
@@ -33,2 +32,2 @@ # Installation | ||
# License | ||
MIT | ||
MIT |
@@ -1,14 +0,8 @@ | ||
var Code = require('code') | ||
var Lab = require('lab') | ||
/* eslint-env jest */ | ||
var isCircular = require('../') | ||
var lab = exports.lab = Lab.script() | ||
var describe = lab.describe | ||
var it = lab.it | ||
var expect = Code.expect | ||
describe('is-circular', function () { | ||
it('should error if passed a non-object', function (done) { | ||
expect(isCircular.bind(null, 2)).to.throw(/object/) | ||
expect(isCircular.bind(null, 2)).toThrow(/object/) | ||
done() | ||
@@ -20,3 +14,3 @@ }) | ||
x.cyclic = { a: 1, x: x } | ||
expect(isCircular(x)).to.equal(true) | ||
expect(isCircular(x)).toEqual(true) | ||
@@ -29,3 +23,3 @@ done() | ||
x.cyclic = { a: {}, x: x } | ||
expect(isCircular(x)).to.equal(true) | ||
expect(isCircular(x)).toEqual(true) | ||
@@ -38,3 +32,3 @@ done() | ||
x.cyclic = { a: {}, indirect: { x: x } } | ||
expect(isCircular(x)).to.equal(true) | ||
expect(isCircular(x)).toEqual(true) | ||
@@ -47,3 +41,3 @@ done() | ||
x.cyclic = { a: 1, b: 2 } | ||
expect(isCircular(x)).to.equal(false) | ||
expect(isCircular(x)).toEqual(false) | ||
@@ -57,3 +51,3 @@ done() | ||
x.cyclic = { a: y, b: y } | ||
expect(isCircular(x)).to.equal(false) | ||
expect(isCircular(x)).toEqual(false) | ||
@@ -60,0 +54,0 @@ done() |
Sorry, the diff of this file is not supported yet
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
6028
8
126
4