Skip to content

Commit 709df19

Browse files
authored
Merge pull request #5 from strmer15/patch-1
Fix the hex regex
2 parents a0bf6e5 + 09dd8ab commit 709df19

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

strnum.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const hexRegex = /0x[a-zA-Z0-9]+/;
1+
const hexRegex = /^[-+]?0x[a-fA-F0-9]+$/;
22
const numRegex = /^([\-\+])?(0*)(\.[0-9]+(e\-?[0-9]+)?|[0-9]+(\.[0-9]+(e\-?[0-9]+)?)?)$/;
33
// const octRegex = /0x[a-z0-9]+/;
44
// const binRegex = /0x[a-z0-9]+/;
@@ -47,4 +47,4 @@ function toNumber(str, options = {}){
4747
}
4848
}
4949

50-
module.exports = toNumber
50+
module.exports = toNumber

strnum.test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ describe("Should convert all the valid numeric strings to number", () => {
1818
expect(toNumber("12+12")).toEqual("12+12");
1919
expect(toNumber("1212+")).toEqual("1212+");
2020
})
21-
it("should parse hexaDecimal values", () => {
21+
it("should parse hexadecimal values", () => {
2222
expect(toNumber("0x2f")).toEqual(47);
2323
expect(toNumber("-0x2f")).toEqual(-47);
2424
expect(toNumber("0x2f", { hex : true})).toEqual(47);
2525
expect(toNumber("-0x2f", { hex : true})).toEqual(-47);
2626
expect(toNumber("0x2f", { hex : false})).toEqual("0x2f");
2727
expect(toNumber("-0x2f", { hex : false})).toEqual("-0x2f");
2828
})
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+
})
2935
it("leading zeros", () => {
3036
expect(toNumber("06")).toEqual(6);
3137
expect(toNumber("06", { leadingZeros : true})).toEqual(6);
@@ -103,5 +109,4 @@ describe("Should convert all the valid numeric strings to number", () => {
103109
it("should ignore sorrounded spaces ", () => {
104110
expect(toNumber(" +1212 ")).toEqual(1212);
105111
})
106-
107-
});
112+
});

0 commit comments

Comments
 (0)