Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 5dfec97

Browse files
Ibrahim SassiKent C. Dodds
authored andcommitted
feat(isNumeric): add isNumeric function (#112)
* feat(isNumeric): add isNumeric function * fixing indentation
1 parent 29d4e14 commit 5dfec97

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import round from './round'
3737
import checkPalindrome from './checkPalindrome'
3838
import isFunction from './is-function'
3939
import isOdd from './is-odd'
40+
import isNumeric from './is-numeric'
4041

4142
export {
4243
isOdd,
@@ -78,5 +79,6 @@ export {
7879
checkPalindrome,
7980
isFunction,
8081
subtraction,
82+
isNumeric,
8183
}
8284

src/is-numeric.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default isNumeric
2+
3+
/**
4+
* Original Source:
5+
* https://stackoverflow.com/questions/9716468/is-there-any-function-like-isnumeric-in-javascript-to-validate-numbers
6+
*
7+
* This method will check whether an expression can be evaluated as a number or not.
8+
*
9+
* @param {Object} n - number to check
10+
* @return {Boolean} - True if n is numeric, false if not
11+
*/
12+
function isNumeric(n) {
13+
return !isNaN(parseFloat(n)) && isFinite(n)
14+
}
15+

test/is-numeric.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Created by Ibrahim on 07/08/2017.
3+
*/
4+
import test from 'ava'
5+
import {isNumeric} from '../src'
6+
7+
test('checks if expression is number and returns true', t => {
8+
const n = "5";
9+
const expected = true;
10+
const actual = isNumeric(n);
11+
t.deepEqual(actual, expected)
12+
});
13+
14+
test('checks if expression is number and returns false', t => {
15+
const n = "hello";
16+
const expected = false;
17+
const actual = isNumeric(n);
18+
t.deepEqual(actual, expected)
19+
});

0 commit comments

Comments
 (0)