Skip to content

Commit 563c0f5

Browse files
committed
refactor type castings
1 parent 4d8bb53 commit 563c0f5

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/index.ts

+24-20
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,23 @@ function waitFrames<T>({
2121

2222
if (!isWin) {
2323
const isJquery = Cypress.dom.isJquery(subject)
24-
if (!isJquery) {throw new Error(targetErr)}
24+
if (!isJquery) {
25+
throw new Error(targetErr)
26+
}
2527

2628
target = (subject as JQuery<HTMLElement | SVGElement>)['0']
2729
}
2830

29-
if (!target) {throw new Error(targetErr)}
30-
if (!Array.isArray(property) && typeof property !== 'string') {throw new Error(propsErr)}
31+
if (!target) {
32+
throw new Error(targetErr)
33+
}
34+
if (!Array.isArray(property) && typeof property !== 'string') {
35+
throw new Error(propsErr)
36+
}
3137

32-
if (typeof property === 'string') {property = [property]}
38+
if (typeof property === 'string') {
39+
property = [property]
40+
}
3341

3442
return await Cypress.Promise.all(
3543
property.map((prop) =>
@@ -54,40 +62,36 @@ function getValue<T>({ isWin, cyWin, target, prop }: GetValueOptions<T>): Primit
5462

5563
const emptyValue = Symbol('')
5664

65+
type Key = keyof typeof target
66+
5767
if (typeof prop === 'string' && prop.includes('.')) {
58-
let objValue: symbol | Primitive = emptyValue
68+
let objValue: Primitive | symbol = emptyValue
5969

6070
const [objOrMethod, _prop] = prop.split('.')
6171

62-
if (typeof target[objOrMethod as keyof typeof target] === 'function') {
63-
objValue = (
64-
(target as HTMLElement)[objOrMethod as keyof HTMLElement] as CallableFunction
65-
)?.()?.[_prop]
72+
if (typeof target[objOrMethod as Key] === 'function') {
73+
objValue = (target[objOrMethod as Key] as CallableFunction)()?.[_prop]
6674
}
6775

68-
if (typeof target[objOrMethod as keyof typeof target] === 'object') {
69-
objValue = (
70-
(target as HTMLElement)[objOrMethod as keyof HTMLElement] as Record<string, Primitive>
71-
)?.[_prop]
76+
if (typeof target[objOrMethod as Key] === 'object') {
77+
objValue = (target[objOrMethod as Key] as Record<string, any>)?.[_prop]
7278
}
7379

74-
if (objValue !== emptyValue) {return objValue as Primitive}
80+
if (objValue !== emptyValue && isPrimitive(objValue)) {return objValue as Primitive}
7581

7682
throw new Error(
77-
`${ERR} Invalid or unsupported ${isWin ? 'window' : ''} property: ${prop as string}`
83+
`${ERR} Invalid or unsupported ${isWin ? 'window' : ''} property: ${prop as Key}`
7884
)
7985
}
8086

81-
if (prop in target && isPrimitive(target[prop as keyof typeof target])) {
82-
return target[prop as keyof typeof target] as Primitive
83-
}
87+
if (prop in target && isPrimitive(target[prop as Key])) {return target[prop as Key] as Primitive}
8488

85-
if (isWin) {throw new Error(`${ERR} Invalid window property: ${prop as string}`)}
89+
if (isWin) {throw new Error(`${ERR} Invalid window property: ${prop as Key}`)}
8690

8791
if (typeof prop === 'string' && prop.startsWith('--')) {return getCSS()}
8892

8993
if (!(prop in cyWin.getComputedStyle(target as HTMLElement))) {
90-
throw new Error(`${ERR} Invalid element DOM/CSS property: ${prop as string}`)
94+
throw new Error(`${ERR} Invalid element DOM/CSS property: ${prop as Key}`)
9195
}
9296

9397
return getCSS()

0 commit comments

Comments
 (0)