@namchee/decora

1.0.5 • Public • Published

Decora

NPM Badge

Useful ECMAScript decorator for personal purposes. Compatible with both browser and NodeJS environment.

Although it's written in TypeScript, these decorators should be compatible with normal JS files as long as it supports ES7.

Will be updated indefinitely.

Installation

npm install @namchee/decora

TypeScript

Remember to toggle the experimentalDecorators option to true in your tsconfig.json

JavaScript

Install @babel/plugin-proposal-decorators and add it to your Babel configuration

@benchmark

Decorator Type: Method decorator

Add benchmarking functionality to a method from a class. Benchmark result will be written to console.

Will transform the decorated function into an asynchronous function.

class Example {
  @benchmark()
  logMe() {
    console.log('test');
  }
}

Parameters

Name Required? Values Default Value Description
metric false ['s', 'ms', 'ns'] 'ms' Metric to be used when logging function runtime
precision false number, n > 1 undefined Number of digits after comma. Passing undefined will print an abruptly long string.
stream true NodeJS.WritableStream process.stdout Destination for the log to be written to. Detailed Explanation. This option is ONLY available on NodeJS environment.

@timeout

Decorator Type: Method decorator

Limits a function execution time to a certain time limit. Will throw an error if timeout value exceeded.

Will transform the decorated function into an asynchronous function.

class Example {
  @benchmark(1000)
  timeoutMe() {
    setTimeoutPromise(5000); // will throw an error
  }
}

Parameters

Name Required? Values Default Value Description
time true number - Time limit for the function
metric false ['s', 'ms', 'ns'] 'ms' Metric to be used when logging function runtime

Readme

Keywords

Package Sidebar

Install

npm i @namchee/decora

Weekly Downloads

3

Version

1.0.5

License

MIT

Unpacked Size

45.5 kB

Total Files

30

Last publish

Collaborators

  • namcheee