Conditions | 5 |
Total Lines | 59 |
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 | // type SubMap<K extends ClassNamesMap> = {[k in keyof K]?: ClassHash} |
||
50 | // type ToggleMap<K extends ClassNamesMap> = {[k in keyof K]?: boolean} |
||
51 | |||
52 | /** |
||
53 | * @example const classes = classNamingCtx(this.props) |
||
54 | * @example const classes = classNamingCtx({className, classnames}) |
||
55 | * @example const classes = classNamingCtx({classnames}) |
||
56 | */ |
||
57 | |||
58 | function classNaming< |
||
59 | //TODO `extends ReactRelated` |
||
60 | Source extends ClassNamesMap |
||
61 | >( |
||
62 | this: void | ClassNamingThis<Source>, |
||
63 | // arg0?: typeof this extends void ? ClassNamingContext<Source> : (true | ToggleMap<Source>), |
||
64 | // arg1?: typeof this extends void ? never : typeof arg0 extends true ? ToggleMap<Source> : never, |
||
65 | arg0?: ClassNamingContext<Source> | (string | true | ActionsMap<Source>), |
||
66 | arg1?: [Extract<typeof arg0, true|string>] extends [never] ? never : ActionsMap<Source> |
||
67 | ): ClassNaming<Source> { |
||
68 | // istanbul ignore next //TODO Solve TS tricks with context |
||
69 | const thisArg = this || {} |
||
70 | |||
71 | context_assign: |
||
72 | if ( |
||
73 | !(stackedKey in thisArg) |
||
74 | && typeof arg0 === "object" |
||
75 | ) { |
||
76 | const {classnames, className} = arg0 // as ClassNamingContext<Source> |
||
77 | if ( |
||
78 | classnames !== null && typeof classnames === "object" |
||
79 | && ( |
||
80 | className === undefined || typeof className === "string" |
||
81 | ) |
||
82 | ) { |
||
83 | emptize(classnames) |
||
84 | const host: ClassNamingCall<Source> = classNaming.bind({classnames, className, [stackedKey]: undefined}) |
||
85 | |||
86 | return wrapper(host, className) |
||
87 | } |
||
88 | } |
||
89 | |||
90 | const { |
||
91 | className, |
||
92 | classnames, |
||
93 | [stackedKey]: preStacked, |
||
94 | } = thisArg as ClassNamingThis<Source> |
||
95 | , withPropagation = arg0 === true |
||
96 | , source = typeof arg0 === "object" ? arg0 as ActionsMap<Source>: arg1 |
||
97 | , allowed = source && resolver(classnames, source) |
||
98 | , withInjection = typeof arg0 !== "string" ? preStacked : joinWithLead(preStacked, arg0) |
||
99 | , stacked = joinWithLead(withInjection, allowed) |
||
100 | , result = joinWithLead(withPropagation && className, stacked) |
||
101 | , host: ClassNamingCall<Source> = classNaming.bind({classnames, className, [stackedKey]: stacked}) |
||
102 | |||
103 | classnames && emptize(classnames) |
||
104 | |||
105 | return wrapper( |
||
106 | host, |
||
107 | result, |
||
108 | ) |
||
111 |