strnum 1.1.0 → 1.1.2
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 +19 -0
- package/package.json +8 -2
- package/strnum.js +11 -6
- package/strnum.test.js +14 -3
- package/test.js +0 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
1
|
|
|
2
|
+
**1.1.2 / 2025-02-27**
|
|
3
|
+
- fix skiplike for 0
|
|
4
|
+
|
|
5
|
+
**1.1.1 / 2025-02-21**
|
|
6
|
+
- All recent fixes of version 2
|
|
7
|
+
|
|
8
|
+
**2.0.4 / 2025-02-20**
|
|
9
|
+
- remove console log
|
|
10
|
+
|
|
11
|
+
**2.0.3 / 2025-02-20**
|
|
12
|
+
- fix for string which are falsly identified as e-notation
|
|
13
|
+
|
|
14
|
+
**2.0.1 / 2025-02-20**
|
|
15
|
+
- fix: handle only zeros
|
|
16
|
+
- fix: return original string when NaN
|
|
17
|
+
|
|
18
|
+
**2.0.0 / 2025-02-20**
|
|
19
|
+
- Migrating to ESM modules. No functional change
|
|
20
|
+
|
|
2
21
|
**1.1.0 / 2025-02-20**
|
|
3
22
|
- fix (#9): support missing floating point and e notations
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "strnum",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Parse String to Number based on configuration",
|
|
5
5
|
"main": "strnum.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,7 +18,13 @@
|
|
|
18
18
|
},
|
|
19
19
|
"author": "Amit Gupta (https://amitkumargupta.work/)",
|
|
20
20
|
"license": "MIT",
|
|
21
|
+
"funding": [
|
|
22
|
+
{
|
|
23
|
+
"type": "github",
|
|
24
|
+
"url": "https://github.com/sponsors/NaturalIntelligence"
|
|
25
|
+
}
|
|
26
|
+
],
|
|
21
27
|
"devDependencies": {
|
|
22
|
-
"jasmine": "^
|
|
28
|
+
"jasmine": "^5.6.0"
|
|
23
29
|
}
|
|
24
30
|
}
|
package/strnum.js
CHANGED
|
@@ -18,27 +18,29 @@ function toNumber(str, options = {}){
|
|
|
18
18
|
if(!str || typeof str !== "string" ) return str;
|
|
19
19
|
|
|
20
20
|
let trimmedStr = str.trim();
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
23
|
+
else if(str==="0") return 0;
|
|
23
24
|
else if (options.hex && hexRegex.test(trimmedStr)) {
|
|
24
25
|
return parse_int(trimmedStr, 16);
|
|
25
26
|
// }else if (options.oct && octRegex.test(str)) {
|
|
26
27
|
// return Number.parseInt(val, 8);
|
|
27
28
|
}else if (trimmedStr.search(/[eE]/)!== -1) { //eNotation
|
|
28
|
-
const notation = trimmedStr.match(/^([-\+])?(0*)([0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+)
|
|
29
|
+
const notation = trimmedStr.match(/^([-\+])?(0*)([0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+)$/);
|
|
29
30
|
// +00.123 => [ , '+', '00', '.123', ..
|
|
30
31
|
if(notation){
|
|
32
|
+
// console.log(notation)
|
|
31
33
|
if(options.leadingZeros){ //accept with leading zeros
|
|
32
34
|
trimmedStr = (notation[1] || "") + notation[3];
|
|
33
35
|
}else{
|
|
34
36
|
if(notation[2] === "0" && notation[3][0]=== "."){ //valid number
|
|
35
37
|
}else{
|
|
36
|
-
return
|
|
38
|
+
return str;
|
|
37
39
|
}
|
|
38
40
|
}
|
|
39
|
-
return options.eNotation ? Number(trimmedStr) :
|
|
41
|
+
return options.eNotation ? Number(trimmedStr) : str;
|
|
40
42
|
}else{
|
|
41
|
-
return
|
|
43
|
+
return str;
|
|
42
44
|
}
|
|
43
45
|
// }else if (options.parseBin && binRegex.test(str)) {
|
|
44
46
|
// return Number.parseInt(val, 2);
|
|
@@ -54,6 +56,8 @@ function toNumber(str, options = {}){
|
|
|
54
56
|
|
|
55
57
|
if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123
|
|
56
58
|
else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123
|
|
59
|
+
else if(options.leadingZeros && leadingZeros===str) return 0; //00
|
|
60
|
+
|
|
57
61
|
else{//no leading zeros or leading zeros are allowed
|
|
58
62
|
const num = Number(trimmedStr);
|
|
59
63
|
const numStr = "" + num;
|
|
@@ -103,4 +107,5 @@ function parse_int(numStr, base){
|
|
|
103
107
|
else if(window && window.parseInt) return window.parseInt(numStr, base);
|
|
104
108
|
else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
|
|
105
109
|
}
|
|
106
|
-
|
|
110
|
+
|
|
111
|
+
module.exports = toNumber;
|
package/strnum.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const toNumber = require("./strnum");
|
|
1
|
+
const toNumber = require("./strnum.js");
|
|
2
2
|
|
|
3
3
|
describe("Should convert all the valid numeric strings to number", () => {
|
|
4
4
|
it("should return undefined, null, empty string, or non-numeric as it is", () => {
|
|
@@ -6,6 +6,8 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
6
6
|
expect(toNumber(null)).toEqual(null);
|
|
7
7
|
expect(toNumber("")).toEqual("");
|
|
8
8
|
expect(toNumber("string")).toEqual("string");
|
|
9
|
+
expect(toNumber("e89794659669cb7bb967db73a7ea6889c3891727")).toEqual("e89794659669cb7bb967db73a7ea6889c3891727");
|
|
10
|
+
|
|
9
11
|
});
|
|
10
12
|
it("should not parse number with spaces or comma", () => {
|
|
11
13
|
expect(toNumber("12,12")).toEqual("12,12");
|
|
@@ -34,6 +36,14 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
34
36
|
expect(toNumber("JVBERi0xLjMNCiXi48")).toEqual("JVBERi0xLjMNCiXi48");
|
|
35
37
|
})
|
|
36
38
|
it("leading zeros", () => {
|
|
39
|
+
expect(toNumber("0")).toEqual(0);
|
|
40
|
+
expect(toNumber("00")).toEqual(0);
|
|
41
|
+
expect(toNumber("00.0")).toEqual(0);
|
|
42
|
+
|
|
43
|
+
expect(toNumber("0",{ leadingZeros : false})).toEqual(0);
|
|
44
|
+
expect(toNumber("00",{ leadingZeros : false})).toEqual("00");
|
|
45
|
+
expect(toNumber("00.0",{ leadingZeros : false})).toEqual("00.0");
|
|
46
|
+
|
|
37
47
|
expect(toNumber("06")).toEqual(6);
|
|
38
48
|
expect(toNumber("06", { leadingZeros : true})).toEqual(6);
|
|
39
49
|
expect(toNumber("06", { leadingZeros : false})).toEqual("06");
|
|
@@ -51,12 +61,12 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
51
61
|
expect(toNumber("20.21.030") ).toEqual("20.21.030");
|
|
52
62
|
expect(toNumber("0.21.030") ).toEqual("0.21.030");
|
|
53
63
|
expect(toNumber("0.21.") ).toEqual("0.21.");
|
|
64
|
+
});
|
|
65
|
+
it("floating point and leading zeros", () => {
|
|
54
66
|
expect(toNumber("0.")).toEqual(0);
|
|
55
67
|
expect(toNumber("+0.")).toEqual(0);
|
|
56
68
|
expect(toNumber("-0.")).toEqual(-0);
|
|
57
69
|
expect(toNumber("1.") ).toEqual(1);
|
|
58
|
-
});
|
|
59
|
-
it("floating point and leading zeros", () => {
|
|
60
70
|
expect(toNumber("00.00")).toEqual(0);
|
|
61
71
|
expect(toNumber("0.06")).toEqual(0.06);
|
|
62
72
|
expect(toNumber("00.6")).toEqual(0.6);
|
|
@@ -130,6 +140,7 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
130
140
|
});
|
|
131
141
|
|
|
132
142
|
it("should skip matching pattern", () => {
|
|
143
|
+
expect(toNumber("0", { skipLike: /.*/ })).toEqual("0");
|
|
133
144
|
expect(toNumber("+12", { skipLike: /\+[0-9]{10}/} )).toEqual(12);
|
|
134
145
|
expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/} )).toEqual("12+12");
|
|
135
146
|
expect(toNumber("12+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("12+1212121212");
|