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

ts-toggle

v2.0.1

Published

A simple and flexible feature toggle system based on Typescript decorators and classes

Downloads

8

Readme

ts-toggle

Install

In terminal, run:

npm i ts-toggle

Usage

Import

You can import the decorator :

import { Toggle } from 'ts-toggle';

You can also import the toggler class to manage your togglable classes :

// For the toggler class
import { Toggler } from 'ts-toggle';

// For the classes extending Togglable
import { Togglable } from 'ts-toggle';

Decorate methods

You can decorate any method to easily enable/disable them, based on the enable parameter. This parameter can either be a boolean or a function to be called at runtime. The decorator allows you to granularly toggle some methods in your app.

Examples:

class Demo {

	@Toggle(true)
	foo(): void {
		console.log('Enabled!');
	}

	@Toggle(enable)
	bar(): void {
		console.log('Disabled?');
	}

	enable(config): boolean {
		return config.feature.enabled;
	}
}

You can also provide an optional fallback function that will be called if feature is disabled.

Examples:

class Demo {

	@Toggle(false, (...args) => { console.log('Feature disabled.', { args }); })
	foo(...args): void {
		console.log('Enabled!');
	}
}

Manage (non-)Togglable classes with the Toggler

You can create one or multiple Togglers to handle your classes extending the Togglable class. This way, entire components can be toggled on and off easily.

Togglable class

A class extending Togglable can be managed through the Toggler. To instantiate such a class, it must be constructed with to parameters:

  • isTogglable: Whether this class can be toggled on/off
  • isEnabledByDefault: Whether this class should be toggled on by default or not

If isTogglable is set to false, the class will be enabled/disabled according to the value of isEnabledByDefault and won't be togglable after instantiation.

Your class can also use the method tg_ThrowIfDisabled to throw where needed (and an optional custom error message can be passed).

Examples:

class Feature extends Togglable {
	constructor(isTogglable: boolean, isEnabledByDefault: boolean) {
		super(isTogglable, isEnabledByDefault);
	}

	foo() {
		this.tg_ThrowIfDisabled('Feature is disabled');

		// ...do something if enabled
	}
}

Toggler

The Toggler is used to hold references of Togglable class you instantiate, so you can get their state and toggle them whenever you want at runtime.

If isTogglable is set to false, the class will be enabled/disabled according to the value of isEnabledByDefault and won't be togglable after instantiation.

Examples:

// FeatureA class extends Togglable, is togglable and enabled by default
const featureA = new FeatureA(true, true);

const toggler = new Toggler();

toggler.addTogglableRef('Feature A', featureA);

...

// Somewhere else in the code (for instance, admin methods)
getFeatureStates(): Record<string, boolean> {
	return toggler.getAllTogglableStates();
}

toggleFeature(feature: string): boolean {
	return toggler.toggleTogglableState(feature);
}

Contribute

Please feel free to suggest improvements, features or bug fix through Git issues. Pull Requests for that are also more than welcome.

Keywords

toggle feature switch killswitch flag decorator typescript node