-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
69 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Parses the input and returns it as a number if it's valid, otherwise returns undefined. | ||
* | ||
* @param {string | number} input | ||
* @returns {number | undefined} | ||
*/ | ||
export const parseNumber = input => { | ||
if (typeof input === 'number' && !Number.isNaN(input)) { | ||
return input; | ||
} | ||
|
||
if (typeof input === 'string' && /^-?\d+(\.\d+)?$/.test(input.trim())) { | ||
return Number(input); | ||
} | ||
|
||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import test from 'ava'; | ||
import { parseNumber } from '../src/number-parse.js'; | ||
|
||
// Test for valid number input | ||
test('returns the number itself if input is a valid number', t => { | ||
t.is(parseNumber(42), 42); | ||
t.is(parseNumber(-42), -42); | ||
t.is(parseNumber(0), 0); | ||
t.is(parseNumber(3.14), 3.14); | ||
t.is(parseNumber(-3.14), -3.14); | ||
}); | ||
|
||
// Test for valid string input | ||
test('returns the number when input is a valid numeric string', t => { | ||
t.is(parseNumber('42'), 42); | ||
t.is(parseNumber('-42'), -42); | ||
t.is(parseNumber('3.14'), 3.14); | ||
t.is(parseNumber('-3.14'), -3.14); | ||
t.is(parseNumber(' 42 '), 42); | ||
}); | ||
|
||
// Test for invalid string input | ||
test('returns undefined for invalid numeric strings', t => { | ||
t.is(parseNumber('abc'), undefined); | ||
t.is(parseNumber('42abc'), undefined); | ||
t.is(parseNumber(''), undefined); | ||
t.is(parseNumber(' '), undefined); | ||
t.is(parseNumber('42..42'), undefined); | ||
}); | ||
|
||
// Test for NaN input | ||
test('returns undefined for NaN', t => { | ||
t.is(parseNumber(NaN), undefined); | ||
}); | ||
|
||
// Test for non-string and non-number input | ||
test('returns undefined for non-string, non-number input', t => { | ||
t.is(parseNumber(null), undefined); | ||
t.is(parseNumber(undefined), undefined); | ||
t.is(parseNumber({}), undefined); | ||
t.is(parseNumber([]), undefined); | ||
}); |