Skip to content

Commit

Permalink
Add validation input (#1)
Browse files Browse the repository at this point in the history
Add validation input
  • Loading branch information
abranhe authored Jul 4, 2019
2 parents b666333 + bdc1972 commit 0c25ab1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ module.exports = (input, options) => {
...options
};

if (typeof input !== 'string' && typeof input !== 'number') {
throw new TypeError(`Expected an String/Number in the second argument, got ${typeof input}`);
}

if (isNaN(input)) {
throw new TypeError(`Expected an valid number, or a valid numerical string, got '${input}'`);
}

if (input < 1e3) {
return input;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "short-numbers",
"version": "0.0.0",
"version": "0.1.0",
"description": "Make short numbers from long numbers",
"license": "MIT",
"repository": "abranhe/short-numbers",
Expand Down
15 changes: 11 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import test from 'ava';
import shortNumbers from '.';

// eslint-disable-next-line no-warning-comments
// TODO Expected a number or a parsable number only
test('Invalid input', t => {
const error = t.throws(() => shortNumbers({}), TypeError);
t.is(error.message, 'Expected an String/Number in the second argument, got object');
});

test('Non numerical string', t => {
const error = t.throws(() => shortNumbers('abc'), TypeError);
t.is(error.message, 'Expected an valid number, or a valid numerical string, got \'abc\'');
});

test('Negative numbers', t => {
t.is(shortNumbers(-1), -1);
Expand Down Expand Up @@ -37,6 +44,6 @@ test('Options', t => {
t.is(shortNumbers('1000', {k: 'k'}), '1k');
t.is(shortNumbers('10300', {space: true}), '10.3 K');
t.is(shortNumbers('1000000', {m: 'million', space: true}), '1 million');
t.is(shortNumbers('1000000000', {b: 'Billion', space: true}), '1 Billion');
t.is(shortNumbers('1000000000000', {t: 'Trillion', space: true}), '1 Trillion');
t.is(shortNumbers('1000000000', {b: 'billion', space: true}), '1 billion');
t.is(shortNumbers('1000000000000', {t: 'trillion', space: true}), '1 trillion');
});

0 comments on commit 0c25ab1

Please sign in to comment.