object creator and dependency manager

Usage no npm install needed!

<script type="module">
  import factoryTt from 'https://cdn.skypack.dev/factory-tt';


build status coverage status npm version


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.


npm install factory-tt


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

// 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; } }


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}; }


// 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

// 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(),

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(),


  • 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.