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

contextizer

v1.0.1

Published

Context injector

Downloads

3

Readme

Contextizer

Contextizer is a transient context injection system. Contextizer simplifies complex multi-stage, pipeline, and/or promise-based code, and makes unit testing a breeze. It performs a role similar to Express's middleware system, but in a declarative dependency-injection-like style.

Inputs are what make Contextizer different from a normal dependency injection system. While normal dependency injection systems are made to initialize a program, Contextizer is designed to be used with varying inputs many times during a program's runtime.

Usage

To install to your project, use npm install --save contextizer.

Below is a contrived of a simple context. It has an input value a, some goals that calculate information about a and a constant b, and a final goal that prints out the information.

const Contextizer = require('contextizer');

let context = new Contextizer();

context.register('a').asInput();
context.register('b').asConstant(5);
context.register('sum').asFunction({
  deps: [ 'a', 'b' ], // dependencies.
  func({ a, b }) {
    return a + b;
  }
});
context.register('product').asFunction({
  deps: [ 'a', 'b' ],
  func({ a, b }) {
    return a * b;
  }
});
context.register('info').asFunction({
  deps: [ 'sum', 'product' ],
  func({ sum, product }) {
    console.log(`The sum of a and b is ${sum}.`);
    console.log(`The product of a and b is ${product}.`);
  }
});

// Execute 'info' with input a = 4.
context.execute('info', { a: 4 });
// "The sum of a and b is 9."
// "The product of a and b is 20."

This example shows all three types of objects that can be registered.

Inputs

Inputs are what makes Contextizer different from dependency injection. Inputs are not defined at registration time. Instead, they are passed in at every execution.

context.register('inputName').asInput();

Constants

Constants are constant values that do not change across multiple executions. Constants may be values or promises.

// Register value.
context.register('pi').asConstant(Math.PI);

// Register promise.
const req = require('request-promise-native'); // https://github.com/request/request-promise-native
context.register('robots').asConstant(req('https://www.google.com/robots.txt'));

Functions

Functions are where the good stuff happens. Functions can depend on inputs, constants, or other functions.

The .asFunction(arg) method takes in a single arg object. The object has the following properties:

  • deps (REQUIRED) - List of dependencies (string array).
  • func (REQUIRED) - The function. Dependencies are passed in as a single argument object, so using the es6 destructuring assigment syntax is recommended. This may return a value or a promise.
  • cleanup (optional) - A function that is called after the execution completes, in order to clean up resources. This callback receives the same argument object as func with an additional $value property for the object returned by func. This may be called even if func hasn't been, due to upstream thrown exceptions. Note that this may not execute immediately so it is not reliable to change params here.
  • cached (optional, default false) - Boolean. If true, this function will only be executed once, then the value will be saved permanently. Use with care.
  • params (optional) - Object, for holding state object. These parameters will always be passed in to func so they can be used to hold state across calls.
context.register('sum').asFunction({
  deps: [ 'a', 'b' ], // Dependencies, registration not shown in this example.
  func({ a, b }) {
    return a + b;
  }
});

// Fibonacci sequence using params.
context.register('fib').asFunction({
  deps: [], // No dependencies.
  func({ vals }) { // `vals` from params.
    vals.push(vals[0] + vals[1]);
    return vals.shift();
  },
  params: {
    vals: [ 1, 1 ]
  }
});

Namespaces

For organizational purposes, objects can be registered in nested namespaces. Think of this like Java packages.

When resolving dependencies, Contextizer will start in the current namespace and move up to the global namespace. Alternatively, absolute paths can be used.

A dot prefix can be used to make a dependency path absolute.

context.register('value').asConstant(10);
context.register('package.value').asConstant(20);
context.register('package.func').asFunction({
  deps: [ 'value' ],
  func({ value }) {
    // value === 20 because `func` is in the same namespace as `package.value`.
  }
});
context.register('package.func2').asFunction({
  deps: [ '.value' ],
  func({ '.value': value }) {
    // value === 10 because '.value' is considered an absolute path.
  }
});