MCPcopy
hub / github.com/processing/p5.js / _validatedVectorOperation

Function _validatedVectorOperation

src/math/patch-vector.js:29–76  ·  view source on GitHub ↗
(expectsSoloNumberArgument)

Source from the content-addressed store, hash-verified

27 * @internal
28 */
29export function _validatedVectorOperation(expectsSoloNumberArgument){
30 return function(target){
31 return function (...args) {
32 if (args.length === 0) {
33 // No arguments? No action
34 return this;
35 } else if (args[0] instanceof Vector) {
36 // First argument is a vector? Make it an array
37 args = args[0].values;
38 } else if (Array.isArray(args[0])) {
39 // First argument is an array? Great, keep it!
40 args = args[0];
41 } else if (args.length === 1){
42 // Special case for a solo numeric arguments only applies sometimes
43 if (expectsSoloNumberArgument) {
44 args = args[0];
45 }
46 }
47
48 if(Array.isArray(args)){
49 for (let i = 0; i < args.length; i++) {
50 const v = args[i];
51 if (typeof v !== 'number' || !Number.isFinite(v)) {
52 if (!this.friendlyErrorsDisabled()) {
53 this._friendlyError(
54 'Arguments contain non-finite numbers',
55 'p5.Vector'
56 );
57 }
58 return this;
59 }
60 }
61 } else {
62 if (typeof args !== 'number' || !Number.isFinite(args)) {
63 if (!this.friendlyErrorsDisabled()) {
64 this._friendlyError(
65 'Arguments contain non-finite numbers',
66 'p5.Vector'
67 );
68 }
69 return this;
70 }
71 }
72
73 return target.call(this, args);
74 };
75 };
76}
77
78/**
79 * Each of the following decorators validates the data on vector operations.

Callers 2

p5.Vector.jsFile · 0.90
vectorValidationFunction · 0.85

Calls 2

_friendlyErrorMethod · 0.45

Tested by

no test coverage detected