Gathering detailed insights and metrics for ts-core-decorators
Gathering detailed insights and metrics for ts-core-decorators
Gathering detailed insights and metrics for ts-core-decorators
Gathering detailed insights and metrics for ts-core-decorators
npm install ts-core-decorators
Typescript
Module System
Node Version
NPM Version
71.5
Supply Chain
98.9
Quality
76
Maintenance
100
Vulnerability
99.6
License
TypeScript (99.85%)
JavaScript (0.15%)
Total Downloads
8,303
Last Day
4
Last Week
10
Last Month
59
Last Year
567
MIT License
1 Stars
20 Commits
2 Watchers
4 Branches
2 Contributors
Updated on Jan 27, 2023
Minified
Minified + Gzipped
Latest Version
1.0.33
Package Id
ts-core-decorators@1.0.33
Unpacked Size
40.04 kB
Size
11.93 kB
File Count
64
NPM Version
5.6.0
Node Version
8.9.4
Cumulative downloads
Total Downloads
Last Day
0%
4
Compared to previous day
Last Week
-64.3%
10
Compared to previous week
Last Month
5.4%
59
Compared to previous month
Last Year
29.2%
567
Compared to previous year
2
4
Motivation: decorators is cool :heart: :fire:
table of decorators:
decorator | arguments | description | target | service |
---|---|---|---|---|
log | toConosle - default is false | logging all method calls and set to logService | class | logService |
timerify | - | record function time execution. Also have statistic as total time and long execing function | method | performanceService |
select | map - Map<K,V> ot WeakMap | Select value by key from map like collection | property | - |
pure | - | checking function or argument for not undefined and throw error if is not pure | method, argument | - |
autowired | args: any[] | creating new instance of type annotation | property | - |
save | handler: () => never | make function save - if handler is not set - don't stop executing program | method | - |
trigger | ProxyObject | make class as Proxy. Make intercept functions, contructor calling and may redirect to new target | class | - |
usage: @autowired()
decorator:
1import { autowired } from '../src/decorators/common'; 2 3class Autowired { 4 public readonly someReadOnlyProp = 4; 5} 6 7class Autowired2 { 8 constructor(private prop: string) { 9 } 10} 11 12class Temp { 13 14 @autowired('123') 15 public autoWired2!: Autowired2; // create Autowired2 instance 16 17 @autowired() 18 public autoWired!: Autowired; // note: ! symbol for strict mode 19} 20 21const temp = new Temp(); 22console.dir(temp.autoWired); // Autowired {someReadOnlyProp: 4} 23console.dir(temp.autoWired2); // Autowired2 {prop: "123"}
usage: @log()
decorator
1import { log } from 'ts-core-decorators/core/decorators/common'; 2import { logService } from 'ts-core-decorators/core/services/log'; 3 4@log({ toConsole: true }) 5class Temp { 6 constructor() {} 7 8 someFn(test: number) { 9 return test; 10 } 11} 12 13const a = new Temp(); 14a.someFn(21); 15 16logService.writeToFile('./logger.json'); // writing result to file
another example:
1import { Log } from 'ts-core-decorators/core/decorators/common'; 2import { logService } from 'ts-core-decorators/core/services/log'; 3 4@Log() 5class Temp { 6 constructor() {} 7 8 someFn(test: number) { 9 return test; 10 } 11} 12 13const a = new Temp(); 14a.someFn(21); 15 16logService.writeToFile('./logger.json'); // writing result to file
usage: @timerify()
decorator
1import { timerify } from 'ts-core-decorators/core/decorators/performance'; 2 3import { PerformanceService } from 'ts-core-decorators/core/services/performance'; 4 5const performance = PerformanceService.connect(); // one time only. for example on prepare hook 6 7class Temp { 8 constructor() {} 9 10 @timerify() 11 public longTask() { 12 return 500; 13 } 14} 15 16new Temp().lognTask(); // usage 17 18performance.profiler.writeToFile('./profiler.json'); // write performance information to file 19performance.disconect(); // clear GC from performance information. for example on destroy
usage: @select()
decorator
1import { select } from 'ts-core-decorators/core/decorators/collections'; 2 3const map: Map<string, number> = new Map(); 4 5map.set('test', 123); 6map.set('test2', 4564); 7 8class Temp { 9 @select(map, 'test') 10 public test?: number; // 123 11 12 constructor() { 13 console.log(this.test); 14 } 15}
usage: @pure()
decorator
1import { pure } from 'ts-core-decorators/core/decorators/common'; 2 3class Temp { 4 constructor() {} 5 6 @pure<number>() 7 public someFn(@pure<number>() someArg?: number, anotherNumber?: number): number { 8 return someArg || 1; 9 } 10 11 @pure<number>() 12 public anotherFn(@pure<number>() someArg?: number, @pure<number>() anotherNumber?: number): number { 13 return someArg || 1; 14 } 15} 16const temp = new Temp(); 17 18temp.someFn(2, 3); 19temp.anotherFn(1); // runtime error here. someArg is defined, but anotherNumber is not
1import { save } from 'ts-core-decorators/core/decorators/common'; 2class SaveDecoratorExample { 3 @save() 4 public unsaveFunction(arg: any) { 5 return arg.length > 0; 6 } 7} 8const saveDecoratorExample = new SaveDecoratorExample(); 9 10console.log('start executing'); 11saveDecoratorExample.unsaveFunction(null); 12console.log('continue executing'); 13// start executing 14// cannot read property 'length' of null 15// continue executing 16
1import { trigger } from 'ts-core-decorators/core/decorators/common'; 2 3@trigger({ 4 construct: function (target: any, arrayList: any) { 5 const instance = new target(...arrayList) as Temp; 6 // can use Temp methods here 7 console.dir(instance); 8 return instance; 9 }, 10}) 11class Temp { 12 constructor() { } 13 14 public longTask() { 15 console.log('long task executed'); 16 return 500; 17 } 18} 19 20const temp = new Temp(); 21temp.longTask(); 22
More examples? Visit examples folder for more.
No vulnerabilities found.
No security vulnerabilities found.