Conditions | 5 |
Total Lines | 57 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import type { ClassNamingContext, ClassNamed, ClassNamesMap, ClassHash } from "./defs" |
||
49 | |||
50 | /** |
||
51 | * @example const classes = classNamingCtx(this.props) |
||
52 | * @example const classes = classNamingCtx({className, classnames}) |
||
53 | * @example const classes = classNamingCtx({classnames}) |
||
54 | */ |
||
55 | |||
56 | function classNamingCtx< |
||
57 | //TODO `extends ReactRelated` |
||
58 | Source extends ClassNamesMap |
||
59 | >( |
||
60 | this: void | ClassNamingThis<Source>, |
||
61 | // arg0?: typeof this extends void ? ClassNamingContext<Source> : (true | ToggleMap<Source>), |
||
62 | // arg1?: typeof this extends void ? never : typeof arg0 extends true ? ToggleMap<Source> : never, |
||
63 | arg0?: ClassNamingContext<Source> | (string | true | ActionsMap<Source>), |
||
64 | arg1?: [Extract<typeof arg0, true|string>] extends [never] ? never : ActionsMap<Source> |
||
65 | ): ClassNaming<Source> { |
||
66 | // istanbul ignore next //TODO Solve TS tricks with context |
||
67 | const thisArg = this || {} |
||
68 | |||
69 | context_assign: |
||
70 | if ( |
||
71 | !(stackedKey in thisArg) |
||
72 | && typeof arg0 === "object" |
||
73 | ) { |
||
74 | const {classnames, className} = arg0 // as ClassNamingContext<Source> |
||
75 | if ( |
||
76 | classnames !== null && typeof classnames === "object" |
||
77 | && ( |
||
78 | className === undefined || typeof className === "string" |
||
79 | ) |
||
80 | ) { |
||
81 | emptize(classnames) |
||
82 | const host: ClassNamingCall<Source> = classNamingCtx.bind({classnames, className, [stackedKey]: undefined}) |
||
83 | |||
84 | return wrapper(host, className) |
||
85 | } |
||
86 | } |
||
87 | |||
88 | const { |
||
89 | className, |
||
90 | classnames, |
||
91 | [stackedKey]: preStacked, |
||
92 | } = thisArg as ClassNamingThis<Source> |
||
93 | , withPropagation = arg0 === true |
||
94 | , source = typeof arg0 === "object" ? arg0 as ActionsMap<Source>: arg1 |
||
95 | , allowed = source && resolver(classnames, source) |
||
96 | , withInjection = typeof arg0 !== "string" ? preStacked : joinWithLead(preStacked, arg0) |
||
97 | , stacked = joinWithLead(withInjection, allowed) |
||
98 | , result = joinWithLead(withPropagation && className, stacked) |
||
99 | , host: ClassNamingCall<Source> = classNamingCtx.bind({classnames, className, [stackedKey]: stacked}) |
||
100 | |||
101 | classnames && emptize(classnames) |
||
102 | |||
103 | return wrapper( |
||
104 | host, |
||
105 | result, |
||
106 | ) |
||
109 |