Skip to content

Commit

Permalink
Use isThing rather than instanceof; add typedefs and tests (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherChudzicki authored Jan 12, 2023
1 parent eecb912 commit 558ffc2
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
*/
export * from "./types";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export declare const Primitives: any;
import * as Primitives from "./primitives";
export { Primitives };
14 changes: 7 additions & 7 deletions src/primitives/types/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ const _Types = {
value = [value];
}

if (value instanceof Vector2) {
if (value?.isVector2) {
target.copy(value);
} else if (value instanceof Array) {
value = value.concat(defaults.slice(value.length));
Expand Down Expand Up @@ -655,7 +655,7 @@ const _Types = {
value = [value];
}

if (value instanceof Vector3) {
if (value?.isVector3) {
target.copy(value);
} else if (value instanceof Array) {
value = value.concat(defaults.slice(value.length));
Expand Down Expand Up @@ -731,7 +731,7 @@ const _Types = {
value = [value];
}

if (value instanceof Vector4) {
if (value?.isVector4) {
target.copy(value);
} else if (value instanceof Array) {
value = value.concat(defaults.slice(value.length));
Expand Down Expand Up @@ -826,7 +826,7 @@ const _Types = {
return m;
},
validate(value, target, invalid) {
if (value instanceof Matrix3) {
if (value?.isMatrix3) {
target.copy(value);
} else if (value instanceof Array) {
value = value.concat(defaults.slice(value.length));
Expand Down Expand Up @@ -951,7 +951,7 @@ const _Types = {
return m;
},
validate(value, target, invalid) {
if (value instanceof Matrix4) {
if (value?.isMatrix4) {
target.copy(value);
} else if (value instanceof Array) {
value = value.concat(defaults.slice(value.length));
Expand Down Expand Up @@ -987,7 +987,7 @@ const _Types = {
return new Quaternion();
},
validate(value, target, invalid) {
if (value instanceof Quaternion) {
if (value?.isQuaternion) {
target.copy(value);
} else {
target = vec4.validate(value, target, invalid);
Expand Down Expand Up @@ -1046,7 +1046,7 @@ const _Types = {
value = new Color(value);
}

if (value instanceof Color) {
if (value?.isColor) {
target.copy(value);
} else if (value instanceof Array) {
value = value.concat(defaults.slice(value.length));
Expand Down
85 changes: 75 additions & 10 deletions src/primitives/types/types_typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
* specifying types that are only consumed in our source code, but no good for
* specifying types that should be included in the output.
*/
import type {
Color,
Matrix3,
Matrix4,
Quaternion,
Vector2,
Vector3,
Vector4,
} from "three";
import type { MathboxNode, MathboxSelection } from "../../types";

import { Types as TypesUntyped } from "./types";

type OnInvalid = () => void;
Expand Down Expand Up @@ -61,6 +71,19 @@ export type Alignments = "left" | "middle" | "right" | number;
*/
export type TransitionStates = "enter" | "visible" | "exiit" | number;

/**
* A representation of a color. Can be:
* - a string, which is parsed by THREE.Color
* - a THREE.Color instance
* - a number, which is interpreted as a hex value
* - an array of numbers, which is interpreted as an RGB value
*/
type ColorDescription = string | Color | number | number[];

type Vec2Like = number | number[] | Vector2;
type Vec3Like = number | number[] | Vector3;
type Vec4Like = number | number[] | Vector4;

export type TypeGenerators = {
// Helpers
nullable<I, O>(type: Type<I, O>): Type<null | I, null | O>;
Expand Down Expand Up @@ -121,18 +144,60 @@ export type TypeGenerators = {
object: any;
timestamp: any;

vec2: any;
ivec2: any;
vec3: any;
ivec3: any;
vec4: any;
ivec4: any;
vec2(x?: number, y?: number): Type<Vec2Like, Vector2>;
ivec2(x?: number, y?: number): Type<Vec2Like, Vector2>;
vec3(x?: number, y?: number, z?: number): Type<Vec3Like, Vector3>;
ivec3(x?: number, y?: number, z?: number): Type<Vec3Like, Vector3>;
vec4(x?: number, y?: number, z?: number, w?: number): Type<Vec4Like, Vector4>;
ivec4(
x?: number,
y?: number,
z?: number,
w?: number
): Type<Vec4Like, Vector4>;

mat3: any;
mat4: any;
mat3(
n11?: number,
n12?: number,
n13?: number,
n21?: number,
n22?: number,
n23?: number,
n31?: number,
n32?: number,
n33?: number
): Type<number[] | Matrix3, Matrix3>;
mat4(
n11?: number,
n12?: number,
n13?: number,
n14?: number,
n21?: number,
n22?: number,
n23?: number,
n24?: number,
n31?: number,
n32?: number,
n33?: number,
n34?: number,
n41?: number,
n42?: number,
n43?: number,
n44?: number
): Type<number[] | Matrix4, Matrix4>;

quat: any;
color: any;
quat(
x?: number,
y?: number,
z?: number,
w?: number
): Type<Vec4Like | Quaternion, Quaternion>;
color(
r?: number,
g?: number,
b?: number,
a?: number
): Type<ColorDescription, Color>;
transpose(order?: string | Axes[]): Type<Optional<string | Axes[]>, number[]>;

swizzle(
Expand Down
Loading

0 comments on commit 558ffc2

Please sign in to comment.