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