npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

topo-kahn

v1.1.0

Published

Topological sort (Kahn algorithm) an oriented graph containing any kind of node, using ES6 Maps & Sets.

Downloads

399

Readme

topo-kahn

Build Status Coverage Status Codacy Badge npm bundle size NPM npm

Topological sort (Kahn algorithm) an oriented graph containing any kind of node, using ES6 Maps & Sets.

Examples

let's start with some family members :

import sort, { reverseMatrix } from topo-kahn ;

const g = { name: "George" };
const mt = { name: "Marie-Thérèse" };
const p = { name: "Patrice" };
const j = { name: "Josette" };
const pj = { name: "Pierre-Jean" };
const m = { name: "Mathias" };
const pandj = new Set([p, j]);

...

Static style, by passing a Map that represents dependency matrix

...

const parents = new Map([
  [m, pandj],
  [g, null],
  [pj, pandj],
  [mt, null],
  [p, new Set([g, mt])],
  [j, null],
]);

const sorted = sort(parents); // sorted = new Set([g, mt, j, p, pj, m]);

Functional style, by passing a Set that represents family members and a generator function

...

const family = new Set([m, g, pj, mt, p, j]);

const getParents = member => {
  switch (member) {
    case m:
      return pandj;
    case g:
      return null;
    case pj:
      return pandj;
    case mt:
      return null;
    case p:
      return new Set([g, mt]);
    case j:
    default:
      return null;
  }
};

const sorted = sort(family, { generator: getParents }); // same result !

You can even pass a reversed matrix / generator and work the other way around !

// reverseMatrix is a util function provided by this package
const sorted = sort(reverseMatrix(parents), { type: 'children' }); // same result !

// or

const sorted = sort(family, { generator: getChildren, type: 'children' }); // same result !

FAQ

  • Why should I use it ?
    • If you like this way to express a graph
    • Or if you already have a function that defines dependencies between nodes
    • If you want / need to define any kind of node (objects, strings, numbers, ...) by using ES6 Maps & Sets
  • What if there's a loop in my graph ? topo-kahn will throw an error
  • What if I have a matrix containing node's children, and not parents ? Just pass a type option set to children
import sort from topo-kahn ;

const sorted = sort(children, { type: 'children' });
// eq. to const sorted = sort(parents);

Reverse a matrix

topo-kahn exports a specific function called reverseMatrix if you need to.