-
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
92 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,15 @@ | ||
/** | ||
* Parses the input and returns it as a number if it's valid, otherwise throws error. | ||
* | ||
* @param {string} input | ||
* @returns {number} | ||
*/ | ||
export const parseNumber = (input = '') => { | ||
const result = /[0-9]/.test(input) ? Number(input) : NaN; | ||
|
||
if (Number.isNaN(result)) { | ||
throw new Error(`Invalid number: ${input}`); | ||
} | ||
|
||
return result; | ||
}; |
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,67 @@ | ||
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 (includes binary, octal and hexadecimal) | ||
test('returns the number when input is a valid numeric string', t => { | ||
t.is(parseNumber('42'), 42); | ||
t.is(parseNumber('-40.0'), -40.0); | ||
t.is(parseNumber('.5'), 0.5); | ||
t.is(parseNumber('1.337e3'), 1337); | ||
t.is(parseNumber(' +1 '), 1); | ||
t.is(parseNumber('0B111'), 7); | ||
t.is(parseNumber('0o040'), 32); | ||
t.is(parseNumber('0xf00'), 3840); | ||
t.is(parseNumber(' -40.0 '), -40.0); | ||
// Test for binary, octal and hexadecimal | ||
t.is(parseNumber('0B111'), 7); | ||
t.is(parseNumber('0o040'), 32); | ||
t.is(parseNumber('0xf00'), 3840); | ||
}); | ||
|
||
// Test for invalid string input | ||
test('throws an error when input is not a valid numeric string', t => { | ||
const error = t.throws(() => parseNumber('f00')); | ||
t.is(error.message, 'Invalid number: f00'); | ||
|
||
const error2 = t.throws(() => parseNumber('NaN')); | ||
t.is(error2.message, 'Invalid number: NaN'); | ||
|
||
const error3 = t.throws(() => parseNumber('Infinity')); | ||
t.is(error3.message, 'Invalid number: Infinity'); | ||
|
||
const error4 = t.throws(() => parseNumber('7up')); | ||
t.is(error4.message, 'Invalid number: 7up'); | ||
|
||
const error5 = t.throws(() => parseNumber(' ')); // Unicode character | ||
t.is(error5.message, 'Invalid number: '); | ||
|
||
const error6 = t.throws(() => parseNumber('')); | ||
t.is(error6.message, 'Invalid number: '); | ||
|
||
const error7 = t.throws(() => parseNumber(' ')); | ||
t.is(error7.message, 'Invalid number: '); | ||
}); | ||
|
||
// 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: '); | ||
|
||
const error3 = t.throws(() => parseNumber({})); | ||
t.is(error3.message, 'Invalid number: [object Object]'); | ||
|
||
const error4 = t.throws(() => parseNumber([])); | ||
t.is(error4.message, 'Invalid number: '); | ||
}); |