@@ -6,12 +6,27 @@ const samplers = {
66 builtIn : builtInMathEval
77}
88
9- if ( ( global as any ) . math ) {
10- samplers . builtIn = ( global as any ) . math . compile
9+ // getMathJS returns checks if mathjs is loaded.
10+ function getMathJS ( ) : { compile : any } | null {
11+ if ( typeof global === 'object' && 'math' in global ) {
12+ // @ts -ignore
13+ return global [ 'math' ] as any
14+ }
15+ if ( typeof window === 'object' && 'math' in window ) {
16+ // @ts -ignore
17+ return window [ 'math' ]
18+ }
19+ return null
20+ }
21+
22+ let mathJS = getMathJS ( )
23+ if ( mathJS ) {
24+ // override the built-in module with mathjs's compile
25+ samplers . builtIn = mathJS . compile
1126}
1227
13- function generateEvaluator ( samplerName : 'interval' | 'builtIn' ) {
14- function doCompile ( expression : string | { eval : ( scope : any ) => any } ) {
28+ function generateEvaluator ( samplerName : 'interval' | 'builtIn' ) {
29+ function doCompile ( expression : string | { eval : ( scope : any ) => any } ) {
1530 // compiles does the following
1631 //
1732 // when expression === string
@@ -36,7 +51,7 @@ function generateEvaluator (samplerName: 'interval' | 'builtIn') {
3651 // othewise throw an error
3752 if ( typeof expression === 'string' ) {
3853 const compiled = samplers [ samplerName ] ( expression )
39- if ( ( global as any ) . math && samplerName === 'builtIn' ) {
54+ if ( mathJS && samplerName === 'builtIn' ) {
4055 // if mathjs is included use its evaluate method instead
4156 return { eval : compiled . evaluate || compiled . eval }
4257 }
@@ -48,7 +63,7 @@ function generateEvaluator (samplerName: 'interval' | 'builtIn') {
4863 }
4964 }
5065
51- function compileIfPossible ( meta : any , property : string ) {
66+ function compileIfPossible ( meta : any , property : string ) {
5267 // compile the function using interval arithmetic, cache the result
5368 // so that multiple calls with the same argument don't trigger the
5469 // kinda expensive compilation process
@@ -61,7 +76,7 @@ function generateEvaluator (samplerName: 'interval' | 'builtIn') {
6176 }
6277 }
6378
64- function getCompiledExpression ( meta : any , property : string ) {
79+ function getCompiledExpression ( meta : any , property : string ) {
6580 return meta [ samplerName + '_Compiled_' + property ]
6681 }
6782
@@ -79,7 +94,7 @@ function generateEvaluator (samplerName: 'interval' | 'builtIn') {
7994 * @returns {Number|Array } The builtIn evaluator returns a number, the
8095 * interval evaluator an array
8196 */
82- function evaluate ( meta : any , property : string , variables : any ) {
97+ function evaluate ( meta : any , property : string , variables : any ) {
8398 // e.g.
8499 //
85100 // meta: {
@@ -91,9 +106,7 @@ function generateEvaluator (samplerName: 'interval' | 'builtIn') {
91106 //
92107 compileIfPossible ( meta , property )
93108
94- return getCompiledExpression ( meta , property ) . eval (
95- Object . assign ( { } , meta . scope || { } , variables )
96- )
109+ return getCompiledExpression ( meta , property ) . eval ( Object . assign ( { } , meta . scope || { } , variables ) )
97110 }
98111
99112 return evaluate
0 commit comments