strnum 1.1.0 → 1.1.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,16 @@
1
1
 
2
+ **2.0.4 / 2025-02-20**
3
+ - remove console log
4
+
5
+ **2.0.3 / 2025-02-20**
6
+ - fix for string which are falsly identified as e-notation
7
+
8
+ **2.0.1 / 2025-02-20**
9
+ - fix: handle only zeros
10
+ - fix: return original string when NaN
11
+
12
+ **2.0.0 / 2025-02-20**
13
+ - Migrating to ESM modules. No functional change
14
+
2
15
  **1.1.0 / 2025-02-20**
3
16
  - fix (#9): support missing floating point and e notations
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strnum",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Parse String to Number based on configuration",
5
5
  "main": "strnum.js",
6
6
  "scripts": {
@@ -18,7 +18,13 @@
18
18
  },
19
19
  "author": "Amit Gupta (https://amitkumargupta.work/)",
20
20
  "license": "MIT",
21
+ "funding": [
22
+ {
23
+ "type": "github",
24
+ "url": "https://github.com/sponsors/NaturalIntelligence"
25
+ }
26
+ ],
21
27
  "devDependencies": {
22
- "jasmine": "^3.10.0"
28
+ "jasmine": "^5.6.0"
23
29
  }
24
30
  }
package/strnum.js CHANGED
@@ -16,7 +16,8 @@ const consider = {
16
16
  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;
@@ -25,20 +26,21 @@ function toNumber(str, options = {}){
25
26
  // }else if (options.oct && octRegex.test(str)) {
26
27
  // return Number.parseInt(val, 8);
27
28
  }else if (trimmedStr.search(/[eE]/)!== -1) { //eNotation
28
- const notation = trimmedStr.match(/^([-\+])?(0*)([0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+)/);
29
+ const notation = trimmedStr.match(/^([-\+])?(0*)([0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+)$/);
29
30
  // +00.123 => [ , '+', '00', '.123', ..
30
31
  if(notation){
32
+ // console.log(notation)
31
33
  if(options.leadingZeros){ //accept with leading zeros
32
34
  trimmedStr = (notation[1] || "") + notation[3];
33
35
  }else{
34
36
  if(notation[2] === "0" && notation[3][0]=== "."){ //valid number
35
37
  }else{
36
- return trimmedStr;
38
+ return str;
37
39
  }
38
40
  }
39
- return options.eNotation ? Number(trimmedStr) : trimmedStr;
41
+ return options.eNotation ? Number(trimmedStr) : str;
40
42
  }else{
41
- return trimmedStr;
43
+ return str;
42
44
  }
43
45
  // }else if (options.parseBin && binRegex.test(str)) {
44
46
  // return Number.parseInt(val, 2);
@@ -54,6 +56,8 @@ function toNumber(str, options = {}){
54
56
 
55
57
  if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123
56
58
  else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123
59
+ else if(options.leadingZeros && leadingZeros===str) return 0; //00
60
+
57
61
  else{//no leading zeros or leading zeros are allowed
58
62
  const num = Number(trimmedStr);
59
63
  const numStr = "" + num;
@@ -103,4 +107,5 @@ function parse_int(numStr, base){
103
107
  else if(window && window.parseInt) return window.parseInt(numStr, base);
104
108
  else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
105
109
  }
106
- module.exports = toNumber
110
+
111
+ module.exports = toNumber;
package/strnum.test.js CHANGED
@@ -1,4 +1,4 @@
1
- const toNumber = require("./strnum");
1
+ const toNumber = require("./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", () => {
@@ -6,6 +6,7 @@ describe("Should convert all the valid numeric strings to number", () => {
6
6
  expect(toNumber(null)).toEqual(null);
7
7
  expect(toNumber("")).toEqual("");
8
8
  expect(toNumber("string")).toEqual("string");
9
+ expect(toNumber("e89794659669cb7bb967db73a7ea6889c3891727")).toEqual("e89794659669cb7bb967db73a7ea6889c3891727");
9
10
  });
10
11
  it("should not parse number with spaces or comma", () => {
11
12
  expect(toNumber("12,12")).toEqual("12,12");
@@ -34,6 +35,14 @@ describe("Should convert all the valid numeric strings to number", () => {
34
35
  expect(toNumber("JVBERi0xLjMNCiXi48")).toEqual("JVBERi0xLjMNCiXi48");
35
36
  })
36
37
  it("leading zeros", () => {
38
+ expect(toNumber("0")).toEqual(0);
39
+ expect(toNumber("00")).toEqual(0);
40
+ expect(toNumber("00.0")).toEqual(0);
41
+
42
+ expect(toNumber("0",{ leadingZeros : false})).toEqual(0);
43
+ expect(toNumber("00",{ leadingZeros : false})).toEqual("00");
44
+ expect(toNumber("00.0",{ leadingZeros : false})).toEqual("00.0");
45
+
37
46
  expect(toNumber("06")).toEqual(6);
38
47
  expect(toNumber("06", { leadingZeros : true})).toEqual(6);
39
48
  expect(toNumber("06", { leadingZeros : false})).toEqual("06");
@@ -51,12 +60,12 @@ describe("Should convert all the valid numeric strings to number", () => {
51
60
  expect(toNumber("20.21.030") ).toEqual("20.21.030");
52
61
  expect(toNumber("0.21.030") ).toEqual("0.21.030");
53
62
  expect(toNumber("0.21.") ).toEqual("0.21.");
63
+ });
64
+ it("floating point and leading zeros", () => {
54
65
  expect(toNumber("0.")).toEqual(0);
55
66
  expect(toNumber("+0.")).toEqual(0);
56
67
  expect(toNumber("-0.")).toEqual(-0);
57
68
  expect(toNumber("1.") ).toEqual(1);
58
- });
59
- it("floating point and leading zeros", () => {
60
69
  expect(toNumber("00.00")).toEqual(0);
61
70
  expect(toNumber("0.06")).toEqual(0.06);
62
71
  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"))