|
| 1 | +import { |
| 2 | + isNil, |
| 3 | + isVec2, |
| 4 | + isVec3, |
| 5 | + isMat2d, |
| 6 | + isMat4, |
| 7 | + isVec2Object, |
| 8 | + isVec3Object, |
| 9 | +} from './utils'; |
| 10 | + |
| 11 | +type PropTypes = { [key: string]: any }; |
| 12 | +interface PropTypeValidator { |
| 13 | + <P extends PropTypes>( |
| 14 | + props: P, |
| 15 | + propName: string & keyof P, |
| 16 | + componentName: string, |
| 17 | + ): null | Error; |
| 18 | +} |
| 19 | +interface PropTypeWithRequired extends PropTypeValidator { |
| 20 | + isRequired: PropTypeValidator; |
| 21 | +} |
| 22 | + |
| 23 | +function createPropType<P extends PropTypes>( |
| 24 | + validator: PropTypeValidator, |
| 25 | + isRequired: boolean, |
| 26 | +) { |
| 27 | + return (props: P, propName: string & keyof P, componentName: string) => { |
| 28 | + const prop = props[propName]; |
| 29 | + |
| 30 | + if (isNil(prop)) { |
| 31 | + if (isRequired) { |
| 32 | + return new Error( |
| 33 | + `${propName} is required in ${componentName} but received ${prop}`, |
| 34 | + ); |
| 35 | + } |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + return validator(props, propName, componentName); |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +const vec2ObjValidator = <P extends PropTypes>( |
| 44 | + props: P, |
| 45 | + propName: string & keyof P, |
| 46 | + componentName: string, |
| 47 | +) => { |
| 48 | + const passes = isVec2Object(props[propName]); |
| 49 | + return passes |
| 50 | + ? null |
| 51 | + : new Error(`${propName} in ${componentName} is not a vec2 shape`); |
| 52 | +}; |
| 53 | + |
| 54 | +export const vec2Obj: PropTypeWithRequired = createPropType( |
| 55 | + vec2ObjValidator, |
| 56 | + false, |
| 57 | +) as PropTypeWithRequired; |
| 58 | +vec2Obj.isRequired = createPropType(vec2ObjValidator, true); |
| 59 | + |
| 60 | +const vec3ObjValidator = <P extends PropTypes>( |
| 61 | + props: P, |
| 62 | + propName: string & keyof P, |
| 63 | + componentName: string, |
| 64 | +) => { |
| 65 | + const passes = isVec3Object(props[propName]); |
| 66 | + return passes |
| 67 | + ? null |
| 68 | + : new Error(`${propName} in ${componentName} is not a vec3 shape`); |
| 69 | +}; |
| 70 | + |
| 71 | +export const vec3Obj: PropTypeWithRequired = createPropType( |
| 72 | + vec3ObjValidator, |
| 73 | + false, |
| 74 | +) as PropTypeWithRequired; |
| 75 | +vec3Obj.isRequired = createPropType(vec3ObjValidator, true); |
| 76 | + |
| 77 | +const vec2GlMatrixValidator = <P extends PropTypes>( |
| 78 | + props: P, |
| 79 | + propName: string & keyof P, |
| 80 | + componentName: string, |
| 81 | +) => { |
| 82 | + const passes = isVec2(props[propName]); |
| 83 | + return passes |
| 84 | + ? null |
| 85 | + : new Error(`${propName} in ${componentName} is not a gl-matrix vec2`); |
| 86 | +}; |
| 87 | + |
| 88 | +export const vec2GlMatrix: PropTypeWithRequired = createPropType( |
| 89 | + vec2GlMatrixValidator, |
| 90 | + false, |
| 91 | +) as PropTypeWithRequired; |
| 92 | +vec2GlMatrix.isRequired = createPropType(vec2GlMatrixValidator, true); |
| 93 | + |
| 94 | +const vec3GlMatrixValidator = <P extends PropTypes>( |
| 95 | + props: P, |
| 96 | + propName: string & keyof P, |
| 97 | + componentName: string, |
| 98 | +) => { |
| 99 | + const passes = isVec3(props[propName]); |
| 100 | + return passes |
| 101 | + ? null |
| 102 | + : new Error(`${propName} in ${componentName} is not a gl-matrix vec3`); |
| 103 | +}; |
| 104 | +export const vec3GlMatrix: PropTypeWithRequired = createPropType( |
| 105 | + vec3GlMatrixValidator, |
| 106 | + false, |
| 107 | +) as PropTypeWithRequired; |
| 108 | +vec3GlMatrix.isRequired = createPropType(vec3GlMatrixValidator, true); |
| 109 | + |
| 110 | +const mat2dGlMatrixValidator = <P extends PropTypes>( |
| 111 | + props: P, |
| 112 | + propName: string & keyof P, |
| 113 | + componentName: string, |
| 114 | +) => { |
| 115 | + const passes = isMat2d(props[propName]); |
| 116 | + return passes |
| 117 | + ? null |
| 118 | + : new Error(`${propName} in ${componentName} is not a gl-matrix mat2d`); |
| 119 | +}; |
| 120 | + |
| 121 | +export const mat2dGlMatrix: PropTypeWithRequired = createPropType( |
| 122 | + mat2dGlMatrixValidator, |
| 123 | + false, |
| 124 | +) as PropTypeWithRequired; |
| 125 | +mat2dGlMatrix.isRequired = createPropType(mat2dGlMatrixValidator, true); |
| 126 | + |
| 127 | +export const mat4GlMatrixValidator = <P extends PropTypes>( |
| 128 | + props: P, |
| 129 | + propName: string & keyof P, |
| 130 | + componentName: string, |
| 131 | +) => { |
| 132 | + const passes = isMat4(props[propName]); |
| 133 | + return passes |
| 134 | + ? null |
| 135 | + : new Error(`${propName} in ${componentName} is not a gl-matrix mat4`); |
| 136 | +}; |
| 137 | + |
| 138 | +export const mat4GlMatrix: PropTypeWithRequired = createPropType( |
| 139 | + mat4GlMatrixValidator, |
| 140 | + false, |
| 141 | +) as PropTypeWithRequired; |
| 142 | +mat4GlMatrix.isRequired = createPropType(mat4GlMatrixValidator, true); |
0 commit comments