strnum 2.0.3 → 2.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/CHANGELOG.md +13 -1
- package/package.json +1 -1
- package/strnum.js +5 -5
- package/strnum.test.js +7 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
|
|
2
|
-
**2.0.
|
|
2
|
+
**2.0.5 / 2025-02-27**
|
|
3
|
+
- changes done in 1.1.2
|
|
4
|
+
|
|
5
|
+
**1.1.2 / 2025-02-27**
|
|
6
|
+
- fix skiplike for 0
|
|
7
|
+
|
|
8
|
+
**1.1.1 / 2025-02-21**
|
|
9
|
+
- All recent fixes of version 2
|
|
10
|
+
|
|
11
|
+
**2.0.4 / 2025-02-20**
|
|
12
|
+
- remove console log
|
|
13
|
+
|
|
14
|
+
**2.0.3 / 2025-02-20**
|
|
3
15
|
- fix for string which are falsly identified as e-notation
|
|
4
16
|
|
|
5
17
|
**2.0.1 / 2025-02-20**
|
package/package.json
CHANGED
package/strnum.js
CHANGED
|
@@ -16,11 +16,11 @@ 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
|
-
|
|
20
|
-
|
|
19
|
+
|
|
21
20
|
let trimmedStr = str.trim();
|
|
22
|
-
|
|
21
|
+
|
|
23
22
|
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
23
|
+
else if(str==="0") return 0;
|
|
24
24
|
else if (options.hex && hexRegex.test(trimmedStr)) {
|
|
25
25
|
return parse_int(trimmedStr, 16);
|
|
26
26
|
// }else if (options.oct && octRegex.test(str)) {
|
|
@@ -29,7 +29,7 @@ export default function toNumber(str, options = {}){
|
|
|
29
29
|
const notation = trimmedStr.match(/^([-\+])?(0*)([0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+)$/);
|
|
30
30
|
// +00.123 => [ , '+', '00', '.123', ..
|
|
31
31
|
if(notation){
|
|
32
|
-
console.log(notation)
|
|
32
|
+
// console.log(notation)
|
|
33
33
|
if(options.leadingZeros){ //accept with leading zeros
|
|
34
34
|
trimmedStr = (notation[1] || "") + notation[3];
|
|
35
35
|
}else{
|
|
@@ -106,4 +106,4 @@ function parse_int(numStr, base){
|
|
|
106
106
|
else if(Number.parseInt) return Number.parseInt(numStr, base);
|
|
107
107
|
else if(window && window.parseInt) return window.parseInt(numStr, base);
|
|
108
108
|
else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
|
|
109
|
-
}
|
|
109
|
+
}
|
package/strnum.test.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import toNumber from "./strnum.js";
|
|
2
2
|
|
|
3
3
|
describe("Should convert all the valid numeric strings to number", () => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
it("should return undefined, null, empty string, or non-numeric as it is", () => {
|
|
5
|
+
expect(toNumber(undefined)).not.toBeDefined();
|
|
6
|
+
expect(toNumber(null)).toEqual(null);
|
|
7
|
+
expect(toNumber("")).toEqual("");
|
|
8
|
+
expect(toNumber("string")).toEqual("string");
|
|
9
9
|
expect(toNumber("e89794659669cb7bb967db73a7ea6889c3891727")).toEqual("e89794659669cb7bb967db73a7ea6889c3891727");
|
|
10
|
+
|
|
10
11
|
});
|
|
11
12
|
it("should not parse number with spaces or comma", () => {
|
|
12
13
|
expect(toNumber("12,12")).toEqual("12,12");
|
|
@@ -139,6 +140,7 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
139
140
|
});
|
|
140
141
|
|
|
141
142
|
it("should skip matching pattern", () => {
|
|
143
|
+
expect(toNumber("0", { skipLike: /.*/ })).toEqual("0");
|
|
142
144
|
expect(toNumber("+12", { skipLike: /\+[0-9]{10}/} )).toEqual(12);
|
|
143
145
|
expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/} )).toEqual("12+12");
|
|
144
146
|
expect(toNumber("12+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("12+1212121212");
|