🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

strnum

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

strnum - npm Package Compare versions

Comparing version

to
2.1.0

algo.stflow

5

CHANGELOG.md
**2.1.0 / 2025-05-01**
- fix e-notation
- to return string when invalid enotation is found. Eg `E24`
- to return valid number when only leading zero before e char is present
**2.0.5 / 2025-02-27**

@@ -3,0 +8,0 @@ - changes done in 1.1.2

2

package.json
{
"name": "strnum",
"version": "2.0.5",
"version": "2.1.0",
"description": "Parse String to Number based on configuration",

@@ -5,0 +5,0 @@ "type": "module",

@@ -28,19 +28,4 @@ const hexRegex = /^[-+]?0x[a-fA-F0-9]+$/;

// return Number.parseInt(val, 8);
}else if (trimmedStr.search(/[eE]/)!== -1) { //eNotation
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
trimmedStr = (notation[1] || "") + notation[3];
}else{
if(notation[2] === "0" && notation[3][0]=== "."){ //valid number
}else{
return str;
}
}
return options.eNotation ? Number(trimmedStr) : str;
}else{
return str;
}
}else if (trimmedStr.search(/.+[eE].+/)!== -1) { //eNotation
return resolveEnotation(str,trimmedStr,options);
// }else if (options.parseBin && binRegex.test(str)) {

@@ -53,29 +38,38 @@ // return Number.parseInt(val, 2);

if(match){
const sign = match[1];
const sign = match[1] || "";
const leadingZeros = match[2];
let numTrimmedByZeros = trimZeros(match[3]); //complete num without leading zeros
const decimalAdjacentToLeadingZeros = sign ? // 0., -00., 000.
str[leadingZeros.length+1] === "."
: str[leadingZeros.length] === ".";
//trim ending zeros for floating number
if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123
else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123
else if(options.leadingZeros && leadingZeros===str) return 0; //00
if(!options.leadingZeros //leading zeros are not allowed
&& (leadingZeros.length > 1
|| (leadingZeros.length === 1 && !decimalAdjacentToLeadingZeros))){
// 00, 00.3, +03.24, 03, 03.24
return str;
}
else{//no leading zeros or leading zeros are allowed
const num = Number(trimmedStr);
const numStr = "" + num;
const parsedStr = String(num);
if(numStr.search(/[eE]/) !== -1){ //given number is long and parsed to eNotation
if( num === 0 || num === -0) return num;
if(parsedStr.search(/[eE]/) !== -1){ //given number is long and parsed to eNotation
if(options.eNotation) return num;
else return str;
}else if(trimmedStr.indexOf(".") !== -1){ //floating number
if(numStr === "0" && (numTrimmedByZeros === "") ) return num; //0.0
else if(numStr === numTrimmedByZeros) return num; //0.456. 0.79000
else if( sign && numStr === "-"+numTrimmedByZeros) return num;
if(parsedStr === "0") return num; //0.0
else if(parsedStr === numTrimmedByZeros) return num; //0.456. 0.79000
else if( parsedStr === `${sign}${numTrimmedByZeros}`) return num;
else return str;
}
let n = leadingZeros? numTrimmedByZeros : trimmedStr;
if(leadingZeros){
return (numTrimmedByZeros === numStr) || (sign+numTrimmedByZeros === numStr) ? num : str
// -009 => -9
return (n === parsedStr) || (sign+n === parsedStr) ? num : str
}else {
return (trimmedStr === numStr) || (trimmedStr === sign+numStr) ? num : str
// +9
return (n === parsedStr) || (n === sign+parsedStr) ? num : str
}

@@ -89,2 +83,28 @@ }

const eNotationRegx = /^([-+])?(0*)(\d*(\.\d*)?[eE][-\+]?\d+)$/;
function resolveEnotation(str,trimmedStr,options){
if(!options.eNotation) return str;
const notation = trimmedStr.match(eNotationRegx);
if(notation){
let sign = notation[1] || "";
const eChar = notation[3].indexOf("e") === -1 ? "E" : "e";
const leadingZeros = notation[2];
const eAdjacentToLeadingZeros = sign ? // 0E.
str[leadingZeros.length+1] === eChar
: str[leadingZeros.length] === eChar;
if(leadingZeros.length > 1 && eAdjacentToLeadingZeros) return str;
else if(leadingZeros.length === 1
&& (notation[3].startsWith(`.${eChar}`) || notation[3][0] === eChar)){
return Number(trimmedStr);
}else if(options.leadingZeros && !eAdjacentToLeadingZeros){ //accept with leading zeros
//remove leading 0s
trimmedStr = (notation[1] || "") + notation[3];
return Number(trimmedStr);
}else return str;
}else{
return str;
}
}
/**

@@ -100,3 +120,3 @@ *

else if(numStr[0] === ".") numStr = "0"+numStr;
else if(numStr[numStr.length-1] === ".") numStr = numStr.substr(0,numStr.length-1);
else if(numStr[numStr.length-1] === ".") numStr = numStr.substring(0,numStr.length-1);
return numStr;

@@ -103,0 +123,0 @@ }

@@ -98,2 +98,3 @@ import toNumber from "./strnum.js";

expect(toNumber("-06.0")).toEqual(-6);
expect(toNumber("+06.0")).toEqual(6);

@@ -141,2 +142,9 @@ expect(toNumber("-0.0" , { leadingZeros : false})).toEqual(-0);

expect(toNumber("1.0E-2")).toEqual(0.01);
expect(toNumber("E-2")).toEqual("E-2");
expect(toNumber("E2")).toEqual("E2");
expect(toNumber("0E2")).toEqual(0);
expect(toNumber("-0E2")).toEqual(-0);
expect(toNumber("00E2")).toEqual("00E2");
expect(toNumber("00E2", { leadingZeros : false})).toEqual("00E2");
});

@@ -143,0 +151,0 @@