mirror of
https://github.com/Instadapp/Swap-Aggregator-Subgraph.git
synced 2024-07-29 21:57:12 +00:00
39 lines
1009 B
JavaScript
39 lines
1009 B
JavaScript
var Node = require('./lib/node')
|
|
|
|
module.exports = isCircular
|
|
|
|
/**
|
|
* 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) {
|
|
if (!(obj instanceof Object)) {
|
|
throw new TypeError('"obj" must be an object (or inherit from it)')
|
|
}
|
|
return _isCircular(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
|
|
*/
|
|
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 (parentList.contains(val) || _isCircular(val, parentList)) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|