1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createMapTransform = void 0;
|
7 |
|
8 | var _is = require("../../utils/is");
|
9 |
|
10 | var _function = require("../../utils/function");
|
11 |
|
12 | var _array = require("../../utils/array");
|
13 |
|
14 | var _factory = require("../../utils/factory");
|
15 |
|
16 | var _compileInlineExpression = require("./utils/compileInlineExpression");
|
17 |
|
18 | var name = 'map';
|
19 | var dependencies = ['typed'];
|
20 | var createMapTransform =
|
21 |
|
22 | (0, _factory.factory)(name, dependencies, function (_ref) {
|
23 | var typed = _ref.typed;
|
24 |
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function mapTransform(args, math, scope) {
|
32 | var x, callback;
|
33 |
|
34 | if (args[0]) {
|
35 | x = args[0].compile().evaluate(scope);
|
36 | }
|
37 |
|
38 | if (args[1]) {
|
39 | if ((0, _is.isSymbolNode)(args[1]) || (0, _is.isFunctionAssignmentNode)(args[1])) {
|
40 |
|
41 | callback = args[1].compile().evaluate(scope);
|
42 | } else {
|
43 |
|
44 | callback = (0, _compileInlineExpression.compileInlineExpression)(args[1], math, scope);
|
45 | }
|
46 | }
|
47 |
|
48 | return map(x, callback);
|
49 | }
|
50 |
|
51 | mapTransform.rawArgs = true;
|
52 |
|
53 | var map = typed('map', {
|
54 | 'Array, function': function ArrayFunction(x, callback) {
|
55 | return _map(x, callback, x);
|
56 | },
|
57 | 'Matrix, function': function MatrixFunction(x, callback) {
|
58 | return x.create(_map(x.valueOf(), callback, x));
|
59 | }
|
60 | });
|
61 | return mapTransform;
|
62 | }, {
|
63 | isTransformFunction: true
|
64 | });
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | exports.createMapTransform = createMapTransform;
|
75 |
|
76 | function _map(array, callback, orig) {
|
77 |
|
78 | var argsCount = (0, _function.maxArgumentCount)(callback);
|
79 |
|
80 | function recurse(value, index) {
|
81 | if (Array.isArray(value)) {
|
82 | return (0, _array.map)(value, function (child, i) {
|
83 |
|
84 | return recurse(child, index.concat(i + 1));
|
85 | });
|
86 | } else {
|
87 |
|
88 | if (argsCount === 1) {
|
89 | return callback(value);
|
90 | } else if (argsCount === 2) {
|
91 | return callback(value, index);
|
92 | } else {
|
93 |
|
94 | return callback(value, index, orig);
|
95 | }
|
96 | }
|
97 | }
|
98 |
|
99 | return recurse(array, []);
|
100 | } |
\ | No newline at end of file |