Skip to content

Commit 323ae19

Browse files
committed
Allow separators in all numeric sequences
1 parent d9ed4b2 commit 323ae19

File tree

8 files changed

+796
-488
lines changed

8 files changed

+796
-488
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"editor.formatOnSave": true
2+
"editor.formatOnSave": true,
3+
"prettier.prettierPath": "./node_modules/prettier"
34
}

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Changed
1111

1212
- `numericQuantity` is now a named export; there is no default export.
13-
- Parsing logic now accepts/ignores any trailing non-numeric characters, to more closely resemble the behavior of `parseFloat`.
13+
- Parsing logic now accepts/ignores any trailing non-numeric characters, more closely resembling the behavior of `parseFloat`.
1414
- UMD build assigns all exports, including `numericQuantity`, to the global object `NumericQuantity`. Previously, it assigned the main function to the global namespace as `numericQuantity`.
1515

1616
### Added
1717

18-
- Support for comma (`','`) and underscore (`'_'`) separators within the whole number or numerator segments.
19-
- Support for Roman numerals (modern/strict rules), including Unicode code points `U+2160` through `U+217F`.
20-
- Support for Unicode "Fraction Numerator One" code point (`'⅟'`, `U+215F`), which must be followed by a string of pure digit characters (the denominator) to be valid.
18+
- Support for comma (`','`) and underscore (`'_'`) separators within Arabic numeral sequences. If a numeric sequence has a leading or trailing separator, that sequence and everything after it will be ignored.
19+
- Support for Roman numerals with modern, strict rules, including the Unicode code points `U+2160` through `U+217F`.
20+
- Support for Unicode "Fraction Numerator One" code point (`'⅟'`, `U+215F`), which must be followed by a valid numeric sequence (the denominator) to be included in the conversion.
2121
- Named exports of regular expressions, character maps, types, and other internal tools.
2222
- Build with ([tsup](https://tsup.egoist.dev/)).
2323

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@
4242
"pretty-print": "prettier --write *.{mjs,js,ts,json} src/*.*"
4343
},
4444
"devDependencies": {
45-
"@babel/core": "^7.22.1",
46-
"@babel/preset-env": "^7.22.4",
47-
"@babel/preset-typescript": "^7.21.5",
45+
"@babel/core": "^7.22.5",
46+
"@babel/preset-env": "^7.22.5",
47+
"@babel/preset-typescript": "^7.22.5",
4848
"@types/jest": "^29.5.2",
49-
"@types/node": "^20.2.5",
49+
"@types/node": "^20.3.0",
5050
"gh-pages": "^5.0.0",
5151
"jest": "^29.5.0",
52-
"np": "^8.0.2",
52+
"np": "^8.0.3",
5353
"prettier": "^2.8.8",
5454
"prettier-plugin-organize-imports": "^3.2.2",
5555
"tsup": "^6.7.0",

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const vulgarFractionToAsciiMap: Record<VulgarFraction, string> = {
6363
* numericRegex.exec("2 / 3") // [ "2 / 3", "2", "/ 3", null ]
6464
*/
6565
export const numericRegex =
66-
/^(?=-?\s*\.\d|-?\s*\d+)(-)?\s*((?:\d+[\d,_]*)*)(\.\d+|(\s+\d*\s*)?\s*\/\s*\d+)?(?:\s*[^\.\d\/].*)?/;
66+
/^(?=-?\s*\.\d|-?\s*\d)(-)?\s*((?:\d(?:[\d,_]*\d)?)*)(\.\d(?:[\d,_]*\d)?|(\s+\d(?:[\d,_]*\d)?\s*)?\s*\/\s*\d(?:[\d,_]*\d)?)?(?:\s*[^\.\d\/].*)?/;
6767

6868
/**
6969
* Captures any Unicode vulgar fractions

src/index.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { numericQuantityTests } from './numericQuantityTests';
44
for (const [title, tests] of Object.entries(numericQuantityTests)) {
55
it(title, () => {
66
for (const [arg, expected] of tests) {
7+
const expectation = expect(numericQuantity(arg));
78
if (isNaN(expected)) {
8-
expect(numericQuantity(arg)).toBeNaN();
9+
expectation.toBeNaN();
910
} else {
10-
expect(numericQuantity(arg)).toBe(expected);
11+
expectation.toBe(expected);
1112
}
1213
}
1314
});

src/numericQuantity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ export const numericQuantity = (quantity: string | number) => {
4444
return parseRomanNumerals(quantityAsString);
4545
}
4646

47-
const [, dash, ng1temp, numberGroup2] = regexResult;
47+
const [, dash, ng1temp, ng2temp] = regexResult;
4848
const numberGroup1 = ng1temp.replace(/[,_]/g, '');
49+
const numberGroup2 = ng2temp?.replace(/[,_]/g, '');
4950

5051
// Numerify capture group 1
5152
if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith('.')) {

src/numericQuantityTests.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,30 @@ export const numericQuantityTests: Record<string, [string | number, number][]> =
4949
['-1_000', -1000],
5050
['1_1 1/2', 11.5],
5151
['1,1 1/2', 11.5],
52-
// TODO: Should separators in the numerator be allowed?
5352
['1_1/22', 0.5],
5453
['1,1/22', 0.5],
54+
['11 1_1/2_2', 11.5],
55+
['11 1,1/2,2', 11.5],
56+
['1.2,3', 1.23],
57+
['1.2_3', 1.23],
58+
['1/2,3', 0.043],
59+
['1/2_3', 0.043],
60+
['1 2/3,4', 1.059],
61+
['1 2/3_4', 1.059],
62+
],
63+
'Invalid/ignored separators': [
64+
['_11 11/22', NaN],
65+
[',11 11/22', NaN],
66+
['11 _11/22', 11],
67+
['11 ,11/22', 11],
68+
['11 11/_22', 11],
69+
['11 11/,22', 11],
70+
['11_ 11/22', 11],
71+
['11, 11/22', 11],
72+
['11 11_/22', 11],
73+
['11 11,/22', 11],
74+
['11 11/22_', 11.5],
75+
['11 11/22,', 11.5],
5576
],
5677
'Trailing invalid characters': [
5778
['1 2 3', 1],
@@ -64,14 +85,6 @@ export const numericQuantityTests: Record<string, [string | number, number][]> =
6485
['\u215F', 1],
6586
['0 . 1', 0],
6687
['0.1.2', 0.1],
67-
['1.2,3', 1.2],
68-
['1.2_3', 1.2],
69-
['1/2,3', 0.5],
70-
['1/2_3', 0.5],
71-
['1 2/3,4', 1.667],
72-
['1 2/3_4', 1.667],
73-
['11 2_3/45', 11],
74-
['11 2,3/45', 11],
7588
],
7689
Decimals: [
7790
['.9', 0.9],

0 commit comments

Comments
 (0)