GitXplorerGitXplorer
a

ts-guard-decorator

public
7 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
1f446bbec14210ae5593bfb5936a8aad72c0d76f

0.2.2

eecheung-amzn committed 8 years ago
Unverified
934e9797b8486a55878c34f5d3442efc651e39f9

Note Babel package, make test cases match with README

eecheung-amzn committed 8 years ago
Unverified
eea8248ff4f014355d513d082cbfa54964b68044

Minor edits

eecheung-amzn committed 8 years ago
Unverified
920870e01936cb4e508c1176520312e0b11dbec8

0.2.1

eecheung-amzn committed 8 years ago
Unverified
a3a070d99bc876bc6593c395d49948b68a400741

Include typings in package.json

eecheung-amzn committed 8 years ago
Unverified
f83f12a8136f7ad2c985c666b2b2f57d3ef2b1e2

0.2.0

eecheung-amzn committed 8 years ago

README

The README file for this repository.

ts-guard-decorator 🛡

Decorator for running a check before running a method.

NPM

Installation

npm install --save ts-guard-decorator

Usage

Decorators are supported in TypeScript or with Babel.

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;
    }
    // ...
  }
}

Arguments

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 testGuardFunc(arg1, arg2) {
  return arg1 === arg2;
}

class TestClass {
  @guard(true)
  guardTrue() {
    return true;
  }  //=> true

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

  @guard(true, 'hello')
  guardTrueRetVal() {
    return true;
  }  //=> true

  @guard(false, 'hello')
  guardFalseRetVal() {
    return true;
  }  //=> "hello"

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

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