Skip to content

Commit 619c5ab

Browse files
Update p5.Vector.js
- use `...args` - add missing `@return`
1 parent 90fdd8b commit 619c5ab

File tree

1 file changed

+65
-50
lines changed

1 file changed

+65
-50
lines changed

src/math/p5.Vector.js

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ p5.Vector = class {
435435
* @param {p5.Vector | Number[]} value divisor vector.
436436
* @chainable
437437
*/
438-
rem (x, y, z) {
438+
rem (...args) {
439+
let [x, y, z] = args;
439440
if (x instanceof p5.Vector) {
440441
if ([x.x,x.y,x.z].every(Number.isFinite)) {
441442
const xComponent = parseFloat(x.x);
@@ -456,15 +457,15 @@ p5.Vector = class {
456457
return this.calculateRemainder3D(x[0], x[1], x[2]);
457458
}
458459
}
459-
} else if (arguments.length === 1) {
460-
if (Number.isFinite(arguments[0]) && arguments[0] !== 0) {
461-
this.x = this.x % arguments[0];
462-
this.y = this.y % arguments[0];
463-
this.z = this.z % arguments[0];
460+
} else if (args.length === 1) {
461+
if (Number.isFinite(args[0]) && args[0] !== 0) {
462+
this.x = this.x % args[0];
463+
this.y = this.y % args[0];
464+
this.z = this.z % args[0];
464465
return this;
465466
}
466-
} else if (arguments.length === 2) {
467-
const vectorComponents = [...arguments];
467+
} else if (args.length === 2) {
468+
const vectorComponents = args;
468469
if (vectorComponents.every(element => Number.isFinite(element))) {
469470
if (vectorComponents.length === 2) {
470471
return this.calculateRemainder2D(
@@ -473,8 +474,8 @@ p5.Vector = class {
473474
);
474475
}
475476
}
476-
} else if (arguments.length === 3) {
477-
const vectorComponents = [...arguments];
477+
} else if (args.length === 3) {
478+
const vectorComponents = args;
478479
if (vectorComponents.every(element => Number.isFinite(element))) {
479480
if (vectorComponents.length === 3) {
480481
return this.calculateRemainder3D(
@@ -745,7 +746,8 @@ p5.Vector = class {
745746
* @chainable
746747
*/
747748

748-
mult(x, y, z) {
749+
mult(...args) {
750+
let [x, y, z] = args;
749751
if (x instanceof p5.Vector) {
750752
// new p5.Vector will check that values are valid upon construction but it's possible
751753
// that someone could change the value of a component after creation, which is why we still
@@ -795,21 +797,21 @@ p5.Vector = class {
795797
return this;
796798
}
797799

798-
const vectorComponents = [...arguments];
800+
const vectorComponents = args;
799801
if (
800802
vectorComponents.every(element => Number.isFinite(element)) &&
801803
vectorComponents.every(element => typeof element === 'number')
802804
) {
803-
if (arguments.length === 1) {
805+
if (args.length === 1) {
804806
this.x *= x;
805807
this.y *= x;
806808
this.z *= x;
807809
}
808-
if (arguments.length === 2) {
810+
if (args.length === 2) {
809811
this.x *= x;
810812
this.y *= y;
811813
}
812-
if (arguments.length === 3) {
814+
if (args.length === 3) {
813815
this.x *= x;
814816
this.y *= y;
815817
this.z *= z;
@@ -962,7 +964,8 @@ p5.Vector = class {
962964
* @param {p5.Vector} v vector to divide the components of the original vector by.
963965
* @chainable
964966
*/
965-
div(x, y, z) {
967+
div(...args) {
968+
let [x, y, z] = args;
966969
if (x instanceof p5.Vector) {
967970
// new p5.Vector will check that values are valid upon construction but it's possible
968971
// that someone could change the value of a component after creation, which is why we still
@@ -1025,7 +1028,7 @@ p5.Vector = class {
10251028
return this;
10261029
}
10271030

1028-
const vectorComponents = [...arguments];
1031+
const vectorComponents = args;
10291032
if (
10301033
vectorComponents.every(element => Number.isFinite(element)) &&
10311034
vectorComponents.every(element => typeof element === 'number')
@@ -1035,16 +1038,16 @@ p5.Vector = class {
10351038
return this;
10361039
}
10371040

1038-
if (arguments.length === 1) {
1041+
if (args.length === 1) {
10391042
this.x /= x;
10401043
this.y /= x;
10411044
this.z /= x;
10421045
}
1043-
if (arguments.length === 2) {
1046+
if (args.length === 2) {
10441047
this.x /= x;
10451048
this.y /= y;
10461049
}
1047-
if (arguments.length === 3) {
1050+
if (args.length === 3) {
10481051
this.x /= x;
10491052
this.y /= y;
10501053
this.z /= z;
@@ -2400,10 +2403,7 @@ p5.Vector = class {
24002403
* </code>
24012404
* </div>
24022405
*/
2403-
static fromAngle(angle, length) {
2404-
if (typeof length === 'undefined') {
2405-
length = 1;
2406-
}
2406+
static fromAngle(angle, length = 1) {
24072407
return new p5.Vector(length * Math.cos(angle), length * Math.sin(angle), 0);
24082408
}
24092409

@@ -2451,10 +2451,7 @@ p5.Vector = class {
24512451
* </code>
24522452
* </div>
24532453
*/
2454-
static fromAngles(theta, phi, length) {
2455-
if (typeof length === 'undefined') {
2456-
length = 1;
2457-
}
2454+
static fromAngles(theta, phi, length = 1) {
24582455
const cosPhi = Math.cos(phi);
24592456
const sinPhi = Math.sin(phi);
24602457
const cosTheta = Math.cos(theta);
@@ -2565,10 +2562,11 @@ p5.Vector = class {
25652562
* @return {p5.Vector} resulting <a href="#/p5.Vector">p5.Vector</a>.
25662563
*/
25672564

2568-
static add(v1, v2, target) {
2565+
static add(...args) {
2566+
let [v1, v2, target] = args;
25692567
if (!target) {
25702568
target = v1.copy();
2571-
if (arguments.length === 3) {
2569+
if (args.length === 3) {
25722570
p5._friendlyError(
25732571
'The target parameter is undefined, it should be of type p5.Vector',
25742572
'p5.Vector.add'
@@ -2616,10 +2614,11 @@ p5.Vector = class {
26162614
* @return {p5.Vector} The resulting <a href="#/p5.Vector">p5.Vector</a>
26172615
*/
26182616

2619-
static sub(v1, v2, target) {
2617+
static sub(...args) {
2618+
let [v1, v2, target] = args;
26202619
if (!target) {
26212620
target = v1.copy();
2622-
if (arguments.length === 3) {
2621+
if (args.length === 3) {
26232622
p5._friendlyError(
26242623
'The target parameter is undefined, it should be of type p5.Vector',
26252624
'p5.Vector.sub'
@@ -2651,6 +2650,7 @@ p5.Vector = class {
26512650
* @param {p5.Vector} v
26522651
* @param {Number} n
26532652
* @param {p5.Vector} [target] vector to receive the result.
2653+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
26542654
*/
26552655

26562656
/**
@@ -2659,6 +2659,7 @@ p5.Vector = class {
26592659
* @param {p5.Vector} v0
26602660
* @param {p5.Vector} v1
26612661
* @param {p5.Vector} [target]
2662+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
26622663
*/
26632664

26642665
/**
@@ -2667,11 +2668,13 @@ p5.Vector = class {
26672668
* @param {p5.Vector} v0
26682669
* @param {Number[]} arr
26692670
* @param {p5.Vector} [target]
2671+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
26702672
*/
2671-
static mult(v, n, target) {
2673+
static mult(...args) {
2674+
let [v, n, target] = args;
26722675
if (!target) {
26732676
target = v.copy();
2674-
if (arguments.length === 3) {
2677+
if (args.length === 3) {
26752678
p5._friendlyError(
26762679
'The target parameter is undefined, it should be of type p5.Vector',
26772680
'p5.Vector.mult'
@@ -2694,9 +2697,11 @@ p5.Vector = class {
26942697
* @param {p5.Vector} v
26952698
* @param {Number} angle
26962699
* @param {p5.Vector} [target] The vector to receive the result
2700+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
26972701
*/
2698-
static rotate(v, a, target) {
2699-
if (arguments.length === 2) {
2702+
static rotate(...args) {
2703+
let [v, a, target] = args;
2704+
if (args.length === 2) {
27002705
target = v.copy();
27012706
} else {
27022707
if (!(target instanceof p5.Vector)) {
@@ -2730,6 +2735,7 @@ p5.Vector = class {
27302735
* @param {p5.Vector} v
27312736
* @param {Number} n
27322737
* @param {p5.Vector} [target] The vector to receive the result
2738+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
27332739
*/
27342740

27352741
/**
@@ -2738,6 +2744,7 @@ p5.Vector = class {
27382744
* @param {p5.Vector} v0
27392745
* @param {p5.Vector} v1
27402746
* @param {p5.Vector} [target]
2747+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
27412748
*/
27422749

27432750
/**
@@ -2746,12 +2753,14 @@ p5.Vector = class {
27462753
* @param {p5.Vector} v0
27472754
* @param {Number[]} arr
27482755
* @param {p5.Vector} [target]
2756+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
27492757
*/
2750-
static div(v, n, target) {
2758+
static div(...args) {
2759+
let [v, n, target] = args;
27512760
if (!target) {
27522761
target = v.copy();
27532762

2754-
if (arguments.length === 3) {
2763+
if (args.length === 3) {
27552764
p5._friendlyError(
27562765
'The target parameter is undefined, it should be of type p5.Vector',
27572766
'p5.Vector.div'
@@ -2820,10 +2829,11 @@ p5.Vector = class {
28202829
* @param {p5.Vector} [target] The vector to receive the result
28212830
* @return {p5.Vector} The lerped value
28222831
*/
2823-
static lerp(v1, v2, amt, target) {
2832+
static lerp(...args) {
2833+
let [v1, v2, amt, target] = args;
28242834
if (!target) {
28252835
target = v1.copy();
2826-
if (arguments.length === 4) {
2836+
if (args.length === 4) {
28272837
p5._friendlyError(
28282838
'The target parameter is undefined, it should be of type p5.Vector',
28292839
'p5.Vector.lerp'
@@ -2851,10 +2861,11 @@ p5.Vector = class {
28512861
* @param {p5.Vector} [target] vector to receive the result.
28522862
* @return {p5.Vector} slerped vector between v1 and v2
28532863
*/
2854-
static slerp(v1, v2, amt, target) {
2864+
static slerp(...args) {
2865+
let [v1, v2, amt, target] = args;
28552866
if (!target) {
28562867
target = v1.copy();
2857-
if (arguments.length === 4) {
2868+
if (args.length === 4) {
28582869
p5._friendlyError(
28592870
'The target parameter is undefined, it should be of type p5.Vector',
28602871
'p5.Vector.slerp'
@@ -2907,8 +2918,9 @@ p5.Vector = class {
29072918
* @param {p5.Vector} [target] The vector to receive the result
29082919
* @return {p5.Vector} The vector v, normalized to a length of 1
29092920
*/
2910-
static normalize(v, target) {
2911-
if (arguments.length < 2) {
2921+
static normalize(...args) {
2922+
let [v, target] = args;
2923+
if (args.length < 2) {
29122924
target = v.copy();
29132925
} else {
29142926
if (!(target instanceof p5.Vector)) {
@@ -2934,8 +2946,9 @@ p5.Vector = class {
29342946
* @param {p5.Vector} [target] the vector to receive the result (Optional)
29352947
* @return {p5.Vector} v with a magnitude limited to max
29362948
*/
2937-
static limit(v, max, target) {
2938-
if (arguments.length < 3) {
2949+
static limit(...args) {
2950+
let [v, max, target] = args;
2951+
if (args.length < 3) {
29392952
target = v.copy();
29402953
} else {
29412954
if (!(target instanceof p5.Vector)) {
@@ -2961,8 +2974,9 @@ p5.Vector = class {
29612974
* @param {p5.Vector} [target] the vector to receive the result (Optional)
29622975
* @return {p5.Vector} v with a magnitude set to len
29632976
*/
2964-
static setMag(v, len, target) {
2965-
if (arguments.length < 3) {
2977+
static setMag(...args) {
2978+
let [v, len, target] = args;
2979+
if (args.length < 3) {
29662980
target = v.copy();
29672981
} else {
29682982
if (!(target instanceof p5.Vector)) {
@@ -3020,8 +3034,9 @@ p5.Vector = class {
30203034
* @param {p5.Vector} [target] vector to receive the result.
30213035
* @return {p5.Vector} the reflected vector
30223036
*/
3023-
static reflect(incidentVector, surfaceNormal, target) {
3024-
if (arguments.length < 3) {
3037+
static reflect(...args) {
3038+
let [incidentVector, surfaceNormal, target] = args;
3039+
if (args.length < 3) {
30253040
target = incidentVector.copy();
30263041
} else {
30273042
if (!(target instanceof p5.Vector)) {

0 commit comments

Comments
 (0)