# decorate-this

> Simple, vanilla JS type checking through ES7 decorators ...and a few other decorators, to boot.

Latest version **0.5.0** (published 2015-06-08) · ALv2.0 license · 0 weekly downloads

## Install

```sh
npm install decorate-this
pnpm add decorate-this
yarn add decorate-this
bun add decorate-this
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2015-06-08 |
| First published | 2015-05-27 |
| Weekly downloads | 0 |
| License | ALv2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 536 |
| Author | Jake Scott |
| Maintainers | mako-taco |
| Keywords | decorators, types, annotation |

## Links

- npm: https://www.npmjs.com/package/decorate-this
- Repository: https://github.com/mako-taco/DecorateThis
- Issues: https://github.com/mako-taco/DecorateThis/issues
- npm.io page: https://npm.io/package/decorate-this

## Dependencies (3)

- [css-loader](https://npm.io/package/css-loader.md) ^0.9.1
- [style-loader](https://npm.io/package/style-loader.md) ^0.9.0
- [babel-runtime](https://npm.io/package/babel-runtime.md) ^5.4.7

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.5.0 (latest) — 2015-06-08
- 0.4.0 — 2015-06-04
- 0.3.5 — 2015-06-02
- 0.3.4 — 2015-06-02
- 0.3.3 — 2015-06-02
- 0.3.2 — 2015-06-02
- 0.3.1 — 2015-06-02
- 0.3.0 — 2015-06-02
- 0.2.4 — 2015-06-02
- 0.2.3 — 2015-06-02
- 0.2.2 — 2015-06-01
- 0.2.1 — 2015-06-01
- 0.2.0 — 2015-06-01
- 0.1.0 — 2015-05-31
- 0.0.3 — 2015-05-27
- … 2 more at https://npm.io/package/decorate-this/versions

## README

# DecorateThis
Simple, vanilla JS type checking through ES7 decorators
...and a few other decorators, to boot.

- [Installation](#incorporating-in-your-project)
- [Changelog](/docs/CHANGELOG.md)
- Documentation
  - [Type validation](#type-validation)
    - [param](#type-validation)
    - [returns](#type-validation)
    - [promises](#promises)
    - [Validating Complex Types](/docs/TYPE_VALIDATOR_API.md)
  - [memoize](#memoization)
  - [debounce](#debouncing)
  - [curry](#currying)
  - [configurable](#property-descriptors)
  - [writable](#property-descriptors)
  - [enumerable](#property-descriptors)

If you like this project, be sure to check out [FluxThis](https://github.com/addthis/FluxThis), the immutable Flux framework by [AddThis](http://www.addthis.com).

[![npm version](https://badge.fury.io/js/decorate-this.svg)](http://badge.fury.io/js/decorate-this)
[![Build Status](https://travis-ci.org/mako-taco/DecorateThis.svg?branch=master)](https://travis-ci.org/mako-taco/DecorateThis)

# Type Validation
Throw errors when unexpected types are provided or returned from class or object
functions. For more details, see the [Type Validator API](/docs/TYPE_VALIDATOR_API.md).
```js
import {param, returns} from 'decorate-this';

class Point {
    constructor() {
        this.x = 0;
        this.y = 0;
    }

    // The first param of this method takes a Point, and will throw
    // a type error if a non-Point is passed
    @param(Point)
    // The method returns a Number. If it doesn't, a type error will
    // be thrown before the value is returned.
    @returns(Number)
    distanceTo(point) {
        let squaredDistance = (point.x - this.x) ** 2 +
            (point.y - this.y) ** 2;

        return Math.sqrt(squaredDistance);
    }

    // Two number args, no return value
    @param(Number)
    @param(Number)
    addToDimensions(x, y) {
        this.x += x;
        this.y += y;
    }
}
```

# Memoization
Automatically memoize functions for greater efficiency
```js
import memoize from 'decorate-this';
let obj = {
    // Results of the function are stored in a map, which maps arguments
    // to the function's result. This expensive func is only run a single
    // time for a given a/b pair.
    @memoize
    expensiveFunc(a, b) {
        return Math.sin(Math.sqrt(a ** b));
    }
};
```

# Property descriptors
```js
import {enumerable, writable, configurable} from 'decorate-this';

class T {
    @configurable(false)
    @enumerable(false)
    @writable(false)
    hiddenMethod() {
        /* ... */
    }
}
```

# Currying
Build up long argument lists with currying
```js
let obj = {
    @curry
    curriedAdd(a, b, c) {
        return a + b + c;
    }
}

let addToFive = obj.curriedAdd(5);   // Function
let addToFiveAndThree = addToFive(3);  // Function
let sum = addToFiveAndThree(7);      // 15
```

# Debouncing
Rate-limit expensive or frequently called functions

```js
let obj = {
    @debounce(500) // call after 500ms of no further calls
    debouncedFn(event) {
        console.log(event.clientX, event.clientY);
    }
}
```

# Promises
Similar to `returns`, but validates the fulfillment value of a promise
```js
let obj = {
    @promises(ArrayOf(Number))
    getPoints() {
        return new Promise(resolve => {
            setTimeout(() => resolve([1, 2, 3]), 5000);
        });
    }
}
```

# Incorporating in your project
```
npm install decorate-this
```

- Build your project with Babel
- Enable stage 1 (experimental) features
- Profit

---
_Source: https://npm.io/package/decorate-this · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
