Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/writetome51/get-class-modification-decorator
Returns a TypeScript ClassDecorator factory.
https://github.com/writetome51/get-class-modification-decorator
class decorator factory function typescript
Last synced: 18 days ago
JSON representation
Returns a TypeScript ClassDecorator factory.
- Host: GitHub
- URL: https://github.com/writetome51/get-class-modification-decorator
- Owner: writetome51
- License: mit
- Created: 2019-11-13T02:24:47.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-09T01:51:29.000Z (over 4 years ago)
- Last Synced: 2024-12-09T05:24:05.563Z (about 1 month ago)
- Topics: class, decorator, factory, function, typescript
- Language: TypeScript
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# getClassModificationDecorator(
modifyInstance: (
instance, decoratorArgs: any[]
) => void
): (...decoratorArgs) => ClassDecoratorReturns a TypeScript ClassDecorator factory. You set the decorator behavior with the
callback `modifyInstance()`. The decorator creates a new constructor for the class
being decorated. The new constructor first calls the original, then the class instance
is passed into `modifyInstance()`, where you manipulate it however you want. (The
prototype chain is kept intact, and the `instanceof` operator will still work.)## Examples
```ts
// Here we make a decorator that adds properties:export const add_properties = getClassModificationDecorator(
(instance, decoratorArgs: [object]) => {
let newProperties = decoratorArgs[0];
Object.assign(instance, newProperties);
}
);// Now use it:
@add_properties({hair: 'amazing', age: 50, income: 200000, wife: 'hot'})
export class Boss {
}@add_properties({address: '100 fleet street', income: 600000})
export class CEO extends Boss {
}let ceo = new CEO();
console.log(ceo);
/*************
CEO {
age: 50,
address: '100 fleet street',
hair: 'amazing',
income: 600000,
wife: 'hot'
}
**************/
```## Installation
```bash
npm i @writetome51/get-class-modification-decorator
```## Loading
```js
import {getClassModificationDecorator}
from '@writetome51/get-class-modification-decorator';
```