Gathering detailed insights and metrics for ts-toggle
Gathering detailed insights and metrics for ts-toggle
npm install ts-toggle
Typescript
Module System
Node Version
NPM Version
68.3
Supply Chain
99.3
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (80.31%)
JavaScript (19.69%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
459
Last Day
1
Last Week
5
Last Month
32
Last Year
186
MIT License
6 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Dec 30, 2023
Minified
Minified + Gzipped
Latest Version
2.0.1
Package Id
ts-toggle@2.0.1
Unpacked Size
10.99 kB
Size
3.94 kB
File Count
5
NPM Version
10.2.3
Node Version
20.10.0
Published on
Jan 04, 2024
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
-58.3%
5
Compared to previous week
Last Month
255.6%
32
Compared to previous month
Last Year
-31.9%
186
Compared to previous year
A simple and flexible feature toggle system based on Typescript decorators and classes.
In terminal, run:
1npm i ts-toggle
You can import the decorator :
1import { Toggle } from 'ts-toggle';
You can also import the toggler class to manage your togglable classes :
1// For the toggler class 2import { Toggler } from 'ts-toggle'; 3 4// For the classes extending Togglable 5import { Togglable } from 'ts-toggle';
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:
1class Demo { 2 3 @Toggle(true) 4 foo(): void { 5 console.log('Enabled!'); 6 } 7 8 @Toggle(enable) 9 bar(): void { 10 console.log('Disabled?'); 11 } 12 13 enable(config): boolean { 14 return config.feature.enabled; 15 } 16}
You can also provide an optional fallback function that will be called if feature is disabled.
Examples:
1class Demo { 2 3 @Toggle(false, (...args) => { console.log('Feature disabled.', { args }); }) 4 foo(...args): void { 5 console.log('Enabled!'); 6 } 7}
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.
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/offisEnabledByDefault
: Whether this class should be toggled on by default or notIf 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:
1class Feature extends Togglable { 2 constructor(isTogglable: boolean, isEnabledByDefault: boolean) { 3 super(isTogglable, isEnabledByDefault); 4 } 5 6 foo() { 7 this.tg_ThrowIfDisabled('Feature is disabled'); 8 9 // ...do something if enabled 10 } 11}
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:
1// FeatureA class extends Togglable, is togglable and enabled by default 2const featureA = new FeatureA(true, true); 3 4const toggler = new Toggler(); 5 6toggler.addTogglableRef('Feature A', featureA); 7 8... 9 10// Somewhere else in the code (for instance, admin methods) 11getFeatureStates(): Record<string, boolean> { 12 return toggler.getAllTogglableStates(); 13} 14 15toggleFeature(feature: string): boolean { 16 return toggler.toggleTogglableState(feature); 17}
Please feel free to suggest improvements, features or bug fix through Git issues. Pull Requests for that are also more than welcome.
toggle
feature
switch
killswitch
flag
decorator
typescript
node
No vulnerabilities found.
No security vulnerabilities found.