Skip to content

Commit

Permalink
fix(vector): correct jdocs generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Mar 31, 2020
1 parent 3ac2188 commit 70443ea
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 267 deletions.
114 changes: 23 additions & 91 deletions src/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class APoint {
const sa = Math.sin(angle);
const ca = Math.cos(angle);

const x = (this.x * ca) - (this.y * sa);
const y = (this.x * sa) + (this.y * ca);
const x = this.x * ca - this.y * sa;
const y = this.x * sa + this.y * ca;

return new this.constructor(x, y);
}
Expand Down Expand Up @@ -272,16 +272,6 @@ cachedMethod(APoint, 'getRad');
cachedGetter(APoint, 'length');
cachedGetter(APoint, 'lengthSq');

/**
* Point
*
* new Point(x: *number*, y: *number*): [[Point]]
*
* new Point(arr: *[number, number]*): [[Point]]
*
* new Point(alg: (*function*(): *number*)): [[Point]]
*
*/
export class Point extends APoint {
/**
*
Expand Down Expand Up @@ -331,16 +321,6 @@ export class Point extends APoint {
}
}

/**
* IPoint
*
* new IPoint(x: *number*, y: *number*): [[IPoint]]
*
* new IPoint(arr: *[number, number]*): [[IPoint]]
*
* new IPoint(alg: (*function*(): *number*)): [[IPoint]]
*
*/
export class IPoint extends APoint {
/**
* @returns {PointType}
Expand All @@ -362,82 +342,34 @@ export function calc(alg) {
const pointFactory = cachedFactory(Point);

/**
* @typedef {() => PointType} PointZero
* @typedef {(alg: Alg) => PointType} PointAlg
* @typedef {(x: number , y: number) => PointType} PointCon
* @typedef {(data: [number, number]) => PointType} PointArr
* @typedef {PointAlg & PointCon & PointArr & PointZero} point
*
* @param {number | [number, number] | Alg} [x]
* @param {number} [y]
* @returns {PointType}
* @hidden
* @template P
* @typedef {() => P} Zero
*/
/**
* @template P
* @typedef {(alg: Alg) => P} Algh
*/
/**
* @template P
* @typedef {(x: number, y: number) => P} Con
*/
/**
* @template P
* @typedef {(data: [number, number]) => P} Arr
*/
export const point = function point(x, y) {
return pointFactory(x, y);
};

/**
* @type {Zero<PointType> & Algh<PointType> & Con<PointType> & Arr<PointType>}
*/
export const point = (...args) => pointFactory(...args);

const ipointFactory = cachedFactory(IPoint);

/**
* @typedef {() => IPointType} IPointZero
* @typedef {(alg: Alg) => IPointType} IPointAlg
* @typedef {(x: number , y: number) => IPointType} IPointCon
* @typedef {(data: [number, number]) => IPointType} IPointArr
* @typedef {IPointAlg & IPointCon & IPointArr & IPointType & IPointZero}
*
* @param {number | [number, number] | Alg} [x]
* @param {number} [y]
* @returns {IPointType}
* @hidden
* @type {Zero<IPointType> & Algh<IPointType> & Con<IPointType> & Arr<IPointType>}
*/
export function ipoint(x, y) {
return ipointFactory(x, y);
}
export const ipoint = (x, y) => ipointFactory(x, y);

export const ZERO = ipoint(0, 0);
export const FORWARD = ipoint(0, -1);
export const LEFT = ipoint(-1, 0);

export const Export = {
/**
* @param {Alg} alg
* @return {PointType | IPointType}
*/
calc: alg => operatorCalc(alg),

/**
* @type {PointZero}
*/
point: () => pointFactory(),

/**
* @type {PointAlg}
*/
point: alg => pointFactory(alg),

/**
* @type {PointArr}
*/
point: arr => pointFactory(arr),

/**
* @type {PointCon}
*/
point: (x, y) => pointFactory(x, y),

/**
* @type {IPointAlg}
*/
ipoint: alg => ipointFactory(alg),

/**
* @type {IPointArr}
*/
ipoint: arr => ipointFactory(arr),

/**
* @type {IPointCon}
*/
ipoint: (x, y) => ipointFactory(x, y)
};
142 changes: 35 additions & 107 deletions src/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class AVector {
}
}


/**
* @returns {number}
*/
Expand All @@ -63,7 +62,11 @@ class AVector {
*/
normalize() {
const { length } = this;
return new this.constructor(this.x / length, this.y / length, this.z / length);
return new this.constructor(
this.x / length,
this.y / length,
this.z / length
);
}

/**
Expand Down Expand Up @@ -154,7 +157,9 @@ class AVector {
* @returns {number}
*/
distance(v) {
return Math.sqrt(square(this.x - v.x) + square(this.y - v.y) + square(this.z - v.z));
return Math.sqrt(
square(this.x - v.x) + square(this.y - v.y) + square(this.z - v.z)
);
}

/**
Expand All @@ -174,11 +179,10 @@ class AVector {
return [this.x, this.y, this.z];
}


/**
* @param {string} target
* @returns {VectorType | VictorType | IPointType}
*/
* @param {string} target
* @returns {VectorType | VictorType | IPointType}
*/
swizzle(target) {
const data = target.split('').map(t => this[t]);
if (data.length === 2) {
Expand Down Expand Up @@ -219,7 +223,9 @@ class AVector {
* @returns {string}
*/
toString() {
return `{ x: ${formatNumber(this.x)}, y: ${formatNumber(this.y)}, z: ${formatNumber(this.z)} }`;
return `{ x: ${formatNumber(this.x)}, y: ${formatNumber(
this.y
)}, z: ${formatNumber(this.z)} }`;
}

/**
Expand Down Expand Up @@ -390,17 +396,6 @@ cachedMethod(AVector, 'toArray');
cachedGetter(AVector, 'length');
cachedGetter(AVector, 'lengthSq');

/**
*
* Vector
*
* new Vector(x: *number*, y: *number*, z: *number*): [[Vector]]
*
* new Vector(arr: *[number, number, number]*): [[Vector]]
*
* new Vector(alg: (*function*(): *number*)): [[Vector]]
*
*/
export class Vector extends AVector {
/**
*
Expand Down Expand Up @@ -466,17 +461,6 @@ export class Vector extends AVector {
}
}

/**
* Victor
*
* new Victor(x: *number*, y: *number*, z: *number*): [[Victor]]
*
* new Victor(arr: *[number, number, number]*): [[Victor]]
*
* new Victor(alg: (*function*(): *number*)): [[Victor]]
*
*
*/
export class Victor extends AVector {
/**
* @returns {VictorType}
Expand Down Expand Up @@ -505,89 +489,33 @@ export function calc(alg) {
const vectorFactory = cachedFactory(Vector);

/**
* @typedef {() => VectorType} VectorZero
* @typedef {(alg: Alg) => VectorType} VectorAlg
* @typedef {(x: number , y: number, z: number) => VectorType} VectorCon
* @typedef {(data: [number, number, number]) => VectorType} VectorArr
* @typedef {VectorAlg & VectorCon & VectorArr & VectorZero}
*
* @param {number | [number, number, number] | Alg} [x]
* @param {number} [y]
* @param {number} [z]
* @returns {VectorType}
* @hidden
* @template Vec
* @typedef {() => Vec} Zero
*/
export function vector(x, y, z) {
return vectorFactory(x, y, z);
}

const victorFactory = cachedFactory(Victor);

/**
* @typedef {() => VictorType} VictorZero
* @typedef {(alg: Alg) => VictorType} VictorAlg
* @typedef {(x: number , y: number, z: number) => VictorType} VictorCon
* @typedef {(data: [number, number, number]) => VictorType} VictorArr
* @typedef {VictorAlg & VictorCon & VictorArr & VictorZero}
*
* @param {number | [number, number, number] | Alg} [x]
* @param {number} [y]
* @param {number} [z]
* @returns {VictorType}
* @hidden
* @template Vec
* @typedef {(alg: Alg) => Vec} Algh
*/
/**
* @template Vec
* @typedef {(x: number, y: number, z: number) => Vec} Con
*/
/**
* @template Vec
* @typedef {(data: [number, number, number]) => Vec} Arr
*/
export function victor(x, y, z) {
return victorFactory(x, y, z);
}

export const Export = {

/**
* @param {Alg} alg
* @return {VectorType | VictorType}
*/
calc: alg => operatorCalc(alg),

/**
* @type {VectorZero}
*/
vector: () => vectorFactory(),

/**
* @type {VectorAlg}
*/
vector: alg => vectorFactory(alg),

/**
* @type {VectorArr}
*/
vector: arr => vectorFactory(arr),

/**
* @type {VectorCon}
*/
vector: (x, y, z) => vectorFactory(x, y, z),

/**
* @type {VictorZero}
*/
victor: () => victorFactory(),

/**
* @type {VictorAlg}
*/
victor: alg => victorFactory(alg),
/**
* @type {Zero<VectorType> & Algh<VectorType> & Con<VectorType> & Arr<VectorType>}
*/
export const vector = (...args) => vectorFactory(...args);

/**
* @type {VictorArr}
*/
victor: arr => victorFactory(arr),
const victorFactory = cachedFactory(Victor);

/**
* @type {VictorCon}
*/
victor: (x, y, z) => victorFactory(x, y, z)
};
/**
* @type {Zero<VictorType> & Algh<VictorType> & Con<VictorType> & Arr<VictorType>}
*/
export const victor = (...args) => victorFactory(...args);

export const ZERO = victor(0, 0, 0);
export const FORWARD = victor(0, 0, -1);
Expand Down
2 changes: 1 addition & 1 deletion test/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '../src/point';

/**
* @param {ipoint | point} vec2
* @param {(typeof ipoint) | (typeof point)} vec2
* @param {(typeof Point) | (typeof IPoint)} Vec2
*/
const pointTest = (vec2, Vec2) => {
Expand Down
Loading

0 comments on commit 70443ea

Please sign in to comment.