Conditions | 4 |
Total Lines | 52 |
Code Lines | 23 |
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 { CssModule } from "./defs" |
||
19 | |||
20 | /** |
||
21 | * Makes `className` string or settle context |
||
22 | * @example |
||
23 | * // To set context |
||
24 | * const classes = classNaming({classnames, className}) |
||
25 | * |
||
26 | * // Using in Components |
||
27 | * <div {...classNaming(...)} data-block={`${classNaming(...)}`} /> |
||
28 | * <Component {...{ |
||
29 | * ...classNaming(...)}, |
||
30 | * ...classnames |
||
31 | * }/> |
||
32 | * |
||
33 | * // With destructed `classnames` |
||
34 | * classNaming(className?, {App__Container, App__Item})} /> |
||
35 | * |
||
36 | * // Toggler |
||
37 | * classNaming(true?, {Btn_Float: true, Btn___disabled: false}) |
||
38 | |||
39 | * // Pipe-able |
||
40 | * const Cell = classNaming(className), Col1 = Cell({Column_1}) |
||
41 | * <div {...Col1({Row_1})} /> |
||
42 | */ |
||
43 | |||
44 | function classNaming< |
||
45 | //TODO #8 `extends ReactRelated` |
||
46 | Source extends CssModule |
||
47 | >( |
||
48 | arg0: ClassNamingContext<Source> | (string | true | ActionsMap<Source>), |
||
49 | arg1?: [Extract<typeof arg0, true | string>] extends [never] ? never : ActionsMap<Source> |
||
50 | ) { |
||
51 | if (arg0 !== null && typeof arg0 === "object" && "classnames" in arg0) { |
||
52 | const {classnames, className: cn} = arg0 |
||
53 | if (classnames !== null && typeof classnames === "object") { |
||
54 | emptize(classnames) |
||
55 | |||
56 | const className = typeof cn === "string" ? cn : undefined |
||
57 | , host: ClassNamingCall<Source> = _classNaming.bind({ |
||
58 | classnames, |
||
59 | className, |
||
60 | [stackedKey]: undefined |
||
61 | }) |
||
62 | |||
63 | return wrapper(host, className) |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return _classNaming.call( |
||
68 | defaultCtx, |
||
69 | //@ts-expect-error TS forgets about previous `if` |
||
70 | arg0, // as Exclude<typeof arg0, ClassNamingContext<Source>>, |
||
71 | arg1 |
||
104 |