strnum 1.0.5 → 2.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 +6 -0
- package/README.md +14 -3
- package/package.json +3 -2
- package/strnum.js +37 -56
- package/strnum.test.js +10 -6
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
# strnum
|
|
2
2
|
Parse string into Number based on configuration
|
|
3
3
|
|
|
4
|
+
## Users
|
|
5
|
+
|
|
6
|
+
<a href="https://github.com/aws-amplify" target="_blank"><img src="https://avatars.githubusercontent.com/u/41077760?s=100&v=4"></a>
|
|
7
|
+
<a href="https://github.com/astrapay" target="_blank"><img src="https://avatars.githubusercontent.com/u/90901882?s=100&v=4"></a>
|
|
8
|
+
<a href="https://github.com/process-analytics" target="_blank"><img src="https://avatars.githubusercontent.com/u/60110287?s=100&v=4"></a>
|
|
9
|
+
<a href="https://github.com/NaturalIntelligence" target="_blank"><img src="https://avatars.githubusercontent.com/u/16322633?s=100&v=4"></a>
|
|
10
|
+
Many React Native projects and plugins
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
4
14
|
```bash
|
|
5
15
|
npm install strnum
|
|
6
16
|
```
|
|
@@ -80,7 +90,8 @@ toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} )); //"+1212121212"
|
|
|
80
90
|
|
|
81
91
|
Supported Options
|
|
82
92
|
```js
|
|
83
|
-
hex
|
|
93
|
+
hex: true, //when hexadecimal string should be parsed
|
|
84
94
|
leadingZeros: true, //when number with leading zeros like 08 should be parsed. 0.0 is not impacted
|
|
85
|
-
eNotation: true
|
|
86
|
-
|
|
95
|
+
eNotation: true, //when number with eNotation or number parsed in eNotation should be considered
|
|
96
|
+
skipLike: /regex/ //when string should not be parsed when it matches the specified regular expression
|
|
97
|
+
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "strnum",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Parse String to Number based on configuration",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "strnum.js",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "jasmine strnum.test.js"
|
|
@@ -19,6 +20,6 @@
|
|
|
19
20
|
"author": "Amit Gupta (https://amitkumargupta.work/)",
|
|
20
21
|
"license": "MIT",
|
|
21
22
|
"devDependencies": {
|
|
22
|
-
"jasmine": "^
|
|
23
|
+
"jasmine": "^5.6.0"
|
|
23
24
|
}
|
|
24
25
|
}
|
package/strnum.js
CHANGED
|
@@ -1,78 +1,67 @@
|
|
|
1
1
|
const hexRegex = /^[-+]?0x[a-fA-F0-9]+$/;
|
|
2
|
-
const numRegex = /^([\-\+])?(0*)(
|
|
3
|
-
// const octRegex =
|
|
2
|
+
const numRegex = /^([\-\+])?(0*)([0-9]*(\.[0-9]*)?)$/;
|
|
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
7
|
const consider = {
|
|
17
8
|
hex : true,
|
|
9
|
+
// oct: false,
|
|
18
10
|
leadingZeros: true,
|
|
19
11
|
decimalPoint: "\.",
|
|
20
|
-
eNotation: true
|
|
12
|
+
eNotation: true,
|
|
21
13
|
//skipLike: /regex/
|
|
22
14
|
};
|
|
23
15
|
|
|
24
|
-
function toNumber(str, options = {}){
|
|
25
|
-
// const options = Object.assign({}, consider);
|
|
26
|
-
// if(opt.leadingZeros === false){
|
|
27
|
-
// options.leadingZeros = false;
|
|
28
|
-
// }else if(opt.hex === false){
|
|
29
|
-
// options.hex = false;
|
|
30
|
-
// }
|
|
31
|
-
|
|
16
|
+
export default function toNumber(str, options = {}){
|
|
32
17
|
options = Object.assign({}, consider, options );
|
|
33
18
|
if(!str || typeof str !== "string" ) return str;
|
|
34
19
|
|
|
35
20
|
let trimmedStr = str.trim();
|
|
36
|
-
// if(trimmedStr === "0.0") return 0;
|
|
37
|
-
// else if(trimmedStr === "+0.0") return 0;
|
|
38
|
-
// else if(trimmedStr === "-0.0") return -0;
|
|
39
21
|
|
|
40
22
|
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
41
23
|
else if (options.hex && hexRegex.test(trimmedStr)) {
|
|
42
|
-
return
|
|
43
|
-
// }
|
|
24
|
+
return parse_int(trimmedStr, 16);
|
|
25
|
+
// }else if (options.oct && octRegex.test(str)) {
|
|
44
26
|
// return Number.parseInt(val, 8);
|
|
27
|
+
}else if (trimmedStr.search(/[eE]/)!== -1) { //eNotation
|
|
28
|
+
const notation = trimmedStr.match(/^([-\+])?(0*)([0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+)/);
|
|
29
|
+
// +00.123 => [ , '+', '00', '.123', ..
|
|
30
|
+
if(notation){
|
|
31
|
+
if(options.leadingZeros){ //accept with leading zeros
|
|
32
|
+
trimmedStr = (notation[1] || "") + notation[3];
|
|
33
|
+
}else{
|
|
34
|
+
if(notation[2] === "0" && notation[3][0]=== "."){ //valid number
|
|
35
|
+
}else{
|
|
36
|
+
return trimmedStr;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return options.eNotation ? Number(trimmedStr) : trimmedStr;
|
|
40
|
+
}else{
|
|
41
|
+
return trimmedStr;
|
|
42
|
+
}
|
|
45
43
|
// }else if (options.parseBin && binRegex.test(str)) {
|
|
46
44
|
// return Number.parseInt(val, 2);
|
|
47
45
|
}else{
|
|
48
46
|
//separate negative sign, leading zeros, and rest number
|
|
49
47
|
const match = numRegex.exec(trimmedStr);
|
|
48
|
+
// +00.123 => [ , '+', '00', '.123', ..
|
|
50
49
|
if(match){
|
|
51
50
|
const sign = match[1];
|
|
52
51
|
const leadingZeros = match[2];
|
|
53
52
|
let numTrimmedByZeros = trimZeros(match[3]); //complete num without leading zeros
|
|
54
53
|
//trim ending zeros for floating number
|
|
55
54
|
|
|
56
|
-
const eNotation = match[4] || match[6];
|
|
57
55
|
if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123
|
|
58
56
|
else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123
|
|
59
57
|
else{//no leading zeros or leading zeros are allowed
|
|
60
58
|
const num = Number(trimmedStr);
|
|
61
59
|
const numStr = "" + num;
|
|
60
|
+
|
|
62
61
|
if(numStr.search(/[eE]/) !== -1){ //given number is long and parsed to eNotation
|
|
63
62
|
if(options.eNotation) return num;
|
|
64
63
|
else return str;
|
|
65
|
-
}else if(eNotation){ //given number has enotation
|
|
66
|
-
if(options.eNotation) return num;
|
|
67
|
-
else return str;
|
|
68
64
|
}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
65
|
if(numStr === "0" && (numTrimmedByZeros === "") ) return num; //0.0
|
|
77
66
|
else if(numStr === numTrimmedByZeros) return num; //0.456. 0.79000
|
|
78
67
|
else if( sign && numStr === "-"+numTrimmedByZeros) return num;
|
|
@@ -80,26 +69,11 @@ function toNumber(str, options = {}){
|
|
|
80
69
|
}
|
|
81
70
|
|
|
82
71
|
if(leadingZeros){
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
// }else return str;
|
|
87
|
-
if(numTrimmedByZeros === numStr) return num;
|
|
88
|
-
else if(sign+numTrimmedByZeros === numStr) return num;
|
|
89
|
-
else return str;
|
|
72
|
+
return (numTrimmedByZeros === numStr) || (sign+numTrimmedByZeros === numStr) ? num : str
|
|
73
|
+
}else {
|
|
74
|
+
return (trimmedStr === numStr) || (trimmedStr === sign+numStr) ? num : str
|
|
90
75
|
}
|
|
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
76
|
}
|
|
101
|
-
// else if(!eNotation && trimmedStr && trimmedStr !== Number(trimmedStr) ) return str;
|
|
102
|
-
|
|
103
77
|
}else{ //non-numeric string
|
|
104
78
|
return str;
|
|
105
79
|
}
|
|
@@ -121,4 +95,11 @@ function trimZeros(numStr){
|
|
|
121
95
|
}
|
|
122
96
|
return numStr;
|
|
123
97
|
}
|
|
124
|
-
|
|
98
|
+
|
|
99
|
+
function parse_int(numStr, base){
|
|
100
|
+
//polyfill
|
|
101
|
+
if(parseInt) return parseInt(numStr, base);
|
|
102
|
+
else if(Number.parseInt) return Number.parseInt(numStr, base);
|
|
103
|
+
else if(window && window.parseInt) return window.parseInt(numStr, base);
|
|
104
|
+
else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
|
|
105
|
+
}
|
package/strnum.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import toNumber from "./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", () => {
|
|
@@ -51,11 +51,12 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
51
51
|
expect(toNumber("20.21.030") ).toEqual("20.21.030");
|
|
52
52
|
expect(toNumber("0.21.030") ).toEqual("0.21.030");
|
|
53
53
|
expect(toNumber("0.21.") ).toEqual("0.21.");
|
|
54
|
-
expect(toNumber("0.")
|
|
55
|
-
expect(toNumber("
|
|
54
|
+
expect(toNumber("0.")).toEqual(0);
|
|
55
|
+
expect(toNumber("+0.")).toEqual(0);
|
|
56
|
+
expect(toNumber("-0.")).toEqual(-0);
|
|
57
|
+
expect(toNumber("1.") ).toEqual(1);
|
|
56
58
|
});
|
|
57
59
|
it("floating point and leading zeros", () => {
|
|
58
|
-
expect(toNumber("0.0")).toEqual(0);
|
|
59
60
|
expect(toNumber("00.00")).toEqual(0);
|
|
60
61
|
expect(toNumber("0.06")).toEqual(0.06);
|
|
61
62
|
expect(toNumber("00.6")).toEqual(0.6);
|
|
@@ -108,10 +109,13 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
108
109
|
|
|
109
110
|
expect(toNumber("-1.0e2") ).toEqual(-100);
|
|
110
111
|
expect(toNumber("1.0e-2")).toEqual(0.01);
|
|
111
|
-
|
|
112
|
+
|
|
112
113
|
expect(toNumber("420926189200190257681175017717") ).toEqual(4.209261892001902e+29);
|
|
113
114
|
expect(toNumber("420926189200190257681175017717" , { eNotation: false} )).toEqual("420926189200190257681175017717");
|
|
114
|
-
|
|
115
|
+
|
|
116
|
+
expect(toNumber("1e-2")).toEqual(0.01);
|
|
117
|
+
expect(toNumber("1e+2")).toEqual(100);
|
|
118
|
+
expect(toNumber("1.e+2")).toEqual(100);
|
|
115
119
|
});
|
|
116
120
|
|
|
117
121
|
it("scientific notation with upper E", () => {
|