diff --git a/md-variables.json b/md-variables.json index 568053e9..27f74bad 100644 --- a/md-variables.json +++ b/md-variables.json @@ -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", diff --git a/packages/string-is-numeric/LICENSE b/packages/string-is-numeric/LICENSE new file mode 100644 index 00000000..30c9ed74 --- /dev/null +++ b/packages/string-is-numeric/LICENSE @@ -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. diff --git a/packages/string-is-numeric/README.md b/packages/string-is-numeric/README.md new file mode 100644 index 00000000..9752795a --- /dev/null +++ b/packages/string-is-numeric/README.md @@ -0,0 +1,29 @@ + + + +## 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 +``` diff --git a/packages/string-is-numeric/index.cjs b/packages/string-is-numeric/index.cjs new file mode 100644 index 00000000..b31de187 --- /dev/null +++ b/packages/string-is-numeric/index.cjs @@ -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) + ); +} diff --git a/packages/string-is-numeric/index.d.ts b/packages/string-is-numeric/index.d.ts new file mode 100644 index 00000000..7eeed3f4 --- /dev/null +++ b/packages/string-is-numeric/index.d.ts @@ -0,0 +1,2 @@ +declare function isNumeric(value: string | number): boolean; +export default isNumeric; diff --git a/packages/string-is-numeric/index.mjs b/packages/string-is-numeric/index.mjs new file mode 100644 index 00000000..25b78b96 --- /dev/null +++ b/packages/string-is-numeric/index.mjs @@ -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}; diff --git a/packages/string-is-numeric/index.tests.ts b/packages/string-is-numeric/index.tests.ts new file mode 100644 index 00000000..1b193c82 --- /dev/null +++ b/packages/string-is-numeric/index.tests.ts @@ -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); diff --git a/packages/string-is-numeric/package.json b/packages/string-is-numeric/package.json new file mode 100644 index 00000000..0ef410ec --- /dev/null +++ b/packages/string-is-numeric/package.json @@ -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" + } +} diff --git a/test/string-is-numeric/index.cjs b/test/string-is-numeric/index.cjs new file mode 100644 index 00000000..9b1d010a --- /dev/null +++ b/test/string-is-numeric/index.cjs @@ -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(); +});