strnum
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "strnum", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Parse String to Number based on configuration", | ||
"main": "strnum.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jasmine strnum.test.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "keywords": [ |
@@ -76,2 +76,5 @@ # strnum | ||
toNumber("1.0e-2"); //0.01) | ||
toNumber("+1212121212"); // 1212121212 | ||
toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} )); //"+1212121212" | ||
``` |
const hexRegex = /0x[a-zA-Z0-9]+/; | ||
const numRegex = /^(\-)?(0*)(\.[0-9]+(e\-?[0-9]+)?|[0-9]+(\.[0-9]+(e\-?[0-9]+)?)?)$/; | ||
const numRegex = /^([\-\+])?(0*)(\.[0-9]+(e\-?[0-9]+)?|[0-9]+(\.[0-9]+(e\-?[0-9]+)?)?)$/; | ||
// const octRegex = /0x[a-z0-9]+/; | ||
@@ -9,3 +9,4 @@ // const binRegex = /0x[a-z0-9]+/; | ||
leadingZeros: true, | ||
decimalPoint: "\." | ||
decimalPoint: "\.", | ||
//skipLike: /regex/ | ||
}; | ||
@@ -23,2 +24,3 @@ | ||
if(!str || typeof str !== "string" ) return str; | ||
else if(options.skipLike !== undefined && options.skipLike.test(str)) return str; | ||
else if (options.hex && hexRegex.test(str)) { | ||
@@ -25,0 +27,0 @@ return Number.parseInt(str, 16); |
@@ -16,2 +16,7 @@ const toNumber = require("./strnum"); | ||
}) | ||
it("should consider + sign", () => { | ||
expect(toNumber("+12")).toEqual(12); | ||
expect(toNumber("12+12")).toEqual("12+12"); | ||
expect(toNumber("1212+")).toEqual("1212+"); | ||
}) | ||
it("should parse hexaDecimal values", () => { | ||
@@ -88,2 +93,10 @@ expect(toNumber("0x2f")).toEqual(47); | ||
it("should skip matching pattern", () => { | ||
expect(toNumber("+12", { skipLike: /\+[0-9]{10}/} )).toEqual(12); | ||
expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/} )).toEqual("12+12"); | ||
expect(toNumber("12+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("12+1212121212"); | ||
expect(toNumber("+1212121212") ).toEqual(1212121212); | ||
expect(toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("+1212121212"); | ||
}) | ||
}); |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
10978
133
0
79