Skip to content

Commit 56b5f83

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 4eb3a4d + 27ddb5c commit 56b5f83

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ The tag follows this format: `[operation][direction][start:end][currency]`
2828
* Use a colon-separated format (e.g., `1:3` for the first three cells). The indices are 1-based.
2929
* You can also just specify a start (e.g., `2`).
3030
* **`[currency]`** (Optional): A 2-4 letter currency code (e.g., `USD`, `EUR`, `GBP`). If provided, the result will be formatted with the specified currency symbol.
31+
* **`[format]`** (Optional): Format options for input and output.
32+
* #e[n] Only accepts inputs written in scientific notations, and outputs results in scientific notation with n fraction digits.
3133

3234
## Examples
3335

@@ -63,6 +65,20 @@ The tag follows this format: `[operation][direction][start:end][currency]`
6365
| **TOTAL:** | | | SUM^2:4USD |
6466
```
6567

68+
**Scientific notation example:**
69+
70+
Note: when using #e[n] format the input values must be in scientific notation and cannot include currency units.
71+
```
72+
| Correct | Incorrect |
73+
| ------: | --------: |
74+
| 1000 | 1,000 |
75+
| -1.0 | -1,0 |
76+
| 2.0e1 | $20 |
77+
| 0.1 | 0,1 |
78+
| 1E-1 | 10 % |
79+
| SUM^#e4 | SUM^#e4 |
80+
```
81+
The correct column will ouput 1.0192e+3 while the incorrect column will output 0.0000e+0.
6682
## Key Features
6783

6884
* **Real-time Updates:** Calculations are performed automatically as you type and edit your tables.

src/main.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,25 @@ export default class SimpleTableMath extends Plugin {
124124
...(this.settings.styleLastRow ? [] : ['off']),
125125
];
126126

127+
const defaultNumRegex = /-?\d+(?:[.,'`\u202f]\d{3})*(?:[.,]\d+)?/;
128+
const exponentialRegex = /^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/;
129+
127130
tables.forEach((table) => {
128131
const rows = Array.from(table.querySelectorAll('tr'));
129132
rows.forEach((row, rowIndex) => {
130133
const cells = Array.from(row.children) as HTMLTableCellElement[];
131134
cells.forEach((cell, colIndex) => {
132135
const rawText = this.extractCellContent(cell).trim().toLowerCase() || '';
133-
const match = rawText.match(/^([a-z]{3})([<^])(?:(\d+)(?::(\d+))?)?([a-z]{2,4})?$/i);
136+
const match = rawText.match(/^([a-z]{3})([<^])(?:(\d+)(?::(\d+))?)?([a-z]{2,4})?(?:\#e(\d+))?$/i);
134137
const isActiveElement = this.isDocumentActiveElementChildOf(cell)
135138
if (match && !isActiveElement) {
136139
const operation = match[1].toLowerCase();
137140
const direction = match[2];
138141
const startStr = match[3];
139142
const endStr = match[4];
140143
const currency = match[5]?.toUpperCase() || null;
144+
const exponential = match[6] ? parseInt(match[6], 10) : 0;
145+
const numRegex = exponential ? exponentialRegex : defaultNumRegex;
141146
const values: number[] = [];
142147

143148
let startIndex = startStr ? parseInt(startStr, 10) - 1 : 0;
@@ -158,7 +163,7 @@ export default class SimpleTableMath extends Plugin {
158163
for (let r = actualStartRow; r <= finalEndRow; r++) {
159164
const aboveCell = rows[r]?.children?.[colIndex] as HTMLTableCellElement | undefined | null;
160165
const textContent = this.extractCellContent(aboveCell, true);
161-
const value = this.extractNumber(textContent);
166+
const value = this.extractNumber(textContent, numRegex);
162167
if (value !== null) {
163168
values.push(value);
164169
}
@@ -172,7 +177,7 @@ export default class SimpleTableMath extends Plugin {
172177
for (let c = actualStartCol; c <= finalEndCol; c++) {
173178
const leftCell = cells[c] as HTMLTableCellElement | undefined | null;
174179
const textContent = this.extractCellContent(leftCell, true);
175-
const value = this.extractNumber(textContent);
180+
const value = this.extractNumber(textContent, numRegex);
176181
if (value !== null) {
177182
values.push(value);
178183
}
@@ -219,12 +224,16 @@ export default class SimpleTableMath extends Plugin {
219224
cell.tabIndex = -1;
220225
cell.closest('tr')?.classList.add(...rowClasses);
221226
const defaultLocale = getLanguage();
222-
vElement.textContent = result.toLocaleString(this.settings.locale || defaultLocale, {
223-
style: currency ? 'currency' : 'decimal',
224-
currency: currency || undefined,
225-
minimumFractionDigits: this.settings.fractions,
226-
maximumFractionDigits: this.settings.fractions,
227-
});
227+
if (exponential) {
228+
vElement.textContent = result.toExponential(exponential)
229+
} else {
230+
vElement.textContent = result.toLocaleString(this.settings.locale || defaultLocale, {
231+
style: currency ? 'currency' : 'decimal',
232+
currency: currency || undefined,
233+
minimumFractionDigits: this.settings.fractions,
234+
maximumFractionDigits: this.settings.fractions,
235+
})
236+
};
228237
}
229238
}
230239
} else if (!isActiveElement && cell.classList.contains('stm-cell')) {
@@ -333,12 +342,11 @@ export default class SimpleTableMath extends Plugin {
333342
return wrapper?.textContent || '';
334343
}
335344

336-
extractNumber(str: string | null): number | null {
345+
extractNumber(str: string | null, numRegex: RegExp): number | null{
337346
if (!str) {
338347
return null;
339348
}
340-
341-
const match = str.match(/-?\d+(?:[.,'`\u202f]\d{3})*(?:[.,]\d+)?/);
349+
const match = str.match(numRegex);
342350
if (!match) {
343351
return null;
344352
}

0 commit comments

Comments
 (0)