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

industry

v2.9.0

Published

Define factories from ES6 classes and immutably manipulate the class inheritance chain.

Downloads

61

Readme

Industry.js

Define factories from ES6 classes and immutably manipulate the class inheritance chain.

Define the factory

Use Industry’s factory method to define a factory from an ES6 class:

// hello.js
//
import { factory } from "industry"

class Hello {
  say() { return "hello" }
}

export factory(Hello)

Use the factory

Import the factory and call it:

// say_hello.js
//
import factory from "./hello"
factory() // undefined

Nothing happened!

The factory function call returns undefined because we haven't told Industry what to do when we call this factory.

Our first extension

Using the last example, let's create an extension that tells the factory function to return an instance of the class. We will also modify the return value of the say function.

// hello.js
//
import { factory } from "industry"

class Hello {
  say() { return "world" }
}

let ext = Class =>
  class extends Class {
    static factory(...args) {
      super.factory(...args)
      return new this()
    }
    
    say() { return `hello ${super.say()}` }
  }

export default factory(Hello)
  .set("my_extension", ext)

Now our factory() call returns an instance of Hello and changes the return value of say:

// say_hello.js
//
import factory from "./hello"
factory().say() // "hello world"

How the extension works

An extension is a function that receives a class and returns a class. So we start by defining a function that receives a class as an argument with an implicit return:

let ext = Class =>

In this case, Class is the class from the factory(class {}) call. We refer to it as the "factory class".

Next, we extend the factory class to add our own static factory function:

  class extends Class {
    static factory() {

The static factory function determines the return value of the factory function.

We call the superclass with the same arguments we received:

      super.factory(...args)

It is important to keep the call going up the inheritance chain in order to execute the logic of the superclass and any other extensions in between.

At the end of the static factory, we return the new instance of our class:

      return new this()

Here we extend the say function to add "hello" before "world":

    say() { return `hello ${super.say()}` }

Industry provides a similar API as Map to modify the extension hierarchy of a factory:

export default factory(Hello)
  .set("my_extension", ext)

For a continuation of the above extension, take a look at Instance.

Existing extensions

Inverse Labs has already authored a few extensions that you can use out of the box:

  • Chain - Chain synchronous or async methods using a common parameter namespace.
  • Exception - Pretty error logging for StandardIO functions.
  • Functions - Retrieve factory instance and class methods.
  • Include - Provides a dependency tree object to your method parameters.
  • Instance - Defines a factory function that builds instances.
  • Pattern - Pattern matching on factory class and instance methods.
  • StandardIO - Adds StandardIO compliance to factory methods.
  • State - Immutable state for your factories.