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

definite

v1.2.8

Published

[![Build Status](https://travis-ci.org/invrs/definite.svg?branch=master)](https://travis-ci.org/invrs/definite)

Downloads

26

Readme

Definite

Build Status

A pattern for writing concise and testable Javascript classes.

Definite supports mixins that can add option immutability, call stack graphing, and more.

Eliminate vague variables

First let's look at what its like to require and use a definite class:

import thing from "./thing"

thing() // new instance of Thing!

Why is this cool? Because when you call the function again, it returns the same instance:

import thing from "./thing"

thing() // new instance
thing() // same instance!

Everywhere you see thing, you know exactly what it is.

You can create multiple instances using a key:

import thing from "./thing"

thing("a") // new instance of Thing "a"
thing("a") // same instance of Thing "a"

thing("b") // new instance of Thing "b"
thing("b") // same instance of Thing "b"

One options API to rule them all

Classes can receive options as well:

import thing from "./thing"

thing({ hello: "world" }) // new instance with options
thing({ hello: "me" })    // update instance with options

And yes, options work with keys:

import thing from "./thing"

thing("a", { hello: "world" }) // new "a" instance with options
thing("a", { hello: "me" })    // update "b" instance with options

Do one thing and do it well

Introducing then:

import thing from "./thing"

thing().then() // do the thing

Think of then as the single entry point to executing any post-initialization logic.

And oh yeah, its chainable:

import thing from "./thing"
import otherThing from "./other-thing"

thing().then(otherThing(), () => {
  console.log("did all the things!")
})

Above we passed multiple thenable parameters to then to automatically chain them.

Thenable parameters can be promises, functions, or other definite classes.

How to define a class

Let's look at how we define a skeleton definite class:

// skeleton.js
//
import def from "definite"

export default def(class {
  constructor(options) {} // initialize options
  set(options) {}         // update options
  then(...args) {}        // returns definite instance, promise, or value
})

Now let's use the class we made:

import skeleton from "./skeleton"

skeleton({ a: true })  // calls `constructor(options)`
skeleton({ a: false }) // calls `set(options)`
skeleton().then()      // calls `then()`

Dependency autoloading

Its easy to autoload other definite classes in your project.

Configure a definite builder:

// lib/component.js
//
import definite from "definite"

export default definite({
  autoload: [
    `${__dirname}/../components`,
    `${__dirname}/../models`
  ]
})

And use it in your classes:

// components/hello.js
//
import component from "../lib/component"

class Hello {
  constructor() {
    this.components.thing()  // new instance of `components/thing`
    this.models.otherThing() // new instance of `models/other-thing`
  }
}

export default component(Hello)

Mixins

Its easy to extend your definite classes:

// lib/component.js
//
import def from "definite"

let mixin = (Extend) =>
  class extends Extend {
    constructor(options) {
      super(options)
    }
    set(options) {
      super(options)
    }
    then(...args) {
      return super(...args)
    }
  }

export default def({
  mixins: [ mixin ]
})

Then use it to define your classes:

// components/hello.js
//
import component from "../lib/component"

class Hello {
  constructor() {}
  set(options) {}
  then(...args) {}
})

export default component(Hello)

Definite runner

The def command automatically requires your definite class and executes then on it:

def path/to/class -o "{ option: true }"

Use -o to specify options to pass into the class constructor.