strnum 2.1.2 → 2.2.1
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 +5 -2
- package/README.md +1 -0
- package/package.json +3 -3
- package/strnum.js +91 -60
- package/tests/infinity_test.js +18 -0
- package/{strnum.test.js → tests/strnum_test.js} +69 -68
- package/tests/temp.js +8 -0
- package/test.js +0 -9
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -94,6 +94,7 @@ hex: true, //when hexadecimal string should be parsed
|
|
|
94
94
|
leadingZeros: true, //when number with leading zeros like 08 should be parsed. 0.0 is not impacted
|
|
95
95
|
eNotation: true, //when number with eNotation or number parsed in eNotation should be considered
|
|
96
96
|
skipLike: /regex/ //when string should not be parsed when it matches the specified regular expression
|
|
97
|
+
infinity: "original", // "null", "infinity" (Infinity type), "string" ("Infinity" (the string literal))
|
|
97
98
|
```
|
|
98
99
|
|
|
99
100
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "strnum",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "Parse String to Number based on configuration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "strnum.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "jasmine
|
|
8
|
+
"test": "jasmine tests/*_test.js"
|
|
9
9
|
},
|
|
10
10
|
"keywords": [
|
|
11
11
|
"string",
|
|
@@ -28,4 +28,4 @@
|
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"jasmine": "^5.6.0"
|
|
30
30
|
}
|
|
31
|
-
}
|
|
31
|
+
}
|
package/strnum.js
CHANGED
|
@@ -3,103 +3,111 @@ const numRegex = /^([\-\+])?(0*)([0-9]*(\.[0-9]*)?)$/;
|
|
|
3
3
|
// const octRegex = /^0x[a-z0-9]+/;
|
|
4
4
|
// const binRegex = /0x[a-z0-9]+/;
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
const consider = {
|
|
8
|
-
hex
|
|
8
|
+
hex: true,
|
|
9
9
|
// oct: false,
|
|
10
10
|
leadingZeros: true,
|
|
11
11
|
decimalPoint: "\.",
|
|
12
12
|
eNotation: true,
|
|
13
|
-
//skipLike: /regex
|
|
13
|
+
//skipLike: /regex/,
|
|
14
|
+
infinity: "original", // "null", "infinity" (Infinity type), "string" ("Infinity" (the string literal))
|
|
14
15
|
};
|
|
15
16
|
|
|
16
|
-
export default function toNumber(str, options = {}){
|
|
17
|
-
options = Object.assign({}, consider, options
|
|
18
|
-
if(!str || typeof str !== "string"
|
|
19
|
-
|
|
20
|
-
let trimmedStr
|
|
21
|
-
|
|
22
|
-
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
23
|
-
else if(str==="0") return 0;
|
|
17
|
+
export default function toNumber(str, options = {}) {
|
|
18
|
+
options = Object.assign({}, consider, options);
|
|
19
|
+
if (!str || typeof str !== "string") return str;
|
|
20
|
+
|
|
21
|
+
let trimmedStr = str.trim();
|
|
22
|
+
|
|
23
|
+
if (options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
24
|
+
else if (str === "0") return 0;
|
|
24
25
|
else if (options.hex && hexRegex.test(trimmedStr)) {
|
|
25
26
|
return parse_int(trimmedStr, 16);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}else if (
|
|
29
|
-
return
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
// }else if (options.oct && octRegex.test(str)) {
|
|
28
|
+
// return Number.parseInt(val, 8);
|
|
29
|
+
} else if (!isFinite(trimmedStr)) { //Infinity
|
|
30
|
+
return handleInfinity(str, Number(trimmedStr), options);
|
|
31
|
+
} else if (trimmedStr.includes('e') || trimmedStr.includes('E')) { //eNotation
|
|
32
|
+
return resolveEnotation(str, trimmedStr, options);
|
|
33
|
+
// }else if (options.parseBin && binRegex.test(str)) {
|
|
34
|
+
// return Number.parseInt(val, 2);
|
|
35
|
+
} else {
|
|
33
36
|
//separate negative sign, leading zeros, and rest number
|
|
34
37
|
const match = numRegex.exec(trimmedStr);
|
|
35
38
|
// +00.123 => [ , '+', '00', '.123', ..
|
|
36
|
-
if(match){
|
|
39
|
+
if (match) {
|
|
37
40
|
const sign = match[1] || "";
|
|
38
41
|
const leadingZeros = match[2];
|
|
39
42
|
let numTrimmedByZeros = trimZeros(match[3]); //complete num without leading zeros
|
|
40
43
|
const decimalAdjacentToLeadingZeros = sign ? // 0., -00., 000.
|
|
41
|
-
str[leadingZeros.length+1] === "."
|
|
44
|
+
str[leadingZeros.length + 1] === "."
|
|
42
45
|
: str[leadingZeros.length] === ".";
|
|
43
46
|
|
|
44
47
|
//trim ending zeros for floating number
|
|
45
|
-
if(!options.leadingZeros //leading zeros are not allowed
|
|
46
|
-
&& (leadingZeros.length > 1
|
|
47
|
-
|| (leadingZeros.length === 1 && !decimalAdjacentToLeadingZeros))){
|
|
48
|
+
if (!options.leadingZeros //leading zeros are not allowed
|
|
49
|
+
&& (leadingZeros.length > 1
|
|
50
|
+
|| (leadingZeros.length === 1 && !decimalAdjacentToLeadingZeros))) {
|
|
48
51
|
// 00, 00.3, +03.24, 03, 03.24
|
|
49
52
|
return str;
|
|
50
53
|
}
|
|
51
|
-
else{//no leading zeros or leading zeros are allowed
|
|
54
|
+
else {//no leading zeros or leading zeros are allowed
|
|
52
55
|
const num = Number(trimmedStr);
|
|
53
56
|
const parsedStr = String(num);
|
|
54
57
|
|
|
55
|
-
if(
|
|
56
|
-
if(parsedStr.search(/[eE]/) !== -1){ //given number is long and parsed to eNotation
|
|
57
|
-
if(options.eNotation) return num;
|
|
58
|
+
if (num === 0) return num;
|
|
59
|
+
if (parsedStr.search(/[eE]/) !== -1) { //given number is long and parsed to eNotation
|
|
60
|
+
if (options.eNotation) return num;
|
|
58
61
|
else return str;
|
|
59
|
-
}else if(trimmedStr.indexOf(".") !== -1){ //floating number
|
|
60
|
-
if(parsedStr === "0") return num; //0.0
|
|
61
|
-
else if(parsedStr === numTrimmedByZeros) return num; //0.456. 0.79000
|
|
62
|
-
else if(
|
|
62
|
+
} else if (trimmedStr.indexOf(".") !== -1) { //floating number
|
|
63
|
+
if (parsedStr === "0") return num; //0.0
|
|
64
|
+
else if (parsedStr === numTrimmedByZeros) return num; //0.456. 0.79000
|
|
65
|
+
else if (parsedStr === `${sign}${numTrimmedByZeros}`) return num;
|
|
63
66
|
else return str;
|
|
64
67
|
}
|
|
65
|
-
|
|
66
|
-
let n = leadingZeros? numTrimmedByZeros : trimmedStr;
|
|
67
|
-
if(leadingZeros){
|
|
68
|
+
|
|
69
|
+
let n = leadingZeros ? numTrimmedByZeros : trimmedStr;
|
|
70
|
+
if (leadingZeros) {
|
|
68
71
|
// -009 => -9
|
|
69
|
-
return (n === parsedStr) || (sign+n === parsedStr) ? num : str
|
|
70
|
-
}else
|
|
72
|
+
return (n === parsedStr) || (sign + n === parsedStr) ? num : str
|
|
73
|
+
} else {
|
|
71
74
|
// +9
|
|
72
|
-
return (n === parsedStr) || (n === sign+parsedStr) ? num : str
|
|
75
|
+
return (n === parsedStr) || (n === sign + parsedStr) ? num : str
|
|
73
76
|
}
|
|
74
77
|
}
|
|
75
|
-
}else{ //non-numeric string
|
|
78
|
+
} else { //non-numeric string
|
|
76
79
|
return str;
|
|
77
80
|
}
|
|
78
81
|
}
|
|
79
82
|
}
|
|
80
83
|
|
|
81
84
|
const eNotationRegx = /^([-+])?(0*)(\d*(\.\d*)?[eE][-\+]?\d+)$/;
|
|
82
|
-
function resolveEnotation(str,trimmedStr,options){
|
|
83
|
-
if(!options.eNotation) return str;
|
|
84
|
-
const notation = trimmedStr.match(eNotationRegx);
|
|
85
|
-
if(notation){
|
|
85
|
+
function resolveEnotation(str, trimmedStr, options) {
|
|
86
|
+
if (!options.eNotation) return str;
|
|
87
|
+
const notation = trimmedStr.match(eNotationRegx);
|
|
88
|
+
if (notation) {
|
|
86
89
|
let sign = notation[1] || "";
|
|
87
90
|
const eChar = notation[3].indexOf("e") === -1 ? "E" : "e";
|
|
88
91
|
const leadingZeros = notation[2];
|
|
89
92
|
const eAdjacentToLeadingZeros = sign ? // 0E.
|
|
90
|
-
str[leadingZeros.length+1] === eChar
|
|
93
|
+
str[leadingZeros.length + 1] === eChar
|
|
91
94
|
: str[leadingZeros.length] === eChar;
|
|
92
95
|
|
|
93
|
-
if(leadingZeros.length > 1 && eAdjacentToLeadingZeros) return str;
|
|
94
|
-
else if(leadingZeros.length === 1
|
|
95
|
-
&& (notation[3].startsWith(`.${eChar}`) || notation[3][0] === eChar)){
|
|
96
|
+
if (leadingZeros.length > 1 && eAdjacentToLeadingZeros) return str;
|
|
97
|
+
else if (leadingZeros.length === 1
|
|
98
|
+
&& (notation[3].startsWith(`.${eChar}`) || notation[3][0] === eChar)) {
|
|
99
|
+
return Number(trimmedStr);
|
|
100
|
+
} else if (leadingZeros.length > 0) {
|
|
101
|
+
// Has leading zeros — only accept if leadingZeros option allows it
|
|
102
|
+
if (options.leadingZeros && !eAdjacentToLeadingZeros) {
|
|
103
|
+
trimmedStr = (notation[1] || "") + notation[3];
|
|
96
104
|
return Number(trimmedStr);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
105
|
+
} else return str;
|
|
106
|
+
} else {
|
|
107
|
+
// No leading zeros — always valid e-notation, parse it
|
|
100
108
|
return Number(trimmedStr);
|
|
101
|
-
}
|
|
102
|
-
}else{
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
103
111
|
return str;
|
|
104
112
|
}
|
|
105
113
|
}
|
|
@@ -109,21 +117,44 @@ function resolveEnotation(str,trimmedStr,options){
|
|
|
109
117
|
* @param {string} numStr without leading zeros
|
|
110
118
|
* @returns
|
|
111
119
|
*/
|
|
112
|
-
function trimZeros(numStr){
|
|
113
|
-
if(numStr && numStr.indexOf(".") !== -1){//float
|
|
120
|
+
function trimZeros(numStr) {
|
|
121
|
+
if (numStr && numStr.indexOf(".") !== -1) {//float
|
|
114
122
|
numStr = numStr.replace(/0+$/, ""); //remove ending zeros
|
|
115
|
-
if(numStr === ".")
|
|
116
|
-
else if(numStr[0] === ".")
|
|
117
|
-
else if(numStr[numStr.length-1] === ".")
|
|
123
|
+
if (numStr === ".") numStr = "0";
|
|
124
|
+
else if (numStr[0] === ".") numStr = "0" + numStr;
|
|
125
|
+
else if (numStr[numStr.length - 1] === ".") numStr = numStr.substring(0, numStr.length - 1);
|
|
118
126
|
return numStr;
|
|
119
127
|
}
|
|
120
128
|
return numStr;
|
|
121
129
|
}
|
|
122
130
|
|
|
123
|
-
function parse_int(numStr, base){
|
|
131
|
+
function parse_int(numStr, base) {
|
|
124
132
|
//polyfill
|
|
125
|
-
if(parseInt) return parseInt(numStr, base);
|
|
126
|
-
else if(Number.parseInt) return Number.parseInt(numStr, base);
|
|
127
|
-
else if(window && window.parseInt) return window.parseInt(numStr, base);
|
|
133
|
+
if (parseInt) return parseInt(numStr, base);
|
|
134
|
+
else if (Number.parseInt) return Number.parseInt(numStr, base);
|
|
135
|
+
else if (window && window.parseInt) return window.parseInt(numStr, base);
|
|
128
136
|
else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Handle infinite values based on user option
|
|
141
|
+
* @param {string} str - original input string
|
|
142
|
+
* @param {number} num - parsed number (Infinity or -Infinity)
|
|
143
|
+
* @param {object} options - user options
|
|
144
|
+
* @returns {string|number|null} based on infinity option
|
|
145
|
+
*/
|
|
146
|
+
function handleInfinity(str, num, options) {
|
|
147
|
+
const isPositive = num === Infinity;
|
|
148
|
+
|
|
149
|
+
switch (options.infinity.toLowerCase()) {
|
|
150
|
+
case "null":
|
|
151
|
+
return null;
|
|
152
|
+
case "infinity":
|
|
153
|
+
return num; // Return Infinity or -Infinity
|
|
154
|
+
case "string":
|
|
155
|
+
return isPositive ? "Infinity" : "-Infinity";
|
|
156
|
+
case "original":
|
|
157
|
+
default:
|
|
158
|
+
return str; // Return original string like "1e1000"
|
|
159
|
+
}
|
|
129
160
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import toNumber from "../strnum.js";
|
|
2
|
+
|
|
3
|
+
describe("Should convert all the valid numeric strings to number", () => {
|
|
4
|
+
it("should return infinity as per user option", () => {
|
|
5
|
+
expect(toNumber("1e1000", { infinity: "original" })).toEqual("1e1000");
|
|
6
|
+
expect(toNumber("1e1000", { infinity: "null" })).toEqual(null);
|
|
7
|
+
expect(toNumber("1e1000", { infinity: "infinity" })).toEqual(Infinity);
|
|
8
|
+
expect(toNumber("1e1000", { infinity: "string" })).toEqual("Infinity");
|
|
9
|
+
expect(toNumber("-1e1000", { infinity: "original" })).toEqual("-1e1000");
|
|
10
|
+
expect(toNumber("-1e1000", { infinity: "null" })).toEqual(null);
|
|
11
|
+
expect(toNumber("-1e1000", { infinity: "infinity" })).toEqual(-Infinity);
|
|
12
|
+
expect(toNumber("-1e1000", { infinity: "string" })).toEqual("-Infinity");
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
expect(toNumber("1e309")).toEqual("1e309");
|
|
16
|
+
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import toNumber from "
|
|
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", () => {
|
|
@@ -7,7 +7,6 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
7
7
|
expect(toNumber("")).toEqual("");
|
|
8
8
|
expect(toNumber("string")).toEqual("string");
|
|
9
9
|
expect(toNumber("e89794659669cb7bb967db73a7ea6889c3891727")).toEqual("e89794659669cb7bb967db73a7ea6889c3891727");
|
|
10
|
-
|
|
11
10
|
});
|
|
12
11
|
it("should not parse number with spaces or comma", () => {
|
|
13
12
|
expect(toNumber("12,12")).toEqual("12,12");
|
|
@@ -24,10 +23,10 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
24
23
|
it("should parse hexadecimal values", () => {
|
|
25
24
|
expect(toNumber("0x2f")).toEqual(47);
|
|
26
25
|
expect(toNumber("-0x2f")).toEqual(-47);
|
|
27
|
-
expect(toNumber("0x2f", { hex
|
|
28
|
-
expect(toNumber("-0x2f", { hex
|
|
29
|
-
expect(toNumber("0x2f", { hex
|
|
30
|
-
expect(toNumber("-0x2f", { hex
|
|
26
|
+
expect(toNumber("0x2f", { hex: true })).toEqual(47);
|
|
27
|
+
expect(toNumber("-0x2f", { hex: true })).toEqual(-47);
|
|
28
|
+
expect(toNumber("0x2f", { hex: false })).toEqual("0x2f");
|
|
29
|
+
expect(toNumber("-0x2f", { hex: false })).toEqual("-0x2f");
|
|
31
30
|
})
|
|
32
31
|
it("should not parse strings with 0x embedded", () => {
|
|
33
32
|
expect(toNumber("0xzz")).toEqual("0xzz");
|
|
@@ -40,54 +39,54 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
40
39
|
expect(toNumber("00")).toEqual(0);
|
|
41
40
|
expect(toNumber("00.0")).toEqual(0);
|
|
42
41
|
|
|
43
|
-
expect(toNumber("0",{ leadingZeros
|
|
44
|
-
expect(toNumber("00",{ leadingZeros
|
|
45
|
-
expect(toNumber("00.0",{ leadingZeros
|
|
42
|
+
expect(toNumber("0", { leadingZeros: false })).toEqual(0);
|
|
43
|
+
expect(toNumber("00", { leadingZeros: false })).toEqual("00");
|
|
44
|
+
expect(toNumber("00.0", { leadingZeros: false })).toEqual("00.0");
|
|
46
45
|
|
|
47
46
|
expect(toNumber("06")).toEqual(6);
|
|
48
|
-
expect(toNumber("06", { leadingZeros
|
|
49
|
-
expect(toNumber("06", { leadingZeros
|
|
47
|
+
expect(toNumber("06", { leadingZeros: true })).toEqual(6);
|
|
48
|
+
expect(toNumber("06", { leadingZeros: false })).toEqual("06");
|
|
50
49
|
|
|
51
50
|
expect(toNumber("006")).toEqual(6);
|
|
52
|
-
expect(toNumber("006", { leadingZeros
|
|
53
|
-
expect(toNumber("006", { leadingZeros
|
|
51
|
+
expect(toNumber("006", { leadingZeros: true })).toEqual(6);
|
|
52
|
+
expect(toNumber("006", { leadingZeros: false })).toEqual("006");
|
|
54
53
|
|
|
55
|
-
expect(toNumber("000000000000000000000000017717"
|
|
56
|
-
expect(toNumber("000000000000000000000000017717"
|
|
57
|
-
expect(toNumber("020211201030005811824")
|
|
58
|
-
expect(toNumber("0420926189200190257681175017717")
|
|
54
|
+
expect(toNumber("000000000000000000000000017717", { leadingZeros: false })).toEqual("000000000000000000000000017717");
|
|
55
|
+
expect(toNumber("000000000000000000000000017717", { leadingZeros: true })).toEqual(17717);
|
|
56
|
+
expect(toNumber("020211201030005811824")).toEqual("020211201030005811824");
|
|
57
|
+
expect(toNumber("0420926189200190257681175017717")).toEqual(4.209261892001902e+29);
|
|
59
58
|
})
|
|
60
59
|
it("invalid floating number", () => {
|
|
61
|
-
expect(toNumber("20.21.030")
|
|
62
|
-
expect(toNumber("0.21.030")
|
|
63
|
-
expect(toNumber("0.21.")
|
|
60
|
+
expect(toNumber("20.21.030")).toEqual("20.21.030");
|
|
61
|
+
expect(toNumber("0.21.030")).toEqual("0.21.030");
|
|
62
|
+
expect(toNumber("0.21.")).toEqual("0.21.");
|
|
64
63
|
});
|
|
65
64
|
it("floating point and leading zeros", () => {
|
|
66
65
|
expect(toNumber("0.")).toEqual(0);
|
|
67
66
|
expect(toNumber("+0.")).toEqual(0);
|
|
68
67
|
expect(toNumber("-0.")).toEqual(-0);
|
|
69
|
-
expect(toNumber("1.")
|
|
68
|
+
expect(toNumber("1.")).toEqual(1);
|
|
70
69
|
expect(toNumber("00.00")).toEqual(0);
|
|
71
70
|
expect(toNumber("0.06")).toEqual(0.06);
|
|
72
71
|
expect(toNumber("00.6")).toEqual(0.6);
|
|
73
72
|
expect(toNumber(".006")).toEqual(0.006);
|
|
74
73
|
expect(toNumber("6.0")).toEqual(6);
|
|
75
74
|
expect(toNumber("06.0")).toEqual(6);
|
|
76
|
-
|
|
77
|
-
expect(toNumber("0.0",
|
|
78
|
-
expect(toNumber("00.00",
|
|
79
|
-
expect(toNumber("0.06",
|
|
80
|
-
expect(toNumber("00.6",
|
|
81
|
-
expect(toNumber(".006", { leadingZeros
|
|
82
|
-
expect(toNumber("6.0"
|
|
83
|
-
expect(toNumber("06.0"
|
|
75
|
+
|
|
76
|
+
expect(toNumber("0.0", { leadingZeros: false })).toEqual(0);
|
|
77
|
+
expect(toNumber("00.00", { leadingZeros: false })).toEqual("00.00");
|
|
78
|
+
expect(toNumber("0.06", { leadingZeros: false })).toEqual(0.06);
|
|
79
|
+
expect(toNumber("00.6", { leadingZeros: false })).toEqual("00.6");
|
|
80
|
+
expect(toNumber(".006", { leadingZeros: false })).toEqual(0.006);
|
|
81
|
+
expect(toNumber("6.0", { leadingZeros: false })).toEqual(6);
|
|
82
|
+
expect(toNumber("06.0", { leadingZeros: false })).toEqual("06.0");
|
|
84
83
|
})
|
|
85
84
|
it("negative number leading zeros", () => {
|
|
86
85
|
expect(toNumber("+06")).toEqual(6);
|
|
87
86
|
expect(toNumber("-06")).toEqual(-6);
|
|
88
|
-
expect(toNumber("-06", { leadingZeros
|
|
89
|
-
expect(toNumber("-06", { leadingZeros
|
|
90
|
-
|
|
87
|
+
expect(toNumber("-06", { leadingZeros: true })).toEqual(-6);
|
|
88
|
+
expect(toNumber("-06", { leadingZeros: false })).toEqual("-06");
|
|
89
|
+
|
|
91
90
|
expect(toNumber("-0.0")).toEqual(-0);
|
|
92
91
|
expect(toNumber("-00.00")).toEqual(-0);
|
|
93
92
|
expect(toNumber("-0.06")).toEqual(-0.06);
|
|
@@ -96,47 +95,49 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
96
95
|
expect(toNumber("-6.0")).toEqual(-6);
|
|
97
96
|
expect(toNumber("-06.0")).toEqual(-6);
|
|
98
97
|
expect(toNumber("+06.0")).toEqual(6);
|
|
99
|
-
|
|
100
|
-
expect(toNumber("-0.0"
|
|
101
|
-
expect(toNumber("-00.00",
|
|
102
|
-
expect(toNumber("-0.06",
|
|
103
|
-
expect(toNumber("-00.6",
|
|
104
|
-
expect(toNumber("-.006",
|
|
105
|
-
expect(toNumber("-6.0"
|
|
106
|
-
expect(toNumber("-06.0"
|
|
98
|
+
|
|
99
|
+
expect(toNumber("-0.0", { leadingZeros: false })).toEqual(-0);
|
|
100
|
+
expect(toNumber("-00.00", { leadingZeros: false })).toEqual("-00.00");
|
|
101
|
+
expect(toNumber("-0.06", { leadingZeros: false })).toEqual(-0.06);
|
|
102
|
+
expect(toNumber("-00.6", { leadingZeros: false })).toEqual("-00.6");
|
|
103
|
+
expect(toNumber("-.006", { leadingZeros: false })).toEqual(-0.006);
|
|
104
|
+
expect(toNumber("-6.0", { leadingZeros: false })).toEqual(-6);
|
|
105
|
+
expect(toNumber("-06.0", { leadingZeros: false })).toEqual("-06.0");
|
|
107
106
|
})
|
|
108
107
|
it("long number", () => {
|
|
109
|
-
expect(toNumber("020211201030005811824")
|
|
110
|
-
expect(toNumber("20211201030005811824")
|
|
111
|
-
expect(toNumber("20.211201030005811824")
|
|
112
|
-
expect(toNumber("0.211201030005811824")
|
|
108
|
+
expect(toNumber("020211201030005811824")).toEqual("020211201030005811824");
|
|
109
|
+
expect(toNumber("20211201030005811824")).toEqual("20211201030005811824");
|
|
110
|
+
expect(toNumber("20.211201030005811824")).toEqual("20.211201030005811824");
|
|
111
|
+
expect(toNumber("0.211201030005811824")).toEqual("0.211201030005811824");
|
|
113
112
|
});
|
|
114
113
|
it("scientific notation", () => {
|
|
115
|
-
expect(toNumber("01.0e2"
|
|
116
|
-
expect(toNumber("-01.0e2"
|
|
117
|
-
expect(toNumber("01.0e2")
|
|
118
|
-
expect(toNumber("-01.0e2")
|
|
119
|
-
expect(toNumber("1.0e2")
|
|
114
|
+
expect(toNumber("01.0e2", { leadingZeros: false })).toEqual("01.0e2");
|
|
115
|
+
expect(toNumber("-01.0e2", { leadingZeros: false })).toEqual("-01.0e2");
|
|
116
|
+
expect(toNumber("01.0e2")).toEqual(100);
|
|
117
|
+
expect(toNumber("-01.0e2")).toEqual(-100);
|
|
118
|
+
expect(toNumber("1.0e2")).toEqual(100);
|
|
120
119
|
|
|
121
|
-
expect(toNumber("-1.0e2")
|
|
120
|
+
expect(toNumber("-1.0e2")).toEqual(-100);
|
|
122
121
|
expect(toNumber("1.0e-2")).toEqual(0.01);
|
|
123
|
-
|
|
124
|
-
expect(toNumber("420926189200190257681175017717")
|
|
125
|
-
expect(toNumber("420926189200190257681175017717"
|
|
126
|
-
|
|
122
|
+
|
|
123
|
+
expect(toNumber("420926189200190257681175017717")).toEqual(4.209261892001902e+29);
|
|
124
|
+
expect(toNumber("420926189200190257681175017717", { eNotation: false })).toEqual("420926189200190257681175017717");
|
|
125
|
+
|
|
127
126
|
expect(toNumber("1e-2")).toEqual(0.01);
|
|
128
127
|
expect(toNumber("1e+2")).toEqual(100);
|
|
129
128
|
expect(toNumber("1.e+2")).toEqual(100);
|
|
129
|
+
|
|
130
|
+
expect(toNumber("1.5e3", { leadingZeros: false })).toEqual(1500);
|
|
130
131
|
});
|
|
131
132
|
|
|
132
133
|
it("scientific notation with upper E", () => {
|
|
133
|
-
expect(toNumber("01.0E2"
|
|
134
|
-
expect(toNumber("-01.0E2"
|
|
135
|
-
expect(toNumber("01.0E2")
|
|
136
|
-
expect(toNumber("-01.0E2")
|
|
137
|
-
expect(toNumber("1.0E2")
|
|
134
|
+
expect(toNumber("01.0E2", { leadingZeros: false })).toEqual("01.0E2");
|
|
135
|
+
expect(toNumber("-01.0E2", { leadingZeros: false })).toEqual("-01.0E2");
|
|
136
|
+
expect(toNumber("01.0E2")).toEqual(100);
|
|
137
|
+
expect(toNumber("-01.0E2")).toEqual(-100);
|
|
138
|
+
expect(toNumber("1.0E2")).toEqual(100);
|
|
138
139
|
|
|
139
|
-
expect(toNumber("-1.0E2")
|
|
140
|
+
expect(toNumber("-1.0E2")).toEqual(-100);
|
|
140
141
|
expect(toNumber("1.0E-2")).toEqual(0.01);
|
|
141
142
|
|
|
142
143
|
expect(toNumber("E-2")).toEqual("E-2");
|
|
@@ -144,16 +145,16 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
144
145
|
expect(toNumber("0E2")).toEqual(0);
|
|
145
146
|
expect(toNumber("-0E2")).toEqual(-0);
|
|
146
147
|
expect(toNumber("00E2")).toEqual("00E2");
|
|
147
|
-
expect(toNumber("00E2",
|
|
148
|
+
expect(toNumber("00E2", { leadingZeros: false })).toEqual("00E2");
|
|
148
149
|
});
|
|
149
|
-
|
|
150
|
+
|
|
150
151
|
it("should skip matching pattern", () => {
|
|
151
152
|
expect(toNumber("0", { skipLike: /.*/ })).toEqual("0");
|
|
152
|
-
expect(toNumber("+12", { skipLike: /\+[0-9]{10}/}
|
|
153
|
-
expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/}
|
|
154
|
-
expect(toNumber("12+1212121212", { skipLike: /\+[0-9]{10}/}
|
|
155
|
-
expect(toNumber("+1212121212")
|
|
156
|
-
expect(toNumber("+1212121212", { skipLike: /\+[0-9]{10}/}
|
|
153
|
+
expect(toNumber("+12", { skipLike: /\+[0-9]{10}/ })).toEqual(12);
|
|
154
|
+
expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/ })).toEqual("12+12");
|
|
155
|
+
expect(toNumber("12+1212121212", { skipLike: /\+[0-9]{10}/ })).toEqual("12+1212121212");
|
|
156
|
+
expect(toNumber("+1212121212")).toEqual(1212121212);
|
|
157
|
+
expect(toNumber("+1212121212", { skipLike: /\+[0-9]{10}/ })).toEqual("+1212121212");
|
|
157
158
|
})
|
|
158
159
|
it("should not change string if not number", () => {
|
|
159
160
|
expect(toNumber("+12 12")).toEqual("+12 12");
|
|
@@ -162,7 +163,7 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
162
163
|
it("should ignore sorrounded spaces ", () => {
|
|
163
164
|
expect(toNumber(" +1212 ")).toEqual(1212);
|
|
164
165
|
})
|
|
165
|
-
|
|
166
|
+
|
|
166
167
|
it("negative numbers", () => {
|
|
167
168
|
expect(toNumber("+1212")).toEqual(1212);
|
|
168
169
|
expect(toNumber("+12.12")).toEqual(12.12);
|
package/tests/temp.js
ADDED
package/test.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import toNumber from "./strnum.js";
|
|
2
|
-
|
|
3
|
-
describe("Should convert all the valid numeric strings to number", () => {
|
|
4
|
-
it("should return undefined, null, empty string, or non-numeric as it is", () => {
|
|
5
|
-
// expect(toNumber("+ 90")).toEqual("+ 90");
|
|
6
|
-
// expect(toNumber("- 90")).toEqual("- 90");
|
|
7
|
-
expect(toNumber("-10E2")).toEqual(100);
|
|
8
|
-
});
|
|
9
|
-
});
|