Total Complexity | 6 |
Complexity/F | 2 |
Lines of Code | 47 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { isClass, isFunction } from 'myrmidon'; |
||
2 | import { mergeConfigs } from './utils'; |
||
3 | import { |
||
4 | FunctionDecorator, |
||
5 | classMethodDecorator, |
||
6 | classDecorator |
||
7 | } from './decorators'; |
||
8 | import defaultConfig from './defaults'; |
||
9 | import { sanitizeRegexp } from './utils/sanitizers'; |
||
10 | |||
11 | const dfc = [ defaultConfig ]; |
||
12 | |||
13 | export class Decorator { |
||
14 | constructor(...opts) { |
||
15 | // eslint-disable-next-line no-constructor-return |
||
16 | return (...args) => { |
||
|
|||
17 | const config = mergeConfigs(args, opts, dfc); |
||
18 | |||
19 | return (target, methodName, descriptor) => { |
||
20 | if (methodName && descriptor) { |
||
21 | return classMethodDecorator( |
||
22 | { target, methodName, descriptor }, |
||
23 | config |
||
24 | ); |
||
25 | } |
||
26 | |||
27 | if (isClass(target)) { |
||
28 | return classDecorator(target, config); |
||
29 | } |
||
30 | |||
31 | if (isFunction(target)) { |
||
32 | const functionDecorator = new FunctionDecorator({ config }); |
||
33 | |||
34 | return functionDecorator.run(target); |
||
35 | } |
||
36 | |||
37 | throw new Error(`Can't decorate ${typeof target}, only functions, classes and class methods are allowed`); |
||
38 | }; |
||
39 | }; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | export default new Decorator(); |
||
44 | |||
45 | export { |
||
46 | sanitizeRegexp |
||
47 | }; |
||
48 | |||
49 |