singletonify.js

Implementation of Singleton design pattern

Usage no npm install needed!

<script type="module">
  import singletonifyJs from 'https://cdn.skypack.dev/singletonify.js';
</script>

README

singletonify.js

Create Singleton version of a class with ease

Commitizen friendly

Installation

Via NPM

$ npm install singletonify.js

Via Yarn

$ yarn add singletonify.js

Usage

// ESM
import { Singletonify } from "singletonify.js";

// CommonJS
// const { Singletonify } = require("singletonify.js");

class Car {
  brand: string;

  constructor() {
    this.brand = "Mercedes";
  }
}

const SingletonifiedCar = Singletonify.singletonify(Car);
const car = SingletonifiedCar.getInstance();
console.log(car === SingletonifiedCar.getInstance()) // true

API

Library exports the Singletonify class.

static singletonify(Class)

The Singletonify class implements singletonify static method which accepts any class as an argument and returns a singletonified copy of it.

Tests

The tests are launched by the following command:

npm run test

Build

A production ready bundle is built using npm run build command.

After a successful build the following files will be generated in the dist folder:

├── singletonify.common.js
├── singletonify.esm.js
├── singletonify.js

License

MIT