Socket
Book a DemoInstallSign in
Socket

@alt-javascript/decorates

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alt-javascript/decorates

A simple convention-based class decorator pattern assertion function, supporting strings and classes with static .is and .with properties.

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

A Simple Decorator Pattern For JavaScript

NPM
Language Badge Package Badge
release notes

Introduction

A simple convention-based class decorator pattern assertion function, supporting strings and classes with static .is and .with properties.

Usage

Define yourself some "Decorators" as so, taking note of the funky wrapping function convention (it's important for property decorators with parameters).

const Spicy  = class Spicy {
}
module.exports = function (name,exists) { return new Spicy();}

Define a class, and decorate it as so

const Sour = require('../decorators/Sour');
const Spicy = require('../decorators/Spicy');

module.exports = new class TomYum extends Dish {
    static is = 'Soup';
    static with = ['Sweet',Sour(),Spicy];

    constructor(){
        super ('Thai')// .. => this.origin = origin;
    }
}

All good, now you can use the assertion function to check if your class is decorated. The function converts the Decorator name down to string, so you can mix and match with just simple strings if you are being super breezy.


const decorates = require('../decorates');
const Sour = require('./decorators/Sour');
const Salty = require('./decorated/Salty');
const TomYum = require('./decorated/TomYum');

//...

assert.isTrue(decorates(Sour,TomYum), "Sour decorates TomYum is true");
assert.isTrue(decorates('Spicy',TomYum), "'Spicy' decorates TomYum is true");
assert.isFalse(decorates(Salty(),TomYum), "Salty() decorates TomYum is false");
//...
assert.isTrue(decorates('Thai', (new TomYum).origin), "Thai decorates tomYum.origin is true");

Might come in useful.

Keywords

decorator

FAQs

Package last updated on 16 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.