strnum 2.0.0 → 2.0.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 +4 -0
- package/package.json +7 -1
- package/strnum.js +7 -4
- package/strnum.test.js +10 -2
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "strnum",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Parse String to Number based on configuration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "strnum.js",
|
|
@@ -19,6 +19,12 @@
|
|
|
19
19
|
},
|
|
20
20
|
"author": "Amit Gupta (https://amitkumargupta.work/)",
|
|
21
21
|
"license": "MIT",
|
|
22
|
+
"funding": [
|
|
23
|
+
{
|
|
24
|
+
"type": "github",
|
|
25
|
+
"url": "https://github.com/sponsors/NaturalIntelligence"
|
|
26
|
+
}
|
|
27
|
+
],
|
|
22
28
|
"devDependencies": {
|
|
23
29
|
"jasmine": "^5.6.0"
|
|
24
30
|
}
|
package/strnum.js
CHANGED
|
@@ -16,7 +16,8 @@ const consider = {
|
|
|
16
16
|
export default function toNumber(str, options = {}){
|
|
17
17
|
options = Object.assign({}, consider, options );
|
|
18
18
|
if(!str || typeof str !== "string" ) return str;
|
|
19
|
-
|
|
19
|
+
else if(str==="0") return 0;
|
|
20
|
+
|
|
20
21
|
let trimmedStr = str.trim();
|
|
21
22
|
|
|
22
23
|
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
@@ -33,12 +34,12 @@ export default function toNumber(str, options = {}){
|
|
|
33
34
|
}else{
|
|
34
35
|
if(notation[2] === "0" && notation[3][0]=== "."){ //valid number
|
|
35
36
|
}else{
|
|
36
|
-
return
|
|
37
|
+
return str;
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
|
-
return options.eNotation ? Number(trimmedStr) :
|
|
40
|
+
return options.eNotation ? Number(trimmedStr) : str;
|
|
40
41
|
}else{
|
|
41
|
-
return
|
|
42
|
+
return str;
|
|
42
43
|
}
|
|
43
44
|
// }else if (options.parseBin && binRegex.test(str)) {
|
|
44
45
|
// return Number.parseInt(val, 2);
|
|
@@ -54,6 +55,8 @@ export default function toNumber(str, options = {}){
|
|
|
54
55
|
|
|
55
56
|
if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123
|
|
56
57
|
else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123
|
|
58
|
+
else if(options.leadingZeros && leadingZeros===str) return 0; //00
|
|
59
|
+
|
|
57
60
|
else{//no leading zeros or leading zeros are allowed
|
|
58
61
|
const num = Number(trimmedStr);
|
|
59
62
|
const numStr = "" + num;
|
package/strnum.test.js
CHANGED
|
@@ -34,6 +34,14 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
34
34
|
expect(toNumber("JVBERi0xLjMNCiXi48")).toEqual("JVBERi0xLjMNCiXi48");
|
|
35
35
|
})
|
|
36
36
|
it("leading zeros", () => {
|
|
37
|
+
expect(toNumber("0")).toEqual(0);
|
|
38
|
+
expect(toNumber("00")).toEqual(0);
|
|
39
|
+
expect(toNumber("00.0")).toEqual(0);
|
|
40
|
+
|
|
41
|
+
expect(toNumber("0",{ leadingZeros : false})).toEqual(0);
|
|
42
|
+
expect(toNumber("00",{ leadingZeros : false})).toEqual("00");
|
|
43
|
+
expect(toNumber("00.0",{ leadingZeros : false})).toEqual("00.0");
|
|
44
|
+
|
|
37
45
|
expect(toNumber("06")).toEqual(6);
|
|
38
46
|
expect(toNumber("06", { leadingZeros : true})).toEqual(6);
|
|
39
47
|
expect(toNumber("06", { leadingZeros : false})).toEqual("06");
|
|
@@ -51,12 +59,12 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
51
59
|
expect(toNumber("20.21.030") ).toEqual("20.21.030");
|
|
52
60
|
expect(toNumber("0.21.030") ).toEqual("0.21.030");
|
|
53
61
|
expect(toNumber("0.21.") ).toEqual("0.21.");
|
|
62
|
+
});
|
|
63
|
+
it("floating point and leading zeros", () => {
|
|
54
64
|
expect(toNumber("0.")).toEqual(0);
|
|
55
65
|
expect(toNumber("+0.")).toEqual(0);
|
|
56
66
|
expect(toNumber("-0.")).toEqual(-0);
|
|
57
67
|
expect(toNumber("1.") ).toEqual(1);
|
|
58
|
-
});
|
|
59
|
-
it("floating point and leading zeros", () => {
|
|
60
68
|
expect(toNumber("00.00")).toEqual(0);
|
|
61
69
|
expect(toNumber("0.06")).toEqual(0.06);
|
|
62
70
|
expect(toNumber("00.6")).toEqual(0.6);
|