{"id":13929913,"url":"https://github.com/liitfr/topo-kahn","last_synced_at":"2025-04-05T12:45:07.549Z","repository":{"id":46926854,"uuid":"197812200","full_name":"liitfr/topo-kahn","owner":"liitfr","description":"Topological sort (Kahn algorithm) an oriented graph containing any kind of node, using ES6 Maps \u0026 Sets.","archived":false,"fork":false,"pushed_at":"2023-01-04T04:51:51.000Z","size":530,"stargazers_count":2,"open_issues_count":11,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T14:59:27.664Z","etag":null,"topics":["dependency-graph","dependency-resolution","kahn","kahns-alogrithm","topological-order","topological-sort"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liitfr.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2019-07-19T16:56:52.000Z","updated_at":"2023-09-10T21:00:14.000Z","dependencies_parsed_at":"2023-02-01T21:46:56.461Z","dependency_job_id":null,"html_url":"https://github.com/liitfr/topo-kahn","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/liitfr%2Ftopo-kahn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liitfr%2Ftopo-kahn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liitfr%2Ftopo-kahn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liitfr%2Ftopo-kahn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liitfr","download_url":"https://codeload.github.com/liitfr/topo-kahn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247339122,"owners_count":20923009,"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":["dependency-graph","dependency-resolution","kahn","kahns-alogrithm","topological-order","topological-sort"],"created_at":"2024-08-07T18:02:37.077Z","updated_at":"2025-04-05T12:45:07.521Z","avatar_url":"https://github.com/liitfr.png","language":"TypeScript","funding_links":[],"categories":["others"],"sub_categories":[],"readme":"# topo-kahn\n\n[![Build Status](https://travis-ci.org/liitfr/topo-kahn.svg?branch=master)](https://travis-ci.org/liitfr/topo-kahn)\n[![Coverage Status](https://coveralls.io/repos/github/liitfr/topo-kahn/badge.svg?branch=master)](https://coveralls.io/github/liitfr/topo-kahn?branch=master)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/f4857de1aa3244f88cf135e4360e83d5)](https://www.codacy.com/app/liitfr/topo-kahn?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=liitfr/topo-kahn\u0026utm_campaign=Badge_Grade)\n![npm bundle size](https://img.shields.io/bundlephobia/min/topo-kahn.svg)\n![NPM](https://img.shields.io/npm/l/topo-kahn.svg)\n![npm](https://img.shields.io/npm/v/topo-kahn.svg)\n\nTopological sort (Kahn algorithm) an oriented graph containing any kind of node, using ES6 Maps \u0026 Sets.\n\n## Examples\n\nlet's start with some family members :\n\n```js\nimport sort, { reverseMatrix } from topo-kahn ;\n\nconst g = { name: \"George\" };\nconst mt = { name: \"Marie-Thérèse\" };\nconst p = { name: \"Patrice\" };\nconst j = { name: \"Josette\" };\nconst pj = { name: \"Pierre-Jean\" };\nconst m = { name: \"Mathias\" };\nconst pandj = new Set([p, j]);\n\n...\n```\n\n### Static style, by passing a Map that represents dependency matrix\n\n```js\n...\n\nconst parents = new Map([\n  [m, pandj],\n  [g, null],\n  [pj, pandj],\n  [mt, null],\n  [p, new Set([g, mt])],\n  [j, null],\n]);\n\nconst sorted = sort(parents); // sorted = new Set([g, mt, j, p, pj, m]);\n```\n\n### Functional style, by passing a Set that represents family members and a generator function\n\n```js\n...\n\nconst family = new Set([m, g, pj, mt, p, j]);\n\nconst getParents = member =\u003e {\n  switch (member) {\n    case m:\n      return pandj;\n    case g:\n      return null;\n    case pj:\n      return pandj;\n    case mt:\n      return null;\n    case p:\n      return new Set([g, mt]);\n    case j:\n    default:\n      return null;\n  }\n};\n\nconst sorted = sort(family, { generator: getParents }); // same result !\n```\n\n### You can even pass a reversed matrix / generator and work the other way around !\n\n```js\n// reverseMatrix is a util function provided by this package\nconst sorted = sort(reverseMatrix(parents), { type: 'children' }); // same result !\n\n// or\n\nconst sorted = sort(family, { generator: getChildren, type: 'children' }); // same result !\n```\n\n## FAQ\n\n- Why should I use it ?\n  - If you like this way to express a graph\n  - Or if you already have a function that defines dependencies between nodes\n  - If you want / need to define any kind of node (objects, strings, numbers, ...) by using ES6 Maps \u0026 Sets\n- What if there's a loop in my graph ? `topo-kahn` will throw an error\n- What if I have a matrix containing node's children, and not parents ? Just pass a `type` option set to `children`\n\n```js\nimport sort from topo-kahn ;\n\nconst sorted = sort(children, { type: 'children' });\n// eq. to const sorted = sort(parents);\n```\n\n## Reverse a matrix\n\n`topo-kahn` exports a specific function called `reverseMatrix` if you need to.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliitfr%2Ftopo-kahn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliitfr%2Ftopo-kahn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliitfr%2Ftopo-kahn/lists"}