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 🙏

© 2026 – Pkg Stats / Ryan Hefner

demo-decorator

v0.3.1

Published

Conditionally replaces a class at runtime with another one

Readme

demo-decorator

Conditionally replaces a class at runtime with another one. No dependencies.

Use cases

  • An enduser wants to access your mobile app/web app without creating an account or any access to the backend at all (demo mode playground)
  • A developer wants to create a mobile app/web app prototype without real API/storage access (prototyping)
  • A developer wants to create and configure different scenarios for UI testing (mocking)

Installation

Install using npm:

npm install --save demo-decorator

Install using yarn:

yarn add demo-decorator

Configuration

If you want to use decorators add the following to your tsconfig.json

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

If you also use React Native you have to adjust your babel.config.js and install the plugins

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ['@babel/plugin-transform-flow-strip-types'],
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    ['@babel/plugin-proposal-class-properties', { loose: true }],
  ],
};

Usage

function Demo(demoInstance: Object, isDemoEnabled: () => boolean)

The first parameter is the instance/object whose methods are called in case the demo mode is active.

The second parameter is a function that is called before each method call the determine if the demo mode is active or not.

If demo mode is active and a method from the original class is not specified in the demo instance then the original method is called.

Example with decorator

Demo mode state is stored in a variable but you are free to use any other method (e.g call a custom function) to determine the state at runtime.

import Demo from 'demo-decorator';

let active = true;

class SampleApiMock {
    add(a: number, b: number): number {
        return 42;
    }
    doSomething(): Promise<string> {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve("Hello Demo!")
            }, 1500);
        })
    }
}

@Demo(new SampleApiMock(), () => active)
class SampleApi {
    add(a: number, b: number): number {
        return a + b;
    }
    doSomething(): Promise<string> {
        return Promise.resolve("Hello World!");
    }
    theone(): string {
        return "the one and only";
    }
}

(async () => {
    const sampleApi = new SampleApi();

    console.log('add', sampleApi.add(2, 3)); // add 42
    console.log('doSomething', await sampleApi.doSomething()); // doSomething Hello Demo!
    console.log('theone', sampleApi.theone()); // theone the one and only

    active = false;

    console.log('add', sampleApi.add(2, 3)); // add 5
    console.log('doSomething', await sampleApi.doSomething()); // doSomething Hello World!
    console.log('theone', sampleApi.theone()); // theone the one and only
  })();

Example without decorator

// ...

// @Demo(new SampleApiMock(), () => active)
class SampleApiCls {
    add(a: number, b: number): number {
        return a + b;
    }
    doSomething(): Promise<string> {
        return Promise.resolve("Hello World!");
    }
    theone(): string {
        return "the one and only";
    }
}

const SampleApi = Demo(new SampleApiMock(), () => active)(SampleApiCls);
const sampleApi = new SampleApi();

// ...

See unit tests src/demo-decorator.spec.ts for more examples.

Caveats

If the second parameter is an async function then every method gets async too.

Built With

  • Typescript - TypeScript is a typed superset of JavaScript that compiles to plain JavaScript

License

This project is licensed under the MIT License - see the LICENSE file for details.