-
Notifications
You must be signed in to change notification settings - Fork 208
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
9 changed files
with
187 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2024 Evandro Goncalves | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,29 @@ | ||
<!-- DO NOT EDIT THIS FILE! THIS FILE WAS AUTOGENERATED BY TEMPLATE-MATE --> | ||
<!-- SEE https://github.com/angus-c/just/blob/master/CONTRIBUTING.md#readme-template --> | ||
|
||
## just-is-numeric | ||
|
||
Part of a [library](https://anguscroll.com/just) of zero-dependency npm modules that do just do one thing. | ||
Guilt-free utilities for every occasion. | ||
|
||
[`🍦 Try it`](https://anguscroll.com/just/just-is-numeric) | ||
|
||
```shell | ||
npm install just-is-numeric | ||
``` | ||
```shell | ||
yarn add just-is-numeric | ||
``` | ||
|
||
Checks whether a value is numeric | ||
|
||
```js | ||
import isNumeric from 'just-is-numeric'; | ||
|
||
isNumeric(3); // true | ||
isNumeric('3'); // true | ||
isNumeric(3.4); // true | ||
isNumeric('3.4'); // true | ||
isNumeric('3.4.5'); // false | ||
isNumeric('3a'); // false | ||
``` |
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,23 @@ | ||
module.exports = isNumeric; | ||
|
||
/* | ||
isNumeric(""); // false | ||
isNumeric("123"); // true | ||
isNumeric("123.123"); // true | ||
isNumeric("123.123.123"); // false | ||
isNumeric("abc"); // false | ||
isNumeric("1e8"); // true | ||
isNumeric("1e-8"); // true | ||
isNumeric("1e-8.1"); // false | ||
isNumeric("1aa"); // false | ||
*/ | ||
|
||
function isNumeric(str) { | ||
var type = typeof str; | ||
|
||
return ( | ||
(type === 'string' || type === 'number') && | ||
!isNaN(parseFloat(str)) && | ||
isFinite(str) | ||
); | ||
} |
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,2 @@ | ||
declare function isNumeric(value: string | number): boolean; | ||
export default isNumeric; |
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,25 @@ | ||
var stringIsNumeric = isNumeric; | ||
|
||
/* | ||
isNumeric(""); // false | ||
isNumeric("123"); // true | ||
isNumeric("123.123"); // true | ||
isNumeric("123.123.123"); // false | ||
isNumeric("abc"); // false | ||
isNumeric("1e8"); // true | ||
isNumeric("1e-8"); // true | ||
isNumeric("1e-8.1"); // false | ||
isNumeric("1aa"); // false | ||
*/ | ||
|
||
function isNumeric(str) { | ||
var type = typeof str; | ||
|
||
return ( | ||
(type === 'string' || type === 'number') && | ||
!isNaN(parseFloat(str)) && | ||
isFinite(str) | ||
); | ||
} | ||
|
||
export {stringIsNumeric as isNumeric}; |
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,19 @@ | ||
import isNumeric from './index'; | ||
|
||
// OK | ||
isNumeric('1'); | ||
isNumeric(1); | ||
isNumeric('1.1'); | ||
isNumeric(1.1); | ||
|
||
// Not OK | ||
// @ts-expect-error | ||
isNumeric([]); | ||
// @ts-expect-error | ||
isNumeric({}); | ||
// @ts-expect-error | ||
isNumeric(); | ||
// @ts-expect-error | ||
isNumeric(false); | ||
// @ts-expect-error | ||
isNumeric(null); |
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,34 @@ | ||
{ | ||
"name": "is-numeric", | ||
"version": "1.0.0", | ||
"description": "Checks whether a value is numeric", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"types": "./index.d.ts", | ||
"require": "./index.cjs", | ||
"import": "./index.mjs" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "index.cjs", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "rollup -c" | ||
}, | ||
"repository": "https://github.com/angus-c/just", | ||
"keywords": [ | ||
"number", | ||
"string", | ||
"float", | ||
"integer", | ||
"no-dependencies", | ||
"just" | ||
], | ||
"author": "evandrolg", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/angus-c/just/issues" | ||
} | ||
} |
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,19 @@ | ||
var test = require('../util/test.js')(__filename); | ||
var isNumeric = require('../../packages/string-is-numeric'); | ||
|
||
test('detects invalid cases', function(t) { | ||
t.false(isNumeric('')); | ||
t.false(isNumeric('123.123.123')); | ||
t.false(isNumeric('abc')); | ||
t.false(isNumeric('1e-8.1')); | ||
t.false(isNumeric('1aa')); | ||
t.end(); | ||
}); | ||
|
||
test('detects valid cases', function(t) { | ||
t.true(isNumeric('123')); | ||
t.true(isNumeric('123.123')); | ||
t.true(isNumeric('1e8')); | ||
t.true(isNumeric('1e-8')); | ||
t.end(); | ||
}); |