Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the isNumeric module #578

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions md-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@
"intersect([1, 2, 2, 4, 5], [3, 2, 2, 5, 7]); // [2, 5] "
]
},
"just-is-numeric": {
"packageName": "just-is-numeric",
"dir": "string-is-numeric",
"description": "Checks whether a value is numeric",
"examples": [
"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"
]
},
"just-last": {
"packageName": "just-last",
"dir": "array-last",
Expand Down
21 changes: 21 additions & 0 deletions packages/string-is-numeric/LICENSE
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.
29 changes: 29 additions & 0 deletions packages/string-is-numeric/README.md
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
```
23 changes: 23 additions & 0 deletions packages/string-is-numeric/index.cjs
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)
);
}
2 changes: 2 additions & 0 deletions packages/string-is-numeric/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare function isNumeric(value: string | number): boolean;
export default isNumeric;
25 changes: 25 additions & 0 deletions packages/string-is-numeric/index.mjs
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};
19 changes: 19 additions & 0 deletions packages/string-is-numeric/index.tests.ts
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);
34 changes: 34 additions & 0 deletions packages/string-is-numeric/package.json
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"
}
}
19 changes: 19 additions & 0 deletions test/string-is-numeric/index.cjs
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();
});
Loading