@alwatr/is-number 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # 1.0.0 (2024-01-16)
7
+
8
+ ### Bug Fixes
9
+
10
+ * **is-number:** Remove Number.isFinite polyfill ([aa149f3](https://github.com/Alwatr/nanolib/commit/aa149f302f96d961b058fc3a9d70399c1023cbe3)) by @AliMD
11
+
12
+ ### Features
13
+
14
+ * **is-number:** extract from @alwatr/util ([1c8a676](https://github.com/Alwatr/nanolib/commit/1c8a676ccefcad12436f41b96eeb39c60cc09040)) by @njfamirm
15
+ * **is-number:** Update is-number package description and add Number.isFinite polyfill ([a7c8e38](https://github.com/Alwatr/nanolib/commit/a7c8e38eb3e939199cf5637feaf08ac0ed98e2e6)) by @AliMD
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 S. Ali Mihandoost
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # is-number
2
+
3
+ A simple utility to Check the value is number or can convert to a number, for example string ' 123 ' can be converted to 123.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ yarn add @alwatr/is-number
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import {isNumber} from '@alwatr/is-number';
15
+
16
+ isNumber('1'); // true
17
+ ```
18
+
19
+ ## Why is this needed?
20
+
21
+ ```ts
22
+ console.log(typeof '123'); //=> 'string'
23
+ console.log(+[]); //=> 0
24
+ console.log(+''); //=> 0
25
+ console.log(+' '); //=> 0
26
+ console.log(typeof NaN); //=> 'number'
27
+ console.log(typeof Infinity); //=> 'number'
28
+ ```
29
+
30
+ ### True
31
+
32
+ <!-- prettier-ignore -->
33
+ ```ts
34
+ isNumber(5e3); // true
35
+ isNumber(0xff); // true
36
+ isNumber(-1.1); // true
37
+ isNumber(0); // true
38
+ isNumber(1); // true
39
+ isNumber(1.1); // true
40
+ isNumber('-1.1'); // true
41
+ isNumber('0'); // true
42
+ isNumber('0xff'); // true
43
+ isNumber('1'); // true
44
+ isNumber('1.1'); // true
45
+ isNumber('5e3'); // true
46
+ isNumber('012'); // true
47
+ isNumber(parseInt('012')); // true
48
+ isNumber(parseFloat('012')); // true
49
+ ```
50
+
51
+ ### False
52
+
53
+ <!-- prettier-ignore -->
54
+ ```ts
55
+ isNumber(Infinity); // false
56
+ isNumber(NaN); // false
57
+ isNumber(null); // false
58
+ isNumber(undefined); // false
59
+ isNumber(''); // false
60
+ isNumber(' '); // false
61
+ isNumber('foo'); // false
62
+ isNumber([1]); // false
63
+ isNumber([]); // false
64
+ isNumber(function () {}); // false
65
+ isNumber({}); // false
66
+ ```
package/dist/main.cjs ADDED
@@ -0,0 +1,3 @@
1
+ /* @alwatr/is-number v1.0.0 */
2
+ "use strict";var e=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var s=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var m=(i,n)=>{for(var t in n)e(i,t,{get:n[t],enumerable:!0})},p=(i,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of s(n))!b.call(i,r)&&r!==t&&e(i,r,{get:()=>n[r],enumerable:!(o=f(n,r))||o.enumerable});return i};var u=i=>p(e({},"__esModule",{value:!0}),i);var N={};m(N,{isNumber:()=>F});module.exports=u(N);function F(i){return typeof i=="number"?i-i===0:typeof i=="string"&&i.trim()!==""?Number.isFinite?Number.isFinite(+i):isFinite(+i):!1}0&&(module.exports={isNumber});
3
+ //# sourceMappingURL=main.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/main.ts"],
4
+ "sourcesContent": ["/**\n * Check the value is number or can convert to a number, for example string ' 123 ' can be converted to 123\n *\n * @param value - the value must check numeric.\n *\n * @return true if the value is number or can convert to a number, otherwise false.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * isNumber('123'); // true\n * isNumber(' 123 '); // true\n * isNumber(''); // false\n * isNumber(' '); // false\n * isNumber(' 123a '); // false\n * isNumber(' 123 a '); // false\n * ```\n */\nexport function isNumber(value: unknown): boolean {\n if (typeof value === 'number') {\n return value - value === 0;\n }\n if (typeof value === 'string' && value.trim() !== '') {\n return Number.isFinite ? Number.isFinite(+value) : isFinite(+value);\n }\n return false;\n}\n"],
5
+ "mappings": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,IAAA,eAAAC,EAAAH,GAkBO,SAASE,EAASE,EAAyB,CAChD,OAAI,OAAOA,GAAU,SACZA,EAAQA,IAAU,EAEvB,OAAOA,GAAU,UAAYA,EAAM,KAAK,IAAM,GACzC,OAAO,SAAW,OAAO,SAAS,CAACA,CAAK,EAAI,SAAS,CAACA,CAAK,EAE7D,EACT",
6
+ "names": ["main_exports", "__export", "isNumber", "__toCommonJS", "value"]
7
+ }
package/dist/main.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Check the value is number or can convert to a number, for example string ' 123 ' can be converted to 123
3
+ *
4
+ * @param value - the value must check numeric.
5
+ *
6
+ * @return true if the value is number or can convert to a number, otherwise false.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * isNumber(123); // true
11
+ * isNumber('123'); // true
12
+ * isNumber(' 123 '); // true
13
+ * isNumber(''); // false
14
+ * isNumber(' '); // false
15
+ * isNumber(' 123a '); // false
16
+ * isNumber(' 123 a '); // false
17
+ * ```
18
+ */
19
+ export declare function isNumber(value: unknown): boolean;
20
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAQhD"}
package/dist/main.mjs ADDED
@@ -0,0 +1,3 @@
1
+ /* @alwatr/is-number v1.0.0 */
2
+ function n(i){return typeof i=="number"?i-i===0:typeof i=="string"&&i.trim()!==""?Number.isFinite?Number.isFinite(+i):isFinite(+i):!1}export{n as isNumber};
3
+ //# sourceMappingURL=main.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/main.ts"],
4
+ "sourcesContent": ["/**\n * Check the value is number or can convert to a number, for example string ' 123 ' can be converted to 123\n *\n * @param value - the value must check numeric.\n *\n * @return true if the value is number or can convert to a number, otherwise false.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * isNumber('123'); // true\n * isNumber(' 123 '); // true\n * isNumber(''); // false\n * isNumber(' '); // false\n * isNumber(' 123a '); // false\n * isNumber(' 123 a '); // false\n * ```\n */\nexport function isNumber(value: unknown): boolean {\n if (typeof value === 'number') {\n return value - value === 0;\n }\n if (typeof value === 'string' && value.trim() !== '') {\n return Number.isFinite ? Number.isFinite(+value) : isFinite(+value);\n }\n return false;\n}\n"],
5
+ "mappings": ";AAkBO,SAASA,EAASC,EAAyB,CAChD,OAAI,OAAOA,GAAU,SACZA,EAAQA,IAAU,EAEvB,OAAOA,GAAU,UAAYA,EAAM,KAAK,IAAM,GACzC,OAAO,SAAW,OAAO,SAAS,CAACA,CAAK,EAAI,SAAS,CAACA,CAAK,EAE7D,EACT",
6
+ "names": ["isNumber", "value"]
7
+ }
package/package.json ADDED
@@ -0,0 +1,96 @@
1
+ {
2
+ "name": "@alwatr/is-number",
3
+ "version": "1.0.0",
4
+ "description": "A simple utility to Check the value is number or can convert to a number, for example string ' 123 ' can be converted to 123.",
5
+ "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
6
+ "keywords": [
7
+ "is-number",
8
+ "isnumber",
9
+ "isnan",
10
+ "finite",
11
+ "isfinite",
12
+ "is-nan",
13
+ "is-num",
14
+ "integer",
15
+ "is",
16
+ "kind",
17
+ "math",
18
+ "nan",
19
+ "num",
20
+ "number",
21
+ "numeric",
22
+ "parseFloat",
23
+ "parseInt",
24
+ "type",
25
+ "typeof",
26
+ "mathematics",
27
+ "cross-platform",
28
+ "ECMAScript",
29
+ "typescript",
30
+ "javascript",
31
+ "node",
32
+ "nodejs",
33
+ "browser",
34
+ "esm",
35
+ "module",
36
+ "utility",
37
+ "util",
38
+ "utils",
39
+ "nanolib",
40
+ "alwatr"
41
+ ],
42
+ "type": "module",
43
+ "main": "./dist/main.cjs",
44
+ "module": "./dist/main.mjs",
45
+ "types": "./dist/main.d.ts",
46
+ "exports": {
47
+ ".": {
48
+ "import": "./dist/main.mjs",
49
+ "require": "./dist/main.cjs",
50
+ "types": "./dist/main.d.ts"
51
+ }
52
+ },
53
+ "license": "MIT",
54
+ "files": [
55
+ "**/*.{js,mjs,cjs,map,d.ts,html,md}",
56
+ "!demo/**/*"
57
+ ],
58
+ "publishConfig": {
59
+ "access": "public"
60
+ },
61
+ "repository": {
62
+ "type": "git",
63
+ "url": "https://github.com/Alwatr/nanolib",
64
+ "directory": "packages/is-number"
65
+ },
66
+ "homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/is-number#readme",
67
+ "bugs": {
68
+ "url": "https://github.com/Alwatr/nanolib/issues"
69
+ },
70
+ "prettier": "@alwatr/prettier-config",
71
+ "scripts": {
72
+ "b": "yarn run build",
73
+ "t": "yarn run test",
74
+ "w": "yarn run watch",
75
+ "c": "yarn run clean",
76
+ "cb": "yarn run clean && yarn run build",
77
+ "d": "yarn run build:es && yarn node --enable-source-maps --trace-warnings",
78
+ "build": "yarn run build:ts & yarn run build:es",
79
+ "build:es": "nano-build --preset=module",
80
+ "build:ts": "tsc --build",
81
+ "test": "NODE_OPTIONS=\"$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\" jest",
82
+ "watch": "yarn run watch:ts & yarn run watch:es",
83
+ "watch:es": "yarn run build:es --watch",
84
+ "watch:ts": "yarn run build:ts --watch --preserveWatchOutput",
85
+ "clean": "rm -rfv dist *.tsbuildinfo"
86
+ },
87
+ "devDependencies": {
88
+ "@alwatr/nano-build": "^1.3.1",
89
+ "@alwatr/prettier-config": "^1.0.4",
90
+ "@alwatr/tsconfig-base": "^1.1.1",
91
+ "@types/node": "^20.11.3",
92
+ "jest": "^29.7.0",
93
+ "typescript": "^5.3.3"
94
+ },
95
+ "gitHead": "369887cfdcbf5c6f3899880a5e217b102b36706c"
96
+ }
@@ -0,0 +1,45 @@
1
+ import { isNumber } from '@alwatr/is-number';
2
+
3
+ describe('isNumber', () => {
4
+ it('should return true for numbers', () => {
5
+ expect(isNumber(123)).toBe(true);
6
+ expect(isNumber(0)).toBe(true);
7
+ expect(isNumber(-123)).toBe(true);
8
+ expect(isNumber(1.23)).toBe(true);
9
+ });
10
+
11
+ it('should return true for numeric strings', () => {
12
+ expect(isNumber('123')).toBe(true);
13
+ expect(isNumber('0')).toBe(true);
14
+ expect(isNumber('-123')).toBe(true);
15
+ expect(isNumber('1.23')).toBe(true);
16
+ });
17
+
18
+ it('should return false for non-numeric strings', () => {
19
+ expect(isNumber('abc')).toBe(false);
20
+ expect(isNumber('123abc')).toBe(false);
21
+ expect(isNumber('')).toBe(false);
22
+ expect(isNumber(' ')).toBe(false);
23
+ });
24
+
25
+ it('should return false for boolean values', () => {
26
+ expect(isNumber(true)).toBe(false);
27
+ expect(isNumber(false)).toBe(false);
28
+ });
29
+
30
+ it('should return false for null and undefined', () => {
31
+ expect(isNumber(null)).toBe(false);
32
+ expect(isNumber(undefined)).toBe(false);
33
+ });
34
+
35
+ it('should return false for objects and arrays', () => {
36
+ expect(isNumber({})).toBe(false);
37
+ expect(isNumber([])).toBe(false);
38
+ });
39
+
40
+ it('should return false for NaN and Infinity', () => {
41
+ expect(isNumber(NaN)).toBe(false);
42
+ expect(isNumber(Infinity)).toBe(false);
43
+ expect(isNumber(-Infinity)).toBe(false);
44
+ });
45
+ });