@goadminuiux/calculator 0.0.0-stage โ 1.0.1
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/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +223 -2
- package/dist/calculator.esm.js +1049 -0
- package/dist/calculator.js +1234 -0
- package/dist/calculator.min.js +18 -0
- package/package.json +50 -4
- package/src/calculator.js +641 -0
- package/src/formatter.js +88 -0
- package/src/index.js +434 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [1.0.0] - 2026-10-14
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- Core `Calculator` engine completely decoupled from HTML/DOM.
|
|
10
|
+
- Safe, zero-dependency mathematical expression evaluator `Calculator.evaluate(expr)` supporting operator precedence, parentheses, decimals, and percentages.
|
|
11
|
+
- Arithmetic helpers: `Calculator.add()`, `subtract()`, `multiply()`, `divide()`, `modulo()`, `power()`.
|
|
12
|
+
- Smart percentage calculation logic: `percent()`, `addPercent()`, `subtractPercent()`, `multiplyPercent()`, `dividePercent()`.
|
|
13
|
+
- Scientific functions: `sin()`, `cos()`, `tan()`, `asin()`, `acos()`, `atan()`, `sqrt()`, `sqr()`, `cube()`, `log()`, `ln()`, `exp()`, `reciprocal()`, `abs()`, `factorial()`.
|
|
14
|
+
- Financial utilities: `tax()`, `tip()`, `discount()`.
|
|
15
|
+
- Formatter utilities with IEEE 754 precision rounding, comma grouping, and currency prefixes.
|
|
16
|
+
- Stateful `StreamCalculator` engine for desk/handheld calculator workflows (history tokens, memory reel, tape history).
|
|
17
|
+
- `AdminUIUXCalculator` DOM binding component with automated `DOMContentLoaded` discovery, keyboard shortcuts, output target binding (`data-calculator-output`), calculation target sync (`data-calculator-calculation`), and `calculator:change` custom events.
|
|
18
|
+
- UMD, ESM, CommonJS, and Browser Global packaging support.
|
|
19
|
+
- Unit test suite and interactive demo application.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AdminUIUX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,224 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @goadminuiux/calculator
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Production-ready, zero-dependency universal JavaScript calculator engine & DOM component with chained stream arithmetic, smart percentage handling, scientific math functions, and accessibility.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@goadminuiux/calculator)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
[](package.json)
|
|
8
|
+
[](dist/calculator.min.js)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## ๐ Features
|
|
13
|
+
|
|
14
|
+
- **Zero External Dependencies**: Pure vanilla JavaScript. No Bootstrap, jQuery, React, or build requirements needed for basic execution.
|
|
15
|
+
- **Headless Core Engine**: Complete mathematical calculation engine (`Calculator`) completely decoupled from the DOM.
|
|
16
|
+
- **Safe Expression Evaluator**: `Calculator.evaluate("100 + 25 * 2")` follows standard operator precedence (PEMDAS / BODMAS) without unsafe `eval()` or `new Function()`.
|
|
17
|
+
- **Smart Percentage Logic**: Resolves `100 ร 10% = 10`, `100 + 10% = 110`, `100 โ 10% = 90`, and `100 รท 10% = 1000`.
|
|
18
|
+
- **Stream / Desk Calculator**: Stateful engine simulating physical handheld calculators with chained arithmetic, operand stack, and memory (`M+`, `M-`, `MR`, `MC`).
|
|
19
|
+
- **Scientific Functions**: Trigonometric (`sin`, `cos`, `tan`, inverses), logarithmic (`log`, `ln`), exponential (`exp`), powers, roots (`sqrt`, `sqr`, `cube`), and factorial (`fact`).
|
|
20
|
+
- **Financial Helpers**: Instant tax addition/deduction (`tax`), tipping (`tip`), and discounts (`discount`).
|
|
21
|
+
- **DOM Binding Layer (`AdminUIUXCalculator`)**: Automatic discovery and initialization on `.calculator` containers with live display syncing, keyboard listeners, RTL support, and custom events.
|
|
22
|
+
- **Universal Package**: Supports ES Modules, CommonJS, AMD, and Browser `<script>` tags via jsDelivr CDN.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## ๐ฆ Installation
|
|
27
|
+
|
|
28
|
+
### Via npm
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @goadminuiux/calculator
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Via jsDelivr CDN
|
|
35
|
+
|
|
36
|
+
```html
|
|
37
|
+
<!-- Minified Production Build -->
|
|
38
|
+
<script src="https://cdn.jsdelivr.net/npm/@goadminuiux/calculator@1.0.0/dist/calculator.min.js"></script>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Or directly from your local repository:
|
|
42
|
+
|
|
43
|
+
```html
|
|
44
|
+
<script src="path/to/plugins-custom/calculator.min.js"></script>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## ๐ Quick Usage
|
|
50
|
+
|
|
51
|
+
### 1. Headless JavaScript API (ES Modules)
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
import { Calculator, Formatter } from "@goadminuiux/calculator";
|
|
55
|
+
|
|
56
|
+
// Evaluate mathematical expressions
|
|
57
|
+
const total = Calculator.evaluate("100 + 25 * 2"); // 150
|
|
58
|
+
const grouped = Calculator.evaluate("(10 + 20) * 3"); // 90
|
|
59
|
+
const percentCalc = Calculator.evaluate("100 * 10%"); // 10
|
|
60
|
+
|
|
61
|
+
// Arithmetic helpers
|
|
62
|
+
Calculator.add(10, 25); // 35
|
|
63
|
+
Calculator.subtract(100, 45); // 55
|
|
64
|
+
Calculator.multiply(12, 12); // 144
|
|
65
|
+
Calculator.divide(100, 4); // 25
|
|
66
|
+
|
|
67
|
+
// Percentages
|
|
68
|
+
Calculator.percent(250, 15); // 37.5
|
|
69
|
+
Calculator.addPercent(100, 10); // 110
|
|
70
|
+
Calculator.subtractPercent(100, 10); // 90
|
|
71
|
+
|
|
72
|
+
// Formatting
|
|
73
|
+
Formatter.format(1234567.89, { currency: "$" }); // "$ 1,234,567.89"
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 2. CommonJS (Node.js)
|
|
77
|
+
|
|
78
|
+
```javascript
|
|
79
|
+
const { Calculator, Formatter } = require("@goadminuiux/calculator");
|
|
80
|
+
|
|
81
|
+
console.log(Calculator.evaluate("2 ^ 3 + 10")); // 18
|
|
82
|
+
console.log(Calculator.tax(100, 0.18)); // 118
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 3. Direct Browser `<script>` & CDN
|
|
86
|
+
|
|
87
|
+
```html
|
|
88
|
+
<!-- Include Calculator Script -->
|
|
89
|
+
<script src="https://cdn.jsdelivr.net/npm/@goadminuiux/calculator@1.0.0/dist/calculator.min.js"></script>
|
|
90
|
+
|
|
91
|
+
<script>
|
|
92
|
+
// Access global namespace
|
|
93
|
+
const result = window.Calculator.evaluate("500 - 15%");
|
|
94
|
+
console.log(result); // 425
|
|
95
|
+
</script>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## ๐ฅ๏ธ Drop-in HTML Widget (`AdminUIUXCalculator`)
|
|
101
|
+
|
|
102
|
+
Add HTML with data attributes. The library automatically detects and binds to `.calculator` containers on `DOMContentLoaded`:
|
|
103
|
+
|
|
104
|
+
```html
|
|
105
|
+
<div class="calculator card shadow-sm" data-calculator-type="basic" data-calculator-output="#orderTotal">
|
|
106
|
+
<div class="card-body">
|
|
107
|
+
<!-- Display Screen -->
|
|
108
|
+
<div class="calculator-screen bg-dark text-white p-3 rounded-3 mb-3">
|
|
109
|
+
<p class="calculator-history small opacity-75 font-monospace text-end" data-calc-history> </p>
|
|
110
|
+
<h4 class="calculator-display font-monospace text-end mb-0" data-calc-display>0</h4>
|
|
111
|
+
</div>
|
|
112
|
+
|
|
113
|
+
<!-- Buttons Grid -->
|
|
114
|
+
<div class="row g-2 mb-2">
|
|
115
|
+
<div class="col-3"><button type="button" class="btn btn-danger w-100" data-calc-action="clear">C</button></div>
|
|
116
|
+
<div class="col-3">
|
|
117
|
+
<button type="button" class="btn btn-secondary w-100" data-calc-action="backspace"><i class="bi bi-backspace"></i></button>
|
|
118
|
+
</div>
|
|
119
|
+
<div class="col-3"><button type="button" class="btn btn-secondary w-100" data-calc-action="percent">%</button></div>
|
|
120
|
+
<div class="col-3"><button type="button" class="btn btn-primary w-100" data-calc-action="operator" data-val="/">รท</button></div>
|
|
121
|
+
</div>
|
|
122
|
+
<div class="row g-2 mb-2">
|
|
123
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="7">7</button></div>
|
|
124
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="8">8</button></div>
|
|
125
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="9">9</button></div>
|
|
126
|
+
<div class="col-3"><button type="button" class="btn btn-primary w-100" data-calc-action="operator" data-val="*">ร</button></div>
|
|
127
|
+
</div>
|
|
128
|
+
<div class="row g-2 mb-2">
|
|
129
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="4">4</button></div>
|
|
130
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="5">5</button></div>
|
|
131
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="6">6</button></div>
|
|
132
|
+
<div class="col-3"><button type="button" class="btn btn-primary w-100" data-calc-action="operator" data-val="-">โ</button></div>
|
|
133
|
+
</div>
|
|
134
|
+
<div class="row g-2 mb-2">
|
|
135
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="1">1</button></div>
|
|
136
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="2">2</button></div>
|
|
137
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="3">3</button></div>
|
|
138
|
+
<div class="col-3"><button type="button" class="btn btn-primary w-100" data-calc-action="operator" data-val="+">+</button></div>
|
|
139
|
+
</div>
|
|
140
|
+
<div class="row g-2">
|
|
141
|
+
<div class="col-3"><button type="button" class="btn btn-secondary w-100" data-calc-action="negate">ยฑ</button></div>
|
|
142
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val="0">0</button></div>
|
|
143
|
+
<div class="col-3"><button type="button" class="btn btn-light w-100" data-calc-action="num" data-val=".">.</button></div>
|
|
144
|
+
<div class="col-3"><button type="button" class="btn btn-primary w-100" data-calc-action="equals">=</button></div>
|
|
145
|
+
</div>
|
|
146
|
+
</div>
|
|
147
|
+
</div>
|
|
148
|
+
|
|
149
|
+
<!-- Output Target receiving live sync -->
|
|
150
|
+
<div>Total: <span id="orderTotal">0</span></div>
|
|
151
|
+
|
|
152
|
+
<script src="dist/calculator.min.js"></script>
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Manual JavaScript Initialization
|
|
156
|
+
|
|
157
|
+
```javascript
|
|
158
|
+
import { AdminUIUXCalculator } from "@goadminuiux/calculator";
|
|
159
|
+
|
|
160
|
+
// Initialize on specific element
|
|
161
|
+
const calc = AdminUIUXCalculator.init("#myCalculator", {
|
|
162
|
+
precision: 4,
|
|
163
|
+
currency: "$",
|
|
164
|
+
enableKeyboard: true,
|
|
165
|
+
onCalculate: (result, history) => {
|
|
166
|
+
console.log("Calculation complete:", result, history);
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## ๐ API Reference
|
|
174
|
+
|
|
175
|
+
### `Calculator`
|
|
176
|
+
|
|
177
|
+
| Method | Parameters | Description |
|
|
178
|
+
| ---------------------------- | -------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
|
|
179
|
+
| `evaluate(expr, options?)` | `expr: string`, `options?: { precision?: number }` | Evaluates string expression with full operator precedence (e.g. `100 + 25 * 2`, `(10 + 20) * 3`, `100 - 10%`). |
|
|
180
|
+
| `add(a, b, precision?)` | `a: number`, `b: number` | Adds two numbers with IEEE 754 precision rounding. |
|
|
181
|
+
| `subtract(a, b, precision?)` | `a: number`, `b: number` | Subtracts `b` from `a`. |
|
|
182
|
+
| `multiply(a, b, precision?)` | `a: number`, `b: number` | Multiplies `a` by `b`. |
|
|
183
|
+
| `divide(a, b, precision?)` | `a: number`, `b: number` | Divides `a` by `b` (returns `0` on divide-by-zero). |
|
|
184
|
+
| `power(a, b, precision?)` | `a: number`, `b: number` | Computes $a^b$. |
|
|
185
|
+
| `modulo(a, b, precision?)` | `a: number`, `b: number` | Computes $a \pmod b$. |
|
|
186
|
+
| `percent(val, pct)` | `val: number`, `pct: number` | Computes `(val * pct) / 100`. |
|
|
187
|
+
| `addPercent(val, pct)` | `val: number`, `pct: number` | Computes `val + (val * pct) / 100`. |
|
|
188
|
+
| `subtractPercent(val, pct)` | `val: number`, `pct: number` | Computes `val - (val * pct) / 100`. |
|
|
189
|
+
| `multiplyPercent(val, pct)` | `val: number`, `pct: number` | Computes `(val * pct) / 100`. |
|
|
190
|
+
| `dividePercent(val, pct)` | `val: number`, `pct: number` | Computes `val / (pct / 100)`. |
|
|
191
|
+
| `tax(amt, rate?, isAdd?)` | `amt: number`, `rate?: number`, `isAdd?: boolean` | Computes tax addition or extraction. |
|
|
192
|
+
| `tip(amt, percent?)` | `amt: number`, `percent?: number` | Computes amount with percentage tip. |
|
|
193
|
+
| `discount(amt, percent?)` | `amt: number`, `percent?: number` | Computes amount after discount percentage. |
|
|
194
|
+
| `sin(val, angleMode?)` | `val: number`, `angleMode?: 'deg' \| 'rad'` | Sine function in degrees or radians. |
|
|
195
|
+
| `cos(val, angleMode?)` | `val: number`, `angleMode?: 'deg' \| 'rad'` | Cosine function in degrees or radians. |
|
|
196
|
+
| `tan(val, angleMode?)` | `val: number`, `angleMode?: 'deg' \| 'rad'` | Tangent function in degrees or radians. |
|
|
197
|
+
| `sqrt(val)` | `val: number` | Square root. |
|
|
198
|
+
| `factorial(n)` | `n: number` | Factorial ($n!$). |
|
|
199
|
+
|
|
200
|
+
### `Formatter`
|
|
201
|
+
|
|
202
|
+
| Method | Parameters | Description |
|
|
203
|
+
| ------------------------- | ---------------------------------------------------------------------------- | --------------------------------------------------------------- |
|
|
204
|
+
| `round(val, precision=8)` | `val: number`, `precision?: number` | Rounds float safely avoiding `0.30000000000000004` artifacts. |
|
|
205
|
+
| `format(num, options?)` | `num: number\|string`, `options?: { currency?: string, precision?: number }` | Groups thousands with commas and prepends currency if provided. |
|
|
206
|
+
| `parse(str)` | `str: string` | Strips formatting and extracts numeric float value. |
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## ๐ Development & Testing
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
# Run unit test suite
|
|
214
|
+
npm test
|
|
215
|
+
|
|
216
|
+
# Build production bundles
|
|
217
|
+
npm run build
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## ๐ License
|
|
223
|
+
|
|
224
|
+
MIT ยฉ [AdminUIUX](https://adminuiux.com)
|