diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index a264fb811e..d7f880dd77 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -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 *
@@ -2952,6 +2955,7 @@ class Vector { *
*/ array() { + this._showArrayDeprecationWarning(); return [this.x || 0, this.y || 0, this.z || 0]; } @@ -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 @@ -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} diff --git a/test/unit/math/p5.Vector.js b/test/unit/math/p5.Vector.js index 4028ee2dba..d24f8d7c94 100644 --- a/test/unit/math/p5.Vector.js +++ b/test/unit/math/p5.Vector.js @@ -5,7 +5,8 @@ suite('p5.Vector', function () { var v; const mockP5 = { - _validateParameters: vi.fn() + _validateParameters: vi.fn(), + _friendlyError: vi.fn() }; const mockP5Prototype = {}; @@ -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(); + }); + }); }); });