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

factory-tt

v0.4.0

Published

object creator and dependency manager

Downloads

11

Readme

build status coverage status npm version

FactoryTypeThing

JavaScript object creator and dependency manager.

Register all of your classes, factory functions, and/or services (as well as their dependencies) at the start of your application, and then inject the factory into any object that needs to instantiate or locate other objects.

Install

npm install factory-tt

Usage

Instantiate a factory:

import createFactory from "factory-tt";

let factory = createFactory();

Work with a traditional constructor:

// Constructor function (must start uppercase)
function Turkey() { this.texture = "juicy"; }

// Register the constructor with the factory
factory.register(Turkey);

// Instantiate some delicious case insensitive turkey (it knows to call "new")
let turkey1 = factory.create("Turkey");
let turkey2 = factory.create("tUrKeY");

Work with an ES6 class:

// ES6 class (must start uppercase)
class Cheddar { constructor() { this.flavorRating = 42; } }

factory.register(Cheddar);

let cheddar1 = factory.create("Cheddar");
let cheddar2 = factory.create("cHeDdAr");

Work with a factory function:

// Factory function (must start lowercase)
function wheatCracker() { return {numHoles: 7}; }

factory.register(wheatCracker);

// Mmm crackertime (it knows not to call "new")
let wheatCracker1 = factory.create("WheatCracker");
let wheatCracker2 = factory.create("wHeAtCrAcKeR");

Work with an existing service (singleton):

// A service object; 'register' won't know what to do with this
var pudding = {hasSprinkes: true};

// Use 'registerService' instead; must provide a name as first parameter
factory.registerService("Pudding", pudding);

// Although it doesn't have to be the same name as the variable
factory.registerService("Goop", pudding);

// Use 'locate' instead of 'create' (returns the same object every time)
let pudding1 = factory.locate("Pudding");
let pudding2 = factory.locate("pUdDiNg");
pudding1 === pudding2;

Convert a class or factory function into a service (singleton):

// ES6 class (must start uppercase)
class Brownie { constructor() { this.hasSprinkles = false; } }

// Register the class like normal
factory.register(Brownie);

// Use 'locate' to permanently convert class to a service (singleton)
let brownie1 = factory.locate("Brownie");
let brownie2 = factory.locate("bRowNiE");
brownie1 === brownie2;

Override register's type detection and/or naming:

// Class starts with lowercase so 'register' thinks it's a factory function
class poorlyNamedClass {}

// Use 'registerClass' instead to force it to be registered as a class
factory.registerClass("MyClass", poorlyNamedClass);

// Factory function starts with uppercase so 'register' thinks it's a class
function PoorlyNamedFactory() {}

// Use 'registerFactory' instead to force it to be registered as a factory
factory.registerFactory("MyFactory", PoorlyNamedFactory);

Define dependencies for a constructor that accepts positional parameters:

// Look at all them tasty positional parameters in this constructor
function Lunchable(meat, cheese, cracker, dessert) {}

// Tell the factory how lunchables should be created via an array of strings
factory.register(Lunchable, ["Turkey", "Cheddar", "WheatCracker", "Pudding"]);

// Now doing this
let lunchable1 = factory.create("Lunchable");

// Is the same as doing this
let lunchable2 = new Lunchable(
  new Turkey(),
  new Cheddar(),
  wheatCracker(),
  pudding
);

Define dependencies for a constructor that accepts named parameters:

function Dinnerable({meat, cheese, cracker, dessert}) {}

// Tell the factory how dinnerables should be created via an object of strings
factory.register(Dinnerable, {
  meat: "Turkey",
  cheese: "Cheddar",
  cracker: "WheatCracker",
  dessert: "Pudding",
});

let dinnerable = factory.create("Dinnerable");

Perform 1-time override of first two positional parameters:

function Ham() { this.texture = "firm"; }
class Swiss { constructor() { this.flavorRating = 17; } }

// Override "Turkey" and "Cheddar" dependencies with new Ham and Swiss objects
let nastyLunchable = factory.create("Lunchable", [new Ham(), new Swiss()]);

Perform 1-time override of only the second positional parameter:

// Keep "Turkey" dependency but override "Cheddar" with new Swiss object
let okLunchable = factory.create("Lunchable", [factory.DEFER, new Swiss()]);

Perform 1-time override of some named paramters:

let nastyDinnerable = factory.create("Dinnerable", {
  meat: new Ham(), 
  cheese: new Swiss(),
});

Beware

  • Requires Node v6 or higher (or additional transpiling).
  • If using UglifyJS, then either use its --keep-fnames switch or only use register(Class|Factory|Service) override functions instead of register.
  • Circular dependencies will overflow the stack.
  • Overriding a dependency won't propogate the override to dependencies-of-dependencies.

License

MIT

GLHFDD