Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
/ find-cycles Public archive
forked from dscape/cycle

Fork of crocs cycle package that lives in npm

Notifications You must be signed in to change notification settings

apptension/find-cycles

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

find-cycles

Simple utility for finding cycles in objects.

Usage

import findCycles from 'find-cycles';

let obj = {};
obj.self = obj;

findCycles(obj);
// [['$'], ['$', 'self']]
// it means that one circular object was found
// and paths to it are: $ (root) and $.self

let a = {};
let b = {};
let c = {};
let d = {};
a.dep = b;
b.dep = c;
c.dep = a;
d.dep = d;

let obj = {a,b,c,d};

findCycles(b);
// [
//   ['$', 'a'], ['$', 'a', 'dep', 'dep', 'dep'],
//   ['$', 'd'], ['$', 'd', 'dep']
// ]
// it means that two circular objects were found
// and paths to them are:
//  * $.a and $.a.dep.dep.dep
//  * $.d and $.d.dep

If object has the same value for couple of properties but it's not circular, empty array is returned. Objects of type Number, String, Boolean, RegExp or Date are not taken into account too, it's because they do not cause circular structure error when serializing to JSON.

let obj = new Number(1);
obj.self = obj;

findCycles(obj); // []

let a = {};
let obj = {
  a1: a,
  a2: a
};

findCycles(obj); // []

About

Fork of crocs cycle package that lives in npm

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%