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