Skip to content

Commit d594b93

Browse files
committed
better typings for vectors classes
1 parent 3b58e0e commit d594b93

File tree

9 files changed

+91
-65
lines changed

9 files changed

+91
-65
lines changed

.size-snapshot.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
"gzipped": 4836
3030
},
3131
"type6.js": {
32-
"bundled": 31154,
33-
"minified": 16069,
34-
"gzipped": 4042,
32+
"bundled": 31220,
33+
"minified": 16113,
34+
"gzipped": 4047,
3535
"treeshaked": {
3636
"rollup": {
3737
"code": 2944,

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Version 3.0.0-beta.1 (May 7th 2021)
2+
-----------------------------
3+
* improved typings for Vector2 and Vector3 use with Typescript
4+
15
Version 3.0.0-beta.0 (May 5th 2021)
26
-----------------------------
37
* Vector class:
@@ -7,11 +11,12 @@ Version 3.0.0-beta.0 (May 5th 2021)
711
* Constructor now accept numbers, array or Vector2 as parameters
812
* set() method now accept numbers, array or Vector2 as parameters
913
* Delete copy() and setFromAngle() methods
10-
* Add setFromRadian(), setFromDegree, setMinAxis(), setMaxAxis() methods
14+
* Add isEqualTo(), setFromRadian(), setFromDegree, setMinAxis(), setMaxAxis() methods
1115
* opposite() and absolute() methods now accept an axis name as parameter to set only this axis.
1216
* Vector3 class:
1317
* Constructor now accept numbers, array or Vector3 as parameters
1418
* set() method now accept numbers, array or Vector3 as parameters
19+
* Add isEqualTo() method
1520
* Delete copy() method
1621
* Circle class:
1722
* First parameter of set() method and constructor is now Radius.

dist/type6.cjs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ class Vector {
336336
isEqualTo(scalar) {
337337
return this.compareAxes('!==', scalar);
338338
}
339+
isOrigin() {
340+
return this.compareAxes('!==', 0);
341+
}
339342
toArray() {
340343
let array = [];
341344
for (const axis in this) {

dist/type6.d.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ export declare class Utils {
211211
static isIn(x: number, min: number, max: number): boolean;
212212
static isOut(x: number, min: number, max: number): boolean;
213213
}
214+
215+
214216
export interface Vector {
215217
'x': number;
216218
'y': number;
@@ -222,36 +224,37 @@ export declare class Vector {
222224
constructor();
223225
isPositive(): boolean;
224226
isEqualTo(scalar: number): boolean;
227+
isOrigin(): boolean;
225228
toArray(): number[];
226229
toString(): string;
227-
origin(): Vector;
230+
origin(): Vector2 | Vector3;
228231
getMagnitude(square?: boolean): number;
229232
private getSquaredMagnitude;
230-
getDistance(v: Vector, square?: boolean): number;
231-
add(v: Vector): Vector;
232-
addScaledVector(v: Vector, scalar: number): Vector;
233-
addScalar(scalar: number): Vector;
234-
subtract(v: Vector): Vector;
235-
subtractScaledVector(v: Vector, scalar: number): Vector;
236-
subtractScalar(scalar: number): Vector;
237-
multiply(v: Vector): Vector;
238-
multiplyScaledVector(v: Vector, scalar: number): Vector;
239-
scale(scalar: number): Vector;
240-
divide(v: Vector): Vector;
241-
divideScaledVector(v: Vector, scalar: number): Vector;
242-
halve(): Vector;
243-
max(v: Vector): Vector;
244-
min(v: Vector): Vector;
245-
maxScalar(scalar: number): Vector;
246-
minScalar(scalar: number): Vector;
247-
normalize(): Vector;
248-
absolute(a?: 'x' | 'y' | 'z'): Vector;
249-
opposite(a?: 'x' | 'y' | 'z'): Vector;
250-
dotProduct(v: Vector): number;
233+
getDistance(v: Vector2 | Vector3, square?: boolean): number;
234+
add(v: Vector2 | Vector3): Vector2 | Vector3;
235+
addScaledVector(v: Vector2 | Vector3, scalar: number): Vector2 | Vector3;
236+
addScalar(scalar: number): Vector2 | Vector3;
237+
subtract(v: Vector2 | Vector3): Vector2 | Vector3;
238+
subtractScaledVector(v: Vector2 | Vector3, scalar: number): Vector2 | Vector3;
239+
subtractScalar(scalar: number): Vector2 | Vector3;
240+
multiply(v: Vector2 | Vector3): Vector2 | Vector3;
241+
multiplyScaledVector(v: Vector2 | Vector3, scalar: number): Vector2 | Vector3;
242+
scale(scalar: number): Vector2 | Vector3;
243+
divide(v: Vector2 | Vector3): Vector2 | Vector3;
244+
divideScaledVector(v: Vector2 | Vector3, scalar: number): Vector2 | Vector3;
245+
halve(): Vector2 | Vector3;
246+
max(v: Vector2 | Vector3): Vector2 | Vector3;
247+
min(v: Vector2 | Vector3): Vector2 | Vector3;
248+
maxScalar(scalar: number): Vector2 | Vector3;
249+
minScalar(scalar: number): Vector2 | Vector3;
250+
normalize(): Vector2 | Vector3;
251+
absolute(a?: 'x' | 'y' | 'z'): Vector2 | Vector3;
252+
opposite(a?: 'x' | 'y' | 'z'): Vector2 | Vector3;
253+
dotProduct(v: Vector2 | Vector3): number;
251254
protected setFromArray(array: number[]): void;
252-
protected copy(v: Vector): Vector;
255+
protected copy(v: Vector2 | Vector3): Vector2 | Vector3;
253256
private compareAxes;
254-
protected updateAxes(operator: UpdateOperator, scalar: number): Vector;
257+
protected updateAxes(operator: UpdateOperator, scalar: number): Vector2 | Vector3;
255258
private updateAxesByVector;
256259
private updateAxesWithMath;
257260
private updateAxesWithMathByVector;

dist/type6.iife.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,11 @@ var Type6 = (function (exports) {
586586
value: function isEqualTo(scalar) {
587587
return this.compareAxes('!==', scalar);
588588
}
589+
}, {
590+
key: "isOrigin",
591+
value: function isOrigin() {
592+
return this.compareAxes('!==', 0);
593+
}
589594
}, {
590595
key: "toArray",
591596
value: function toArray() {

dist/type6.iife.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/type6.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ class Vector {
334334
isEqualTo(scalar) {
335335
return this.compareAxes('!==', scalar);
336336
}
337+
isOrigin() {
338+
return this.compareAxes('!==', 0);
339+
}
337340
toArray() {
338341
let array = [];
339342
for (const axis in this) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lcluber/type6js",
3-
"version": "3.0.0-beta.0",
3+
"version": "3.0.0-beta.1",
44
"description": "Mathematics library for Javascript",
55
"keywords": [
66
"maths",

src/vectors/vector.ts

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Vector2 } from './vector2';
2+
import { Vector3 } from './vector3';
13

24
export interface Vector {
35
'x': number;
@@ -20,11 +22,16 @@ export class Vector {
2022
return this.compareAxes('<=', 0);
2123
}
2224

23-
//true if vector is equal to (scalar;scalar)
25+
//true if vector2 is equal to (scalar;scalar)
2426
public isEqualTo(scalar: number): boolean {
2527
return this.compareAxes('!==', scalar);
2628
}
2729

30+
//true if vector2 is equal to (0;0)
31+
public isOrigin(): boolean {
32+
return this.compareAxes('!==', 0);
33+
}
34+
2835
public toArray(): number[] {
2936
let array = [];
3037
for(const axis in this) {
@@ -46,7 +53,7 @@ export class Vector {
4653
// '(x = ' + this.x + '; y = ' + this.y + '; z = ' + this.z + ')';
4754
}
4855

49-
public origin(): Vector {
56+
public origin(): Vector2 | Vector3 {
5057
return this.updateAxes('=', 0.0);
5158
}
5259

@@ -65,109 +72,109 @@ export class Vector {
6572
return squaredMagnitude;
6673
}
6774

68-
public getDistance(v: Vector, square: boolean = false): number {
75+
public getDistance(v: Vector2 | Vector3, square: boolean = false): number {
6976
this.subtract(v);
7077
const magnitude = this.getMagnitude(square);
7178
this.add(v);
7279
return magnitude;
7380
}
7481

75-
public add(v: Vector): Vector {
82+
public add(v: Vector2 | Vector3): Vector2 | Vector3 {
7683
return this.updateAxesByVector('+=', v, null);
7784
}
7885

79-
public addScaledVector(v: Vector, scalar: number): Vector {
86+
public addScaledVector(v: Vector2 | Vector3, scalar: number): Vector2 | Vector3 {
8087
return this.updateAxesByVector('+=', v, scalar);
8188
}
8289

83-
public addScalar(scalar: number): Vector {
90+
public addScalar(scalar: number): Vector2 | Vector3 {
8491
return this.updateAxes('+=', scalar);
8592
}
8693

87-
public subtract(v: Vector): Vector {
94+
public subtract(v: Vector2 | Vector3): Vector2 | Vector3 {
8895
return this.updateAxesByVector('-=', v, null);
8996
}
9097

91-
public subtractScaledVector(v: Vector, scalar: number): Vector {
98+
public subtractScaledVector(v: Vector2 | Vector3, scalar: number): Vector2 | Vector3 {
9299
return this.updateAxesByVector('-=', v, scalar);
93100
}
94101

95-
public subtractScalar(scalar: number): Vector {
102+
public subtractScalar(scalar: number): Vector2 | Vector3 {
96103
return this.updateAxes('-=', scalar);
97104
}
98105

99106
//component product
100-
public multiply(v: Vector): Vector {
107+
public multiply(v: Vector2 | Vector3): Vector2 | Vector3 {
101108
return this.updateAxesByVector('*=', v, null);
102109
}
103110

104-
public multiplyScaledVector(v: Vector, scalar: number): Vector {
111+
public multiplyScaledVector(v: Vector2 | Vector3, scalar: number): Vector2 | Vector3 {
105112
return this.updateAxesByVector('*=', v, scalar);
106113
}
107114

108-
public scale(scalar: number): Vector {
115+
public scale(scalar: number): Vector2 | Vector3 {
109116
return this.updateAxes('*=', scalar);
110117
}
111118

112-
public divide(v: Vector): Vector {
119+
public divide(v: Vector2 | Vector3): Vector2 | Vector3 {
113120
return this.updateAxesByVector('/=', v, null);
114121
}
115122

116-
public divideScaledVector(v: Vector, scalar: number): Vector {
123+
public divideScaledVector(v: Vector2 | Vector3, scalar: number): Vector2 | Vector3 {
117124
return this.updateAxesByVector('/=', v, scalar);
118125
}
119126

120-
public halve(): Vector {
127+
public halve(): Vector2 | Vector3 {
121128
return this.updateAxes('*=', 0.5);
122129
}
123130

124-
public max(v: Vector): Vector {
131+
public max(v: Vector2 | Vector3): Vector2 | Vector3 {
125132
return this.updateAxesWithMathByVector(v, 'max');
126133
}
127134

128-
public min(v: Vector): Vector {
135+
public min(v: Vector2 | Vector3): Vector2 | Vector3 {
129136
return this.updateAxesWithMathByVector(v, 'min');
130137
}
131138

132-
public maxScalar(scalar: number): Vector {
139+
public maxScalar(scalar: number): Vector2 | Vector3 {
133140
return this.updateAxesWithMath(scalar, 'max');
134141
}
135142

136-
public minScalar(scalar: number): Vector {
143+
public minScalar(scalar: number): Vector2 | Vector3 {
137144
return this.updateAxesWithMath(scalar, 'min');
138145
}
139146

140-
public normalize(): Vector {
147+
public normalize(): Vector2 | Vector3 {
141148
let length = this.getMagnitude();
142149
if( length && length != 1) {
143150
this.scale(1/length);
144151
}
145-
return this;
152+
return this as unknown as Vector2 | Vector3;
146153
}
147154

148-
public absolute(a?: 'x' | 'y' | 'z'): Vector {
155+
public absolute(a?: 'x' | 'y' | 'z'): Vector2 | Vector3 {
149156
for(const axis in this) {
150157
if (this.hasOwnProperty(axis)) {
151158
if (!a || a === axis) {
152159
(this[axis] as number) = Math.abs(this[axis]);
153160
}
154161
}
155162
}
156-
return this;
163+
return this as unknown as Vector2 | Vector3;
157164
}
158165

159-
public opposite(a?: 'x' | 'y' | 'z'): Vector {
166+
public opposite(a?: 'x' | 'y' | 'z'): Vector2 | Vector3 {
160167
for(const axis in this) {
161168
if (this.hasOwnProperty(axis)) {
162169
if (!a || a === axis) {
163170
(this[axis] as number) = -this[axis];
164171
}
165172
}
166173
}
167-
return this;
174+
return this as unknown as Vector2 | Vector3;
168175
}
169176

170-
public dotProduct(v: Vector): number { //scalar product
177+
public dotProduct(v: Vector2 | Vector3): number { //scalar product
171178
let dotProduct = 0;
172179
for(const axis in this) {
173180
if (this.hasOwnProperty(axis) && v.hasOwnProperty(axis)) {
@@ -190,7 +197,7 @@ export class Vector {
190197
}
191198
}
192199

193-
protected copy(v: Vector): Vector {
200+
protected copy(v: Vector2 | Vector3): Vector2 | Vector3 {
194201
return this.updateAxesByVector('=', v, null);
195202
}
196203

@@ -205,40 +212,40 @@ export class Vector {
205212
return true;
206213
}
207214

208-
protected updateAxes(operator: UpdateOperator, scalar: number): Vector {
215+
protected updateAxes(operator: UpdateOperator, scalar: number): Vector2 | Vector3 {
209216
for(const axis in this) {
210217
if (this.hasOwnProperty(axis)) {
211218
this[operator](axis, scalar);
212219
}
213220
}
214-
return this;
221+
return this as unknown as Vector2 | Vector3;
215222
}
216223

217-
private updateAxesByVector(operator: UpdateOperator, vector: Vector, scalar: number | null): Vector {
224+
private updateAxesByVector(operator: UpdateOperator, vector: Vector2 | Vector3, scalar: number | null): Vector2 | Vector3 {
218225
for(const axis in this) {
219226
if (this.hasOwnProperty(axis) && vector.hasOwnProperty(axis)) {
220227
this[operator](axis, vector[axis] * (scalar ?? 1.0));
221228
}
222229
}
223-
return this;
230+
return this as unknown as Vector2 | Vector3;
224231
}
225232

226-
private updateAxesWithMath(scalar: number, operator: 'max' | 'min'): Vector {
233+
private updateAxesWithMath(scalar: number, operator: 'max' | 'min'): Vector2 | Vector3 {
227234
for(const axis in this) {
228235
if (this.hasOwnProperty(axis)) {
229236
(this[axis] as number) = Math[operator](this[axis], scalar);
230237
}
231238
}
232-
return this;
239+
return this as unknown as Vector2 | Vector3;
233240
}
234241

235-
private updateAxesWithMathByVector(v: Vector, operator: 'max' | 'min'): Vector {
242+
private updateAxesWithMathByVector(v: Vector2 | Vector3, operator: 'max' | 'min'): Vector2 | Vector3 {
236243
for(const axis in this) {
237244
if (this.hasOwnProperty(axis) && v.hasOwnProperty(axis)) {
238245
(this[axis] as number) = Math[operator](this[axis], v[axis]);
239246
}
240247
}
241-
return this;
248+
return this as unknown as Vector2 | Vector3;
242249
}
243250

244251
private '<='(axis: Extract<keyof this, string>, value: number): boolean {

0 commit comments

Comments
 (0)