-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.mjs
More file actions
150 lines (140 loc) · 3.61 KB
/
Copy pathindex.mjs
File metadata and controls
150 lines (140 loc) · 3.61 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Parsec Equations Library - ES6 Module Entry Point
*
* A generalized JavaScript library that connects to equations-parser WebAssembly
* for cross-platform, high-performance equation evaluation.
*
* This entry point provides ES6 module compatibility for modern JavaScript environments,
* bundlers, and Node.js with ES modules support.
*/
import ParsecEvaluator from './js/equations_parser_wrapper.js'
/**
* Parsec - Main class for evaluating mathematical expressions
*
* @example
* // Basic usage
* import { Parsec } from 'parsec-web';
*
* const parsec = new Parsec();
* await parsec.initialize();
*
* const result = parsec.eval('2 + 3 * sin(pi/2)');
* console.log(result); // 5
*
* @example
* // Default import
* import Parsec from 'parsec-web';
*
* const parsec = new Parsec();
* await parsec.initialize();
*/
class Parsec extends ParsecEvaluator {
constructor() {
super()
this._version = '1.0.0'
this._description = 'Fast mathematical expression evaluator powered by WebAssembly'
}
/**
* Get library version
* @returns {string} Version string
*/
getVersion() {
return this._version
}
/**
* Get library description
* @returns {string} Description string
*/
getDescription() {
return this._description
}
/**
* Get library information
* @returns {Object} Library metadata
*/
getInfo() {
return {
name: 'parsec-web',
version: this._version,
description: this._description,
repository: 'https://github.com/oxeanbits/parsec-web',
supportedPlatforms: [
'Browser (ES6)',
'Node.js (CommonJS/ES6)',
'React/Vue/Angular',
'Flutter Web',
'Webpack/Rollup/Vite',
],
features: [
'WebAssembly performance',
'Offline capability',
'Cross-platform compatibility',
'Comprehensive mathematical functions',
'String manipulation',
'Date/time operations',
'Complex numbers',
'Array operations',
'Type-safe results',
],
}
}
/**
* Batch evaluate multiple equations
* @param {string[]} equations - Array of equations to evaluate
* @returns {Object[]} Array of evaluation results
*
* @example
* const results = evaluator.evaluateBatch([
* '2 + 2',
* 'sqrt(16)',
* 'concat("Hello", " World")'
* ]);
*/
evaluateBatch(equations) {
if (!Array.isArray(equations)) {
throw new Error('equations must be an array of strings')
}
return equations.map((equation, index) => {
try {
const value = this.eval(equation)
return {
index,
equation,
value,
success: true,
}
} catch (error) {
return {
index,
equation,
success: false,
error: error.message || error.toString() || 'Unknown error',
}
}
})
}
/**
* Evaluate equation with timeout
* @param {string} equation - Mathematical expression to evaluate
* @param {number} timeoutMs - Timeout in milliseconds (default: 5000)
* @returns {Promise<Object>} Evaluation result
*/
evaluateWithTimeout(equation, timeoutMs = 5000) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error(`Equation evaluation timed out after ${timeoutMs}ms`))
}, timeoutMs)
try {
const result = this.eval(equation)
clearTimeout(timeout)
resolve(result)
} catch (error) {
clearTimeout(timeout)
reject(error)
}
})
}
}
// Export both named and default
export { Parsec }
export default Parsec