-
Notifications
You must be signed in to change notification settings - Fork 74
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
80 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 throws error. | ||
* | ||
* @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); | ||
} | ||
|
||
throw new Error(`Invalid number: ${input}`); | ||
}; |
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,53 @@ | ||
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('throws an error when input is not a valid numeric string', t => { | ||
const error = t.throws(() => parseNumber('abc')); | ||
t.is(error.message, 'Invalid number: abc'); | ||
|
||
const error2 = t.throws(() => parseNumber('42abc')); | ||
t.is(error2.message, 'Invalid number: 42abc'); | ||
|
||
const error3 = t.throws(() => parseNumber('')); | ||
t.is(error3.message, 'Invalid number: '); | ||
|
||
const error4 = t.throws(() => parseNumber(' ')); | ||
t.is(error4.message, 'Invalid number: '); | ||
|
||
const error5 = t.throws(() => parseNumber('42..42')); | ||
t.is(error5.message, 'Invalid number: 42..42'); | ||
}); | ||
|
||
// Test for invalid input | ||
test('throws an error when input is not a valid number', t => { | ||
const error1 = t.throws(() => parseNumber(null)); | ||
t.is(error1.message, 'Invalid number: null'); | ||
|
||
const error2 = t.throws(() => parseNumber(undefined)); | ||
t.is(error2.message, 'Invalid number: undefined'); | ||
|
||
const error3 = t.throws(() => parseNumber({})); | ||
t.is(error3.message, 'Invalid number: [object Object]'); | ||
|
||
const error4 = t.throws(() => parseNumber([])); | ||
t.is(error4.message, 'Invalid number: '); | ||
}); |