Skip to content

Commit f829369

Browse files
author
Andrii Kirmas
committed
Move classNamer to a separate function
1 parent dab05b9 commit f829369

File tree

1 file changed

+69
-51
lines changed

1 file changed

+69
-51
lines changed

src/ctx.ts

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,80 @@
11
import type { Falsy, ToggleMap, ClassValue, ClassNamer, ClassNamed } from "./defs"
22
import { emptize, stringifyClassNamed, truthyKeys } from "./utils"
33

4+
interface tClassNaming<ClassKeys extends string> {
5+
/**
6+
* @example classes(true) === props.className
7+
* @example classes({class1: true, class2: false}) === "class1"
8+
* @example classes("class3", false && "class4") === "class3"
9+
* @example classes(true, {class1: true, class2: false}, "class3", false && "class4") === `${props.className} class1 class3`
10+
*/
11+
// Using overloads will make error not in certain argument but on all call - 'No overload found'
12+
(
13+
propagate_or_map_or_expression: true | ToggleMap<ClassKeys> | ClassKeys | Falsy,
14+
map_or_expression?: (
15+
[Extract<typeof propagate_or_map_or_expression, true>] extends [never]
16+
? never
17+
: ToggleMap<ClassKeys>
18+
) | ClassKeys | Falsy,
19+
...expressions: (ClassKeys | Falsy)[]
20+
) : ClassNamed
21+
}
22+
423
export default classNamingCtx
524

6-
function classNamingCtx<ClassKeys extends string>(ctx: ClassNamer<ClassKeys>) {
25+
/**
26+
* @example const classes = classNamingCtx(this.props)
27+
* @example const classes = classNamingCtx({className, classNames})
28+
* @example const classes = classNamingCtx({classNames})
29+
*/
30+
function classNamingCtx<ClassKeys extends string>(ctx: ClassNamer<ClassKeys>): tClassNaming<ClassKeys> {
731
emptize(ctx.classNames)
8-
9-
const {classNames, className} = ctx
1032

11-
const classNamer = function classNamer/*<ClassKeys extends string>*/(
12-
// this: ClassNamer<ClassKeys>,
13-
//TODO (typeof className extends string ? true : never)
14-
arg0: ToggleMap<ClassKeys> | ClassKeys | true,
15-
arg1?: [Extract<typeof arg0, boolean>] extends [never]
16-
? (ClassKeys | Falsy)
17-
: (ToggleMap<ClassKeys> | ClassKeys | Falsy),
18-
...args: (ClassKeys | Falsy)[]
19-
): ClassNamed {
20-
// const {className, classNames} = this
21-
const withPropagation = arg0 === true
22-
, allowed: ClassKeys[] = truthyKeys(arg0 === true ? false : arg0)
23-
//@ts-expect-error
24-
.concat(truthyKeys(arg1))
33+
return classNamer.bind(ctx) as tClassNaming<ClassKeys>
34+
}
35+
36+
function classNamer<ClassKeys extends string>(
37+
this: ClassNamer<ClassKeys>,
38+
arg0: true | ToggleMap<ClassKeys> | ClassKeys,
39+
arg1?: ToggleMap<ClassKeys> | ClassKeys,
40+
...args: (ClassKeys | Falsy)[]
41+
): ClassNamed {
42+
const {className, classNames} = this
43+
const withPropagation = arg0 === true
44+
, allowed: ClassKeys[] = truthyKeys(arg0 === true ? false : arg0)
45+
//@ts-expect-error
46+
.concat(truthyKeys(arg1))
47+
//@ts-expect-error
48+
.concat(args)
49+
.filter<ClassKeys>(
2550
//@ts-expect-error
26-
.concat(args)
27-
.filter<ClassKeys>(
28-
//@ts-expect-error
29-
Boolean
30-
)
31-
32-
for (let i = allowed.length; i--;) {
33-
const key = allowed[i]
34-
, hash: ClassValue = classNames[key]
35-
36-
if (hash !== undefined)
37-
//@ts-expect-error
38-
allowed[i] = hash
39-
}
51+
Boolean
52+
)
53+
54+
for (let i = allowed.length; i--;) {
55+
const key = allowed[i]
56+
, hash: ClassValue = classNames[key]
4057

41-
const allowedString = allowed.join(" ")
42-
, propagated = withPropagation && className || ""
43-
, $return = {
44-
className: `${
45-
propagated
46-
}${
47-
propagated && allowedString
48-
? " "
49-
: ""
50-
}${
51-
allowedString
52-
}`
53-
}
54-
55-
stringifyClassNamed($return)
56-
57-
return $return
58-
}
58+
if (hash !== undefined)
59+
//@ts-expect-error
60+
allowed[i] = hash
61+
}
5962

60-
return classNamer
61-
}
63+
const allowedString = allowed.join(" ")
64+
, propagated = withPropagation && className || ""
65+
, $return = {
66+
className: `${
67+
propagated
68+
}${
69+
propagated && allowedString
70+
? " "
71+
: ""
72+
}${
73+
allowedString
74+
}`
75+
}
76+
77+
stringifyClassNamed($return)
6278

79+
return $return
80+
}

0 commit comments

Comments
 (0)