forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.transform.js
More file actions
57 lines (47 loc) · 1.75 KB
/
filter.transform.js
File metadata and controls
57 lines (47 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { createFilter } from '../../function/matrix/filter.js'
import { factory } from '../../utils/factory.js'
import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js'
import { compileInlineExpression } from './utils/compileInlineExpression.js'
import { createTransformCallback } from './utils/transformCallback.js'
const name = 'filter'
const dependencies = ['typed']
export const createFilterTransform = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
/**
* Attach a transform function to math.filter
* Adds a property transform containing the transform function.
*
* This transform adds support for equations as test function for math.filter,
* so you can do something like 'filter([3, -2, 5], x > 0)'.
*/
function filterTransform (args, math, scope) {
const filter = createFilter({ typed })
const transformCallback = createTransformCallback({ typed })
if (args.length === 0) {
return filter()
}
let x = args[0]
if (args.length === 1) {
return filter(x)
}
const N = args.length - 1
let callback = args[N]
if (x) {
x = _compileAndEvaluate(x, scope)
}
if (callback) {
if (isSymbolNode(callback) || isFunctionAssignmentNode(callback)) {
// a function pointer, like filter([3, -2, 5], myTestFunction)
callback = _compileAndEvaluate(callback, scope)
} else {
// an expression like filter([3, -2, 5], x > 0)
callback = compileInlineExpression(callback, math, scope)
}
}
return filter(x, transformCallback(callback, N))
}
filterTransform.rawArgs = true
function _compileAndEvaluate (arg, scope) {
return arg.compile().evaluate(scope)
}
return filterTransform
}, { isTransformFunction: true })