strnum 2.2.0 → 2.2.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 +9 -2
- package/package.json +1 -1
- package/strnum.js +12 -6
- package/tests/strnum_test.js +3 -0
- package/tests/temp.js +8 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
|
|
2
|
-
**2.
|
|
3
|
-
-
|
|
2
|
+
**2.2.2 / 2026-03-23**
|
|
3
|
+
- fix for space string
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
**2.2.1 / 2026-03-19**
|
|
7
|
+
- fix false positive for eNotation when no leading zeros
|
|
8
|
+
|
|
9
|
+
**2.2.0 / 2026-02-28**
|
|
10
|
+
- support infinity
|
|
4
11
|
|
|
5
12
|
**2.1.0 / 2025-05-01**
|
|
6
13
|
- fix e-notation
|
package/package.json
CHANGED
package/strnum.js
CHANGED
|
@@ -20,8 +20,9 @@ export default function toNumber(str, options = {}) {
|
|
|
20
20
|
|
|
21
21
|
let trimmedStr = str.trim();
|
|
22
22
|
|
|
23
|
-
if (
|
|
24
|
-
else if (
|
|
23
|
+
if (trimmedStr.length === 0) return str;
|
|
24
|
+
else if (options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
25
|
+
else if (trimmedStr === "0") return 0;
|
|
25
26
|
else if (options.hex && hexRegex.test(trimmedStr)) {
|
|
26
27
|
return parse_int(trimmedStr, 16);
|
|
27
28
|
// }else if (options.oct && octRegex.test(str)) {
|
|
@@ -97,11 +98,16 @@ function resolveEnotation(str, trimmedStr, options) {
|
|
|
97
98
|
else if (leadingZeros.length === 1
|
|
98
99
|
&& (notation[3].startsWith(`.${eChar}`) || notation[3][0] === eChar)) {
|
|
99
100
|
return Number(trimmedStr);
|
|
100
|
-
} else if (
|
|
101
|
-
//
|
|
102
|
-
|
|
101
|
+
} else if (leadingZeros.length > 0) {
|
|
102
|
+
// Has leading zeros — only accept if leadingZeros option allows it
|
|
103
|
+
if (options.leadingZeros && !eAdjacentToLeadingZeros) {
|
|
104
|
+
trimmedStr = (notation[1] || "") + notation[3];
|
|
105
|
+
return Number(trimmedStr);
|
|
106
|
+
} else return str;
|
|
107
|
+
} else {
|
|
108
|
+
// No leading zeros — always valid e-notation, parse it
|
|
103
109
|
return Number(trimmedStr);
|
|
104
|
-
}
|
|
110
|
+
}
|
|
105
111
|
} else {
|
|
106
112
|
return str;
|
|
107
113
|
}
|
package/tests/strnum_test.js
CHANGED
|
@@ -5,6 +5,7 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
5
5
|
expect(toNumber(undefined)).not.toBeDefined();
|
|
6
6
|
expect(toNumber(null)).toEqual(null);
|
|
7
7
|
expect(toNumber("")).toEqual("");
|
|
8
|
+
expect(toNumber(" ")).toEqual(" ");
|
|
8
9
|
expect(toNumber("string")).toEqual("string");
|
|
9
10
|
expect(toNumber("e89794659669cb7bb967db73a7ea6889c3891727")).toEqual("e89794659669cb7bb967db73a7ea6889c3891727");
|
|
10
11
|
});
|
|
@@ -126,6 +127,8 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
126
127
|
expect(toNumber("1e-2")).toEqual(0.01);
|
|
127
128
|
expect(toNumber("1e+2")).toEqual(100);
|
|
128
129
|
expect(toNumber("1.e+2")).toEqual(100);
|
|
130
|
+
|
|
131
|
+
expect(toNumber("1.5e3", { leadingZeros: false })).toEqual(1500);
|
|
129
132
|
});
|
|
130
133
|
|
|
131
134
|
it("scientific notation with upper E", () => {
|