Conditions | 7 |
Total Lines | 59 |
Code Lines | 45 |
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 { Falsy, ToggleMap, ClassValue, ClassNamer, ClassNamed, ClassNamesMap, EmptyObject } from "./defs" |
||
56 | |||
57 | function classNamer< |
||
58 | ClassKeys extends string, |
||
59 | withClassNames extends boolean|undefined |
||
60 | >( |
||
61 | this: Partial<ClassNamer<ClassKeys> & ClassNamerOptions<withClassNames>>, |
||
62 | arg0?: true | ToggleMap<ClassKeys> | ClassKeys, |
||
63 | arg1?: ToggleMap<ClassKeys> | ClassKeys, |
||
64 | ...args: (ClassKeys | Falsy)[] |
||
65 | ): ClassNamed & Partial<Pick<typeof this, "classNames">> { |
||
66 | const { |
||
67 | className: _propagated, |
||
68 | classNames, |
||
69 | withClassNames, |
||
70 | // withSelf |
||
71 | } = this |
||
72 | , withPropagation = arg0 === true |
||
73 | , allowed: ClassKeys[] = truthyKeys(arg0 === true ? false : arg0) |
||
74 | .concat(truthyKeys(arg1)) |
||
75 | //@ts-expect-error |
||
76 | .concat(args) |
||
77 | .filter<ClassKeys>( |
||
78 | //@ts-expect-error |
||
79 | Boolean |
||
80 | ) |
||
81 | |||
82 | emptize(classNames) |
||
83 | |||
84 | for (let i = allowed.length; i--;) { |
||
85 | const key = allowed[i] |
||
86 | , hash: ClassValue = classNames?.[key] |
||
87 | |||
88 | if (hash !== undefined) |
||
89 | //@ts-expect-error |
||
90 | allowed[i] = hash |
||
91 | } |
||
92 | |||
93 | const allowedString = allowed.join(" ") |
||
94 | , propagated = withPropagation && _propagated || "" |
||
95 | |||
96 | //TODO Consider undefined|empty|never for type error |
||
97 | , className = `${ |
||
98 | propagated |
||
99 | }${ |
||
100 | propagated && allowedString |
||
101 | ? " " |
||
102 | : "" |
||
103 | }${ |
||
104 | allowedString |
||
105 | }` |
||
106 | |||
107 | if (!withClassNames) { |
||
108 | return stringifyClassNamed({ |
||
109 | className |
||
110 | }) |
||
111 | } else { |
||
112 | return stringifyClassNamed({ |
||
113 | className, |
||
114 | classNames |
||
115 | }) |
||
117 | } |