map.transform.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createMapTransform = void 0;
  6. var _is = require("../../utils/is.js");
  7. var _function = require("../../utils/function.js");
  8. var _array = require("../../utils/array.js");
  9. var _factory = require("../../utils/factory.js");
  10. var _compileInlineExpression = require("./utils/compileInlineExpression.js");
  11. var name = 'map';
  12. var dependencies = ['typed'];
  13. var createMapTransform = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  14. var typed = _ref.typed;
  15. /**
  16. * Attach a transform function to math.map
  17. * Adds a property transform containing the transform function.
  18. *
  19. * This transform creates a one-based index instead of a zero-based index
  20. */
  21. function mapTransform(args, math, scope) {
  22. var x, callback;
  23. if (args[0]) {
  24. x = args[0].compile().evaluate(scope);
  25. }
  26. if (args[1]) {
  27. if ((0, _is.isSymbolNode)(args[1]) || (0, _is.isFunctionAssignmentNode)(args[1])) {
  28. // a function pointer, like filter([3, -2, 5], myTestFunction)
  29. callback = args[1].compile().evaluate(scope);
  30. } else {
  31. // an expression like filter([3, -2, 5], x > 0)
  32. callback = (0, _compileInlineExpression.compileInlineExpression)(args[1], math, scope);
  33. }
  34. }
  35. return map(x, callback);
  36. }
  37. mapTransform.rawArgs = true;
  38. // one-based version of map function
  39. var map = typed('map', {
  40. 'Array, function': function ArrayFunction(x, callback) {
  41. return _map(x, callback, x);
  42. },
  43. 'Matrix, function': function MatrixFunction(x, callback) {
  44. return x.create(_map(x.valueOf(), callback, x));
  45. }
  46. });
  47. return mapTransform;
  48. }, {
  49. isTransformFunction: true
  50. });
  51. /**
  52. * Map for a multi dimensional array. One-based indexes
  53. * @param {Array} array
  54. * @param {function} callback
  55. * @param {Array} orig
  56. * @return {Array}
  57. * @private
  58. */
  59. exports.createMapTransform = createMapTransform;
  60. function _map(array, callback, orig) {
  61. // figure out what number of arguments the callback function expects
  62. var argsCount = (0, _function.maxArgumentCount)(callback);
  63. function recurse(value, index) {
  64. if (Array.isArray(value)) {
  65. return (0, _array.map)(value, function (child, i) {
  66. // we create a copy of the index array and append the new index value
  67. return recurse(child, index.concat(i + 1)); // one based index, hence i + 1
  68. });
  69. } else {
  70. // invoke the (typed) callback function with the right number of arguments
  71. if (argsCount === 1) {
  72. return callback(value);
  73. } else if (argsCount === 2) {
  74. return callback(value, index);
  75. } else {
  76. // 3 or -1
  77. return callback(value, index, orig);
  78. }
  79. }
  80. }
  81. return recurse(array, []);
  82. }