Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2934,9 +2934,12 @@ class Vector {
return this.sub(surfaceNormalCopy.mult(2 * this.dot(surfaceNormalCopy)));
}



/**
* Returns the vector's components as an array of numbers.
* Returns the vector's first three components as an array of three numbers.
*
* @deprecated array() will be removed in a future version of p5.js; use the more flexible v.values instead of v.array()
* @return {Number[]} array with the vector's components.
* @example
* <div class = "norender">
Expand All @@ -2952,6 +2955,7 @@ class Vector {
* </div>
*/
array() {
this._showArrayDeprecationWarning();
return [this.x || 0, this.y || 0, this.z || 0];
}

Expand Down Expand Up @@ -3781,6 +3785,7 @@ class Vector {
* method to copy into your own vector.
*/
/**
* @deprecated
* @static
* @param {p5.Vector} v the vector to convert to an array
* @return {Number[]} an Array with the 3 values
Expand Down Expand Up @@ -3900,6 +3905,17 @@ function vector(p5, fn) {
*/
p5.Vector = Vector;

let arrayDeprecationWarningShown = false;
Vector.prototype._showArrayDeprecationWarning = function() {
if (p5._friendlyError && !arrayDeprecationWarningShown) {
p5._friendlyError(
'array() is deprecated and will be removed in a future version of p5.js; use the more flexible v.values instead of v.array()',
'p5.Vector.array'
);
arrayDeprecationWarningShown = true;
}
};

/**
* The x component of the vector
* @type {Number}
Expand Down
20 changes: 19 additions & 1 deletion test/unit/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ suite('p5.Vector', function () {
var v;

const mockP5 = {
_validateParameters: vi.fn()
_validateParameters: vi.fn(),
_friendlyError: vi.fn()
};
const mockP5Prototype = {};

Expand Down Expand Up @@ -2072,5 +2073,22 @@ suite('p5.Vector', function () {
-4.10759023698152e-16, -2.23606797749979, 2
]);
});
suite('deprecation warnings', function () {
test('array() should trigger deprecation warning', function () {
v = new mockP5.Vector(1, 2, 3);
v.array();
expect(mockP5._friendlyError).toHaveBeenCalledWith(
'array() is deprecated and will be removed in a future version of p5.js; use the more flexible v.values instead of v.array()',
'p5.Vector.array'
);
});

test('static array() should delegate to instance array()', function () {
v = new mockP5.Vector(1, 2, 3);
const spy = vi.spyOn(v, 'array');
mockP5.Vector.array(v);
expect(spy).toHaveBeenCalled();
});
});
});
});
Loading