map.js (3879B)
1 import { maxArgumentCount } from '../../utils/function.js'; 2 import { factory } from '../../utils/factory.js'; 3 var name = 'map'; 4 var dependencies = ['typed']; 5 export var createMap = /* #__PURE__ */factory(name, dependencies, _ref => { 6 var { 7 typed 8 } = _ref; 9 10 /** 11 * Create a new matrix or array with the results of a callback function executed on 12 * each entry of a given matrix/array. 13 * 14 * For each entry of the input, the callback is invoked with three arguments: 15 * the value of the entry, the index at which that entry occurs, and the full 16 * matrix/array being traversed. Note that because the matrix/array might be 17 * multidimensional, the "index" argument is always an array of numbers giving 18 * the index in each dimension. This is true even for vectors: the "index" 19 * argument is an array of length 1, rather than simply a number. 20 * 21 * Syntax: 22 * 23 * math.map(x, callback) 24 * 25 * Examples: 26 * 27 * math.map([1, 2, 3], function(value) { 28 * return value * value 29 * }) // returns [1, 4, 9] 30 * 31 * // The calling convention for the callback can cause subtleties: 32 * math.map([1, 2, 3], math.format) 33 * // throws TypeError: map attempted to call 'format(1,[0])' but argument 2 of type Array does not match expected type number or function or Object or string or boolean 34 * // [This happens because `format` _can_ take a second argument, 35 * // but its semantics don't match that of the 2nd argument `map` provides] 36 * 37 * // To avoid this error, use a function that takes exactly the 38 * // desired arguments: 39 * math.map([1, 2, 3], x => math.format(x)) // returns ['1', '2', '3'] 40 * 41 * See also: 42 * 43 * filter, forEach, sort 44 * 45 * @param {Matrix | Array} x The input to iterate on. 46 * @param {Function} callback 47 * The function to call (as described above) on each entry of the input 48 * @return {Matrix | array} 49 * Transformed map of x; always has the same type and shape as x 50 */ 51 return typed(name, { 52 'Array, function': _map, 53 'Matrix, function': function MatrixFunction(x, callback) { 54 return x.map(callback); 55 } 56 }); 57 }); 58 /** 59 * Map for a multi dimensional array 60 * @param {Array} array 61 * @param {Function} callback 62 * @return {Array} 63 * @private 64 */ 65 66 function _map(array, callback) { 67 // figure out what number of arguments the callback function expects 68 var args = maxArgumentCount(callback); 69 70 var recurse = function recurse(value, index) { 71 if (Array.isArray(value)) { 72 return value.map(function (child, i) { 73 // we create a copy of the index array and append the new index value 74 return recurse(child, index.concat(i)); 75 }); 76 } else { 77 try { 78 // invoke the callback function with the right number of arguments 79 if (args === 1) { 80 return callback(value); 81 } else if (args === 2) { 82 return callback(value, index); 83 } else { 84 // 3 or -1 85 return callback(value, index, array); 86 } 87 } catch (err) { 88 // But maybe the arguments still weren't right 89 if (err instanceof TypeError && 'data' in err && err.data.category === 'wrongType') { 90 var newmsg = "map attempted to call '".concat(err.data.fn, "(").concat(value); 91 var indexString = JSON.stringify(index); 92 93 if (args === 2) { 94 newmsg += ',' + indexString; 95 } else if (args !== 1) { 96 newmsg += ",".concat(indexString, ",").concat(array); 97 } 98 99 newmsg += ")' but argument ".concat(err.data.index + 1, " of type "); 100 newmsg += "".concat(err.data.actual, " does not match expected type "); 101 newmsg += err.data.expected.join(' or '); 102 throw new TypeError(newmsg); 103 } 104 105 throw err; 106 } 107 } 108 }; 109 110 return recurse(array, []); 111 }