New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

strnum

Package Overview
Dependencies
Maintainers
0
Versions
12
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 1.0.5 to 1.1.0

CHANGELOG.md

2

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

@@ -5,0 +5,0 @@ "main": "strnum.js",

# strnum
Parse string into Number based on configuration
## Users
<a href="https://github.com/aws-amplify" target="_blank"><img src="https://avatars.githubusercontent.com/u/41077760?s=100&v=4"></a>
<a href="https://github.com/astrapay" target="_blank"><img src="https://avatars.githubusercontent.com/u/90901882?s=100&v=4"></a>
<a href="https://github.com/process-analytics" target="_blank"><img src="https://avatars.githubusercontent.com/u/60110287?s=100&v=4"></a>
<a href="https://github.com/NaturalIntelligence" target="_blank"><img src="https://avatars.githubusercontent.com/u/16322633?s=100&v=4"></a>
Many React Native projects and plugins
## Usage
```bash

@@ -83,5 +93,6 @@ npm install strnum

```js
hex : true, //when hexadecimal string should be parsed
hex: true, //when hexadecimal string should be parsed
leadingZeros: true, //when number with leading zeros like 08 should be parsed. 0.0 is not impacted
eNotation: true //when number with eNotation or number parsed in eNotation should be considered
```
eNotation: true, //when number with eNotation or number parsed in eNotation should be considered
skipLike: /regex/ //when string should not be parsed when it matches the specified regular expression
```
const hexRegex = /^[-+]?0x[a-fA-F0-9]+$/;
const numRegex = /^([\-\+])?(0*)(\.[0-9]+([eE]\-?[0-9]+)?|[0-9]+(\.[0-9]+([eE]\-?[0-9]+)?)?)$/;
// const octRegex = /0x[a-z0-9]+/;
const numRegex = /^([\-\+])?(0*)([0-9]*(\.[0-9]*)?)$/;
// const octRegex = /^0x[a-z0-9]+/;
// const binRegex = /0x[a-z0-9]+/;
//polyfill
if (!Number.parseInt && window.parseInt) {
Number.parseInt = window.parseInt;
}
if (!Number.parseFloat && window.parseFloat) {
Number.parseFloat = window.parseFloat;
}
const consider = {
hex : true,
// oct: false,
leadingZeros: true,
decimalPoint: "\.",
eNotation: true
eNotation: true,
//skipLike: /regex/

@@ -25,9 +17,2 @@ };

function toNumber(str, options = {}){
// const options = Object.assign({}, consider);
// if(opt.leadingZeros === false){
// options.leadingZeros = false;
// }else if(opt.hex === false){
// options.hex = false;
// }
options = Object.assign({}, consider, options );

@@ -37,11 +22,24 @@ if(!str || typeof str !== "string" ) return str;

let trimmedStr = str.trim();
// if(trimmedStr === "0.0") return 0;
// else if(trimmedStr === "+0.0") return 0;
// else if(trimmedStr === "-0.0") return -0;
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
else if (options.hex && hexRegex.test(trimmedStr)) {
return Number.parseInt(trimmedStr, 16);
// } else if (options.parseOct && octRegex.test(str)) {
return parse_int(trimmedStr, 16);
// }else if (options.oct && octRegex.test(str)) {
// 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){
if(options.leadingZeros){ //accept with leading zeros
trimmedStr = (notation[1] || "") + notation[3];
}else{
if(notation[2] === "0" && notation[3][0]=== "."){ //valid number
}else{
return trimmedStr;
}
}
return options.eNotation ? Number(trimmedStr) : trimmedStr;
}else{
return trimmedStr;
}
// }else if (options.parseBin && binRegex.test(str)) {

@@ -52,2 +50,3 @@ // return Number.parseInt(val, 2);

const match = numRegex.exec(trimmedStr);
// +00.123 => [ , '+', '00', '.123', ..
if(match){

@@ -59,3 +58,2 @@ const sign = match[1];

const eNotation = match[4] || match[6];
if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123

@@ -66,16 +64,7 @@ else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123

const numStr = "" + num;
if(numStr.search(/[eE]/) !== -1){ //given number is long and parsed to eNotation
if(options.eNotation) return num;
else return str;
}else if(eNotation){ //given number has enotation
if(options.eNotation) return num;
else return str;
}else if(trimmedStr.indexOf(".") !== -1){ //floating number
// const decimalPart = match[5].substr(1);
// const intPart = trimmedStr.substr(0,trimmedStr.indexOf("."));
// const p = numStr.indexOf(".");
// const givenIntPart = numStr.substr(0,p);
// const givenDecPart = numStr.substr(p+1);
if(numStr === "0" && (numTrimmedByZeros === "") ) return num; //0.0

@@ -88,22 +77,7 @@ else if(numStr === numTrimmedByZeros) return num; //0.456. 0.79000

if(leadingZeros){
// if(numTrimmedByZeros === numStr){
// if(options.leadingZeros) return num;
// else return str;
// }else return str;
if(numTrimmedByZeros === numStr) return num;
else if(sign+numTrimmedByZeros === numStr) return num;
else return str;
return (numTrimmedByZeros === numStr) || (sign+numTrimmedByZeros === numStr) ? num : str
}else {
return (trimmedStr === numStr) || (trimmedStr === sign+numStr) ? num : str
}
if(trimmedStr === numStr) return num;
else if(trimmedStr === sign+numStr) return num;
// else{
// //number with +/- sign
// trimmedStr.test(/[-+][0-9]);
// }
return str;
}
// else if(!eNotation && trimmedStr && trimmedStr !== Number(trimmedStr) ) return str;
}else{ //non-numeric string

@@ -130,2 +104,10 @@ return str;

}
function parse_int(numStr, base){
//polyfill
if(parseInt) return parseInt(numStr, base);
else if(Number.parseInt) return Number.parseInt(numStr, base);
else if(window && window.parseInt) return window.parseInt(numStr, base);
else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
}
module.exports = toNumber

@@ -54,7 +54,8 @@ const toNumber = require("./strnum");

expect(toNumber("0.21.") ).toEqual("0.21.");
expect(toNumber("0.") ).toEqual("0.");
expect(toNumber("1.") ).toEqual("1.");
expect(toNumber("0.")).toEqual(0);
expect(toNumber("+0.")).toEqual(0);
expect(toNumber("-0.")).toEqual(-0);
expect(toNumber("1.") ).toEqual(1);
});
it("floating point and leading zeros", () => {
expect(toNumber("0.0")).toEqual(0);
expect(toNumber("00.00")).toEqual(0);

@@ -112,6 +113,9 @@ expect(toNumber("0.06")).toEqual(0.06);

expect(toNumber("1.0e-2")).toEqual(0.01);
expect(toNumber("420926189200190257681175017717") ).toEqual(4.209261892001902e+29);
expect(toNumber("420926189200190257681175017717" , { eNotation: false} )).toEqual("420926189200190257681175017717");
expect(toNumber("1e-2")).toEqual(0.01);
expect(toNumber("1e+2")).toEqual(100);
expect(toNumber("1.e+2")).toEqual(100);
});

@@ -118,0 +122,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc