Skip to content

Commit f0dad39

Browse files
author
Vatroslav Vrbanic
committed
PropUtils.ts (wip) see #115
- ducktape-fix - use `Array.isArray() ` - refactor / add new methods to avoid redundant Array tcpe checks - removed `isPropType` method since it's not being used anywhere.
1 parent 754a5f1 commit f0dad39

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

src/utils/PropUtils.ts

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,54 @@ export default class PropUtils {
3131
console.warn(message)
3232
}
3333

34-
public static isPropType(p: any): boolean {
35-
if (p?.constructor === Array) return true
36-
if (p?.constructor === Vector3) return true
37-
if (p?.constructor === Color) return true
38-
if (p?.constructor === Matrix4) return true
39-
if (p?.constructor === Euler) return true
40-
if (p?.constructor === Quaternion) return true
41-
return false
42-
}
43-
4434
public static isArray3Nums(p: any): boolean {
45-
return p?.constructor === Array && p.length === 3 && !isNaN(p[0]) && !isNaN(p[1]) && !isNaN(p[2])
35+
return Array.isArray(p) && p.length === 3 && !isNaN(p[0]) && !isNaN(p[1]) && !isNaN(p[2])
4636
}
4737

48-
public static isArray(p: any): boolean {
49-
return p?.constructor === Array
38+
/** Check if an Array contains exactly 3 numbers. */
39+
public static is3Nums(p: any): boolean {
40+
return p.length === 3 && !isNaN(p[0]) && !isNaN(p[1]) && !isNaN(p[2])
5041
}
5142

5243
public static isColor(p: any): boolean {
53-
return p?.constructor === Color
44+
return p?.constructor?.name === "Color"
5445
}
5546

5647
public static isVector3(p: any): boolean {
57-
return p?.constructor === Vector3
48+
return p?.constructor?.name === "Vector3"
5849
}
5950

6051
public static isMatrix4(p: any): boolean {
61-
return p?.constructor === Matrix4
52+
return p?.constructor?.name === "Matrix4"
6253
}
6354

6455
public static isMatrix4ParamsArray(p: any): boolean {
65-
return p?.constructor === Array && p.length === 16 && p.every((el) => !isNaN(el))
56+
return Array.isArray(p) && p.length === 16 && p.every((el) => !isNaN(el))
57+
}
58+
59+
/** Check if an Array contains exactly 16 numbers. */
60+
public static isMatrix4Params(p: any): boolean {
61+
return p.length === 16 && p.every((el) => !isNaN(el))
6662
}
6763

6864
public static isEuler(p: any): boolean {
69-
return p?.constructor === Euler
65+
return p?.constructor?.name === "Euler"
7066
}
7167

7268
public static isEulerParamsArray(p: any): boolean {
7369
return (
74-
p?.constructor === Array &&
70+
Array.isArray(p) &&
71+
p.length === 4 &&
72+
!isNaN(p[0]) &&
73+
!isNaN(p[1]) &&
74+
!isNaN(p[2]) &&
75+
typeof p[3] === "string"
76+
)
77+
}
78+
79+
/** Check if an Array contains exactly 3 numbers and one string. */
80+
public static isEulerParams(p: any): boolean {
81+
return (
7582
p.length === 4 &&
7683
!isNaN(p[0]) &&
7784
!isNaN(p[1]) &&
@@ -81,19 +88,24 @@ export default class PropUtils {
8188
}
8289

8390
public static isQuaternion(p: any): boolean {
84-
return p?.constructor === Quaternion
91+
return p?.constructor?.name === "Quaternion"
8592
}
8693

8794
public static isQuaternionParamsArray(p: any): boolean {
88-
return p?.constructor === Array && p.length === 4 && p.every((el) => !isNaN(el))
95+
return Array.isArray(p) && p.length === 4 && p.every((el) => !isNaN(el))
96+
}
97+
98+
/** Check if an Array contains exactly 4 numbers. */
99+
public static isQuaternionParams(p: any): boolean {
100+
return p.length === 4 && p.every((el) => !isNaN(el))
89101
}
90102

91103
public static checkIfComplexValueType(val: any): ComplexValueType | undefined {
92104
if (Array.isArray(val)) {
93-
if (PropUtils.isArray3Nums(val)) return "Array3Nums"
94-
if (PropUtils.isEulerParamsArray(val)) return "EulerParamsArray"
95-
if (PropUtils.isQuaternionParamsArray(val)) return "QuaternionParamsArray"
96-
if (PropUtils.isMatrix4ParamsArray(val)) return "Matrix4ParamsArray"
105+
if (PropUtils.is3Nums(val)) return "Array3Nums"
106+
if (PropUtils.isEulerParams(val)) return "EulerParamsArray"
107+
if (PropUtils.isQuaternionParams(val)) return "QuaternionParamsArray"
108+
if (PropUtils.isMatrix4Params(val)) return "Matrix4ParamsArray"
97109
} else {
98110
if (PropUtils.isVector3(val)) return "Vector3"
99111
if (PropUtils.isColor(val)) return "Color"
@@ -109,11 +121,11 @@ export default class PropUtils {
109121
switch (complex) {
110122
case undefined:
111123
if (Array.isArray(val)) {
112-
PropUtils.isArray3Nums(val)
124+
PropUtils.is3Nums(val)
113125
? PropUtils.setRotArray3(obj, val as Parameters<Vector3["set"]>)
114-
: PropUtils.isEulerParamsArray(val)
126+
: PropUtils.isEulerParams(val)
115127
? PropUtils.setRotEulerArray(obj, val as Parameters<Euler["set"]>)
116-
: PropUtils.isQuaternionParamsArray(val)
128+
: PropUtils.isQuaternionParams(val)
117129
? PropUtils.setRotQuaternionArray(obj, val as Parameters<Quaternion["set"]>)
118130
: console.error("[ PropUtils ] -> setRotationFromValue : invalid 'rotation' value!", {
119131
obj: obj,

0 commit comments

Comments
 (0)