strnum 2.1.1 → 2.2.0

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/README.md CHANGED
@@ -94,4 +94,11 @@ hex: true, //when hexadecimal string should be parsed
94
94
  leadingZeros: true, //when number with leading zeros like 08 should be parsed. 0.0 is not impacted
95
95
  eNotation: true, //when number with eNotation or number parsed in eNotation should be considered
96
96
  skipLike: /regex/ //when string should not be parsed when it matches the specified regular expression
97
+ infinity: "original", // "null", "infinity" (Infinity type), "string" ("Infinity" (the string literal))
97
98
  ```
99
+
100
+
101
+ # Try out our other work
102
+
103
+ WishIn - You need it if negative thoughts take over all the time <br>
104
+ <a href="https://play.google.com/store/apps/details?id=com.solothought.wishin"> <img src="https://solothought.com/products/assets/images/wishin/YouTubeThumbnail.png" width="500px"/> </a>
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "strnum",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "Parse String to Number based on configuration",
5
5
  "type": "module",
6
6
  "main": "strnum.js",
7
7
  "scripts": {
8
- "test": "jasmine strnum.test.js"
8
+ "test": "jasmine tests/*_test.js"
9
9
  },
10
10
  "keywords": [
11
11
  "string",
@@ -28,4 +28,4 @@
28
28
  "devDependencies": {
29
29
  "jasmine": "^5.6.0"
30
30
  }
31
- }
31
+ }
package/strnum.js CHANGED
@@ -3,103 +3,106 @@ const numRegex = /^([\-\+])?(0*)([0-9]*(\.[0-9]*)?)$/;
3
3
  // const octRegex = /^0x[a-z0-9]+/;
4
4
  // const binRegex = /0x[a-z0-9]+/;
5
5
 
6
-
6
+
7
7
  const consider = {
8
- hex : true,
8
+ hex: true,
9
9
  // oct: false,
10
10
  leadingZeros: true,
11
11
  decimalPoint: "\.",
12
12
  eNotation: true,
13
- //skipLike: /regex/
13
+ //skipLike: /regex/,
14
+ infinity: "original", // "null", "infinity" (Infinity type), "string" ("Infinity" (the string literal))
14
15
  };
15
16
 
16
- export default function toNumber(str, options = {}){
17
- options = Object.assign({}, consider, options );
18
- if(!str || typeof str !== "string" ) return str;
19
-
20
- let trimmedStr = str.trim();
21
-
22
- if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
23
- else if(str==="0") return 0;
17
+ export default function toNumber(str, options = {}) {
18
+ options = Object.assign({}, consider, options);
19
+ if (!str || typeof str !== "string") return str;
20
+
21
+ let trimmedStr = str.trim();
22
+
23
+ if (options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
24
+ else if (str === "0") return 0;
24
25
  else if (options.hex && hexRegex.test(trimmedStr)) {
25
26
  return parse_int(trimmedStr, 16);
26
- // }else if (options.oct && octRegex.test(str)) {
27
- // return Number.parseInt(val, 8);
28
- }else if (trimmedStr.search(/.+[eE].+/)!== -1) { //eNotation
29
- return resolveEnotation(str,trimmedStr,options);
30
- // }else if (options.parseBin && binRegex.test(str)) {
31
- // return Number.parseInt(val, 2);
32
- }else{
27
+ // }else if (options.oct && octRegex.test(str)) {
28
+ // return Number.parseInt(val, 8);
29
+ } else if (!isFinite(trimmedStr)) { //Infinity
30
+ return handleInfinity(str, Number(trimmedStr), options);
31
+ } else if (trimmedStr.includes('e') || trimmedStr.includes('E')) { //eNotation
32
+ return resolveEnotation(str, trimmedStr, options);
33
+ // }else if (options.parseBin && binRegex.test(str)) {
34
+ // return Number.parseInt(val, 2);
35
+ } else {
33
36
  //separate negative sign, leading zeros, and rest number
34
37
  const match = numRegex.exec(trimmedStr);
35
38
  // +00.123 => [ , '+', '00', '.123', ..
36
- if(match){
39
+ if (match) {
37
40
  const sign = match[1] || "";
38
41
  const leadingZeros = match[2];
39
42
  let numTrimmedByZeros = trimZeros(match[3]); //complete num without leading zeros
40
43
  const decimalAdjacentToLeadingZeros = sign ? // 0., -00., 000.
41
- str[leadingZeros.length+1] === "."
44
+ str[leadingZeros.length + 1] === "."
42
45
  : str[leadingZeros.length] === ".";
43
46
 
44
47
  //trim ending zeros for floating number
45
- if(!options.leadingZeros //leading zeros are not allowed
46
- && (leadingZeros.length > 1
47
- || (leadingZeros.length === 1 && !decimalAdjacentToLeadingZeros))){
48
+ if (!options.leadingZeros //leading zeros are not allowed
49
+ && (leadingZeros.length > 1
50
+ || (leadingZeros.length === 1 && !decimalAdjacentToLeadingZeros))) {
48
51
  // 00, 00.3, +03.24, 03, 03.24
49
52
  return str;
50
53
  }
51
- else{//no leading zeros or leading zeros are allowed
54
+ else {//no leading zeros or leading zeros are allowed
52
55
  const num = Number(trimmedStr);
53
56
  const parsedStr = String(num);
54
57
 
55
- if( num === 0) return num;
56
- if(parsedStr.search(/[eE]/) !== -1){ //given number is long and parsed to eNotation
57
- if(options.eNotation) return num;
58
+ if (num === 0) return num;
59
+ if (parsedStr.search(/[eE]/) !== -1) { //given number is long and parsed to eNotation
60
+ if (options.eNotation) return num;
58
61
  else return str;
59
- }else if(trimmedStr.indexOf(".") !== -1){ //floating number
60
- if(parsedStr === "0") return num; //0.0
61
- else if(parsedStr === numTrimmedByZeros) return num; //0.456. 0.79000
62
- else if( parsedStr === `${sign}${numTrimmedByZeros}`) return num;
62
+ } else if (trimmedStr.indexOf(".") !== -1) { //floating number
63
+ if (parsedStr === "0") return num; //0.0
64
+ else if (parsedStr === numTrimmedByZeros) return num; //0.456. 0.79000
65
+ else if (parsedStr === `${sign}${numTrimmedByZeros}`) return num;
63
66
  else return str;
64
67
  }
65
-
66
- let n = leadingZeros? numTrimmedByZeros : trimmedStr;
67
- if(leadingZeros){
68
+
69
+ let n = leadingZeros ? numTrimmedByZeros : trimmedStr;
70
+ if (leadingZeros) {
68
71
  // -009 => -9
69
- return (n === parsedStr) || (sign+n === parsedStr) ? num : str
70
- }else {
72
+ return (n === parsedStr) || (sign + n === parsedStr) ? num : str
73
+ } else {
71
74
  // +9
72
- return (n === parsedStr) || (n === sign+parsedStr) ? num : str
75
+ return (n === parsedStr) || (n === sign + parsedStr) ? num : str
73
76
  }
74
77
  }
75
- }else{ //non-numeric string
78
+ } else { //non-numeric string
76
79
  return str;
77
80
  }
78
81
  }
79
82
  }
80
83
 
81
84
  const eNotationRegx = /^([-+])?(0*)(\d*(\.\d*)?[eE][-\+]?\d+)$/;
82
- function resolveEnotation(str,trimmedStr,options){
83
- if(!options.eNotation) return str;
84
- const notation = trimmedStr.match(eNotationRegx);
85
- if(notation){
85
+ function resolveEnotation(str, trimmedStr, options) {
86
+ if (!options.eNotation) return str;
87
+ const notation = trimmedStr.match(eNotationRegx);
88
+ if (notation) {
86
89
  let sign = notation[1] || "";
87
90
  const eChar = notation[3].indexOf("e") === -1 ? "E" : "e";
88
91
  const leadingZeros = notation[2];
89
92
  const eAdjacentToLeadingZeros = sign ? // 0E.
90
- str[leadingZeros.length+1] === eChar
93
+ str[leadingZeros.length + 1] === eChar
91
94
  : str[leadingZeros.length] === eChar;
92
95
 
93
- if(leadingZeros.length > 1 && eAdjacentToLeadingZeros) return str;
94
- else if(leadingZeros.length === 1
95
- && (notation[3].startsWith(`.${eChar}`) || notation[3][0] === eChar)){
96
- return Number(trimmedStr);
97
- }else if(options.leadingZeros && !eAdjacentToLeadingZeros){ //accept with leading zeros
96
+ if (leadingZeros.length > 1 && eAdjacentToLeadingZeros) return str;
97
+ else if (leadingZeros.length === 1
98
+ && (notation[3].startsWith(`.${eChar}`) || notation[3][0] === eChar)) {
99
+ return Number(trimmedStr);
100
+ } else if (options.leadingZeros && !eAdjacentToLeadingZeros) { //accept with leading zeros
98
101
  //remove leading 0s
99
102
  trimmedStr = (notation[1] || "") + notation[3];
100
103
  return Number(trimmedStr);
101
- }else return str;
102
- }else{
104
+ } else return str;
105
+ } else {
103
106
  return str;
104
107
  }
105
108
  }
@@ -109,21 +112,44 @@ function resolveEnotation(str,trimmedStr,options){
109
112
  * @param {string} numStr without leading zeros
110
113
  * @returns
111
114
  */
112
- function trimZeros(numStr){
113
- if(numStr && numStr.indexOf(".") !== -1){//float
115
+ function trimZeros(numStr) {
116
+ if (numStr && numStr.indexOf(".") !== -1) {//float
114
117
  numStr = numStr.replace(/0+$/, ""); //remove ending zeros
115
- if(numStr === ".") numStr = "0";
116
- else if(numStr[0] === ".") numStr = "0"+numStr;
117
- else if(numStr[numStr.length-1] === ".") numStr = numStr.substring(0,numStr.length-1);
118
+ if (numStr === ".") numStr = "0";
119
+ else if (numStr[0] === ".") numStr = "0" + numStr;
120
+ else if (numStr[numStr.length - 1] === ".") numStr = numStr.substring(0, numStr.length - 1);
118
121
  return numStr;
119
122
  }
120
123
  return numStr;
121
124
  }
122
125
 
123
- function parse_int(numStr, base){
126
+ function parse_int(numStr, base) {
124
127
  //polyfill
125
- if(parseInt) return parseInt(numStr, base);
126
- else if(Number.parseInt) return Number.parseInt(numStr, base);
127
- else if(window && window.parseInt) return window.parseInt(numStr, base);
128
+ if (parseInt) return parseInt(numStr, base);
129
+ else if (Number.parseInt) return Number.parseInt(numStr, base);
130
+ else if (window && window.parseInt) return window.parseInt(numStr, base);
128
131
  else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
132
+ }
133
+
134
+ /**
135
+ * Handle infinite values based on user option
136
+ * @param {string} str - original input string
137
+ * @param {number} num - parsed number (Infinity or -Infinity)
138
+ * @param {object} options - user options
139
+ * @returns {string|number|null} based on infinity option
140
+ */
141
+ function handleInfinity(str, num, options) {
142
+ const isPositive = num === Infinity;
143
+
144
+ switch (options.infinity.toLowerCase()) {
145
+ case "null":
146
+ return null;
147
+ case "infinity":
148
+ return num; // Return Infinity or -Infinity
149
+ case "string":
150
+ return isPositive ? "Infinity" : "-Infinity";
151
+ case "original":
152
+ default:
153
+ return str; // Return original string like "1e1000"
154
+ }
129
155
  }
@@ -0,0 +1,18 @@
1
+ import toNumber from "../strnum.js";
2
+
3
+ describe("Should convert all the valid numeric strings to number", () => {
4
+ it("should return infinity as per user option", () => {
5
+ expect(toNumber("1e1000", { infinity: "original" })).toEqual("1e1000");
6
+ expect(toNumber("1e1000", { infinity: "null" })).toEqual(null);
7
+ expect(toNumber("1e1000", { infinity: "infinity" })).toEqual(Infinity);
8
+ expect(toNumber("1e1000", { infinity: "string" })).toEqual("Infinity");
9
+ expect(toNumber("-1e1000", { infinity: "original" })).toEqual("-1e1000");
10
+ expect(toNumber("-1e1000", { infinity: "null" })).toEqual(null);
11
+ expect(toNumber("-1e1000", { infinity: "infinity" })).toEqual(-Infinity);
12
+ expect(toNumber("-1e1000", { infinity: "string" })).toEqual("-Infinity");
13
+
14
+
15
+ expect(toNumber("1e309")).toEqual("1e309");
16
+
17
+ });
18
+ });
@@ -1,4 +1,4 @@
1
- import toNumber from "./strnum.js";
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", () => {
@@ -7,7 +7,6 @@ describe("Should convert all the valid numeric strings to number", () => {
7
7
  expect(toNumber("")).toEqual("");
8
8
  expect(toNumber("string")).toEqual("string");
9
9
  expect(toNumber("e89794659669cb7bb967db73a7ea6889c3891727")).toEqual("e89794659669cb7bb967db73a7ea6889c3891727");
10
-
11
10
  });
12
11
  it("should not parse number with spaces or comma", () => {
13
12
  expect(toNumber("12,12")).toEqual("12,12");
@@ -24,10 +23,10 @@ describe("Should convert all the valid numeric strings to number", () => {
24
23
  it("should parse hexadecimal values", () => {
25
24
  expect(toNumber("0x2f")).toEqual(47);
26
25
  expect(toNumber("-0x2f")).toEqual(-47);
27
- expect(toNumber("0x2f", { hex : true})).toEqual(47);
28
- expect(toNumber("-0x2f", { hex : true})).toEqual(-47);
29
- expect(toNumber("0x2f", { hex : false})).toEqual("0x2f");
30
- expect(toNumber("-0x2f", { hex : false})).toEqual("-0x2f");
26
+ expect(toNumber("0x2f", { hex: true })).toEqual(47);
27
+ expect(toNumber("-0x2f", { hex: true })).toEqual(-47);
28
+ expect(toNumber("0x2f", { hex: false })).toEqual("0x2f");
29
+ expect(toNumber("-0x2f", { hex: false })).toEqual("-0x2f");
31
30
  })
32
31
  it("should not parse strings with 0x embedded", () => {
33
32
  expect(toNumber("0xzz")).toEqual("0xzz");
@@ -40,54 +39,54 @@ describe("Should convert all the valid numeric strings to number", () => {
40
39
  expect(toNumber("00")).toEqual(0);
41
40
  expect(toNumber("00.0")).toEqual(0);
42
41
 
43
- expect(toNumber("0",{ leadingZeros : false})).toEqual(0);
44
- expect(toNumber("00",{ leadingZeros : false})).toEqual("00");
45
- expect(toNumber("00.0",{ leadingZeros : false})).toEqual("00.0");
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");
46
45
 
47
46
  expect(toNumber("06")).toEqual(6);
48
- expect(toNumber("06", { leadingZeros : true})).toEqual(6);
49
- expect(toNumber("06", { leadingZeros : false})).toEqual("06");
47
+ expect(toNumber("06", { leadingZeros: true })).toEqual(6);
48
+ expect(toNumber("06", { leadingZeros: false })).toEqual("06");
50
49
 
51
50
  expect(toNumber("006")).toEqual(6);
52
- expect(toNumber("006", { leadingZeros : true})).toEqual(6);
53
- expect(toNumber("006", { leadingZeros : false})).toEqual("006");
51
+ expect(toNumber("006", { leadingZeros: true })).toEqual(6);
52
+ expect(toNumber("006", { leadingZeros: false })).toEqual("006");
54
53
 
55
- expect(toNumber("000000000000000000000000017717" , { leadingZeros : false})).toEqual("000000000000000000000000017717");
56
- expect(toNumber("000000000000000000000000017717" , { leadingZeros : true})).toEqual(17717);
57
- expect(toNumber("020211201030005811824") ).toEqual("020211201030005811824");
58
- expect(toNumber("0420926189200190257681175017717") ).toEqual(4.209261892001902e+29);
54
+ expect(toNumber("000000000000000000000000017717", { leadingZeros: false })).toEqual("000000000000000000000000017717");
55
+ expect(toNumber("000000000000000000000000017717", { leadingZeros: true })).toEqual(17717);
56
+ expect(toNumber("020211201030005811824")).toEqual("020211201030005811824");
57
+ expect(toNumber("0420926189200190257681175017717")).toEqual(4.209261892001902e+29);
59
58
  })
60
59
  it("invalid floating number", () => {
61
- expect(toNumber("20.21.030") ).toEqual("20.21.030");
62
- expect(toNumber("0.21.030") ).toEqual("0.21.030");
63
- expect(toNumber("0.21.") ).toEqual("0.21.");
60
+ expect(toNumber("20.21.030")).toEqual("20.21.030");
61
+ expect(toNumber("0.21.030")).toEqual("0.21.030");
62
+ expect(toNumber("0.21.")).toEqual("0.21.");
64
63
  });
65
64
  it("floating point and leading zeros", () => {
66
65
  expect(toNumber("0.")).toEqual(0);
67
66
  expect(toNumber("+0.")).toEqual(0);
68
67
  expect(toNumber("-0.")).toEqual(-0);
69
- expect(toNumber("1.") ).toEqual(1);
68
+ expect(toNumber("1.")).toEqual(1);
70
69
  expect(toNumber("00.00")).toEqual(0);
71
70
  expect(toNumber("0.06")).toEqual(0.06);
72
71
  expect(toNumber("00.6")).toEqual(0.6);
73
72
  expect(toNumber(".006")).toEqual(0.006);
74
73
  expect(toNumber("6.0")).toEqual(6);
75
74
  expect(toNumber("06.0")).toEqual(6);
76
-
77
- expect(toNumber("0.0", { leadingZeros : false})).toEqual(0);
78
- expect(toNumber("00.00", { leadingZeros : false})).toEqual("00.00");
79
- expect(toNumber("0.06", { leadingZeros : false})).toEqual(0.06);
80
- expect(toNumber("00.6", { leadingZeros : false})).toEqual("00.6");
81
- expect(toNumber(".006", { leadingZeros : false})).toEqual(0.006);
82
- expect(toNumber("6.0" , { leadingZeros : false})).toEqual(6);
83
- expect(toNumber("06.0" , { leadingZeros : false})).toEqual("06.0");
75
+
76
+ expect(toNumber("0.0", { leadingZeros: false })).toEqual(0);
77
+ expect(toNumber("00.00", { leadingZeros: false })).toEqual("00.00");
78
+ expect(toNumber("0.06", { leadingZeros: false })).toEqual(0.06);
79
+ expect(toNumber("00.6", { leadingZeros: false })).toEqual("00.6");
80
+ expect(toNumber(".006", { leadingZeros: false })).toEqual(0.006);
81
+ expect(toNumber("6.0", { leadingZeros: false })).toEqual(6);
82
+ expect(toNumber("06.0", { leadingZeros: false })).toEqual("06.0");
84
83
  })
85
84
  it("negative number leading zeros", () => {
86
85
  expect(toNumber("+06")).toEqual(6);
87
86
  expect(toNumber("-06")).toEqual(-6);
88
- expect(toNumber("-06", { leadingZeros : true})).toEqual(-6);
89
- expect(toNumber("-06", { leadingZeros : false})).toEqual("-06");
90
-
87
+ expect(toNumber("-06", { leadingZeros: true })).toEqual(-6);
88
+ expect(toNumber("-06", { leadingZeros: false })).toEqual("-06");
89
+
91
90
  expect(toNumber("-0.0")).toEqual(-0);
92
91
  expect(toNumber("-00.00")).toEqual(-0);
93
92
  expect(toNumber("-0.06")).toEqual(-0.06);
@@ -96,47 +95,47 @@ describe("Should convert all the valid numeric strings to number", () => {
96
95
  expect(toNumber("-6.0")).toEqual(-6);
97
96
  expect(toNumber("-06.0")).toEqual(-6);
98
97
  expect(toNumber("+06.0")).toEqual(6);
99
-
100
- expect(toNumber("-0.0" , { leadingZeros : false})).toEqual(-0);
101
- expect(toNumber("-00.00", { leadingZeros : false})).toEqual("-00.00");
102
- expect(toNumber("-0.06", { leadingZeros : false})).toEqual(-0.06);
103
- expect(toNumber("-00.6", { leadingZeros : false})).toEqual("-00.6");
104
- expect(toNumber("-.006", {leadingZeros : false})).toEqual(-0.006);
105
- expect(toNumber("-6.0" , { leadingZeros : false})).toEqual(-6);
106
- expect(toNumber("-06.0" , { leadingZeros : false})).toEqual("-06.0");
98
+
99
+ expect(toNumber("-0.0", { leadingZeros: false })).toEqual(-0);
100
+ expect(toNumber("-00.00", { leadingZeros: false })).toEqual("-00.00");
101
+ expect(toNumber("-0.06", { leadingZeros: false })).toEqual(-0.06);
102
+ expect(toNumber("-00.6", { leadingZeros: false })).toEqual("-00.6");
103
+ expect(toNumber("-.006", { leadingZeros: false })).toEqual(-0.006);
104
+ expect(toNumber("-6.0", { leadingZeros: false })).toEqual(-6);
105
+ expect(toNumber("-06.0", { leadingZeros: false })).toEqual("-06.0");
107
106
  })
108
107
  it("long number", () => {
109
- expect(toNumber("020211201030005811824") ).toEqual("020211201030005811824");
110
- expect(toNumber("20211201030005811824") ).toEqual("20211201030005811824");
111
- expect(toNumber("20.211201030005811824") ).toEqual("20.211201030005811824");
112
- expect(toNumber("0.211201030005811824") ).toEqual("0.211201030005811824");
108
+ expect(toNumber("020211201030005811824")).toEqual("020211201030005811824");
109
+ expect(toNumber("20211201030005811824")).toEqual("20211201030005811824");
110
+ expect(toNumber("20.211201030005811824")).toEqual("20.211201030005811824");
111
+ expect(toNumber("0.211201030005811824")).toEqual("0.211201030005811824");
113
112
  });
114
113
  it("scientific notation", () => {
115
- expect(toNumber("01.0e2" , { leadingZeros : false})).toEqual("01.0e2");
116
- expect(toNumber("-01.0e2" , { leadingZeros : false})).toEqual("-01.0e2");
117
- expect(toNumber("01.0e2") ).toEqual(100);
118
- expect(toNumber("-01.0e2") ).toEqual(-100);
119
- expect(toNumber("1.0e2") ).toEqual(100);
114
+ expect(toNumber("01.0e2", { leadingZeros: false })).toEqual("01.0e2");
115
+ expect(toNumber("-01.0e2", { leadingZeros: false })).toEqual("-01.0e2");
116
+ expect(toNumber("01.0e2")).toEqual(100);
117
+ expect(toNumber("-01.0e2")).toEqual(-100);
118
+ expect(toNumber("1.0e2")).toEqual(100);
120
119
 
121
- expect(toNumber("-1.0e2") ).toEqual(-100);
120
+ expect(toNumber("-1.0e2")).toEqual(-100);
122
121
  expect(toNumber("1.0e-2")).toEqual(0.01);
123
-
124
- expect(toNumber("420926189200190257681175017717") ).toEqual(4.209261892001902e+29);
125
- expect(toNumber("420926189200190257681175017717" , { eNotation: false} )).toEqual("420926189200190257681175017717");
126
-
122
+
123
+ expect(toNumber("420926189200190257681175017717")).toEqual(4.209261892001902e+29);
124
+ expect(toNumber("420926189200190257681175017717", { eNotation: false })).toEqual("420926189200190257681175017717");
125
+
127
126
  expect(toNumber("1e-2")).toEqual(0.01);
128
127
  expect(toNumber("1e+2")).toEqual(100);
129
128
  expect(toNumber("1.e+2")).toEqual(100);
130
129
  });
131
130
 
132
131
  it("scientific notation with upper E", () => {
133
- expect(toNumber("01.0E2" , { leadingZeros : false})).toEqual("01.0E2");
134
- expect(toNumber("-01.0E2" , { leadingZeros : false})).toEqual("-01.0E2");
135
- expect(toNumber("01.0E2") ).toEqual(100);
136
- expect(toNumber("-01.0E2") ).toEqual(-100);
137
- expect(toNumber("1.0E2") ).toEqual(100);
132
+ expect(toNumber("01.0E2", { leadingZeros: false })).toEqual("01.0E2");
133
+ expect(toNumber("-01.0E2", { leadingZeros: false })).toEqual("-01.0E2");
134
+ expect(toNumber("01.0E2")).toEqual(100);
135
+ expect(toNumber("-01.0E2")).toEqual(-100);
136
+ expect(toNumber("1.0E2")).toEqual(100);
138
137
 
139
- expect(toNumber("-1.0E2") ).toEqual(-100);
138
+ expect(toNumber("-1.0E2")).toEqual(-100);
140
139
  expect(toNumber("1.0E-2")).toEqual(0.01);
141
140
 
142
141
  expect(toNumber("E-2")).toEqual("E-2");
@@ -144,16 +143,16 @@ describe("Should convert all the valid numeric strings to number", () => {
144
143
  expect(toNumber("0E2")).toEqual(0);
145
144
  expect(toNumber("-0E2")).toEqual(-0);
146
145
  expect(toNumber("00E2")).toEqual("00E2");
147
- expect(toNumber("00E2", { leadingZeros : false})).toEqual("00E2");
146
+ expect(toNumber("00E2", { leadingZeros: false })).toEqual("00E2");
148
147
  });
149
-
148
+
150
149
  it("should skip matching pattern", () => {
151
150
  expect(toNumber("0", { skipLike: /.*/ })).toEqual("0");
152
- expect(toNumber("+12", { skipLike: /\+[0-9]{10}/} )).toEqual(12);
153
- expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/} )).toEqual("12+12");
154
- expect(toNumber("12+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("12+1212121212");
155
- expect(toNumber("+1212121212") ).toEqual(1212121212);
156
- expect(toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("+1212121212");
151
+ expect(toNumber("+12", { skipLike: /\+[0-9]{10}/ })).toEqual(12);
152
+ expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/ })).toEqual("12+12");
153
+ expect(toNumber("12+1212121212", { skipLike: /\+[0-9]{10}/ })).toEqual("12+1212121212");
154
+ expect(toNumber("+1212121212")).toEqual(1212121212);
155
+ expect(toNumber("+1212121212", { skipLike: /\+[0-9]{10}/ })).toEqual("+1212121212");
157
156
  })
158
157
  it("should not change string if not number", () => {
159
158
  expect(toNumber("+12 12")).toEqual("+12 12");
@@ -162,7 +161,7 @@ describe("Should convert all the valid numeric strings to number", () => {
162
161
  it("should ignore sorrounded spaces ", () => {
163
162
  expect(toNumber(" +1212 ")).toEqual(1212);
164
163
  })
165
-
164
+
166
165
  it("negative numbers", () => {
167
166
  expect(toNumber("+1212")).toEqual(1212);
168
167
  expect(toNumber("+12.12")).toEqual(12.12);
package/test.js DELETED
@@ -1,9 +0,0 @@
1
- import toNumber from "./strnum.js";
2
-
3
- describe("Should convert all the valid numeric strings to number", () => {
4
- it("should return undefined, null, empty string, or non-numeric as it is", () => {
5
- // expect(toNumber("+ 90")).toEqual("+ 90");
6
- // expect(toNumber("- 90")).toEqual("- 90");
7
- expect(toNumber("-10E2")).toEqual(100);
8
- });
9
- });