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