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 🙏

© 2026 – Pkg Stats / Ryan Hefner

test_pubsub

v0.5.0

Published

Hiring test based on pubsub pattern

Readme

Hiring test based on pubsub pattern

PubSub

The PubSub class, lib/pubsub.js, should be a basic implementation of the Publish<>Subscribe pattern. You can find out more about it here : Wikipedia :)

The idea is that something (a subscriber) can "subscribe" to events "published" by something else ( a publisher ). This events are represented by a name, with a hierarchical pattern in this case.

Goals

You have to code a PubSub class lib/pubsub.js. To help you in this task, you have a complete spec/test file, spec/lib/pubsub.spec.js, that uses jest. This test file shows you how the PubSub class is supposed to work.

The best way to do the test is to launch the tests, and validate each test one by one, in a TDD approach.

Notes:

  • Do not necessary try to do everything, but ...
  • Complete the existing class and code as much methods as you can
  • Validate as much specs/tests as you can
  • Show us the Ninja in you instead of trying to finish the test
  • Explain in a few words your idea/algorithm or what you think you have to code if you don't know how to do it

How to use this project

First, you need to install node.js for your environment, you should have done that before the test

Dependencies

To install the dependencies of the project, just run npm install command:

$ npm i

Execute tests

To execute the tests, just run the test script of the project:

$ npm test

As you can see there is some failures, resolve as much as you can.

You can also use the dev script when you develop, it will automaticaly relaunch the test script when you make some changes to a file (watch mode):

$ npm run dev

Misc

Lodash

You can use lodash, or equivalent, if you want methods like each, merge, etc...

Example require of lodash in your project

	const _ = require('lodash');

Code coverage

If you want to look at the code coverage of your PubSub class, you can use the coverage script of the project:

$ npm run coverage

Typical use of the PubSub Class

See example.js for a basic typical use case.

Specs explanation

The PubSub class must accept an options object on its constructor. There is two default properties that can be override:

    var ps = new PubSub({
            // Separator between each level of a topic name
            // Example with ">" separator : user>update>name
            topicChildSeparator: string,
            // The default priority order of a subscriber, if no priority provided
            defaultPriority: number
        });

The PubSub class must provide two main methods, publish and subscribe.

    // Callback method will be called when something publish to the given topic (or a child of this topic) in the given priority order
    function subscribe(topic, callback, priority){}

    // Send the given value to any subscriber who have subscribe to this topic (or parent of this topic)
    function publish(topic, value){}

With default options, topic publishing order is like this :

    var topic = 'parent>child>sub-child';
    ps.publish(topic,'foo');
    // Subscribers callbacks will be called with 'foo' as first argument in this order :
    // 1 : "parent>child>sub-child" subscribers, according to their priority
    // 2 : "parent>child" subscribers, according to their priority
    // 3 : "parent" subscribers, according to their priority

When you subscribe to a topic, you should receive a subscriber object representing the subscription. The form of the subscription object is like this:

    type SubscriptionType = {
        id: any, // A unique identifier of the subscription
        topic: string,
        priority: number,
        callback: function
    };

This subscription object can be used later to unsubscribe from the topic it was created for :

    ps.unsubcribe(subscription)