strnum 1.0.4 → 1.0.5
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/.vscode/launch.json +25 -0
- package/README.md +7 -0
- package/package.json +5 -2
- package/strnum.js +80 -6
- package/strnum.test.js +30 -3
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.2.0",
|
|
3
|
+
"configurations": [
|
|
4
|
+
{
|
|
5
|
+
"type": "node",
|
|
6
|
+
"request": "launch",
|
|
7
|
+
"name": "Jasmine Tests",
|
|
8
|
+
"program": "${workspaceFolder}/node_modules/jasmine/bin/jasmine.js",
|
|
9
|
+
"args": [
|
|
10
|
+
"${workspaceFolder}/spec/attr_spec.js"
|
|
11
|
+
],
|
|
12
|
+
"internalConsoleOptions": "openOnSessionStart"
|
|
13
|
+
},{
|
|
14
|
+
"type": "node",
|
|
15
|
+
"request": "launch",
|
|
16
|
+
"name": "Jasmine Tests current test file",
|
|
17
|
+
"program": "${workspaceFolder}/node_modules/jasmine/bin/jasmine.js",
|
|
18
|
+
"args": [
|
|
19
|
+
"${file}"
|
|
20
|
+
],
|
|
21
|
+
"internalConsoleOptions": "openOnSessionStart"
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
}
|
package/README.md
CHANGED
|
@@ -76,4 +76,11 @@ toNumber("1.0e-2"); //0.01)
|
|
|
76
76
|
|
|
77
77
|
toNumber("+1212121212"); // 1212121212
|
|
78
78
|
toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} )); //"+1212121212"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Supported Options
|
|
82
|
+
```js
|
|
83
|
+
hex : true, //when hexadecimal string should be parsed
|
|
84
|
+
leadingZeros: true, //when number with leading zeros like 08 should be parsed. 0.0 is not impacted
|
|
85
|
+
eNotation: true //when number with eNotation or number parsed in eNotation should be considered
|
|
79
86
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "strnum",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Parse String to Number based on configuration",
|
|
5
5
|
"main": "strnum.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,5 +17,8 @@
|
|
|
17
17
|
"url": "https://github.com/NaturalIntelligence/strnum"
|
|
18
18
|
},
|
|
19
19
|
"author": "Amit Gupta (https://amitkumargupta.work/)",
|
|
20
|
-
"license": "MIT"
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"jasmine": "^3.10.0"
|
|
23
|
+
}
|
|
21
24
|
}
|
package/strnum.js
CHANGED
|
@@ -3,10 +3,21 @@ const numRegex = /^([\-\+])?(0*)(\.[0-9]+([eE]\-?[0-9]+)?|[0-9]+(\.[0-9]+([eE]\-
|
|
|
3
3
|
// const octRegex = /0x[a-z0-9]+/;
|
|
4
4
|
// const binRegex = /0x[a-z0-9]+/;
|
|
5
5
|
|
|
6
|
+
|
|
7
|
+
//polyfill
|
|
8
|
+
if (!Number.parseInt && window.parseInt) {
|
|
9
|
+
Number.parseInt = window.parseInt;
|
|
10
|
+
}
|
|
11
|
+
if (!Number.parseFloat && window.parseFloat) {
|
|
12
|
+
Number.parseFloat = window.parseFloat;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
6
16
|
const consider = {
|
|
7
17
|
hex : true,
|
|
8
18
|
leadingZeros: true,
|
|
9
19
|
decimalPoint: "\.",
|
|
20
|
+
eNotation: true
|
|
10
21
|
//skipLike: /regex/
|
|
11
22
|
};
|
|
12
23
|
|
|
@@ -22,7 +33,10 @@ function toNumber(str, options = {}){
|
|
|
22
33
|
if(!str || typeof str !== "string" ) return str;
|
|
23
34
|
|
|
24
35
|
let trimmedStr = str.trim();
|
|
25
|
-
|
|
36
|
+
// if(trimmedStr === "0.0") return 0;
|
|
37
|
+
// else if(trimmedStr === "+0.0") return 0;
|
|
38
|
+
// else if(trimmedStr === "-0.0") return -0;
|
|
39
|
+
|
|
26
40
|
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
27
41
|
else if (options.hex && hexRegex.test(trimmedStr)) {
|
|
28
42
|
return Number.parseInt(trimmedStr, 16);
|
|
@@ -34,17 +48,77 @@ function toNumber(str, options = {}){
|
|
|
34
48
|
//separate negative sign, leading zeros, and rest number
|
|
35
49
|
const match = numRegex.exec(trimmedStr);
|
|
36
50
|
if(match){
|
|
37
|
-
const
|
|
51
|
+
const sign = match[1];
|
|
38
52
|
const leadingZeros = match[2];
|
|
39
|
-
|
|
53
|
+
let numTrimmedByZeros = trimZeros(match[3]); //complete num without leading zeros
|
|
54
|
+
//trim ending zeros for floating number
|
|
55
|
+
|
|
40
56
|
const eNotation = match[4] || match[6];
|
|
41
|
-
if(leadingZeros.length
|
|
42
|
-
else if(!options.leadingZeros && leadingZeros.length > 0) return str;
|
|
43
|
-
else
|
|
57
|
+
if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123
|
|
58
|
+
else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123
|
|
59
|
+
else{//no leading zeros or leading zeros are allowed
|
|
60
|
+
const num = Number(trimmedStr);
|
|
61
|
+
const numStr = "" + num;
|
|
62
|
+
if(numStr.search(/[eE]/) !== -1){ //given number is long and parsed to eNotation
|
|
63
|
+
if(options.eNotation) return num;
|
|
64
|
+
else return str;
|
|
65
|
+
}else if(eNotation){ //given number has enotation
|
|
66
|
+
if(options.eNotation) return num;
|
|
67
|
+
else return str;
|
|
68
|
+
}else if(trimmedStr.indexOf(".") !== -1){ //floating number
|
|
69
|
+
// const decimalPart = match[5].substr(1);
|
|
70
|
+
// const intPart = trimmedStr.substr(0,trimmedStr.indexOf("."));
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
// const p = numStr.indexOf(".");
|
|
74
|
+
// const givenIntPart = numStr.substr(0,p);
|
|
75
|
+
// const givenDecPart = numStr.substr(p+1);
|
|
76
|
+
if(numStr === "0" && (numTrimmedByZeros === "") ) return num; //0.0
|
|
77
|
+
else if(numStr === numTrimmedByZeros) return num; //0.456. 0.79000
|
|
78
|
+
else if( sign && numStr === "-"+numTrimmedByZeros) return num;
|
|
79
|
+
else return str;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if(leadingZeros){
|
|
83
|
+
// if(numTrimmedByZeros === numStr){
|
|
84
|
+
// if(options.leadingZeros) return num;
|
|
85
|
+
// else return str;
|
|
86
|
+
// }else return str;
|
|
87
|
+
if(numTrimmedByZeros === numStr) return num;
|
|
88
|
+
else if(sign+numTrimmedByZeros === numStr) return num;
|
|
89
|
+
else return str;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if(trimmedStr === numStr) return num;
|
|
93
|
+
else if(trimmedStr === sign+numStr) return num;
|
|
94
|
+
// else{
|
|
95
|
+
// //number with +/- sign
|
|
96
|
+
// trimmedStr.test(/[-+][0-9]);
|
|
97
|
+
|
|
98
|
+
// }
|
|
99
|
+
return str;
|
|
100
|
+
}
|
|
101
|
+
// else if(!eNotation && trimmedStr && trimmedStr !== Number(trimmedStr) ) return str;
|
|
102
|
+
|
|
44
103
|
}else{ //non-numeric string
|
|
45
104
|
return str;
|
|
46
105
|
}
|
|
47
106
|
}
|
|
48
107
|
}
|
|
49
108
|
|
|
109
|
+
/**
|
|
110
|
+
*
|
|
111
|
+
* @param {string} numStr without leading zeros
|
|
112
|
+
* @returns
|
|
113
|
+
*/
|
|
114
|
+
function trimZeros(numStr){
|
|
115
|
+
if(numStr && numStr.indexOf(".") !== -1){//float
|
|
116
|
+
numStr = numStr.replace(/0+$/, ""); //remove ending zeros
|
|
117
|
+
if(numStr === ".") numStr = "0";
|
|
118
|
+
else if(numStr[0] === ".") numStr = "0"+numStr;
|
|
119
|
+
else if(numStr[numStr.length-1] === ".") numStr = numStr.substr(0,numStr.length-1);
|
|
120
|
+
return numStr;
|
|
121
|
+
}
|
|
122
|
+
return numStr;
|
|
123
|
+
}
|
|
50
124
|
module.exports = toNumber
|
package/strnum.test.js
CHANGED
|
@@ -15,6 +15,7 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
15
15
|
})
|
|
16
16
|
it("should consider + sign", () => {
|
|
17
17
|
expect(toNumber("+12")).toEqual(12);
|
|
18
|
+
expect(toNumber("+ 12")).toEqual("+ 12");
|
|
18
19
|
expect(toNumber("12+12")).toEqual("12+12");
|
|
19
20
|
expect(toNumber("1212+")).toEqual("1212+");
|
|
20
21
|
})
|
|
@@ -40,7 +41,19 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
40
41
|
expect(toNumber("006")).toEqual(6);
|
|
41
42
|
expect(toNumber("006", { leadingZeros : true})).toEqual(6);
|
|
42
43
|
expect(toNumber("006", { leadingZeros : false})).toEqual("006");
|
|
44
|
+
|
|
45
|
+
expect(toNumber("000000000000000000000000017717" , { leadingZeros : false})).toEqual("000000000000000000000000017717");
|
|
46
|
+
expect(toNumber("000000000000000000000000017717" , { leadingZeros : true})).toEqual(17717);
|
|
47
|
+
expect(toNumber("020211201030005811824") ).toEqual("020211201030005811824");
|
|
48
|
+
expect(toNumber("0420926189200190257681175017717") ).toEqual(4.209261892001902e+29);
|
|
43
49
|
})
|
|
50
|
+
it("invalid floating number", () => {
|
|
51
|
+
expect(toNumber("20.21.030") ).toEqual("20.21.030");
|
|
52
|
+
expect(toNumber("0.21.030") ).toEqual("0.21.030");
|
|
53
|
+
expect(toNumber("0.21.") ).toEqual("0.21.");
|
|
54
|
+
expect(toNumber("0.") ).toEqual("0.");
|
|
55
|
+
expect(toNumber("1.") ).toEqual("1.");
|
|
56
|
+
});
|
|
44
57
|
it("floating point and leading zeros", () => {
|
|
45
58
|
expect(toNumber("0.0")).toEqual(0);
|
|
46
59
|
expect(toNumber("00.00")).toEqual(0);
|
|
@@ -59,6 +72,7 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
59
72
|
expect(toNumber("06.0" , { leadingZeros : false})).toEqual("06.0");
|
|
60
73
|
})
|
|
61
74
|
it("negative number leading zeros", () => {
|
|
75
|
+
expect(toNumber("+06")).toEqual(6);
|
|
62
76
|
expect(toNumber("-06")).toEqual(-6);
|
|
63
77
|
expect(toNumber("-06", { leadingZeros : true})).toEqual(-6);
|
|
64
78
|
expect(toNumber("-06", { leadingZeros : false})).toEqual("-06");
|
|
@@ -80,9 +94,10 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
80
94
|
expect(toNumber("-06.0" , { leadingZeros : false})).toEqual("-06.0");
|
|
81
95
|
})
|
|
82
96
|
it("long number", () => {
|
|
83
|
-
expect(toNumber("
|
|
84
|
-
expect(toNumber("
|
|
85
|
-
expect(toNumber("
|
|
97
|
+
expect(toNumber("020211201030005811824") ).toEqual("020211201030005811824");
|
|
98
|
+
expect(toNumber("20211201030005811824") ).toEqual("20211201030005811824");
|
|
99
|
+
expect(toNumber("20.211201030005811824") ).toEqual("20.211201030005811824");
|
|
100
|
+
expect(toNumber("0.211201030005811824") ).toEqual("0.211201030005811824");
|
|
86
101
|
});
|
|
87
102
|
it("scientific notation", () => {
|
|
88
103
|
expect(toNumber("01.0e2" , { leadingZeros : false})).toEqual("01.0e2");
|
|
@@ -93,6 +108,10 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
93
108
|
|
|
94
109
|
expect(toNumber("-1.0e2") ).toEqual(-100);
|
|
95
110
|
expect(toNumber("1.0e-2")).toEqual(0.01);
|
|
111
|
+
|
|
112
|
+
expect(toNumber("420926189200190257681175017717") ).toEqual(4.209261892001902e+29);
|
|
113
|
+
expect(toNumber("420926189200190257681175017717" , { eNotation: false} )).toEqual("420926189200190257681175017717");
|
|
114
|
+
|
|
96
115
|
});
|
|
97
116
|
|
|
98
117
|
it("scientific notation with upper E", () => {
|
|
@@ -120,4 +139,12 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
120
139
|
it("should ignore sorrounded spaces ", () => {
|
|
121
140
|
expect(toNumber(" +1212 ")).toEqual(1212);
|
|
122
141
|
})
|
|
142
|
+
|
|
143
|
+
it("negative numbers", () => {
|
|
144
|
+
expect(toNumber("+1212")).toEqual(1212);
|
|
145
|
+
expect(toNumber("+12.12")).toEqual(12.12);
|
|
146
|
+
expect(toNumber("-12.12")).toEqual(-12.12);
|
|
147
|
+
expect(toNumber("-012.12")).toEqual(-12.12);
|
|
148
|
+
expect(toNumber("-012.12")).toEqual(-12.12);
|
|
149
|
+
})
|
|
123
150
|
});
|