exprify 1.0.1 → 1.0.2

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.
@@ -1,178 +0,0 @@
1
- export function createUnitsStore(initial = {}) {
2
- let units = { ...initial};
3
-
4
- // ---------- Helpers ----------
5
-
6
- function getAllUnitsFlat() {
7
- const result = new Set();
8
-
9
- for (const type in units) {
10
- for (const key in units[type]) {
11
- const u = units[type][key];
12
-
13
- const keyLower = key.toLowerCase();
14
- result.add(keyLower);
15
-
16
- // Unit name
17
- if (u.unit) {
18
- const unitLower = u.unit.toLowerCase();
19
-
20
- // Avoid duplicate like "m" vs "meter"
21
- if (unitLower !== keyLower) {
22
- // Optional: only single-word units
23
- if (unitLower.split(/\s+/).length === 1) {
24
- result.add(unitLower);
25
- }
26
- }
27
- }
28
-
29
- // Symbol
30
- if (u.symbol) {
31
- const symbolLower = u.symbol.toLowerCase();
32
-
33
- // Avoid duplicate with unit name
34
- if (!u.unit || symbolLower !== u.unit.toLowerCase()) {
35
- result.add(symbolLower);
36
- }
37
- }
38
- }
39
- }
40
-
41
- return Array.from(result);
42
- }
43
-
44
- function findUnit(input) {
45
- input = input.toLowerCase();
46
-
47
- for (const type in units) {
48
- for (const key in units[type]) {
49
- const u = units[type][key];
50
-
51
- if (
52
- key.toLowerCase() === input ||
53
- u.unit?.toLowerCase() === input ||
54
- u.symbol?.toLowerCase() === input
55
- ) {
56
- return { type, key , data: u};
57
- }
58
- }
59
- }
60
-
61
- return null;
62
- }
63
-
64
- // ---------- Core Convert ----------
65
-
66
- function convert(value, fromUnit, toUnit) {
67
- const from = findUnit(fromUnit);
68
- const to = findUnit(toUnit);
69
-
70
- if (!from) throw new Error(`Unknown unit: ${fromUnit}`);
71
- if (!to) throw new Error(`Unknown unit: ${toUnit}`);
72
-
73
- if (from.type !== to.type) {
74
- throw new Error(`Cannot convert ${fromUnit} to ${toUnit} (${to.data.unit || to.key}). ${from.data.unit || from.key} conversion units like ${Object.keys(units[from.type]).join(", ")}`);
75
- }
76
-
77
- const result = value * (from.data.value / to.data.value);
78
-
79
- return { value: result, unit: to.key };
80
- }
81
-
82
- // ---------- Public API ----------
83
-
84
- return {
85
- // Get all units
86
- getUnits: () => units,
87
-
88
- // Replace all units
89
- setUnits: (newUnits) => {
90
- units = { ...newUnits };
91
- },
92
-
93
- // Update single type
94
- updateType: (type, data) => {
95
- units[type] = { ...units[type], ...data };
96
- },
97
-
98
- // Add new unit
99
- addUnit: (type, key, unitObj) => {
100
- if (!units[type]) units[type] = {};
101
- units[type][key] = unitObj;
102
- },
103
- compute(op, left, right) {
104
-
105
- const isUnit = (v) =>
106
- v && typeof v === "object" && "value" in v && "unit" in v;
107
-
108
- const apply = (a, b) => {
109
- switch (op) {
110
- case "+": return a + b;
111
- case "-": return a - b;
112
- case "*": return a * b;
113
- case "/": return a / b;
114
- case "%": return a % b;
115
- case "^": return Math.pow(a, b);
116
- }
117
- };
118
-
119
- // BOTH UNIT
120
- if (isUnit(left) && isUnit(right)) {
121
-
122
- const from = this.findUnit(right.unit);
123
- const to = this.findUnit(left.unit);
124
-
125
- if (from.type !== to.type) {
126
- throw new Error(`Cannot operate on different unit types`);
127
- }
128
-
129
- // convert right → left unit
130
- const r = right.value * (from.data.value / to.data.value);
131
-
132
- const result = apply(left.value, r);
133
-
134
- // multiplication/division produce compound units
135
- if (op === "*") {
136
- return { value: result, unit: left.unit };
137
- }
138
-
139
- if (op === "/") {
140
- return { value: result, unit: left.unit };
141
- }
142
-
143
- if (op === "^") {
144
- return { value: result, unit: left.unit };
145
- }
146
-
147
- return { value: result, unit: left.unit };
148
- }
149
-
150
- // ================= LEFT UNIT =================
151
- if (isUnit(left) && !isUnit(right)) {
152
- const result = apply(left.value, right);
153
-
154
- return { value: result, unit: left.unit };
155
- }
156
-
157
- // ================= RIGHT UNIT =================
158
- if (!isUnit(left) && isUnit(right)) {
159
- const result = apply(left, right.value);
160
-
161
- if (op === "/") {
162
- return { value: result, unit: right.unit };
163
- }
164
-
165
- return { value: result, unit: right.unit };
166
- }
167
-
168
- // ================= NORMAL =================
169
- return apply(left, right);
170
- },
171
- // Convert
172
- convert,
173
-
174
- // Search helpers
175
- getAllUnitsFlat,
176
- findUnit
177
- };
178
- }
@@ -1,75 +0,0 @@
1
- const validVarName = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
2
-
3
- export function createVarStore(initial = {}) {
4
- let store = Object.create(null);
5
-
6
-
7
- for (const key in initial) {
8
- store[key] = initial[key];
9
- }
10
-
11
- return {
12
- set(name, value, { override = true } = {}) {
13
-
14
- // Name validation
15
- if (typeof name !== "string" || !name) {
16
- throw new Error("Variable name must be a non-empty string");
17
- }
18
-
19
- if (!validVarName.test(name)) {
20
- throw new Error(`Variable Name Error: '${name}' is not a valid variable name`);
21
- }
22
-
23
- // Value validation
24
- if (value === undefined) {
25
- throw new Error(`Variable Value Error: '${name}' cannot be undefined`);
26
- }
27
-
28
- // Prevent overwrite (optional)
29
- if (!override && name in variablesDB) {
30
- throw new Error(`Variable '${name}' already exists`);
31
- }
32
-
33
- store[name] = value;
34
- },
35
-
36
- //get variable
37
- get(name) {
38
- return store[name];
39
- },
40
-
41
- // check existence
42
- has(name) {
43
- return Object.prototype.hasOwnProperty.call(store, name);
44
- },
45
-
46
- // remove variable
47
- remove(name) {
48
- delete store[name];
49
- },
50
-
51
- // get all variables (snapshot)
52
- all() {
53
- return { ...store };
54
- },
55
-
56
- // clear all
57
- clear() {
58
- store = Object.create(null);
59
- },
60
-
61
- // merge multiple variables
62
- merge(obj = {}) {
63
- for (const key in obj) {
64
- store[key] = obj[key];
65
- }
66
- },
67
-
68
- // clone store (for scoped instances)
69
- clone() {
70
- return createVarStore(store);
71
- }
72
- };
73
- }
74
-
75
- export default { createVarStore };
package/test/browser.html DELETED
@@ -1,23 +0,0 @@
1
- <script src="../dist/exprify.js"></script>
2
- <script>
3
- const expr = new Exprify(); // ✅ no namespace needed (usually)
4
- // console.log(expr.parse("5 + 7 * 2")); // 19
5
- // console.log(expr.evaluate("5 + 7 * 2")); // 19
6
- console.log(expr.parse("max(5, 2)")); // 19
7
- // console.log(expr.evaluate("max('5' + 7 * 2)")); // 19
8
-
9
-
10
- // const exprify = new Exprify();
11
- // const exprFn = exprify.compile("or(a, b)");
12
- // console.log(exprFn({ a: true, b: false })); // 22
13
- // console.log(exprFn({ a: 7, b: 3 })); // 17
14
-
15
- console.log(expr.parse("5 px to em")); // 19
16
- console.log(expr.evaluate("5 px to em")); // 19
17
-
18
- // console.log(expr.parse("value |> double |> sqrt")); // 19
19
- // console.log(expr.evaluate("value |> double |> sqrt")); // 19
20
-
21
-
22
-
23
- </script>
@@ -1,140 +0,0 @@
1
- import Exprify from "../src/core/Exprify.js";
2
-
3
- describe("Exprify Engine - Extended Tests", () => {
4
- let expr;
5
-
6
- beforeEach(() => {
7
- expr = new Exprify();
8
- });
9
-
10
- /* ================= BASIC ================= */
11
- test("addition", () => {
12
- expect(expr.evaluate("2 + 3 + 5")).toBe(10);
13
- });
14
-
15
- test("operator precedence", () => {
16
- expect(expr.evaluate("2 + 3 * 4")).toBe(14);
17
- });
18
-
19
- test("parentheses override precedence", () => {
20
- expect(expr.evaluate("(2 + 3) * 4")).toBe(20);
21
- });
22
-
23
- test("mixed parentheses", () => {
24
- expect(expr.evaluate("(1 + 2) * (3 + 4)")).toBe(21);
25
- });
26
-
27
- /* ================= NESTED ================= */
28
- test("nested parentheses", () => {
29
- expect(expr.evaluate("((2 + 3) * (4 + 1))")).toBe(25);
30
- });
31
-
32
- test("deep nesting", () => {
33
- expect(expr.evaluate("(((1 + 1) + 1) + 1)")).toBe(4);
34
- });
35
-
36
- /* ================= UNARY ================= */
37
- test("unary minus", () => {
38
- expect(expr.evaluate("-5 + 10")).toBe(5);
39
- });
40
-
41
- test("double unary", () => {
42
- expect(expr.evaluate("--5")).toBe(5);
43
- });
44
-
45
- /* ================= POWER ================= */
46
- test("power operator", () => {
47
- expect(expr.evaluate("2 ^ 3")).toBe(8);
48
- });
49
-
50
- test("power precedence", () => {
51
- expect(expr.evaluate("2 + 2 ^ 3")).toBe(10);
52
- });
53
-
54
- /* ================= LOGICAL ================= */
55
- test("logical AND", () => {
56
- expect(expr.evaluate("true && false")).toBe(false);
57
- });
58
-
59
- test("logical OR", () => {
60
- expect(expr.evaluate("true || false")).toBe(true);
61
- });
62
-
63
- /* ================= FUNCTION ================= */
64
- test("function call", () => {
65
- expect(expr.evaluate("max(2, 5, 3)")).toBe(5);
66
- });
67
-
68
- test("nested function", () => {
69
- expect(expr.evaluate("max(2, min(5, 3))")).toBe(3);
70
- });
71
-
72
- test("matrix determinant with semicolon rows", () => {
73
- expect(expr.evaluate("det([-1, 2; 3, 1])")).toBe(-7);
74
- });
75
-
76
- /* ================= STRING ================= */
77
- test("string concat", () => {
78
- expect(expr.evaluate('"Hello " + "World"')).toBe("Hello World");
79
- });
80
-
81
- /* ================= BIGINT ================= */
82
- test("bigint power", () => {
83
- expect(expr.evaluate("11n ^ 2n")).toBe(121n);
84
- });
85
-
86
- /* ================= UNIT ================= */
87
- test("unit conversion", () => {
88
- expect(expr.evaluate("2 inch to cm")).toBe("5.08 cm");
89
- });
90
-
91
- test("unit addition", () => {
92
- expect(expr.evaluate("5 cm + 2 inch")).toBe("10.08 cm");
93
- });
94
-
95
- /* ================= EDGE CASE ================= */
96
- test("division", () => {
97
- expect(expr.evaluate("10 / 2")).toBe(5);
98
- });
99
-
100
- test("modulus", () => {
101
- expect(expr.evaluate("10 % 3")).toBe(1);
102
- });
103
-
104
- test("invalid expression", () => {
105
- expect(() => expr.evaluate("(2 + 3")).toThrow();
106
- });
107
-
108
-
109
- test("set and use variable", () => {
110
- expr.setVariable("x", 5);
111
- expr.setVariable("y", 3);
112
- expect(expr.evaluate("x + y")).toBe(8);
113
- expect(expr.evaluate("x * y + 2")).toBe(17); // 5*3=15 +2=17
114
- });
115
-
116
- test("variable in parentheses", () => {
117
- expr.setVariable("a", 2);
118
- expr.setVariable("b", 4);
119
- expect(expr.evaluate("(a + b) * 3")).toBe(18); // (2+4)*3=18
120
- });
121
-
122
- test("add and use external function", () => {
123
- // Example: double(n) returns n*2
124
- expr.addFunction("double", (n) => n * 2);
125
- expect(expr.evaluate("double(4)")).toBe(8);
126
- expect(expr.evaluate("2 + double(5)")).toBe(12); // 2+10=12
127
- });
128
-
129
- test("external function with multiple arguments", () => {
130
- expr.addFunction("sumThree", (a, b, c) => a + b + c);
131
- expect(expr.evaluate("sumThree(2, 3, 5)")).toBe(10);
132
- });
133
-
134
- test("nested function calls", () => {
135
- expr.addFunction("double", (n) => n * 2);
136
- expr.addFunction("addTen", (n) => n + 10);
137
- expect(expr.evaluate("addTen(double(5))")).toBe(20); // double(5)=10 → addTen(10)=20
138
- });
139
-
140
- });