strnum 1.0.1 → 1.0.5

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.
@@ -0,0 +1,25 @@
1
+ {
2
+ "version": "0.2.0",
3
+ "configurations": [
4
+ {
5
+ "type": "node",
6
+ "request": "launch",
7
+ "name": "Jasmine Tests",
8
+ "program": "${workspaceFolder}/node_modules/jasmine/bin/jasmine.js",
9
+ "args": [
10
+ "${workspaceFolder}/spec/attr_spec.js"
11
+ ],
12
+ "internalConsoleOptions": "openOnSessionStart"
13
+ },{
14
+ "type": "node",
15
+ "request": "launch",
16
+ "name": "Jasmine Tests current test file",
17
+ "program": "${workspaceFolder}/node_modules/jasmine/bin/jasmine.js",
18
+ "args": [
19
+ "${file}"
20
+ ],
21
+ "internalConsoleOptions": "openOnSessionStart"
22
+ }
23
+ ]
24
+
25
+ }
package/README.md CHANGED
@@ -73,4 +73,14 @@ toNumber("1.0e2") ; //100)
73
73
 
74
74
  toNumber("-1.0e2") ; //-100)
75
75
  toNumber("1.0e-2"); //0.01)
76
+
77
+ toNumber("+1212121212"); // 1212121212
78
+ toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} )); //"+1212121212"
79
+ ```
80
+
81
+ Supported Options
82
+ ```js
83
+ hex : true, //when hexadecimal string should be parsed
84
+ leadingZeros: true, //when number with leading zeros like 08 should be parsed. 0.0 is not impacted
85
+ eNotation: true //when number with eNotation or number parsed in eNotation should be considered
76
86
  ```
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "strnum",
3
- "version": "1.0.1",
3
+ "version": "1.0.5",
4
4
  "description": "Parse String to Number based on configuration",
5
5
  "main": "strnum.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "test": "jasmine strnum.test.js"
8
8
  },
9
9
  "keywords": [
10
10
  "string",
@@ -17,5 +17,8 @@
17
17
  "url": "https://github.com/NaturalIntelligence/strnum"
18
18
  },
19
19
  "author": "Amit Gupta (https://amitkumargupta.work/)",
20
- "license": "MIT"
20
+ "license": "MIT",
21
+ "devDependencies": {
22
+ "jasmine": "^3.10.0"
23
+ }
21
24
  }
package/strnum.js CHANGED
@@ -1,12 +1,24 @@
1
- const hexRegex = /0x[a-zA-Z0-9]+/;
2
- const numRegex = /^(\-)?(0*)(\.[0-9]+(e\-?[0-9]+)?|[0-9]+(\.[0-9]+(e\-?[0-9]+)?)?)$/;
1
+ const hexRegex = /^[-+]?0x[a-fA-F0-9]+$/;
2
+ const numRegex = /^([\-\+])?(0*)(\.[0-9]+([eE]\-?[0-9]+)?|[0-9]+(\.[0-9]+([eE]\-?[0-9]+)?)?)$/;
3
3
  // const octRegex = /0x[a-z0-9]+/;
4
4
  // const binRegex = /0x[a-z0-9]+/;
5
5
 
6
+
7
+ //polyfill
8
+ if (!Number.parseInt && window.parseInt) {
9
+ Number.parseInt = window.parseInt;
10
+ }
11
+ if (!Number.parseFloat && window.parseFloat) {
12
+ Number.parseFloat = window.parseFloat;
13
+ }
14
+
15
+
6
16
  const consider = {
7
17
  hex : true,
8
18
  leadingZeros: true,
9
- decimalPoint: "\."
19
+ decimalPoint: "\.",
20
+ eNotation: true
21
+ //skipLike: /regex/
10
22
  };
11
23
 
12
24
  function toNumber(str, options = {}){
@@ -19,27 +31,94 @@ function toNumber(str, options = {}){
19
31
 
20
32
  options = Object.assign({}, consider, options );
21
33
  if(!str || typeof str !== "string" ) return str;
22
- else if (options.hex && hexRegex.test(str)) {
23
- return Number.parseInt(str, 16);
34
+
35
+ let trimmedStr = str.trim();
36
+ // if(trimmedStr === "0.0") return 0;
37
+ // else if(trimmedStr === "+0.0") return 0;
38
+ // else if(trimmedStr === "-0.0") return -0;
39
+
40
+ if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
41
+ else if (options.hex && hexRegex.test(trimmedStr)) {
42
+ return Number.parseInt(trimmedStr, 16);
24
43
  // } else if (options.parseOct && octRegex.test(str)) {
25
44
  // return Number.parseInt(val, 8);
26
45
  // }else if (options.parseBin && binRegex.test(str)) {
27
46
  // return Number.parseInt(val, 2);
28
47
  }else{
29
48
  //separate negative sign, leading zeros, and rest number
30
- const match = numRegex.exec(str);
49
+ const match = numRegex.exec(trimmedStr);
31
50
  if(match){
32
- const negative = match[1];
51
+ const sign = match[1];
33
52
  const leadingZeros = match[2];
34
- const num = match[3]; //complete num
53
+ let numTrimmedByZeros = trimZeros(match[3]); //complete num without leading zeros
54
+ //trim ending zeros for floating number
55
+
35
56
  const eNotation = match[4] || match[6];
36
- if(leadingZeros.length === 1 && num[0] === ".") return Number(str);
37
- else if(!options.leadingZeros && leadingZeros.length > 0) return str;
38
- else return Number(str);
57
+ if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123
58
+ else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123
59
+ else{//no leading zeros or leading zeros are allowed
60
+ const num = Number(trimmedStr);
61
+ const numStr = "" + num;
62
+ if(numStr.search(/[eE]/) !== -1){ //given number is long and parsed to eNotation
63
+ if(options.eNotation) return num;
64
+ else return str;
65
+ }else if(eNotation){ //given number has enotation
66
+ if(options.eNotation) return num;
67
+ else return str;
68
+ }else if(trimmedStr.indexOf(".") !== -1){ //floating number
69
+ // const decimalPart = match[5].substr(1);
70
+ // const intPart = trimmedStr.substr(0,trimmedStr.indexOf("."));
71
+
72
+
73
+ // const p = numStr.indexOf(".");
74
+ // const givenIntPart = numStr.substr(0,p);
75
+ // const givenDecPart = numStr.substr(p+1);
76
+ if(numStr === "0" && (numTrimmedByZeros === "") ) return num; //0.0
77
+ else if(numStr === numTrimmedByZeros) return num; //0.456. 0.79000
78
+ else if( sign && numStr === "-"+numTrimmedByZeros) return num;
79
+ else return str;
80
+ }
81
+
82
+ if(leadingZeros){
83
+ // if(numTrimmedByZeros === numStr){
84
+ // if(options.leadingZeros) return num;
85
+ // else return str;
86
+ // }else return str;
87
+ if(numTrimmedByZeros === numStr) return num;
88
+ else if(sign+numTrimmedByZeros === numStr) return num;
89
+ else return str;
90
+ }
91
+
92
+ if(trimmedStr === numStr) return num;
93
+ else if(trimmedStr === sign+numStr) return num;
94
+ // else{
95
+ // //number with +/- sign
96
+ // trimmedStr.test(/[-+][0-9]);
97
+
98
+ // }
99
+ return str;
100
+ }
101
+ // else if(!eNotation && trimmedStr && trimmedStr !== Number(trimmedStr) ) return str;
102
+
39
103
  }else{ //non-numeric string
40
104
  return str;
41
105
  }
42
106
  }
43
107
  }
44
108
 
45
- module.exports = toNumber
109
+ /**
110
+ *
111
+ * @param {string} numStr without leading zeros
112
+ * @returns
113
+ */
114
+ function trimZeros(numStr){
115
+ if(numStr && numStr.indexOf(".") !== -1){//float
116
+ numStr = numStr.replace(/0+$/, ""); //remove ending zeros
117
+ if(numStr === ".") numStr = "0";
118
+ else if(numStr[0] === ".") numStr = "0"+numStr;
119
+ else if(numStr[numStr.length-1] === ".") numStr = numStr.substr(0,numStr.length-1);
120
+ return numStr;
121
+ }
122
+ return numStr;
123
+ }
124
+ module.exports = toNumber
package/strnum.test.js CHANGED
@@ -13,7 +13,13 @@ describe("Should convert all the valid numeric strings to number", () => {
13
13
  expect(toNumber("12-12")).toEqual("12-12");
14
14
  expect(toNumber("12.12.12")).toEqual("12.12.12");
15
15
  })
16
- it("should parse hexaDecimal values", () => {
16
+ it("should consider + sign", () => {
17
+ expect(toNumber("+12")).toEqual(12);
18
+ expect(toNumber("+ 12")).toEqual("+ 12");
19
+ expect(toNumber("12+12")).toEqual("12+12");
20
+ expect(toNumber("1212+")).toEqual("1212+");
21
+ })
22
+ it("should parse hexadecimal values", () => {
17
23
  expect(toNumber("0x2f")).toEqual(47);
18
24
  expect(toNumber("-0x2f")).toEqual(-47);
19
25
  expect(toNumber("0x2f", { hex : true})).toEqual(47);
@@ -21,6 +27,12 @@ describe("Should convert all the valid numeric strings to number", () => {
21
27
  expect(toNumber("0x2f", { hex : false})).toEqual("0x2f");
22
28
  expect(toNumber("-0x2f", { hex : false})).toEqual("-0x2f");
23
29
  })
30
+ it("should not parse strings with 0x embedded", () => {
31
+ expect(toNumber("0xzz")).toEqual("0xzz");
32
+ expect(toNumber("iweraf0x123qwerqwer")).toEqual("iweraf0x123qwerqwer");
33
+ expect(toNumber("1230x55")).toEqual("1230x55");
34
+ expect(toNumber("JVBERi0xLjMNCiXi48")).toEqual("JVBERi0xLjMNCiXi48");
35
+ })
24
36
  it("leading zeros", () => {
25
37
  expect(toNumber("06")).toEqual(6);
26
38
  expect(toNumber("06", { leadingZeros : true})).toEqual(6);
@@ -29,7 +41,19 @@ describe("Should convert all the valid numeric strings to number", () => {
29
41
  expect(toNumber("006")).toEqual(6);
30
42
  expect(toNumber("006", { leadingZeros : true})).toEqual(6);
31
43
  expect(toNumber("006", { leadingZeros : false})).toEqual("006");
44
+
45
+ expect(toNumber("000000000000000000000000017717" , { leadingZeros : false})).toEqual("000000000000000000000000017717");
46
+ expect(toNumber("000000000000000000000000017717" , { leadingZeros : true})).toEqual(17717);
47
+ expect(toNumber("020211201030005811824") ).toEqual("020211201030005811824");
48
+ expect(toNumber("0420926189200190257681175017717") ).toEqual(4.209261892001902e+29);
32
49
  })
50
+ it("invalid floating number", () => {
51
+ expect(toNumber("20.21.030") ).toEqual("20.21.030");
52
+ expect(toNumber("0.21.030") ).toEqual("0.21.030");
53
+ expect(toNumber("0.21.") ).toEqual("0.21.");
54
+ expect(toNumber("0.") ).toEqual("0.");
55
+ expect(toNumber("1.") ).toEqual("1.");
56
+ });
33
57
  it("floating point and leading zeros", () => {
34
58
  expect(toNumber("0.0")).toEqual(0);
35
59
  expect(toNumber("00.00")).toEqual(0);
@@ -48,6 +72,7 @@ describe("Should convert all the valid numeric strings to number", () => {
48
72
  expect(toNumber("06.0" , { leadingZeros : false})).toEqual("06.0");
49
73
  })
50
74
  it("negative number leading zeros", () => {
75
+ expect(toNumber("+06")).toEqual(6);
51
76
  expect(toNumber("-06")).toEqual(-6);
52
77
  expect(toNumber("-06", { leadingZeros : true})).toEqual(-6);
53
78
  expect(toNumber("-06", { leadingZeros : false})).toEqual("-06");
@@ -69,9 +94,10 @@ describe("Should convert all the valid numeric strings to number", () => {
69
94
  expect(toNumber("-06.0" , { leadingZeros : false})).toEqual("-06.0");
70
95
  })
71
96
  it("long number", () => {
72
- expect(toNumber("420926189200190257681175017717") ).toEqual(4.209261892001902e+29);
73
- expect(toNumber("000000000000000000000000017717" , { leadingZeros : false})).toEqual("000000000000000000000000017717");
74
- expect(toNumber("000000000000000000000000017717" , { leadingZeros : true})).toEqual(17717);
97
+ expect(toNumber("020211201030005811824") ).toEqual("020211201030005811824");
98
+ expect(toNumber("20211201030005811824") ).toEqual("20211201030005811824");
99
+ expect(toNumber("20.211201030005811824") ).toEqual("20.211201030005811824");
100
+ expect(toNumber("0.211201030005811824") ).toEqual("0.211201030005811824");
75
101
  });
76
102
  it("scientific notation", () => {
77
103
  expect(toNumber("01.0e2" , { leadingZeros : false})).toEqual("01.0e2");
@@ -82,6 +108,43 @@ describe("Should convert all the valid numeric strings to number", () => {
82
108
 
83
109
  expect(toNumber("-1.0e2") ).toEqual(-100);
84
110
  expect(toNumber("1.0e-2")).toEqual(0.01);
111
+
112
+ expect(toNumber("420926189200190257681175017717") ).toEqual(4.209261892001902e+29);
113
+ expect(toNumber("420926189200190257681175017717" , { eNotation: false} )).toEqual("420926189200190257681175017717");
114
+
85
115
  });
86
116
 
87
- });
117
+ it("scientific notation with upper E", () => {
118
+ expect(toNumber("01.0E2" , { leadingZeros : false})).toEqual("01.0E2");
119
+ expect(toNumber("-01.0E2" , { leadingZeros : false})).toEqual("-01.0E2");
120
+ expect(toNumber("01.0E2") ).toEqual(100);
121
+ expect(toNumber("-01.0E2") ).toEqual(-100);
122
+ expect(toNumber("1.0E2") ).toEqual(100);
123
+
124
+ expect(toNumber("-1.0E2") ).toEqual(-100);
125
+ expect(toNumber("1.0E-2")).toEqual(0.01);
126
+ });
127
+
128
+ it("should skip matching pattern", () => {
129
+ expect(toNumber("+12", { skipLike: /\+[0-9]{10}/} )).toEqual(12);
130
+ expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/} )).toEqual("12+12");
131
+ expect(toNumber("12+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("12+1212121212");
132
+ expect(toNumber("+1212121212") ).toEqual(1212121212);
133
+ expect(toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("+1212121212");
134
+ })
135
+ it("should not change string if not number", () => {
136
+ expect(toNumber("+12 12")).toEqual("+12 12");
137
+ expect(toNumber(" +12 12 ")).toEqual(" +12 12 ");
138
+ })
139
+ it("should ignore sorrounded spaces ", () => {
140
+ expect(toNumber(" +1212 ")).toEqual(1212);
141
+ })
142
+
143
+ it("negative numbers", () => {
144
+ expect(toNumber("+1212")).toEqual(1212);
145
+ expect(toNumber("+12.12")).toEqual(12.12);
146
+ expect(toNumber("-12.12")).toEqual(-12.12);
147
+ expect(toNumber("-012.12")).toEqual(-12.12);
148
+ expect(toNumber("-012.12")).toEqual(-12.12);
149
+ })
150
+ });