Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/forivall/meh-ts-decorator
Simple function decorator/wrapping util, in typescript
https://github.com/forivall/meh-ts-decorator
hacktoberfest hacktoberfest2020 typescript
Last synced: 2 months ago
JSON representation
Simple function decorator/wrapping util, in typescript
- Host: GitHub
- URL: https://github.com/forivall/meh-ts-decorator
- Owner: forivall
- License: isc
- Created: 2019-04-05T01:05:35.000Z (over 5 years ago)
- Default Branch: latest
- Last Pushed: 2024-09-04T09:43:48.000Z (4 months ago)
- Last Synced: 2024-10-14T09:57:47.837Z (3 months ago)
- Topics: hacktoberfest, hacktoberfest2020, typescript
- Language: TypeScript
- Homepage:
- Size: 1.3 MB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @forivall/decorator
Simple function decorator/wrapping util, in typescript
[![build status](https://github.com/forivall/meh-ts-decorator/actions/workflows/main.yml/badge.svg)](https://github.com/forivall/meh-ts-decorator/actions/workflows/main.yml)
[![dependency status](https://img.shields.io/david/forivall/meh-ts-decorator)](https://david-dm.org/forivall/meh-ts-decorator)
[![dependency status](https://badges.depfu.com/badges/019638eb029618a184c44b4b8a9ebeef/count.svg)](https://depfu.com/github/forivall/meh-ts-decorator?project_id=29951)
[![coverage status](https://coveralls.io/repos/github/forivall/meh-ts-decorator/badge.svg)](https://coveralls.io/github/forivall/meh-ts-decorator)
[![npm version](https://img.shields.io/npm/v/@forivall/decorator)](https://npm.im/@forivall/decorator)
[![bundle size](https://img.shields.io/bundlephobia/minzip/@forivall/decorator)](https://bundlephobia.com/package/@forivall/decorator)## Installation
```
npm install --save @forivall/decorator
```## Usage
Using the following wrapper function for our examples
```ts
function log(fn: F, label: string): F {
return (...args: Parameters) => {
console.log(`enter ${label}`)
const out: ReturnType = fn(...args)
console.log(`exit ${label}`)
return out
} as F
}
```### `wrap()`
```ts
import {wrap} from '@forivall/decorator'
const doStuff = wrap(log, () => console.log('stuff'), 'doStuff')
doStuff()
// Output:
// enter doStuff
// stuff
// exit doStuff
```### `decorator()`
Create a decorator function
```ts
import decorator from '@forivall/decorator'
const logDecorator = decorator(log)
class Stuff {
@logDecorator('doStuff')
doIt() {
console.log('stuff')
}
}
new Stuff().doIt()
// Output:
// enter doStuff
// stuff
// exit doStuff
```### `proxied()`
Add metadata to a function that proxies another. Useful for the proxy pattern.
```ts
class Stuff {
doIt() {
console.log('stuff')
}
somethingElse() {
console.log('more stuff')
}
}import {proxied} from '@forivall/decorator'
class StuffWrapper {
stuff = new Stuff()
}
interface StuffWrapper implements Stuff {}
Object.keys(Stuff.prototype).forEach((k) => {
StuffWrapper.prototype[k] = proxied(Stuff, k, function(this: StuffWrapper, ...args: unknown[]) {
return this.stuff[k](...args)
})
})
```## Credits
[Emily Marigold Klassen](https://github.com/forivall/)## License
ISC