1 | import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
|
2 | import { maxArgumentCount } from '../../utils/function.js';
|
3 | import { forEach } from '../../utils/array.js';
|
4 | import { factory } from '../../utils/factory.js';
|
5 | import { compileInlineExpression } from './utils/compileInlineExpression.js';
|
6 | var name = 'forEach';
|
7 | var dependencies = ['typed'];
|
8 | export var createForEachTransform = factory(name, dependencies, _ref => {
|
9 | var {
|
10 | typed
|
11 | } = _ref;
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function forEachTransform(args, math, scope) {
|
20 | var x, callback;
|
21 |
|
22 | if (args[0]) {
|
23 | x = args[0].compile().evaluate(scope);
|
24 | }
|
25 |
|
26 | if (args[1]) {
|
27 | if (isSymbolNode(args[1]) || isFunctionAssignmentNode(args[1])) {
|
28 |
|
29 | callback = args[1].compile().evaluate(scope);
|
30 | } else {
|
31 |
|
32 | callback = compileInlineExpression(args[1], math, scope);
|
33 | }
|
34 | }
|
35 |
|
36 | return _forEach(x, callback);
|
37 | }
|
38 |
|
39 | forEachTransform.rawArgs = true;
|
40 |
|
41 | var _forEach = typed('forEach', {
|
42 | 'Array | Matrix, function': function ArrayMatrixFunction(array, callback) {
|
43 |
|
44 | var args = maxArgumentCount(callback);
|
45 |
|
46 | var recurse = function recurse(value, index) {
|
47 | if (Array.isArray(value)) {
|
48 | forEach(value, function (child, i) {
|
49 |
|
50 | recurse(child, index.concat(i + 1));
|
51 | });
|
52 | } else {
|
53 |
|
54 | if (args === 1) {
|
55 | callback(value);
|
56 | } else if (args === 2) {
|
57 | callback(value, index);
|
58 | } else {
|
59 |
|
60 | callback(value, index, array);
|
61 | }
|
62 | }
|
63 | };
|
64 |
|
65 | recurse(array.valueOf(), []);
|
66 | }
|
67 | });
|
68 |
|
69 | return forEachTransform;
|
70 | }, {
|
71 | isTransformFunction: true
|
72 | }); |
\ | No newline at end of file |