Comparing version 1.1.0 to 1.1.1
**2.0.4 / 2025-02-20** | ||
- remove console log | ||
**2.0.3 / 2025-02-20** | ||
- fix for string which are falsly identified as e-notation | ||
**2.0.1 / 2025-02-20** | ||
- fix: handle only zeros | ||
- fix: return original string when NaN | ||
**2.0.0 / 2025-02-20** | ||
- Migrating to ESM modules. No functional change | ||
**1.1.0 / 2025-02-20** | ||
- fix (#9): support missing floating point and e notations |
{ | ||
"name": "strnum", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Parse String to Number based on configuration", | ||
@@ -21,5 +21,11 @@ "main": "strnum.js", | ||
"license": "MIT", | ||
"funding": [ | ||
{ | ||
"type": "github", | ||
"url": "https://github.com/sponsors/NaturalIntelligence" | ||
} | ||
], | ||
"devDependencies": { | ||
"jasmine": "^3.10.0" | ||
"jasmine": "^5.6.0" | ||
} | ||
} |
@@ -19,3 +19,4 @@ const hexRegex = /^[-+]?0x[a-fA-F0-9]+$/; | ||
if(!str || typeof str !== "string" ) return str; | ||
else if(str==="0") return 0; | ||
let trimmedStr = str.trim(); | ||
@@ -29,5 +30,6 @@ | ||
}else if (trimmedStr.search(/[eE]/)!== -1) { //eNotation | ||
const notation = trimmedStr.match(/^([-\+])?(0*)([0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+)/); | ||
const notation = trimmedStr.match(/^([-\+])?(0*)([0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+)$/); | ||
// +00.123 => [ , '+', '00', '.123', .. | ||
if(notation){ | ||
// console.log(notation) | ||
if(options.leadingZeros){ //accept with leading zeros | ||
@@ -38,8 +40,8 @@ trimmedStr = (notation[1] || "") + notation[3]; | ||
}else{ | ||
return trimmedStr; | ||
return str; | ||
} | ||
} | ||
return options.eNotation ? Number(trimmedStr) : trimmedStr; | ||
return options.eNotation ? Number(trimmedStr) : str; | ||
}else{ | ||
return trimmedStr; | ||
return str; | ||
} | ||
@@ -60,2 +62,4 @@ // }else if (options.parseBin && binRegex.test(str)) { | ||
else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123 | ||
else if(options.leadingZeros && leadingZeros===str) return 0; //00 | ||
else{//no leading zeros or leading zeros are allowed | ||
@@ -110,2 +114,3 @@ const num = Number(trimmedStr); | ||
} | ||
module.exports = toNumber | ||
module.exports = toNumber; |
@@ -1,2 +0,2 @@ | ||
const toNumber = require("./strnum"); | ||
const toNumber = require("./strnum.js"); | ||
@@ -9,2 +9,3 @@ describe("Should convert all the valid numeric strings to number", () => { | ||
expect(toNumber("string")).toEqual("string"); | ||
expect(toNumber("e89794659669cb7bb967db73a7ea6889c3891727")).toEqual("e89794659669cb7bb967db73a7ea6889c3891727"); | ||
}); | ||
@@ -38,2 +39,10 @@ it("should not parse number with spaces or comma", () => { | ||
it("leading zeros", () => { | ||
expect(toNumber("0")).toEqual(0); | ||
expect(toNumber("00")).toEqual(0); | ||
expect(toNumber("00.0")).toEqual(0); | ||
expect(toNumber("0",{ leadingZeros : false})).toEqual(0); | ||
expect(toNumber("00",{ leadingZeros : false})).toEqual("00"); | ||
expect(toNumber("00.0",{ leadingZeros : false})).toEqual("00.0"); | ||
expect(toNumber("06")).toEqual(6); | ||
@@ -56,2 +65,4 @@ expect(toNumber("06", { leadingZeros : true})).toEqual(6); | ||
expect(toNumber("0.21.") ).toEqual("0.21."); | ||
}); | ||
it("floating point and leading zeros", () => { | ||
expect(toNumber("0.")).toEqual(0); | ||
@@ -61,4 +72,2 @@ expect(toNumber("+0.")).toEqual(0); | ||
expect(toNumber("1.") ).toEqual(1); | ||
}); | ||
it("floating point and leading zeros", () => { | ||
expect(toNumber("00.00")).toEqual(0); | ||
@@ -65,0 +74,0 @@ expect(toNumber("0.06")).toEqual(0.06); |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
19065
269
12
7
7
7
7
7