{"id":20294189,"url":"https://github.com/teambit/cleargraph","last_synced_at":"2025-08-22T00:45:20.319Z","repository":{"id":44213236,"uuid":"233862448","full_name":"teambit/cleargraph","owner":"teambit","description":"A library for modeling and traversing graphs","archived":false,"fork":false,"pushed_at":"2022-12-30T19:31:29.000Z","size":200,"stargazers_count":25,"open_issues_count":4,"forks_count":2,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-25T08:03:27.706Z","etag":null,"topics":["graph"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/teambit.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-14T14:41:10.000Z","updated_at":"2025-01-23T21:58:26.000Z","dependencies_parsed_at":"2023-01-31T14:25:19.984Z","dependency_job_id":null,"html_url":"https://github.com/teambit/cleargraph","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teambit%2Fcleargraph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teambit%2Fcleargraph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teambit%2Fcleargraph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teambit%2Fcleargraph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teambit","download_url":"https://codeload.github.com/teambit/cleargraph/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248386115,"owners_count":21095009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graph"],"created_at":"2024-11-14T15:28:10.758Z","updated_at":"2025-04-11T11:43:04.046Z","avatar_url":"https://github.com/teambit.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cleargraph\n\nCleargraph is a graph library offering:\n * An abstraction over graphs that supports generic data types\n * Traversal over successors and predecessors\n * Use of user-defined filters on nodes and edges information and traversal\n * Strictly-typed implementation\n \n## Installation\n\n```sh\nnpm install cleargraph\nyarn add cleargraph\n```\n\n## Getting started\n\nThe nodes and edges in the graph are represented by key-value pairs where the keys are strings, \nand the generics `N` and `E` represent the node value and edge value respectively.\n\nWhen instantiating the graph, specify the values of `N` and `E`.\nIn addition, in order to allow graph serialization, N and E **should implement `stringify()`**.\nIf it's not implemented, the graph will call its own default serialization method that might not work for complex objects.\n\nHere is an example of N (Node Data) and E (Edge Data) classes:\n\n```typescript\nclass Orb { // a node in the graph\n    name: string;\n    radius: number;\n    constructor(name:string, radius:number){\n        this.name = name;\n        this.radius = radius;\n    }\n    stringify(){ // Add a specific stringify() implementation if your class will not stringify correctly with just JSON.stringiy when serializing the graph\n        return JSON.stringify({name: this.name, radius: this.radius});\n    }\n}\n\nclass OrbRelation{ // an edge in the graph\n    relationType: string;\n    proximity: number;\n    constructor(relationType: string, proximity: number){\n        this.relationType = relationType;\n        this.proximity = proximity;\n    }\n    stringify(){\n        return JSON.stringify({relationType: this.relationType, proximity: this.proximity});\n    }\n}\n```\n\nUsing these classes to implement a graph:\n\n```typescript\n\nlet g = new Graph\u003cOrb, OrbRelation\u003e();\n\ng.setNode('earth', new Orb('earth', 6371));\ng.setNode('moon', new Orb('moon', 1737));\ng.setNode('sun', new Orb('sun', 696340));\ng.setEdge('moon','earth', new OrbRelation('orbits', 384400));\ng.setEdge('earth','sun', new OrbRelation('orbits', 147240000));\n```\n\nSome uses of the graph:\n\n```typescript\ng.node('moon');\n// Orb{name: 'moon', radius: 1737}\n```\n\n```typescript\ng.edge('earth', 'sun');\n//OrbRelation{relationType: 'orbits', proximity: 147240000}\n```\n\n```typescript\ng.succssors('moon'); // returns the immediate nodes the given node point to\n// Map \n// {\"earth\" =\u003e Orb} {key: \"earth\", value: Orb}\n//     key:\"earth\"\n//     value:Orb {name: \"earth\", radius: 6371}\n```\n\n```typescript\ng.successorsArray('moon'); // returns an array of all the nodes the given node points to *recursively*\n// Array(2) [Orb, Orb]\n// 0:Orb {name: \"earth\",radius: 6371}\n// 1:Orb {name: \"sun\", radius: 696340}\n```\n\n```typescript\ng.toposort(); // performs a topological sort on the graph\n// Array(3) [Orb, Orb, Orb]\n// 0:Orb {name: \"moon\",radius: 1737}\n// 1:Orb {name: \"earth\",radius: 6371}\n// 2:Orb {name: \"sun\", radius: 696340}\n```\n\n## Contributing\n\nContributions are always welcome, no matter how large or small.\n\n## License\n\nApache license, version 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteambit%2Fcleargraph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteambit%2Fcleargraph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteambit%2Fcleargraph/lists"}