Skip to content

Commit ac50a34

Browse files
committed
handle string with leading decimal separator
1 parent 68be325 commit ac50a34

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const numbers = '0123456789';
12
/**
23
* Retrieves the computer's locale decimal separator.
34
* @returns Either "." or ",".
@@ -43,7 +44,10 @@ export function parseNumeric(numericString, userParsingOptions) {
4344
* Remove any leading units
4445
*/
4546
while (processingString !== '') {
46-
if ('0123456789'.includes(processingString.charAt(0))) {
47+
if (numbers.includes(processingString.charAt(0)) ||
48+
(processingString.charAt(0) === '.' &&
49+
processingString.length > 1 &&
50+
numbers.includes(processingString.charAt(1)))) {
4751
break;
4852
}
4953
else {

index.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
type DecimalSeparator = '.' | ','
22

3+
const numbers = '0123456789'
4+
35
export interface ParsingOptions {
46
decimalSeparator: DecimalSeparator
57
}
@@ -16,8 +18,14 @@ export const defaultParsingOptions: ParsingOptions = {
1618
decimalSeparator: getLocaleDecimalSeparator()
1719
}
1820

19-
export function parseNumeric(numericString: undefined | null, userParsingOptions?: Partial<ParsingOptions>): undefined
20-
export function parseNumeric(numericString: string, userParsingOptions?: Partial<ParsingOptions>): number
21+
export function parseNumeric(
22+
numericString: undefined | null,
23+
userParsingOptions?: Partial<ParsingOptions>
24+
): undefined
25+
export function parseNumeric(
26+
numericString: string,
27+
userParsingOptions?: Partial<ParsingOptions>
28+
): number
2129

2230
/**
2331
* Converts a string into a number.
@@ -68,7 +76,12 @@ export function parseNumeric(
6876
*/
6977

7078
while (processingString !== '') {
71-
if ('0123456789'.includes(processingString.charAt(0))) {
79+
if (
80+
numbers.includes(processingString.charAt(0)) ||
81+
(processingString.charAt(0) === '.' &&
82+
processingString.length > 1 &&
83+
numbers.includes(processingString.charAt(1)))
84+
) {
7285
break
7386
} else {
7487
if (processingString.startsWith('-')) {
@@ -82,5 +95,4 @@ export function parseNumeric(
8295
return Number.parseFloat(processingString) * finalMultiplier
8396
}
8497

85-
86-
export default parseNumeric
98+
export default parseNumeric

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cityssm/string-to-numeric",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"type": "module",
55
"description": "Parses formatted numeric strings into numbers. Handles cases parseFloat() misses.",
66
"exports": "./index.js",

test/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ await describe('string-to-numeric', async () => {
1212
decimalSeparator: ','
1313
}), 1234.1);
1414
});
15+
await it('Converts strings with leading decimal into numeric values', async () => {
16+
assert.strictEqual(stringToNumeric('.2'), 0.2);
17+
assert.strictEqual(stringToNumeric(',3', { decimalSeparator: ',' }), 0.3);
18+
});
1519
await it('Converts strings with leading units into numeric values', async () => {
1620
assert.strictEqual(stringToNumeric('$2'), 2);
1721
assert.strictEqual(stringToNumeric('# 456'), 456);

test/test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ await describe('string-to-numeric', async () => {
2020
)
2121
})
2222

23+
await it('Converts strings with leading decimal into numeric values', async () => {
24+
assert.strictEqual(stringToNumeric('.2'), 0.2)
25+
26+
assert.strictEqual(stringToNumeric(',3', {decimalSeparator: ','}), 0.3)
27+
})
28+
2329
await it('Converts strings with leading units into numeric values', async () => {
2430
assert.strictEqual(stringToNumeric('$2'), 2)
2531

0 commit comments

Comments
 (0)