strnum 1.1.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
1
 
2
+ **2.0.1 / 2025-02-20**
3
+ - fix: handle only zeros
4
+ - fix: return original string when NaN
5
+
6
+ **2.0.0 / 2025-02-20**
7
+ - Migrating to ESM modules. No functional change
8
+
2
9
  **1.1.0 / 2025-02-20**
3
10
  - fix (#9): support missing floating point and e notations
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "strnum",
3
- "version": "1.1.0",
3
+ "version": "2.0.1",
4
4
  "description": "Parse String to Number based on configuration",
5
+ "type": "module",
5
6
  "main": "strnum.js",
6
7
  "scripts": {
7
8
  "test": "jasmine strnum.test.js"
@@ -19,6 +20,6 @@
19
20
  "author": "Amit Gupta (https://amitkumargupta.work/)",
20
21
  "license": "MIT",
21
22
  "devDependencies": {
22
- "jasmine": "^3.10.0"
23
+ "jasmine": "^5.6.0"
23
24
  }
24
25
  }
package/strnum.js CHANGED
@@ -13,10 +13,11 @@ const consider = {
13
13
  //skipLike: /regex/
14
14
  };
15
15
 
16
- function toNumber(str, options = {}){
16
+ export default function toNumber(str, options = {}){
17
17
  options = Object.assign({}, consider, options );
18
18
  if(!str || typeof str !== "string" ) return str;
19
-
19
+ else if(str==="0") return 0;
20
+
20
21
  let trimmedStr = str.trim();
21
22
 
22
23
  if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
@@ -33,12 +34,12 @@ function toNumber(str, options = {}){
33
34
  }else{
34
35
  if(notation[2] === "0" && notation[3][0]=== "."){ //valid number
35
36
  }else{
36
- return trimmedStr;
37
+ return str;
37
38
  }
38
39
  }
39
- return options.eNotation ? Number(trimmedStr) : trimmedStr;
40
+ return options.eNotation ? Number(trimmedStr) : str;
40
41
  }else{
41
- return trimmedStr;
42
+ return str;
42
43
  }
43
44
  // }else if (options.parseBin && binRegex.test(str)) {
44
45
  // return Number.parseInt(val, 2);
@@ -54,6 +55,8 @@ function toNumber(str, options = {}){
54
55
 
55
56
  if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123
56
57
  else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123
58
+ else if(options.leadingZeros && leadingZeros===str) return 0; //00
59
+
57
60
  else{//no leading zeros or leading zeros are allowed
58
61
  const num = Number(trimmedStr);
59
62
  const numStr = "" + num;
@@ -103,4 +106,3 @@ function parse_int(numStr, base){
103
106
  else if(window && window.parseInt) return window.parseInt(numStr, base);
104
107
  else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
105
108
  }
106
- module.exports = toNumber
package/strnum.test.js CHANGED
@@ -1,4 +1,4 @@
1
- const toNumber = require("./strnum");
1
+ import toNumber from "./strnum.js";
2
2
 
3
3
  describe("Should convert all the valid numeric strings to number", () => {
4
4
  it("should return undefined, null, empty string, or non-numeric as it is", () => {
@@ -34,6 +34,14 @@ describe("Should convert all the valid numeric strings to number", () => {
34
34
  expect(toNumber("JVBERi0xLjMNCiXi48")).toEqual("JVBERi0xLjMNCiXi48");
35
35
  })
36
36
  it("leading zeros", () => {
37
+ expect(toNumber("0")).toEqual(0);
38
+ expect(toNumber("00")).toEqual(0);
39
+ expect(toNumber("00.0")).toEqual(0);
40
+
41
+ expect(toNumber("0",{ leadingZeros : false})).toEqual(0);
42
+ expect(toNumber("00",{ leadingZeros : false})).toEqual("00");
43
+ expect(toNumber("00.0",{ leadingZeros : false})).toEqual("00.0");
44
+
37
45
  expect(toNumber("06")).toEqual(6);
38
46
  expect(toNumber("06", { leadingZeros : true})).toEqual(6);
39
47
  expect(toNumber("06", { leadingZeros : false})).toEqual("06");
@@ -51,12 +59,12 @@ describe("Should convert all the valid numeric strings to number", () => {
51
59
  expect(toNumber("20.21.030") ).toEqual("20.21.030");
52
60
  expect(toNumber("0.21.030") ).toEqual("0.21.030");
53
61
  expect(toNumber("0.21.") ).toEqual("0.21.");
62
+ });
63
+ it("floating point and leading zeros", () => {
54
64
  expect(toNumber("0.")).toEqual(0);
55
65
  expect(toNumber("+0.")).toEqual(0);
56
66
  expect(toNumber("-0.")).toEqual(-0);
57
67
  expect(toNumber("1.") ).toEqual(1);
58
- });
59
- it("floating point and leading zeros", () => {
60
68
  expect(toNumber("00.00")).toEqual(0);
61
69
  expect(toNumber("0.06")).toEqual(0.06);
62
70
  expect(toNumber("00.6")).toEqual(0.6);
package/test.js DELETED
@@ -1,6 +0,0 @@
1
- const toNumber = require("./strnum");
2
-
3
- // console.log(toNumber("01.0e2"))
4
- console.log(toNumber("-06"))
5
- // console.log(toNumber("+01.0e2"))
6
- // console.log(toNumber("+001.0e2"))