Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-guard-decorator

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-guard-decorator

TypeScript decorator for running a check before running a method.

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

ts-guard-decorator 🛡

TypeScript decorator for running a check before running a method.

NPM

Installation

npm install --save ts-guard-decorator

Usage

import guard from 'ts-guard-decorator';

class MyClass {
  // Don't run `myFunc` if `window` doesn't exist
  @guard(typeof window !== 'undefined')
  myFunc() {
    // ...
  }
}

This is equivalent to writing:

class MyClass {
  myFunc() {
    if (typeof window === 'undefined') {
      return;
    }
    // ...
  }
}

Options

The guard accepts 2 arguments:

  1. A boolean expression (i.e. something that evaluates to true or false) indicating whether the method should run.
  2. A optional return value if the method should not run.
function myGuardFunc(arg1: any, arg2: any): boolean {
  return arg1 === arg2;
}

class MyClass {
  @guard(true)
  myFunc1() {
    return true;
  }  //=> true

  @guard(false)
  myFunc2() {
    return true;
  }  //=> undefined

  @guard(1 === 1)
  myFunc3() {
    return true;
  }  //=> true

  @guard(1 === 2, 'hello')
  myFunc4() {
    return true;
  }  //=> "hello"

  @guard(myGuardFunc(1, 1), 'hello')
  myFunc5() {
    return true;
  }  //=> true

  @guard(myGuardFunc(1, 2), 'hello')
  myFunc6() {
    return true;
  }  //=> "hello"
}

FAQs

Package last updated on 11 Apr 2017

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc