exprify 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +135 -0
- package/dist/exprify.cjs.js +519 -0
- package/dist/exprify.cjs.js.map +1 -0
- package/dist/exprify.esm.js +511 -0
- package/dist/exprify.esm.js.map +1 -0
- package/dist/exprify.js +532 -0
- package/dist/exprify.js.map +1 -0
- package/dist/exprify.min.js +3 -0
- package/dist/exprify.min.js.map +1 -0
- package/package.json +53 -0
- package/src/core/Exprify.js +70 -0
- package/src/functions/externalFunctions.js +19 -0
- package/src/functions/internalFunctions.js +53 -0
- package/src/index.js +38 -0
- package/src/math/operations.js +48 -0
- package/src/parser/evaluator.js +57 -0
- package/src/parser/infixToPostfix.js +78 -0
- package/src/parser/tokenizer.js +145 -0
- package/src/utils/typeConverter.js +63 -0
- package/src/variables/variables.js +28 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export function stringToJS(str, variablesDB) {
|
|
2
|
+
if (typeof str !== "string" || str.length === 0) {
|
|
3
|
+
throw new Error("Invalid input: expected a non-empty string.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const firstChar = str[0];
|
|
7
|
+
const lastChar = str[str.length - 1];
|
|
8
|
+
|
|
9
|
+
// HEX (0x...)
|
|
10
|
+
if (/^0x[0-9a-fA-F]+n?$/.test(str)) {
|
|
11
|
+
|
|
12
|
+
// BigInt hex (0xFFn)
|
|
13
|
+
if (lastChar === 'n') {
|
|
14
|
+
return BigInt(str.slice(0, -1));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return Number(str);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (/^[+-]?(\d+(\.\d+)?|\.\d+)(e[+-]?\d+)?n?$/i.test(str)) {
|
|
21
|
+
|
|
22
|
+
// BigInt
|
|
23
|
+
if (lastChar === 'n') {
|
|
24
|
+
const numPart = str.slice(0, -1);
|
|
25
|
+
|
|
26
|
+
if (numPart.includes('.') || /e/i.test(numPart)) {
|
|
27
|
+
throw new Error(`Invalid BigInt: ${str}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return BigInt(numPart);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return Number(str);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (
|
|
37
|
+
(firstChar === '"' && lastChar === '"') ||
|
|
38
|
+
(firstChar === "'" && lastChar === "'") ||
|
|
39
|
+
(firstChar === '`' && lastChar === '`')
|
|
40
|
+
) {
|
|
41
|
+
return str.slice(1, -1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (
|
|
45
|
+
firstChar === '"' ||
|
|
46
|
+
firstChar === "'" ||
|
|
47
|
+
firstChar === '`'
|
|
48
|
+
) {
|
|
49
|
+
throw new Error(`Unmatched or missing quotes: ${str}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (str === "true") return true;
|
|
53
|
+
if (str === "false") return false;
|
|
54
|
+
|
|
55
|
+
if (str in variablesDB) {
|
|
56
|
+
return variablesDB[str];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
throw new Error(
|
|
60
|
+
`${str} is not defined. Use setVariable("${str}", value) first.`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
export default stringToJS;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const variablesDB = {};
|
|
2
|
+
|
|
3
|
+
// Valid JS variable name (full check)
|
|
4
|
+
const validVarName = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
5
|
+
|
|
6
|
+
export function setVariable(name, value, { override = true } = {}) {
|
|
7
|
+
|
|
8
|
+
// Name validation
|
|
9
|
+
if (typeof name !== "string" || name.trim() === "") {
|
|
10
|
+
throw new Error("Variable Name Error: Name must be a non-empty string");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!validVarName.test(name)) {
|
|
14
|
+
throw new Error(`Variable Name Error: '${name}' is not a valid variable name`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Value validation
|
|
18
|
+
if (value === undefined) {
|
|
19
|
+
throw new Error(`Variable Value Error: '${name}' cannot be undefined`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Prevent overwrite (optional)
|
|
23
|
+
if (!override && name in variablesDB) {
|
|
24
|
+
throw new Error(`Variable '${name}' already exists`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
variablesDB[name] = value;
|
|
28
|
+
}
|