map.js 3.9 KB

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