strnum 1.0.3 → 1.0.4
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/package.json +1 -1
- package/strnum.js +3 -3
- package/strnum.test.js +19 -3
package/package.json
CHANGED
package/strnum.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const hexRegex =
|
|
2
|
-
const numRegex = /^([\-\+])?(0*)(\.[0-9]+(
|
|
1
|
+
const hexRegex = /^[-+]?0x[a-fA-F0-9]+$/;
|
|
2
|
+
const numRegex = /^([\-\+])?(0*)(\.[0-9]+([eE]\-?[0-9]+)?|[0-9]+(\.[0-9]+([eE]\-?[0-9]+)?)?)$/;
|
|
3
3
|
// const octRegex = /0x[a-z0-9]+/;
|
|
4
4
|
// const binRegex = /0x[a-z0-9]+/;
|
|
5
5
|
|
|
@@ -47,4 +47,4 @@ function toNumber(str, options = {}){
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
module.exports = toNumber
|
|
50
|
+
module.exports = toNumber
|
package/strnum.test.js
CHANGED
|
@@ -18,7 +18,7 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
18
18
|
expect(toNumber("12+12")).toEqual("12+12");
|
|
19
19
|
expect(toNumber("1212+")).toEqual("1212+");
|
|
20
20
|
})
|
|
21
|
-
it("should parse
|
|
21
|
+
it("should parse hexadecimal values", () => {
|
|
22
22
|
expect(toNumber("0x2f")).toEqual(47);
|
|
23
23
|
expect(toNumber("-0x2f")).toEqual(-47);
|
|
24
24
|
expect(toNumber("0x2f", { hex : true})).toEqual(47);
|
|
@@ -26,6 +26,12 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
26
26
|
expect(toNumber("0x2f", { hex : false})).toEqual("0x2f");
|
|
27
27
|
expect(toNumber("-0x2f", { hex : false})).toEqual("-0x2f");
|
|
28
28
|
})
|
|
29
|
+
it("should not parse strings with 0x embedded", () => {
|
|
30
|
+
expect(toNumber("0xzz")).toEqual("0xzz");
|
|
31
|
+
expect(toNumber("iweraf0x123qwerqwer")).toEqual("iweraf0x123qwerqwer");
|
|
32
|
+
expect(toNumber("1230x55")).toEqual("1230x55");
|
|
33
|
+
expect(toNumber("JVBERi0xLjMNCiXi48")).toEqual("JVBERi0xLjMNCiXi48");
|
|
34
|
+
})
|
|
29
35
|
it("leading zeros", () => {
|
|
30
36
|
expect(toNumber("06")).toEqual(6);
|
|
31
37
|
expect(toNumber("06", { leadingZeros : true})).toEqual(6);
|
|
@@ -89,6 +95,17 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
89
95
|
expect(toNumber("1.0e-2")).toEqual(0.01);
|
|
90
96
|
});
|
|
91
97
|
|
|
98
|
+
it("scientific notation with upper E", () => {
|
|
99
|
+
expect(toNumber("01.0E2" , { leadingZeros : false})).toEqual("01.0E2");
|
|
100
|
+
expect(toNumber("-01.0E2" , { leadingZeros : false})).toEqual("-01.0E2");
|
|
101
|
+
expect(toNumber("01.0E2") ).toEqual(100);
|
|
102
|
+
expect(toNumber("-01.0E2") ).toEqual(-100);
|
|
103
|
+
expect(toNumber("1.0E2") ).toEqual(100);
|
|
104
|
+
|
|
105
|
+
expect(toNumber("-1.0E2") ).toEqual(-100);
|
|
106
|
+
expect(toNumber("1.0E-2")).toEqual(0.01);
|
|
107
|
+
});
|
|
108
|
+
|
|
92
109
|
it("should skip matching pattern", () => {
|
|
93
110
|
expect(toNumber("+12", { skipLike: /\+[0-9]{10}/} )).toEqual(12);
|
|
94
111
|
expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/} )).toEqual("12+12");
|
|
@@ -103,5 +120,4 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
103
120
|
it("should ignore sorrounded spaces ", () => {
|
|
104
121
|
expect(toNumber(" +1212 ")).toEqual(1212);
|
|
105
122
|
})
|
|
106
|
-
|
|
107
|
-
});
|
|
123
|
+
});
|