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