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 - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

10

dist/guard.d.ts

@@ -1,11 +0,7 @@

export declare type GuardCheck = boolean;
export declare type GuardCheckResolver = (...args: any[]) => boolean;
export declare type GuardCheckOrResolver = GuardCheck | GuardCheckResolver;
/**
* A TS method decorator that determines if a method should run or not.
*
* @param check {GuardCheckOrResolver} A boolean value or a function that takes in arguments,
* returning a boolean.
* @param ...checkArgs {any[]} Arguments for a check function.
* @param shouldRun {boolean} Whether the method should run.
* @param retValue {any} An optional return value if the method shouldn't run.
*/
export default function (check: GuardCheckOrResolver, ...checkArgs: any[]): (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
export default function (shouldRun: boolean, retValue?: any): (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;

@@ -6,11 +6,6 @@ "use strict";

*
* @param check {GuardCheckOrResolver} A boolean value or a function that takes in arguments,
* returning a boolean.
* @param ...checkArgs {any[]} Arguments for a check function.
* @param shouldRun {boolean} Whether the method should run.
* @param retValue {any} An optional return value if the method shouldn't run.
*/
function default_1(check) {
var checkArgs = [];
for (var _i = 1; _i < arguments.length; _i++) {
checkArgs[_i - 1] = arguments[_i];
}
function default_1(shouldRun, retValue) {
return function (target, propertyKey, descriptor) {

@@ -23,7 +18,3 @@ var originalMethod = descriptor.value;

}
var shouldRun = (typeof check === 'function') ? check.apply(this, checkArgs) : check;
if (!shouldRun) {
return;
}
return originalMethod.apply(this, args);
return shouldRun ? originalMethod.apply(this, args) : retValue;
};

@@ -30,0 +21,0 @@ return descriptor;

{
"name": "ts-guard-decorator",
"version": "0.1.0",
"version": "0.2.0",
"description": "TypeScript decorator for running a check before running a method.",

@@ -24,3 +24,4 @@ "repository": {

"build:test": "tsc --project ./test/tsconfig.test.json",
"test": "npm run build:test && ava ./test/test.js"
"test": "npm run build && npm run build:test && ava ./test/test.js --verbose",
"prepublish": "npm run build && npm test"
},

@@ -27,0 +28,0 @@ "devDependencies": {

@@ -29,24 +29,22 @@ # ts-guard-decorator 🛡

The guard accepts 2 types of parameters:
This is equivalent to writing:
1. A boolean expression (i.e. something that evaluates to `true` or `false`).
```typescript
class MyClass {
@guard(true)
myFunc1() {} // runs
myFunc() {
if (typeof window === 'undefined') {
return;
}
// ...
}
}
```
@guard(false)
myFunc2() {} // won't run
@guard(1 === 1)
myFunc3() {} // runs
### Options
@guard(1 === 2)
myFunc4() {} // won't run
}
```
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.
2. A function reference and arguments.
```typescript

@@ -58,8 +56,32 @@ function myGuardFunc(arg1: any, arg2: any): boolean {

class MyClass {
@guard(myGuardFunc, 1, 1)
myFunc1() {} // runs
@guard(true)
myFunc1() {
return true;
} //=> true
@guard(myGuardFunc, 1, 2)
myFunc2() {} // won't run
@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"
}
```
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