diff --git a/.eslintrc b/.eslintrc index c8deb2bcd4..203c400fb5 100644 --- a/.eslintrc +++ b/.eslintrc @@ -36,6 +36,7 @@ "no-caller": 2, "no-undef": 2, "no-unused-vars": ["error", { "args": "none" }], + "no-empty": ["error", { "allowEmptyCatch": true }], "no-console": "off" } } diff --git a/Gruntfile.js b/Gruntfile.js index dce7622db1..1f7e37cd0d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -284,7 +284,7 @@ module.exports = function(grunt) { }, dist: { files: { - 'lib/p5.min.js': 'lib/p5.js', + 'lib/p5.min.js': 'lib/p5.pre-min.js', 'lib/addons/p5.dom.min.js': 'lib/addons/p5.dom.js' } } @@ -398,7 +398,12 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-karma'); // Create the multitasks. - grunt.registerTask('build', ['browserify', 'uglify', 'requirejs']); + grunt.registerTask('build', [ + 'browserify', + 'browserify:min', + 'uglify', + 'requirejs' + ]); grunt.registerTask('lint-no-fix', [ 'yui', // required for eslint-samples 'eslint:build', diff --git a/lib/addons/p5.dom.js b/lib/addons/p5.dom.js index 6d90281bcf..cd7d190556 100644 --- a/lib/addons/p5.dom.js +++ b/lib/addons/p5.dom.js @@ -69,6 +69,7 @@ * */ p5.prototype.select = function(e, p) { + p5._validateParameters('select', arguments); var res = null; var container = getContainer(p); if (e[0] === '.') { @@ -139,6 +140,7 @@ * */ p5.prototype.selectAll = function(e, p) { + p5._validateParameters('selectAll', arguments); var arr = []; var res; var container = getContainer(p); @@ -218,6 +220,7 @@ * */ p5.prototype.removeElements = function(e) { + p5._validateParameters('removeElements', arguments); for (var i = 0; i < this._elements.length; i++) { if (!(this._elements[i].elt instanceof HTMLCanvasElement)) { this._elements[i].remove(); @@ -305,6 +308,7 @@ * */ p5.prototype.createImg = function() { + p5._validateParameters('createImg', arguments); var elt = document.createElement('img'); var args = arguments; var self; @@ -347,6 +351,7 @@ * */ p5.prototype.createA = function(href, html, target) { + p5._validateParameters('createA', arguments); var elt = document.createElement('a'); elt.href = href; elt.innerHTML = html; @@ -399,6 +404,7 @@ * */ p5.prototype.createSlider = function(min, max, value, step) { + p5._validateParameters('createSlider', arguments); var elt = document.createElement('input'); elt.type = 'range'; elt.min = min; @@ -441,6 +447,7 @@ * */ p5.prototype.createButton = function(label, value) { + p5._validateParameters('createButton', arguments); var elt = document.createElement('button'); elt.innerHTML = label; if (value) elt.value = value; @@ -474,6 +481,7 @@ * */ p5.prototype.createCheckbox = function() { + p5._validateParameters('createCheckbox', arguments); var elt = document.createElement('div'); var checkbox = document.createElement('input'); checkbox.type = 'checkbox'; @@ -549,6 +557,7 @@ */ p5.prototype.createSelect = function() { + p5._validateParameters('createSelect', arguments); var elt, self; var arg = arguments[0]; if (typeof arg === 'object' && arg.elt.nodeName === 'SELECT') { @@ -667,6 +676,7 @@ * */ p5.prototype.createRadio = function() { + p5._validateParameters('createRadio', arguments); var radios = document.querySelectorAll('input[type=radio]'); var count = 0; if (radios.length > 1) { @@ -764,6 +774,7 @@ * */ p5.prototype.createInput = function(value, type) { + p5._validateParameters('createInput', arguments); var elt = document.createElement('input'); elt.type = type ? type : 'text'; if (value) elt.value = value; @@ -802,6 +813,7 @@ * } */ p5.prototype.createFileInput = function(callback, multiple) { + p5._validateParameters('createFileInput', arguments); // Function to handle when a file is selected // We're simplifying life and assuming that we always // want to load every selected file @@ -832,7 +844,6 @@ } } } - // Is the file stuff supported? if (window.File && window.FileReader && window.FileList && window.Blob) { // Yup, we're ok and make an input file selector @@ -916,6 +927,7 @@ * @return {p5.MediaElement|p5.Element} pointer to video p5.Element */ p5.prototype.createVideo = function(src, callback) { + p5._validateParameters('createVideo', arguments); return createMedia(this, 'video', src, callback); }; @@ -955,6 +967,7 @@ * */ p5.prototype.createAudio = function(src, callback) { + p5._validateParameters('createAudio', arguments); return createMedia(this, 'audio', src, callback); }; @@ -1047,6 +1060,7 @@ * */ p5.prototype.createCapture = function() { + p5._validateParameters('createCapture', arguments); var useVideo = true; var useAudio = true; var constraints; @@ -1123,6 +1137,7 @@ * */ p5.prototype.createElement = function(tag, content) { + p5._validateParameters('createElement', arguments); var elt = document.createElement(tag); if (typeof content !== 'undefined') { elt.innerHTML = content; diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index ec51dcd659..def5b64756 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -17,7 +17,8 @@ require('../core/error_helpers'); * Extracts the alpha value from a color or pixel array. * * @method alpha - * @param {p5.Color|Array} color p5.Color object or pixel array + * @param {p5.Color|Number[]|String} color p5.Color object, color components, + * or CSS color * @return {Number} the alpha value * @example *
@@ -61,7 +62,8 @@ p5.prototype.alpha = function(c) { * Extracts the blue value from a color or pixel array. * * @method blue - * @param {p5.Color|Array} color p5.Color object or pixel array + * @param {p5.Color|Number[]|String} color p5.Color object, color components, + * or CSS color * @return {Number} the blue value * @example *
@@ -90,7 +92,8 @@ p5.prototype.blue = function(c) { * Extracts the HSB brightness value from a color or pixel array. * * @method brightness - * @param {p5.Color|Array} color p5.Color object or pixel array + * @param {p5.Color|Number[]|String} color p5.Color object, color components, + * or CSS color * @return {Number} the brightness value * @example *
@@ -294,7 +297,7 @@ p5.prototype.brightness = function(c) { */ /** * @method color - * @param {Array} values an array containing the red,green,blue & + * @param {Number[]} values an array containing the red,green,blue & * and alpha components of the color * @return {p5.Color} */ @@ -305,6 +308,7 @@ p5.prototype.brightness = function(c) { */ p5.prototype.color = function() { + p5._validateParameters('color', arguments); if (arguments[0] instanceof p5.Color) { return arguments[0]; // Do nothing if argument is already a color object. } else if (arguments[0] instanceof Array) { @@ -314,7 +318,6 @@ p5.prototype.color = function() { return new p5.Color(this._renderer, arguments[0]); } } else { - p5._validateParameters('color', arguments); if (this instanceof p5.Renderer) { return new p5.Color(this, arguments); } else { @@ -327,7 +330,8 @@ p5.prototype.color = function() { * Extracts the green value from a color or pixel array. * * @method green - * @param {p5.Color|Array} color p5.Color object or pixel array + * @param {p5.Color|Number[]|String} color p5.Color object, color components, + * or CSS color * @return {Number} the green value * @example *
@@ -363,7 +367,8 @@ p5.prototype.green = function(c) { * maximum hue setting for each system is different.) * * @method hue - * @param {p5.Color|Array} color p5.Color object or pixel array + * @param {p5.Color|Number[]|String} color p5.Color object, color components, + * or CSS color * @return {Number} the hue * @example *
@@ -490,7 +495,8 @@ p5.prototype.lerpColor = function(c1, c2, amt) { * Extracts the HSL lightness value from a color or pixel array. * * @method lightness - * @param {p5.Color|Array} color p5.Color object or pixel array + * @param {p5.Color|Number[]|String} color p5.Color object, color components, + * or CSS color * @return {Number} the lightness * @example *
@@ -519,7 +525,8 @@ p5.prototype.lightness = function(c) { * Extracts the red value from a color or pixel array. * * @method red - * @param {p5.Color|Array} color p5.Color object or pixel array + * @param {p5.Color|Number[]|String} color p5.Color object, color components, + * or CSS color * @return {Number} the red value * @example *
@@ -563,7 +570,8 @@ p5.prototype.red = function(c) { * HSL saturation otherwise. * * @method saturation - * @param {p5.Color|Array} color p5.Color object or pixel array + * @param {p5.Color|Number[]|String} color p5.Color object, color components, + * or CSS color * @return {Number} the saturation value * @example *
diff --git a/src/color/setting.js b/src/color/setting.js index 52ec8bf194..29e870c4e0 100644 --- a/src/color/setting.js +++ b/src/color/setting.js @@ -301,6 +301,7 @@ p5.prototype.clear = function() { * @chainable */ p5.prototype.colorMode = function() { + p5._validateParameters('colorMode', arguments); if ( arguments[0] === constants.RGB || arguments[0] === constants.HSB || diff --git a/src/core/2d_primitives.js b/src/core/2d_primitives.js index 98334e6fa0..b0078cd59f 100644 --- a/src/core/2d_primitives.js +++ b/src/core/2d_primitives.js @@ -32,7 +32,7 @@ require('./error_helpers'); * @param {Number} start angle to start the arc, specified in radians * @param {Number} stop angle to stop the arc, specified in radians * @param {Constant} [mode] optional parameter to determine the way of drawing - * the arc. Parameter options are OPEN, CHORD or PIE + * the arc. either CHORD, PIE or OPEN * @chainable * @example *
@@ -232,6 +232,16 @@ p5.prototype.ellipse = function() { *3 lines of various stroke sizes. Form top, bottom and right sides of a square. * */ +/** + * @method line + * @param {Number} x1 + * @param {Number} y1 + * @param {Number} z1 the z-coordinate of the first point + * @param {Number} x2 + * @param {Number} y2 + * @param {Number} z2 the z-coordinate of the second point + * @chainable + */ p5.prototype.line = function() { if (!this._renderer._doStroke) { return this; @@ -260,6 +270,7 @@ p5.prototype.line = function() { * @method point * @param {Number} x the x-coordinate * @param {Number} y the y-coordinate + * @param {Number} [z] the z-coordinate * @chainable * @example *
@@ -326,12 +337,16 @@ p5.prototype.point = function() { * @method quad * @param {Number} x1 * @param {Number} y1 + * @param {Number} z1 * @param {Number} x2 * @param {Number} y2 + * @param {Number} z2 * @param {Number} x3 * @param {Number} y3 + * @param {Number} z3 * @param {Number} x4 * @param {Number} y4 + * @param {Number} z4 * @chainable */ p5.prototype.quad = function() { diff --git a/src/core/attributes.js b/src/core/attributes.js index b2c81f12fe..540a1b9db3 100644 --- a/src/core/attributes.js +++ b/src/core/attributes.js @@ -68,6 +68,7 @@ var constants = require('./constants'); * */ p5.prototype.ellipseMode = function(m) { + p5._validateParameters('ellipseMode', arguments); if ( m === constants.CORNER || m === constants.CORNERS || @@ -164,6 +165,7 @@ p5.prototype.noSmooth = function() { * */ p5.prototype.rectMode = function(m) { + p5._validateParameters('rectMode', arguments); if ( m === constants.CORNER || m === constants.CORNERS || @@ -230,6 +232,7 @@ p5.prototype.smooth = function() { * */ p5.prototype.strokeCap = function(cap) { + p5._validateParameters('strokeCap', arguments); if ( cap === constants.ROUND || cap === constants.SQUARE || @@ -296,6 +299,7 @@ p5.prototype.strokeCap = function(cap) { * */ p5.prototype.strokeJoin = function(join) { + p5._validateParameters('strokeJoin', arguments); if ( join === constants.ROUND || join === constants.BEVEL || @@ -330,6 +334,7 @@ p5.prototype.strokeJoin = function(join) { * */ p5.prototype.strokeWeight = function(w) { + p5._validateParameters('strokeWeight', arguments); this._renderer.strokeWeight(w); return this; }; diff --git a/src/core/core.js b/src/core/core.js index 5fb30421a2..523d1553c5 100644 --- a/src/core/core.js +++ b/src/core/core.js @@ -317,8 +317,7 @@ var p5 = function(sketch, node, sync) { this.createCanvas( this._defaultCanvasSize.width, this._defaultCanvasSize.height, - 'p2d', - true + 'p2d' ); // return preload functions to their normal vals if switched by preload diff --git a/src/core/curves.js b/src/core/curves.js index 21c6fbe5cd..88608c9180 100644 --- a/src/core/curves.js +++ b/src/core/curves.js @@ -120,6 +120,7 @@ p5.prototype.bezier = function() { * */ p5.prototype.bezierDetail = function(d) { + p5._validateParameters('bezierDetail', arguments); bezierDetail = d; return this; }; @@ -389,6 +390,7 @@ p5.prototype.curve = function() { * */ p5.prototype.curveDetail = function(d) { + p5._validateParameters('curveDetail', arguments); curveDetail = d; return this; }; @@ -435,6 +437,7 @@ p5.prototype.curveDetail = function(d) { * Line shaped like right-facing arrow,points move with mouse-x and warp shape. */ p5.prototype.curveTightness = function(t) { + p5._validateParameters('curveTightness', arguments); this._renderer._curveTightness = t; }; diff --git a/src/core/environment.js b/src/core/environment.js index 83898db807..c2db274257 100644 --- a/src/core/environment.js +++ b/src/core/environment.js @@ -51,7 +51,7 @@ p5.prototype.print = function(args) { * been displayed since the program started. Inside setup() the value is 0, * after the first iteration of draw it is 1, etc. * - * @property {Number} frameCount + * @property {Integer} frameCount * @readOnly * @example *
@@ -230,6 +230,7 @@ p5.prototype.cursor = function(type, x, y) { * @return {Number} current frameRate */ p5.prototype.frameRate = function(fps) { + p5._validateParameters('frameRate', arguments); if (typeof fps !== 'number' || fps < 0) { return this._frameRate; } else { @@ -465,6 +466,7 @@ p5.prototype.height = 0; * */ p5.prototype.fullscreen = function(val) { + p5._validateParameters('fullscreen', arguments); // no arguments, return fullscreen or not if (typeof val === 'undefined') { return ( @@ -520,6 +522,7 @@ p5.prototype.fullscreen = function(val) { * sharp 50x50 white ellipse with black outline in center of canvas. */ p5.prototype.pixelDensity = function(val) { + p5._validateParameters('pixelDensity', arguments); if (typeof val === 'number') { this._pixelDensity = val; } else { diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 4686e72b3a..6ffbe5ab8a 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -6,339 +6,469 @@ 'use strict'; var p5 = require('./core'); -var doFriendlyWelcome = false; // TEMP until we get it all working LM -// for parameter validation -var dataDoc = require('../../docs/reference/data.json'); -var arrDoc = JSON.parse(JSON.stringify(dataDoc)); - -// -- Borrowed from jQuery 1.11.3 -- -var class2type = {}; -var toString = class2type.toString; -var names = [ - 'Boolean', - 'Number', - 'String', - 'Function', - 'Array', - 'Date', - 'RegExp', - 'Object', - 'Error' -]; -for (var n = 0; n < names.length; n++) { - class2type['[object ' + names[n] + ']'] = names[n].toLowerCase(); -} -var getType = function(obj) { - if (obj == null) { - return obj + ''; - } - return typeof obj === 'object' || typeof obj === 'function' - ? class2type[toString.call(obj)] || 'object' - : typeof obj; -}; -// -- End borrow -- +if (typeof IS_MINIFIED !== 'undefined') { + p5._validateParameters = p5._friendlyFileLoadError = function() {}; +} else { + var doFriendlyWelcome = false; // TEMP until we get it all working LM + // for parameter validation + var dataDoc = require('../../docs/reference/data.json'); + var arrDoc = JSON.parse(JSON.stringify(dataDoc)); -/** - * Prints out a fancy, colorful message to the console log - * - * @private - * @param {String} message the words to be said - * @param {String} func the name of the function to link - * @param {Number|String} color CSS color string or error type - * - * @return console logs - */ -// Wrong number of params, undefined param, wrong type -var FILE_LOAD = 3; -var ERR_PARAMS = 3; -// p5.js blue, p5.js orange, auto dark green; fallback p5.js darkened magenta -// See testColors below for all the color codes and names -var typeColors = ['#2D7BB6', '#EE9900', '#4DB200', '#C83C00']; -function report(message, func, color) { - if (doFriendlyWelcome) { - friendlyWelcome(); - doFriendlyWelcome = false; - } - if ('undefined' === getType(color)) { - color = '#B40033'; // dark magenta - } else if (getType(color) === 'number') { - // Type to color - color = typeColors[color]; + // -- Borrowed from jQuery 1.11.3 -- + var class2type = {}; + var toString = class2type.toString; + var names = [ + 'Boolean', + 'Number', + 'String', + 'Function', + 'Array', + 'Date', + 'RegExp', + 'Object', + 'Error' + ]; + for (var n = 0; n < names.length; n++) { + class2type['[object ' + names[n] + ']'] = names[n].toLowerCase(); } - if (func.substring(0, 4) === 'load') { - console.log( - '> p5.js says: ' + - message + - '[https://github.com/processing/p5.js/wiki/Local-server]' - ); - } else { + var getType = function(obj) { + if (obj == null) { + return obj + ''; + } + return typeof obj === 'object' || typeof obj === 'function' + ? class2type[toString.call(obj)] || 'object' + : typeof obj; + }; + + // -- End borrow -- + + var friendlyWelcome = function() { + // p5.js brand - magenta: #ED225D + //var astrixBgColor = 'transparent'; + //var astrixTxtColor = '#ED225D'; + //var welcomeBgColor = '#ED225D'; + //var welcomeTextColor = 'white'; console.log( - '> p5.js says: ' + - message + - ' [http://p5js.org/reference/#p5/' + - func + - ']' + ' _ \n' + + ' /\\| |/\\ \n' + + " \\ ` ' / \n" + + ' / , . \\ \n' + + ' \\/|_|\\/ ' + + '\n\n> p5.js says: Welcome! ' + + 'This is your friendly debugger. ' + + 'To turn me off switch to using “p5.min.js”.' ); - } -} - -var errorCases = { - '0': { - fileType: 'image', - method: 'loadImage', - message: ' hosting the image online,' - }, - '1': { - fileType: 'XML file', - method: 'loadXML' - }, - '2': { - fileType: 'table file', - method: 'loadTable' - }, - '3': { - fileType: 'text file', - method: 'loadStrings' - }, - '4': { - fileType: 'font', - method: 'loadFont', - message: ' hosting the font online,' - } -}; -p5._friendlyFileLoadError = function(errorType, filePath) { - var errorInfo = errorCases[errorType]; - var message = - 'It looks like there was a problem' + - ' loading your ' + - errorInfo.fileType + - '.' + - ' Try checking if the file path [' + - filePath + - '] is correct,' + - (errorInfo.message || '') + - ' or running a local server.'; - report(message, errorInfo.method, FILE_LOAD); -}; + }; -/** - * Validates parameters - * param {String} func the name of the function - * param {Array} args user input arguments - * - * example: - * var a; - * ellipse(10,10,a,5); - * console ouput: - * "It looks like ellipse received an empty variable in spot #2." - * - * example: - * ellipse(10,"foo",5,5); - * console output: - * "ellipse was expecting a number for parameter #1, - * received "foo" instead." - */ -p5._validateParameters = function validateParameters(func, args) { - if (p5.disableFriendlyErrors || typeof IS_MINIFIED !== 'undefined') { - return; // skip FES - } - var arrDoc = lookupParamDoc(func); - var errorArray = []; - var minErrCount = 999999; - if (arrDoc.length > 1) { - // func has multiple formats - for (var i = 0; i < arrDoc.length; i++) { - var arrError = testParamFormat(args, arrDoc[i]); - if (arrError.length === 0) { - return; // no error - } - // see if this is the format with min number of err - if (minErrCount > arrError.length) { - minErrCount = arrError.length; - errorArray = arrError; - } + /** + * Prints out a fancy, colorful message to the console log + * + * @private + * @param {String} message the words to be said + * @param {String} func the name of the function to link + * @param {Number|String} color CSS color string or error type + * + * @return console logs + */ + // Wrong number of params, undefined param, wrong type + var FILE_LOAD = 3; + var ERR_PARAMS = 3; + // p5.js blue, p5.js orange, auto dark green; fallback p5.js darkened magenta + // See testColors below for all the color codes and names + var typeColors = ['#2D7BB6', '#EE9900', '#4DB200', '#C83C00']; + var report = function(message, func, color) { + if (doFriendlyWelcome) { + friendlyWelcome(); + doFriendlyWelcome = false; } - // generate err msg - for (var n = 0; n < errorArray.length; n++) { - p5._friendlyParamError(errorArray[n], func); + if ('undefined' === getType(color)) { + color = '#B40033'; // dark magenta + } else if (getType(color) === 'number') { + // Type to color + color = typeColors[color]; } - } else { - // func has a single format - errorArray = testParamFormat(args, arrDoc[0]); - for (var m = 0; m < errorArray.length; m++) { - p5._friendlyParamError(errorArray[m], func); + if (func.substring(0, 4) === 'load') { + console.log( + '> p5.js says: ' + + message + + '[https://github.com/processing/p5.js/wiki/Local-server]' + ); + } else { + console.log( + '> p5.js says: ' + + message + + ' [http://p5js.org/reference/#p5/' + + func + + ']' + ); } - } -}; -// validateParameters() helper functions: -// lookupParamDoc() for querying data.json -function lookupParamDoc(func) { - var queryResult = arrDoc.classitems.filter(function(x) { - return x.name === func; - }); - // different JSON structure for funct with multi-format - if (queryResult[0].hasOwnProperty('overloads')) { - var res = []; - for (var i = 0; i < queryResult[0].overloads.length; i++) { - res.push(queryResult[0].overloads[i].params); + }; + + var errorCases = { + '0': { + fileType: 'image', + method: 'loadImage', + message: ' hosting the image online,' + }, + '1': { + fileType: 'XML file', + method: 'loadXML' + }, + '2': { + fileType: 'table file', + method: 'loadTable' + }, + '3': { + fileType: 'text file', + method: 'loadStrings' + }, + '4': { + fileType: 'font', + method: 'loadFont', + message: ' hosting the font online,' } - return res; - } else { - return [queryResult[0].params]; - } -} -function testParamFormat(args, format) { - var errorArray = []; - var error; - for (var p = 0; p < format.length; p++) { - var argType = typeof args[p]; - if ('undefined' === argType || null === argType) { - if (format[p].optional !== true) { - error = { type: 'EMPTY_VAR', position: p }; - errorArray.push(error); + }; + p5._friendlyFileLoadError = function(errorType, filePath) { + var errorInfo = errorCases[errorType]; + var message = + 'It looks like there was a problem' + + ' loading your ' + + errorInfo.fileType + + '.' + + ' Try checking if the file path [' + + filePath + + '] is correct,' + + (errorInfo.message || '') + + ' or running a local server.'; + report(message, errorInfo.method, FILE_LOAD); + }; + + var docCache = {}; + var builtinTypes = [ + 'number', + 'string', + 'boolean', + 'constant', + 'function', + 'integer' + ]; + + // validateParameters() helper functions: + // lookupParamDoc() for querying data.json + var lookupParamDoc = function(func) { + var queryResult = arrDoc.classitems.filter(function(x) { + return x.name === func; + }); + // different JSON structure for funct with multi-format + var overloads = []; + if (queryResult[0].hasOwnProperty('overloads')) { + for (var i = 0; i < queryResult[0].overloads.length; i++) { + overloads.push(queryResult[0].overloads[i].params); } } else { - var types = format[p].type.split('|'); // case accepting multi-types - if (argType === 'object') { - // if object, test for class - if (!testParamClass(args[p], types)) { - // if fails to pass - error = { - type: 'WRONG_CLASS', - position: p, - correctClass: types[p], - wrongClass: args[p].name - }; - errorArray.push(error); + overloads.push(queryResult[0].params); + } + + var mapConstants = {}; + var maxParams = 0; + overloads.forEach(function(formats) { + if (maxParams < formats.length) { + maxParams = formats.length; + } + formats.forEach(function(format) { + format.types = format.type.split('|').map(function ct(type) { + // array + if (type.substr(type.length - 2, 2) === '[]') { + return { + name: type, + array: ct(type.substr(0, type.length - 2)) + }; + } + + var lowerType = type.toLowerCase(); + + // contant + if (lowerType === 'constant') { + var constant; + if (mapConstants.hasOwnProperty(format.name)) { + constant = mapConstants[format.name]; + } else { + // parse possible constant values from description + var myRe = /either\s+(?:[A-Z0-9_]+\s*,?\s*(?:or)?\s*)+/g; + var values = {}; + var names = []; + + constant = mapConstants[format.name] = { + values: values, + names: names + }; + + var myArray = myRe.exec(format.description); + if (func === 'endShape' && format.name === 'mode') { + values[p5.CLOSE] = true; + names.push('CLOSE'); + } else { + var match = myArray[0]; + var reConst = /[A-Z0-9_]+/g; + var matchConst; + while ((matchConst = reConst.exec(match)) !== null) { + var name = matchConst[0]; + if (p5.prototype.hasOwnProperty(name)) { + values[p5.prototype[name]] = true; + names.push(name); + } + } + } + } + return { + name: type, + builtin: lowerType, + names: constant.names, + values: constant.values + }; + } + + // function + if (lowerType.substr(0, 'function'.length) === 'function') { + lowerType = 'function'; + } + // builtin + if (builtinTypes.indexOf(lowerType) >= 0) { + return { name: type, builtin: lowerType }; + } + + // p5 class type + var t = window; + type.split('.').forEach(function(p) { + t = t && t[p]; + }); + if (t) { + return { name: type, prototype: t }; + } + + return { name: type, type: lowerType }; + }); + }); + }); + return { + overloads: overloads, + maxParams: maxParams + }; + }; + + var testParamType = function(param, type) { + var isArray = param instanceof Array; + if (type.array && isArray) { + for (var i = 0; i < param.length; i++) { + if (!testParamType(param[i], type.array)) { + return false; } - } else { - // not object, test for type - if (!testParamType(args[p], types)) { - // if fails to pass - error = { - type: 'WRONG_TYPE', + } + return true; + } else if (type.prototype) { + return param instanceof type.prototype; + } else if (type.builtin) { + switch (type.builtin) { + case 'number': + return typeof param === 'number' || (!!param && !isNaN(param)); + case 'integer': + return ( + (typeof param === 'number' || (!!param && !isNaN(param))) && + Number(param) === Math.floor(param) + ); + case 'boolean': + return true; + case 'array': + return isArray; + case 'string': + return typeof param === 'number' || typeof param === 'string'; + case 'constant': + return type.values.hasOwnProperty(param); + case 'function': + return param instanceof Function; + } + } + + return typeof param === type.t; + }; + + // testType() for non-object type parameter validation + // Returns true if PASS, false if FAIL + var testParamTypes = function(param, types) { + for (var i = 0; i < types.length; i++) { + if (testParamType(param, types[i])) { + return true; + } + } + return false; + }; + + var testParamFormat = function(args, formats) { + var errorArray = []; + for (var p = 0; p < formats.length; p++) { + var arg = args[p]; + var format = formats[p]; + var argType = typeof arg; + if ('undefined' === argType || null === arg) { + if (format.optional !== true) { + errorArray.push({ + type: 'EMPTY_VAR', position: p, - correctType: types[p], - wrongType: argType - }; - errorArray.push(error); + format: format + }); } + } else if (!testParamTypes(arg, format.types)) { + errorArray.push({ + type: 'WRONG_TYPE', + position: p, + format: format, + arg: arg + }); } } - } - return errorArray; -} -// testClass() for object type parameter validation -// Returns true if PASS, false if FAIL -function testParamClass(param, types) { - for (var i = 0; i < types.length; i++) { - if (types[i] === 'Array') { - if (param instanceof Array) { - return true; + return errorArray; + }; + + // function for generating console.log() msg + p5._friendlyParamError = function(errorObj, func) { + var message; + + function formatType() { + var format = errorObj.format; + return format.types + .map(function(type) { + return type.names ? type.names.join('|') : type.name; + }) + .join('|'); + } + + switch (errorObj.type) { + case 'EMPTY_VAR': + message = + func + + '() was expecting ' + + formatType() + + ' for parameter #' + + errorObj.position + + ' (zero-based index), received an empty variable instead.' + + ' If not intentional, this is often a problem with scope:' + + ' [https://p5js.org/examples/data-variable-scope.html]'; + break; + case 'WRONG_TYPE': + var arg = errorObj.arg; + var argType = arg instanceof Array ? 'array' : arg.name || typeof arg; + message = + func + + '() was expecting ' + + formatType() + + ' for parameter #' + + errorObj.position + + ' (zero-based index), received ' + + argType + + ' instead'; + break; + case 'WRONG_ARGUMENT_COUNT': + message = + func + + '() was expecting ' + + errorObj.maxParams + + ' arguments, but received ' + + errorObj.argCount; + break; + } + + if (message) { + try { + var re = /Function\.validateParameters.*[\r\n].*[\r\n].*\(([^)]*)/; + var location = re.exec(new Error().stack)[1]; + if (location) { + message += ' at ' + location; + } + } catch (err) {} + + report(message + '.', func, ERR_PARAMS); + } + }; + + /** + * Validates parameters + * param {String} func the name of the function + * param {Array} args user input arguments + * + * example: + * var a; + * ellipse(10,10,a,5); + * console ouput: + * "It looks like ellipse received an empty variable in spot #2." + * + * example: + * ellipse(10,"foo",5,5); + * console output: + * "ellipse was expecting a number for parameter #1, + * received "foo" instead." + */ + p5._validateParameters = function validateParameters(func, args) { + if (p5.disableFriendlyErrors) { + return; // skip FES + } + + var docs = docCache[func] || (docCache[func] = lookupParamDoc(func)); + var errorArray = []; + var minErrCount = 999999; + var overloads = docs.overloads; + for (var i = 0; i < overloads.length; i++) { + var arrError = testParamFormat(args, overloads[i]); + // see if this is the format with min number of err + if (minErrCount > arrError.length) { + minErrCount = arrError.length; + errorArray = arrError; } - } else { - if (param.name === types[i]) { - return true; // class name match, pass - } else if (types[i] === 'Constant') { - return true; // accepts any constant, pass + if (arrError.length === 0) { + break; // no error } } - } - return false; -} -// testType() for non-object type parameter validation -// Returns true if PASS, false if FAIL -function testParamType(param, types) { - for (var i = 0; i < types.length; i++) { - if (typeof param === types[i].toLowerCase()) { - return true; // type match, pass - } else if (types[i] === 'Constant') { - return true; // accepts any constant, pass + + if (!errorArray.length && args.length > docs.maxParams) { + errorArray.push({ + type: 'WRONG_ARGUMENT_COUNT', + argCount: args.length, + maxParams: docs.maxParams + }); } - } - return false; -} -// function for generating console.log() msg -p5._friendlyParamError = function(errorObj, func) { - var message; - switch (errorObj.type) { - case 'EMPTY_VAR': - message = - 'It looks like ' + - func + - '() received an empty variable in spot #' + - errorObj.position + - ' (zero-based index). If not intentional, this is often a problem' + - ' with scope: [https://p5js.org/examples/data-variable-scope.html].'; - report(message, func, ERR_PARAMS); - break; - case 'WRONG_CLASS': - message = - func + - '() was expecting ' + - errorObj.correctClass + - ' for parameter #' + - errorObj.position + - ' (zero-based index), received '; - // Wrap strings in quotes - message += 'an object with name ' + errorObj.wrongClass + ' instead.'; - report(message, func, ERR_PARAMS); - break; - case 'WRONG_TYPE': - message = - func + - '() was expecting ' + - errorObj.correctType + - ' for parameter #' + - errorObj.position + - ' (zero-based index), received '; - // Wrap strings in quotes - message += errorObj.wrongType + ' instead.'; - report(message, func, ERR_PARAMS); - } -}; -function friendlyWelcome() { - // p5.js brand - magenta: #ED225D - //var astrixBgColor = 'transparent'; - //var astrixTxtColor = '#ED225D'; - //var welcomeBgColor = '#ED225D'; - //var welcomeTextColor = 'white'; - console.log( - ' _ \n' + - ' /\\| |/\\ \n' + - " \\ ` ' / \n" + - ' / , . \\ \n' + - ' \\/|_|\\/ ' + - '\n\n> p5.js says: Welcome! ' + - 'This is your friendly debugger. ' + - 'To turn me off switch to using “p5.min.js”.' - ); -} -/** - * Prints out all the colors in the color pallete with white text. - * For color blindness testing. - */ -/* function testColors() { - var str = 'A box of biscuits, a box of mixed biscuits and a biscuit mixer'; - report(str, 'print', '#ED225D'); // p5.js magenta - report(str, 'print', '#2D7BB6'); // p5.js blue - report(str, 'print', '#EE9900'); // p5.js orange - report(str, 'print', '#A67F59'); // p5.js light brown - report(str, 'print', '#704F21'); // p5.js gold - report(str, 'print', '#1CC581'); // auto cyan - report(str, 'print', '#FF6625'); // auto orange - report(str, 'print', '#79EB22'); // auto green - report(str, 'print', '#B40033'); // p5.js darkened magenta - report(str, 'print', '#084B7F'); // p5.js darkened blue - report(str, 'print', '#945F00'); // p5.js darkened orange - report(str, 'print', '#6B441D'); // p5.js darkened brown - report(str, 'print', '#2E1B00'); // p5.js darkened gold - report(str, 'print', '#008851'); // auto dark cyan - report(str, 'print', '#C83C00'); // auto dark orange - report(str, 'print', '#4DB200'); // auto dark green -} */ + // generate err msg + for (var n = 0; n < errorArray.length; n++) { + p5._friendlyParamError(errorArray[n], func); + } + }; + + /** + * Prints out all the colors in the color pallete with white text. + * For color blindness testing. + */ + /* function testColors() { + var str = 'A box of biscuits, a box of mixed biscuits and a biscuit mixer'; + report(str, 'print', '#ED225D'); // p5.js magenta + report(str, 'print', '#2D7BB6'); // p5.js blue + report(str, 'print', '#EE9900'); // p5.js orange + report(str, 'print', '#A67F59'); // p5.js light brown + report(str, 'print', '#704F21'); // p5.js gold + report(str, 'print', '#1CC581'); // auto cyan + report(str, 'print', '#FF6625'); // auto orange + report(str, 'print', '#79EB22'); // auto green + report(str, 'print', '#B40033'); // p5.js darkened magenta + report(str, 'print', '#084B7F'); // p5.js darkened blue + report(str, 'print', '#945F00'); // p5.js darkened orange + report(str, 'print', '#6B441D'); // p5.js darkened brown + report(str, 'print', '#2E1B00'); // p5.js darkened gold + report(str, 'print', '#008851'); // auto dark cyan + report(str, 'print', '#C83C00'); // auto dark orange + report(str, 'print', '#4DB200'); // auto dark green + } */ + + p5.prototype._validateParameters = p5.validateParameters; +} // This is a lazily-defined list of p5 symbols that may be // misused by beginners at top-level code, outside of setup/draw. We'd like @@ -353,7 +483,7 @@ var FAQ_URL = '#why-cant-i-assign-variables-using-p5-functions-and-' + 'variables-before-setup'; -function defineMisusedAtTopLevelCode() { +var defineMisusedAtTopLevelCode = function() { var uniqueNamesFound = {}; var getSymbols = function(obj) { @@ -399,9 +529,9 @@ function defineMisusedAtTopLevelCode() { misusedAtTopLevelCode.sort(function(a, b) { return b.name.length - a.name.length; }); -} +}; -function helpForMisusedAtTopLevelCode(e, log) { +var helpForMisusedAtTopLevelCode = function(e, log) { if (!log) { log = console.log.bind(console); } @@ -447,11 +577,10 @@ function helpForMisusedAtTopLevelCode(e, log) { return true; } }); -} +}; // Exposing this primarily for unit testing. p5.prototype._helpForMisusedAtTopLevelCode = helpForMisusedAtTopLevelCode; -p5.prototype._validateParameters = p5.validateParameters; if (document.readyState !== 'complete') { window.addEventListener('error', helpForMisusedAtTopLevelCode, false); diff --git a/src/core/rendering.js b/src/core/rendering.js index a5299beed7..b96f70d158 100644 --- a/src/core/rendering.js +++ b/src/core/rendering.js @@ -48,6 +48,7 @@ var defaultId = 'defaultCanvas0'; // this gets set again in createCanvas */ p5.prototype.createCanvas = function(w, h, renderer) { + p5._validateParameters('createCanvas', arguments); //optional: renderer, otherwise defaults to p2d var r = renderer || constants.P2D; var c; @@ -137,6 +138,7 @@ p5.prototype.createCanvas = function(w, h, renderer) { * */ p5.prototype.resizeCanvas = function(w, h, noRedraw) { + p5._validateParameters('resizeCanvas', arguments); if (this._renderer) { // save canvas properties var props = {}; @@ -215,6 +217,7 @@ p5.prototype.noCanvas = function() { * */ p5.prototype.createGraphics = function(w, h, renderer) { + p5._validateParameters('createGraphics', arguments); return new p5.Graphics(w, h, renderer, this); }; @@ -284,6 +287,7 @@ p5.prototype.createGraphics = function(w, h, renderer) { * */ p5.prototype.blendMode = function(mode) { + p5._validateParameters('blendMode', arguments); if ( mode === constants.BLEND || mode === constants.DARKEST || diff --git a/src/core/transform.js b/src/core/transform.js index 888255d7e8..ed29875915 100644 --- a/src/core/transform.js +++ b/src/core/transform.js @@ -178,7 +178,7 @@ p5.prototype.resetMatrix = function() { * @method rotate * @param {Number} angle the angle of rotation, specified in radians * or degrees, depending on current angleMode - * @param {p5.Vector|Array} [axis] (in 3d) the axis to rotate around + * @param {p5.Vector|Number[]} [axis] (in 3d) the axis to rotate around * @chainable * @example *
@@ -194,6 +194,7 @@ p5.prototype.resetMatrix = function() { * */ p5.prototype.rotate = function(angle, axis) { + p5._validateParameters('rotate', arguments); var args = new Array(arguments.length); var r; for (var i = 0; i < args.length; ++i) { @@ -236,6 +237,7 @@ p5.prototype.rotate = function(angle, axis) { * 3d box rotating around the x axis. */ p5.prototype.rotateX = function(rad) { + p5._validateParameters('rotateX', arguments); if (this._renderer.isP3D) { this._renderer.rotateX(rad); } else { @@ -267,6 +269,7 @@ p5.prototype.rotateX = function(rad) { * 3d box rotating around the y axis. */ p5.prototype.rotateY = function(rad) { + p5._validateParameters('rotateY', arguments); if (this._renderer.isP3D) { this._renderer.rotateY(rad); } else { @@ -298,6 +301,7 @@ p5.prototype.rotateY = function(rad) { * 3d box rotating around the z axis. */ p5.prototype.rotateZ = function(rad) { + p5._validateParameters('rotateZ', arguments); if (this._renderer.isP3D) { this._renderer.rotateZ(rad); } else { @@ -322,8 +326,11 @@ p5.prototype.rotateZ = function(rad) { * This function can be further controlled with push() and pop(). * * @method scale - * @param {Number} x percent to scale the object in the x-axis - * @param {Number} y percent to scale the object in the y-axis + * @param {Number|p5.Vector|Number[]} s + * percent to scale the object, or percentage to + * scale the object in the x-axis if multiple arguments + * are given + * @param {Number} [y] percent to scale the object in the y-axis * @param {Number} [z] percent to scale the object in the z-axis (webgl only) * @chainable * @example @@ -350,10 +357,11 @@ p5.prototype.rotateZ = function(rad) { */ /** * @method scale - * @param {p5.Vector|Array} scales per-axis percents to scale the object + * @param {p5.Vector|Number[]} scales per-axis percents to scale the object * @chainable */ p5.prototype.scale = function() { + p5._validateParameters('scale', arguments); var x, y, z; var args = new Array(arguments.length); for (var i = 0; i < args.length; i++) { @@ -420,6 +428,7 @@ p5.prototype.scale = function() { * */ p5.prototype.shearX = function(angle) { + p5._validateParameters('shearX', arguments); if (this._angleMode === constants.DEGREES) { angle = this.radians(angle); } @@ -461,6 +470,7 @@ p5.prototype.shearX = function(angle) { * */ p5.prototype.shearY = function(angle) { + p5._validateParameters('shearY', arguments); if (this._angleMode === constants.DEGREES) { angle = this.radians(angle); } @@ -509,6 +519,7 @@ p5.prototype.shearY = function(angle) { * */ p5.prototype.translate = function(x, y, z) { + p5._validateParameters('translate', arguments); if (this._renderer.isP3D) { this._renderer.translate(x, y, z); } else { diff --git a/src/core/vertex.js b/src/core/vertex.js index 6bcdf68fa3..1063ef293b 100644 --- a/src/core/vertex.js +++ b/src/core/vertex.js @@ -244,6 +244,7 @@ p5.prototype.beginContour = function() { * */ p5.prototype.beginShape = function(kind) { + p5._validateParameters('beginShape', arguments); if ( kind === constants.POINTS || kind === constants.LINES || @@ -313,6 +314,7 @@ p5.prototype.beginShape = function(kind) { * */ p5.prototype.bezierVertex = function(x2, y2, x3, y3, x4, y4) { + p5._validateParameters('bezierVertex', arguments); if (vertices.length === 0) { throw 'vertex() must be used once before calling bezierVertex()'; } else { @@ -368,6 +370,7 @@ p5.prototype.bezierVertex = function(x2, y2, x3, y3, x4, y4) { * */ p5.prototype.curveVertex = function(x, y) { + p5._validateParameters('curveVertex', arguments); isCurve = true; this.vertex(x, y); return this; @@ -564,6 +567,7 @@ p5.prototype.endShape = function(mode) { * */ p5.prototype.quadraticVertex = function(cx, cy, x3, y3) { + p5._validateParameters('quadraticVertex', arguments); //if we're drawing a contour, put the points into an // array for inside drawing if (this._contourInited) { diff --git a/src/events/keyboard.js b/src/events/keyboard.js index 6ba5a37c50..3fba517cc4 100644 --- a/src/events/keyboard.js +++ b/src/events/keyboard.js @@ -78,7 +78,7 @@ p5.prototype.key = ''; * You can also check for custom keys by looking up the keyCode of any key * on a site like this: keycode.info. * - * @property {Number} keyCode + * @property {Integer} keyCode * @readOnly * @example *
@@ -363,6 +363,7 @@ p5.prototype._onblur = function(e) { * */ p5.prototype.keyIsDown = function(code) { + p5._validateParameters('keyIsDown', arguments); return downKeys[code]; }; diff --git a/src/image/image.js b/src/image/image.js index fc463b6bc1..274a7f9f5d 100644 --- a/src/image/image.js +++ b/src/image/image.js @@ -91,6 +91,7 @@ var p5 = require('../core/core'); // This is not global, but JSHint is not aware * */ p5.prototype.createImage = function(width, height) { + p5._validateParameters('createImage', arguments); return new p5.Image(width, height); }; @@ -143,6 +144,7 @@ p5.prototype.createImage = function(width, height) { * @param {String} [extension] */ p5.prototype.saveCanvas = function(cnv, filename, extension) { + p5._validateParameters('saveCanvas', arguments); if (cnv instanceof p5.Element) { cnv = cnv.elt; } else if (!(cnv instanceof HTMLCanvasElement)) { @@ -217,6 +219,7 @@ p5.prototype.saveCanvas = function(cnv, filename, extension) { * */ p5.prototype.saveFrames = function(fName, ext, _duration, _fps, callback) { + p5._validateParameters('saveFrames', arguments); var duration = _duration || 3; duration = p5.prototype.constrain(duration, 0, 15); duration = duration * 1000; diff --git a/src/image/loading_displaying.js b/src/image/loading_displaying.js index 24bb3e3b6e..60d3065465 100644 --- a/src/image/loading_displaying.js +++ b/src/image/loading_displaying.js @@ -64,6 +64,7 @@ require('../core/error_helpers'); * */ p5.prototype.loadImage = function(path, successCallback, failureCallback) { + p5._validateParameters('loadImage', arguments); var img = new Image(); var pImg = new p5.Image(1, 1, this); @@ -241,6 +242,8 @@ p5.prototype.image = function( ) { // set defaults per spec: https://goo.gl/3ykfOq + p5._validateParameters('image', arguments); + var defW = img.width; var defH = img.height; @@ -380,6 +383,7 @@ p5.prototype.image = function( * @param {p5.Color} color the tint color */ p5.prototype.tint = function() { + p5._validateParameters('tint', arguments); var c = this.color.apply(this, arguments); this._renderer._tint = c.levels; }; @@ -516,6 +520,7 @@ p5.prototype._getTintedImageCanvas = function(img) { * */ p5.prototype.imageMode = function(m) { + p5._validateParameters('imageMode', arguments); if ( m === constants.CORNER || m === constants.CORNERS || diff --git a/src/image/p5.Image.js b/src/image/p5.Image.js index 0f4e8d224c..f2dd3c1f1d 100644 --- a/src/image/p5.Image.js +++ b/src/image/p5.Image.js @@ -369,7 +369,7 @@ p5.Image.prototype.get = function(x, y, w, h) { * @method set * @param {Number} x x-coordinate of the pixel * @param {Number} y y-coordinate of the pixel - * @param {Number|Array|Object} a grayscale value | pixel array | + * @param {Number|Number[]|Object} a grayscale value | pixel array | * a p5.Color | image to copy * @example *
diff --git a/src/image/pixels.js b/src/image/pixels.js index ed4cb3de7a..cf19e6b40b 100644 --- a/src/image/pixels.js +++ b/src/image/pixels.js @@ -170,7 +170,7 @@ p5.prototype.blend = function() { * target region. * * @method copy - * @param {p5.Image|undefined} srcImage source image + * @param {p5.Image|p5.Graphics|undefined} srcImage source image * @param {Integer} sx X coordinate of the source's upper left corner * @param {Integer} sy Y coordinate of the source's upper left corner * @param {Integer} sw source image width @@ -205,6 +205,7 @@ p5.prototype.blend = function() { * */ p5.prototype.copy = function() { + p5._validateParameters('copy', arguments); p5.Renderer2D._copyHelper.apply(this, arguments); }; @@ -521,7 +522,7 @@ p5.prototype.loadPixels = function() { * @method set * @param {Number} x x-coordinate of the pixel * @param {Number} y y-coordinate of the pixel - * @param {Number|Array|Object} c insert a grayscale value | a pixel array | + * @param {Number|Number[]|Object} c insert a grayscale value | a pixel array | * a p5.Color object | a p5.Image to copy * @example *
diff --git a/src/io/p5.Table.js b/src/io/p5.Table.js index 1257cdedbe..a14185ad7c 100644 --- a/src/io/p5.Table.js +++ b/src/io/p5.Table.js @@ -121,7 +121,7 @@ p5.Table.prototype.addRow = function(row) { * Removes a row from the table object. * * @method removeRow - * @param {Number} id ID number of the row to remove + * @param {Integer} id ID number of the row to remove * * @example *
@@ -170,7 +170,7 @@ p5.Table.prototype.removeRow = function(id) { * can then be used to get and set values of the selected row. * * @method getRow - * @param {Number} rowID ID number of the row to get + * @param {Integer} rowID ID number of the row to get * @return {p5.TableRow} p5.TableRow object * * @example @@ -269,7 +269,7 @@ p5.Table.prototype.getRows = function() { * * @method findRow * @param {String} value The value to match - * @param {Number|String} column ID number or title of the + * @param {Integer|String} column ID number or title of the * column to search * @return {p5.TableRow} * @@ -334,7 +334,7 @@ p5.Table.prototype.findRow = function(value, column) { * * @method findRows * @param {String} value The value to match - * @param {Number|String} column ID number or title of the + * @param {Integer|String} column ID number or title of the * column to search * @return {p5.TableRow[]} An Array of TableRow objects * @@ -403,7 +403,7 @@ p5.Table.prototype.findRows = function(value, column) { * * @method matchRow * @param {String} regexp The regular expression to match - * @param {String|Number} column The column ID (number) or + * @param {String|Integer} column The column ID (number) or * title (string) * @return {p5.TableRow} TableRow object */ @@ -432,7 +432,7 @@ p5.Table.prototype.matchRow = function(regexp, column) { * * @method matchRows * @param {String} regexp The regular expression to match - * @param {String|Number} [column] The column ID (number) or + * @param {String|Integer} [column] The column ID (number) or * title (string) * @return {p5.TableRow[]} An Array of TableRow objects * @example @@ -638,7 +638,7 @@ p5.Table.prototype.addColumn = function(title) { * Returns the total number of columns in a Table. * * @method getColumnCount - * @return {Number} Number of columns in this table + * @return {Integer} Number of columns in this table */ p5.Table.prototype.getColumnCount = function() { return this.columns.length; @@ -648,7 +648,7 @@ p5.Table.prototype.getColumnCount = function() { * Returns the total number of rows in a Table. * * @method getRowCount - * @return {Number} Number of rows in this table + * @return {Integer} Number of rows in this table */ p5.Table.prototype.getRowCount = function() { return this.rows.length; @@ -663,7 +663,7 @@ p5.Table.prototype.getRowCount = function() { * * @method removeTokens * @param {String} chars String listing characters to be removed - * @param {String|Number} [column] Column ID (number) + * @param {String|Integer} [column] Column ID (number) * or name (string) */ p5.Table.prototype.removeTokens = function(chars, column) { @@ -710,7 +710,7 @@ p5.Table.prototype.removeTokens = function(chars, column) { * may be referenced by either its ID or title. * * @method trim - * @param {String|Number} column Column ID (number) + * @param {String|Integer} column Column ID (number) * or name (string) */ p5.Table.prototype.trim = function(column) { @@ -751,7 +751,7 @@ p5.Table.prototype.trim = function(column) { * would remove the second column, and so on. * * @method removeColumn - * @param {String|Number} column columnName (string) or ID (number) + * @param {String|Integer} column columnName (string) or ID (number) * * @example *
@@ -815,7 +815,7 @@ p5.Table.prototype.removeColumn = function(c) { * by either its ID or title. * * @method set - * @param {String|Number} column column ID (Number) + * @param {String|Integer} column column ID (Number) * or title (String) * @param {String|Number} value value to assign * @@ -864,8 +864,8 @@ p5.Table.prototype.set = function(row, column, value) { * by either its ID or title. * * @method setNum - * @param {Number} row row ID - * @param {String|Number} column column ID (Number) + * @param {Integer} row row ID + * @param {String|Integer} column column ID (Number) * or title (String) * @param {Number} value value to assign * @@ -910,8 +910,8 @@ p5.Table.prototype.setNum = function(row, column, value) { * by either its ID or title. * * @method setString - * @param {Number} row row ID - * @param {String|Number} column column ID (Number) + * @param {Integer} row row ID + * @param {String|Integer} column column ID (Number) * or title (String) * @param {String} value value to assign */ @@ -925,8 +925,8 @@ p5.Table.prototype.setString = function(row, column, value) { * either its ID or title. * * @method get - * @param {Number} row row ID - * @param {String|Number} column columnName (string) or + * @param {Integer} row row ID + * @param {String|Integer} column columnName (string) or * ID (number) * @return {String|Number} * @@ -972,8 +972,8 @@ p5.Table.prototype.get = function(row, column) { * either its ID or title. * * @method getNum - * @param {Number} row row ID - * @param {String|Number} column columnName (string) or + * @param {Integer} row row ID + * @param {String|Integer} column columnName (string) or * ID (number) * @return {Number} * @@ -1017,8 +1017,8 @@ p5.Table.prototype.getNum = function(row, column) { * either its ID or title. * * @method getString - * @param {Number} row row ID - * @param {String|Number} column columnName (string) or + * @param {Integer} row row ID + * @param {String|Integer} column columnName (string) or * ID (number) * @return {String} * diff --git a/src/io/p5.TableRow.js b/src/io/p5.TableRow.js index d66669230b..39141b163e 100644 --- a/src/io/p5.TableRow.js +++ b/src/io/p5.TableRow.js @@ -45,7 +45,7 @@ p5.TableRow = function(str, separator) { * The column may be specified by either its ID or title. * * @method set - * @param {String|Number} column Column ID (Number) + * @param {String|Integer} column Column ID (Number) * or Title (String) * @param {String|Number} value The value to be stored */ @@ -76,7 +76,7 @@ p5.TableRow.prototype.set = function(column, value) { * The column may be specified by either its ID or title. * * @method setNum - * @param {String|Number} column Column ID (Number) + * @param {String|Integer} column Column ID (Number) * or Title (String) * @param {Number} value The value to be stored * as a Float @@ -91,7 +91,7 @@ p5.TableRow.prototype.setNum = function(column, value) { * The column may be specified by either its ID or title. * * @method setString - * @param {String|Number} column Column ID (Number) + * @param {String|Integer} column Column ID (Number) * or Title (String) * @param {String} value The value to be stored * as a String @@ -106,7 +106,7 @@ p5.TableRow.prototype.setString = function(column, value) { * The column may be specified by either its ID or title. * * @method get - * @param {String|Number} column columnName (string) or + * @param {String|Integer} column columnName (string) or * ID (number) * @return {String|Number} */ @@ -124,7 +124,7 @@ p5.TableRow.prototype.get = function(column) { * title. * * @method getNum - * @param {String|Number} column columnName (string) or + * @param {String|Integer} column columnName (string) or * ID (number) * @return {Number} Float Floating point number */ @@ -148,7 +148,7 @@ p5.TableRow.prototype.getNum = function(column) { * title. * * @method getString - * @param {String|Number} column columnName (string) or + * @param {String|Integer} column columnName (string) or * ID (number) * @return {String} String */ diff --git a/src/io/p5.XML.js b/src/io/p5.XML.js index f483130c8b..13761051bd 100644 --- a/src/io/p5.XML.js +++ b/src/io/p5.XML.js @@ -304,7 +304,7 @@ p5.XML.prototype.getChildren = function(param) { * child is found. * * @method getChild - * @param {String|Number} name element name or index + * @param {String|Integer} name element name or index * @return {p5.XML} * @example<animal *
@@ -379,7 +379,7 @@ p5.XML.prototype.addChild = function(node) { * Removes the element specified by name or index. * * @method removeChild - * @param {String|Number} name element name or index + * @param {String|Integer} name element name or index * @example *
* // The following short XML file called "mammals.xml" is parsed @@ -451,7 +451,7 @@ p5.XML.prototype.removeChild = function(param) { * Counts the specified element's number of attributes, returned as an Number. * * @method getAttributeCount - * @return {Number} + * @return {Integer} * @example *
* // The following short XML file called "mammals.xml" is parsed diff --git a/src/math/calculation.js b/src/math/calculation.js index 1f21bed6db..8e5a2f0c0b 100644 --- a/src/math/calculation.js +++ b/src/math/calculation.js @@ -40,7 +40,7 @@ p5.prototype.abs = Math.abs; * * @method ceil * @param {Number} n number to round up - * @return {Number} rounded up number + * @return {Integer} rounded up number * @example *
* function draw() { @@ -62,8 +62,8 @@ p5.prototype.abs = Math.abs; * * // Reformat the float returned by map and draw it. * noStroke(); - * text(nfc(ax, 2, 2), ax, ay - 5); - * text(nfc(bx, 1, 1), bx, by - 5); + * text(nfc(ax, 2), ax, ay - 5); + * text(nfc(bx, 1), bx, by - 5); * } *
* @@ -114,6 +114,7 @@ p5.prototype.ceil = Math.ceil; * */ p5.prototype.constrain = function(n, low, high) { + p5._validateParameters('constrain', arguments); return Math.max(Math.min(n, high), low); }; @@ -152,7 +153,7 @@ p5.prototype.constrain = function(n, low, high) { * push(); * translate((x1 + x2) / 2, (y1 + y2) / 2); * rotate(atan2(y2 - y1, x2 - x1)); - * text(nfc(d, 1, 1), 0, -5); + * text(nfc(d, 1), 0, -5); * pop(); * // Fancy! * } @@ -172,6 +173,7 @@ p5.prototype.constrain = function(n, low, high) { * @return {Number} distance between the two points */ p5.prototype.dist = function() { + p5._validateParameters('dist', arguments); if (arguments.length === 4) { //2D return hypot(arguments[2] - arguments[0], arguments[3] - arguments[1]); @@ -241,7 +243,7 @@ p5.prototype.exp = Math.exp; * * @method floor * @param {Number} n number to round down - * @return {Number} rounded down number + * @return {Integer} rounded down number * @example *
* function draw() { @@ -263,8 +265,8 @@ p5.prototype.exp = Math.exp; * * // Reformat the float returned by map and draw it. * noStroke(); - * text(nfc(ax, 2, 2), ax, ay - 5); - * text(nfc(bx, 1, 1), bx, by - 5); + * text(nfc(ax, 2), ax, ay - 5); + * text(nfc(bx, 1), bx, by - 5); * } *
* @@ -315,6 +317,7 @@ p5.prototype.floor = Math.floor; * */ p5.prototype.lerp = function(start, stop, amt) { + p5._validateParameters('lerp', arguments); return amt * (stop - start) + start; }; @@ -408,6 +411,7 @@ p5.prototype.log = Math.log; * */ p5.prototype.mag = function(x, y) { + p5._validateParameters('mag', arguments); return hypot(x, y); }; @@ -455,6 +459,7 @@ p5.prototype.mag = function(x, y) { * */ p5.prototype.map = function(n, start1, stop1, start2, stop2, withinBounds) { + p5._validateParameters('map', arguments); var newval = (n - start1) / (stop1 - start1) * (stop2 - start2) + start2; if (!withinBounds) { return newval; @@ -472,8 +477,9 @@ p5.prototype.map = function(n, start1, stop1, start2, stop2, withinBounds) { * of any length. * * @method max - * @param {Number|Array} n0 Numbers to compare - * @return {Number} maximum Number + * @param {Number} n0 Number to compare + * @param {Number} n1 Number to compare + * @return {Number} maximum Number * @example *
* function setup() { @@ -501,7 +507,13 @@ p5.prototype.map = function(n, start1, stop1, start2, stop2, withinBounds) { * Small text at top reads: Array Elements 2 1 5 4 8 9. Large text at center: 9 * */ +/** + * @method max + * @param {Number[]} nums Numbers to compare + * @return {Number} + */ p5.prototype.max = function() { + p5._validateParameters('max', arguments); if (arguments[0] instanceof Array) { return Math.max.apply(null, arguments[0]); } else { @@ -515,8 +527,9 @@ p5.prototype.max = function() { * of any length. * * @method min - * @param {Number|Array} n0 Numbers to compare - * @return {Number} minimum Number + * @param {Number} n0 Number to compare + * @param {Number} n1 Number to compare + * @return {Number} minimum Number * @example *
* function setup() { @@ -544,7 +557,13 @@ p5.prototype.max = function() { * Small text at top reads: Array Elements 2 1 5 4 8 9. Large text at center: 1 * */ +/** + * @method min + * @param {Number[]} nums Numbers to compare + * @return {Number} + */ p5.prototype.min = function() { + p5._validateParameters('min', arguments); if (arguments[0] instanceof Array) { return Math.min.apply(null, arguments[0]); } else { @@ -601,6 +620,7 @@ p5.prototype.min = function() { * */ p5.prototype.norm = function(n, start, stop) { + p5._validateParameters('norm', arguments); return this.map(n, start, stop, 0, 1); }; @@ -644,7 +664,7 @@ p5.prototype.pow = Math.pow; * * @method round * @param {Number} n number to round - * @return {Number} rounded number + * @return {Integer} rounded number * @example *
* function draw() { @@ -666,8 +686,8 @@ p5.prototype.pow = Math.pow; * * // Reformat the float returned by map and draw it. * noStroke(); - * text(nfc(ax, 2, 2), ax, ay - 5); - * text(nfc(bx, 1, 1), bx, by - 5); + * text(nfc(ax, 2), ax, ay - 5); + * text(nfc(bx, 1), bx, by - 5); * } *
* diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 5debab5f12..945b790774 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -128,7 +128,7 @@ p5.Vector.prototype.toString = function p5VectorToString() { */ /** * @method set - * @param {p5.Vector|Array} value the vector to set + * @param {p5.Vector|Number[]} value the vector to set * @chainable */ p5.Vector.prototype.set = function set(x, y, z) { @@ -207,7 +207,7 @@ p5.Vector.prototype.copy = function copy() { */ /** * @method add - * @param {p5.Vector|Array} value the vector to add + * @param {p5.Vector|Number[]} value the vector to add * @chainable */ p5.Vector.prototype.add = function add(x, y, z) { @@ -263,7 +263,7 @@ p5.Vector.prototype.add = function add(x, y, z) { */ /** * @method sub - * @param {p5.Vector|Array} value the vector to subtract + * @param {p5.Vector|Number[]} value the vector to subtract * @chainable */ p5.Vector.prototype.sub = function sub(x, y, z) { @@ -853,7 +853,7 @@ p5.Vector.prototype.equals = function equals(x, y, z) { * // Display that variable in an onscreen text. * // (Note the nfc() function to truncate additional decimal places, * // and the "\xB0" character for the degree symbol.) - * var readout = 'angle = ' + nfc(myDegrees, 1, 1) + '\xB0'; + * var readout = 'angle = ' + nfc(myDegrees, 1) + '\xB0'; * noStroke(); * fill(0); * text(readout, 5, 15); diff --git a/src/utilities/array_functions.js b/src/utilities/array_functions.js index 9687e999b2..b7fe46a9cf 100644 --- a/src/utilities/array_functions.js +++ b/src/utilities/array_functions.js @@ -49,10 +49,10 @@ p5.prototype.append = function(array, value) { * * @method arrayCopy * @param {Array} src the source Array - * @param {Number} srcPosition starting position in the source Array + * @param {Integer} srcPosition starting position in the source Array * @param {Array} dst the destination Array - * @param {Number} dstPosition starting position in the destination Array - * @param {Number} length number of Array elements to be copied + * @param {Integer} dstPosition starting position in the destination Array + * @param {Integer} length number of Array elements to be copied * * @example *
@@ -73,7 +73,7 @@ p5.prototype.append = function(array, value) { * @method arrayCopy * @param {Array} src * @param {Array} dst - * @param {Number} [length] + * @param {Integer} [length] */ p5.prototype.arrayCopy = function(src, srcPosition, dst, dstPosition, length) { // the index to begin splicing from dst array @@ -231,7 +231,7 @@ p5.prototype.shuffle = function(arr, bool) { * * @method sort * @param {Array} list Array to sort - * @param {Number} [count] number of elements to sort, starting from 0 + * @param {Integer} [count] number of elements to sort, starting from 0 * * @example *
@@ -279,7 +279,7 @@ p5.prototype.sort = function(list, count) { * @method splice * @param {Array} list Array to splice into * @param {any} value value to be spliced in - * @param {Number} position in the array from which to insert data + * @param {Integer} position in the array from which to insert data * * @example *
@@ -311,8 +311,8 @@ p5.prototype.splice = function(list, value, index) { * * @method subset * @param {Array} list Array to extract from - * @param {Number} start position to begin - * @param {Number} [count] number of values to extract + * @param {Integer} start position to begin + * @param {Integer} [count] number of values to extract * @return {Array} Array of extracted elements * * @example diff --git a/src/utilities/string_functions.js b/src/utilities/string_functions.js index 5845a2c7eb..56a5a931c1 100644 --- a/src/utilities/string_functions.js +++ b/src/utilities/string_functions.js @@ -137,9 +137,9 @@ p5.prototype.matchAll = function(str, reg) { * * @method nf * @param {Number|String} num the Number to format - * @param {Number|String} [left] number of digits to the left of the + * @param {Integer|String} [left] number of digits to the left of the * decimal point - * @param {Number|String} [right] number of digits to the right of the + * @param {Integer|String} [right] number of digits to the right of the * decimal point * @return {String} formatted String * @@ -174,9 +174,9 @@ p5.prototype.matchAll = function(str, reg) { /** * @method nf * @param {Array} nums the Numbers to format - * @param {Number|String} [left] - * @param {Number|String} [right] - * @return {Array} formatted Strings\ + * @param {Integer|String} [left] + * @param {Integer|String} [right] + * @return {String[]} formatted Strings */ p5.prototype.nf = function() { p5._validateParameters('nf', arguments); @@ -245,7 +245,7 @@ function doNf() { * * @method nfc * @param {Number|String} num the Number to format - * @param {Number|String} [right] number of digits to the right of the + * @param {Integer|String} [right] number of digits to the right of the * decimal point * @return {String} formatted String * @@ -262,8 +262,8 @@ function doNf() { * textSize(12); * * // Draw formatted numbers - * text(nfc(num, 4, 2), 10, 30); - * text(nfc(numArr, 2, 1), 10, 80); + * text(nfc(num, 4), 10, 30); + * text(nfc(numArr, 2), 10, 80); * * // Draw dividing line * stroke(120); @@ -278,8 +278,8 @@ function doNf() { /** * @method nfc * @param {Array} nums the Numbers to format - * @param {Number|String} [right] - * @return {Array} formatted Strings + * @param {Integer|String} [right] + * @return {String[]} formatted Strings */ p5.prototype.nfc = function() { p5._validateParameters('nfc', arguments); @@ -323,9 +323,9 @@ function doNfc() { * * @method nfp * @param {Number} num the Number to format - * @param {Number} [left] number of digits to the left of the decimal + * @param {Integer} [left] number of digits to the left of the decimal * point - * @param {Number} [right] number of digits to the right of the + * @param {Integer} [right] number of digits to the right of the * decimal point * @return {String} formatted String * @@ -358,8 +358,8 @@ function doNfc() { /** * @method nfp * @param {Number[]} nums the Numbers to format - * @param {Number} [left] - * @param {Number} [right] + * @param {Integer} [left] + * @param {Integer} [right] * @return {String[]} formatted Strings */ p5.prototype.nfp = function() { @@ -387,9 +387,9 @@ function addNfp() { * * @method nfs * @param {Number} num the Number to format - * @param {Number} [left] number of digits to the left of the decimal + * @param {Integer} [left] number of digits to the left of the decimal * point - * @param {Number} [right] number of digits to the right of the + * @param {Integer} [right] number of digits to the right of the * decimal point * @return {String} formatted String * @@ -422,9 +422,9 @@ function addNfp() { /** * @method nfs * @param {Array} nums the Numbers to format - * @param {Number} [left] - * @param {Number} [right] - * @return {Array} formatted Strings + * @param {Integer} [left] + * @param {Integer} [right] + * @return {String[]} formatted Strings */ p5.prototype.nfs = function() { p5._validateParameters('nfs', arguments); @@ -554,7 +554,7 @@ p5.prototype.splitTokens = function() { /** * @method trim * @param {Array} strs an Array of Strings to be trimmed - * @return {Array} an Array of trimmed Strings + * @return {String[]} an Array of trimmed Strings */ p5.prototype.trim = function(str) { p5._validateParameters('trim', arguments); diff --git a/src/utilities/time_date.js b/src/utilities/time_date.js index d84d9025d2..70b9f04a7e 100644 --- a/src/utilities/time_date.js +++ b/src/utilities/time_date.js @@ -14,7 +14,7 @@ var p5 = require('../core/core'); * returns the current day as a value from 1 - 31. * * @method day - * @return {Number} the current day + * @return {Integer} the current day * @example *
* @@ -36,7 +36,7 @@ p5.prototype.day = function() { * returns the current hour as a value from 0 - 23. * * @method hour - * @return {Number} the current hour + * @return {Integer} the current hour * @example *
* @@ -58,7 +58,7 @@ p5.prototype.hour = function() { * returns the current minute as a value from 0 - 59. * * @method minute - * @return {Number} the current minute + * @return {Integer} the current minute * @example *
* @@ -103,7 +103,7 @@ p5.prototype.millis = function() { * returns the current month as a value from 1 - 12. * * @method month - * @return {Number} the current month + * @return {Integer} the current month * @example *
* @@ -125,7 +125,7 @@ p5.prototype.month = function() { * returns the current second as a value from 0 - 59. * * @method second - * @return {Number} the current second + * @return {Integer} the current second * @example *
* @@ -147,7 +147,7 @@ p5.prototype.second = function() { * returns the current year as an integer (2014, 2015, 2016, etc). * * @method year - * @return {Number} the current year + * @return {Integer} the current year * @example *
* diff --git a/src/webgl/material.js b/src/webgl/material.js index d255b60bfb..bb63cb5b57 100644 --- a/src/webgl/material.js +++ b/src/webgl/material.js @@ -212,7 +212,7 @@ p5.prototype.texture = function() { * example. * @method ambientMaterial * @param {Number} v1 gray value, red or hue value - * (depending on the current color mode) + * (depending on the current color mode), * @param {Number} [v2] green or saturation value * @param {Number} [v3] blue or brightness value * @param {Number} [a] opacity @@ -239,7 +239,7 @@ p5.prototype.texture = function() { */ /** * @method ambientMaterial - * @param {Array|String|p5.Color} color color, color Array, or CSS color string + * @param {Number[]|String|p5.Color} color color, color Array, or CSS color string * @chainable */ p5.prototype.ambientMaterial = function(v1, v2, v3, a) { @@ -258,8 +258,8 @@ p5.prototype.ambientMaterial = function(v1, v2, v3, a) { * possible materials in this * example. * @method specularMaterial - * @param {Number} v1 gray value, red or hue value - * (depending on the current color mode), + * @param {Number} v1 gray value, red or hue value + * (depending on the current color mode), * @param {Number} [v2] green or saturation value * @param {Number} [v3] blue or brightness value * @param {Number} [a] opacity @@ -286,7 +286,7 @@ p5.prototype.ambientMaterial = function(v1, v2, v3, a) { */ /** * @method specularMaterial - * @param {Array|String|p5.Color} color color Array, or CSS color string + * @param {Number[]|String|p5.Color} color color Array, or CSS color string * @chainable */ p5.prototype.specularMaterial = function(v1, v2, v3, a) { diff --git a/src/webgl/p5.Matrix.js b/src/webgl/p5.Matrix.js index 44a9a965a3..ebc3b1b328 100644 --- a/src/webgl/p5.Matrix.js +++ b/src/webgl/p5.Matrix.js @@ -61,7 +61,7 @@ p5.Matrix = function() { * variables, the data from a p5.Matrix, or the values from a float array. * * @method set - * @param {p5.Matrix|Float32Array|Array} [inMatrix] the input p5.Matrix or + * @param {p5.Matrix|Float32Array|Number[]} [inMatrix] the input p5.Matrix or * an Array of length 16 * @chainable */ @@ -147,7 +147,8 @@ p5.Matrix.identity = function() { /** * transpose according to a given matrix * @method transpose - * @param {p5.Matrix|Float32Array|Array} a the matrix to be based on to transpose + * @param {p5.Matrix|Float32Array|Number[]} a the matrix to be + * based on to transpose * @chainable */ p5.Matrix.prototype.transpose = function(a) { @@ -207,7 +208,8 @@ p5.Matrix.prototype.transpose = function(a) { /** * invert matrix according to a give matrix * @method invert - * @param {p5.Matrix|Float32Array|Array} a the matrix to be based on to invert + * @param {p5.Matrix|Float32Array|Number[]} a the matrix to be + * based on to invert * @chainable */ p5.Matrix.prototype.invert = function(a) { @@ -400,7 +402,7 @@ p5.Matrix.prototype.determinant = function() { /** * multiply two mat4s * @method mult - * @param {p5.Matrix|Float32Array|Array} multMatrix The matrix + * @param {p5.Matrix|Float32Array|Number[]} multMatrix The matrix * we want to multiply by * @chainable */ @@ -459,7 +461,7 @@ p5.Matrix.prototype.mult = function(multMatrix) { /** * scales a p5.Matrix by scalars or a vector * @method scale - * @param {p5.Vector|Float32Array|Array} s vector to scale by + * @param {p5.Vector|Float32Array|Number[]} s vector to scale by * @chainable */ p5.Matrix.prototype.scale = function() { @@ -505,7 +507,7 @@ p5.Matrix.prototype.scale = function() { * rotate our Matrix around an axis by the given angle. * @method rotate * @param {Number} a The angle of rotation in radians - * @param {p5.Vector|Array} axis the axis(es) to rotate around + * @param {p5.Vector|Number[]} axis the axis(es) to rotate around * @chainable * inspired by Toji's gl-matrix lib, mat4 rotation */ diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 9ddc4ea398..e3633c2d81 100755 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -356,7 +356,7 @@ p5.RendererGL.prototype.background = function() { * Basic fill material for geometry with a given color * @method fill * @class p5.RendererGL - * @param {Number|Array|String|p5.Color} v1 gray value, + * @param {Number|Number[]|String|p5.Color} v1 gray value, * red or hue value (depending on the current color mode), * or color Array, or CSS color string * @param {Number} [v2] green or saturation value @@ -463,7 +463,7 @@ p5.RendererGL.prototype.noStroke = function() { /** * Basic stroke material for geometry with a given color * @method stroke - * @param {Number|Array|String|p5.Color} v1 gray value, + * @param {Number|Number[]|String|p5.Color} v1 gray value, * red or hue value (depending on the current color mode), * or color Array, or CSS color string * @param {Number} [v2] green or saturation value @@ -942,8 +942,8 @@ p5.RendererGL.prototype._flatten = function(arr) { /** * turn a p5.Vector Array into a one dimensional number array * @private - * @param {Array} arr an array of p5.Vector - * @return {Array]} a one dimensional array of numbers + * @param {p5.Vector[]} arr an array of p5.Vector + * @return {Number[]} a one dimensional array of numbers * [p5.Vector(1, 2, 3), p5.Vector(4, 5, 6)] -> * [1, 2, 3, 4, 5, 6] */ diff --git a/tasks/build/browserify.js b/tasks/build/browserify.js index 3875e3a8fd..9eb192d8ac 100644 --- a/tasks/build/browserify.js +++ b/tasks/build/browserify.js @@ -10,13 +10,16 @@ var bannerTemplate = module.exports = function(grunt) { var srcFilePath = require.resolve('../../src/app.js'); - // This file will not exist until it has been built - var libFilePath = path.resolve('lib/p5.js'); - grunt.registerTask( 'browserify', 'Compile the p5.js source with Browserify', - function() { + function(param) { + var isMin = param === 'min'; + var filename = isMin ? 'p5.pre-min.js' : 'p5.js'; + + // This file will not exist until it has been built + var libFilePath = path.resolve('lib/' + filename); + // Reading and writing files is asynchronous var done = this.async(); @@ -24,11 +27,15 @@ module.exports = function(grunt) { var banner = grunt.template.process(bannerTemplate); // Invoke Browserify programatically to bundle the code - var bundle = browserify(srcFilePath, { + var browseified = browserify(srcFilePath, { standalone: 'p5' - }) - .transform('brfs') - .bundle(); + }); + + if (isMin) { + browseified = browseified.exclude('../../docs/reference/data.json'); + } + + var bundle = browseified.transform('brfs').bundle(); // Start the generated output with the banner comment, var code = banner + '\n'; @@ -48,7 +55,7 @@ module.exports = function(grunt) { // Print a success message grunt.log.writeln( - '>>'.green + ' Bundle ' + 'lib/p5.js'.cyan + ' created.' + '>>'.green + ' Bundle ' + ('lib/' + filename).cyan + ' created.' ); // Complete the task