strnum 1.0.2 → 1.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strnum",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Parse String to Number based on configuration",
5
5
  "main": "strnum.js",
6
6
  "scripts": {
package/strnum.js CHANGED
@@ -20,16 +20,19 @@ function toNumber(str, options = {}){
20
20
 
21
21
  options = Object.assign({}, consider, options );
22
22
  if(!str || typeof str !== "string" ) return str;
23
- else if(options.skipLike !== undefined && options.skipLike.test(str)) return str;
24
- else if (options.hex && hexRegex.test(str)) {
25
- return Number.parseInt(str, 16);
23
+
24
+ let trimmedStr = str.trim();
25
+
26
+ if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
27
+ else if (options.hex && hexRegex.test(trimmedStr)) {
28
+ return Number.parseInt(trimmedStr, 16);
26
29
  // } else if (options.parseOct && octRegex.test(str)) {
27
30
  // return Number.parseInt(val, 8);
28
31
  // }else if (options.parseBin && binRegex.test(str)) {
29
32
  // return Number.parseInt(val, 2);
30
33
  }else{
31
34
  //separate negative sign, leading zeros, and rest number
32
- const match = numRegex.exec(str);
35
+ const match = numRegex.exec(trimmedStr);
33
36
  if(match){
34
37
  const negative = match[1];
35
38
  const leadingZeros = match[2];
@@ -37,7 +40,7 @@ function toNumber(str, options = {}){
37
40
  const eNotation = match[4] || match[6];
38
41
  if(leadingZeros.length === 1 && num[0] === ".") return Number(str);
39
42
  else if(!options.leadingZeros && leadingZeros.length > 0) return str;
40
- else return Number(str);
43
+ else return Number(trimmedStr);
41
44
  }else{ //non-numeric string
42
45
  return str;
43
46
  }
package/strnum.test.js CHANGED
@@ -96,5 +96,12 @@ describe("Should convert all the valid numeric strings to number", () => {
96
96
  expect(toNumber("+1212121212") ).toEqual(1212121212);
97
97
  expect(toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("+1212121212");
98
98
  })
99
+ it("should not change string if not number", () => {
100
+ expect(toNumber("+12 12")).toEqual("+12 12");
101
+ expect(toNumber(" +12 12 ")).toEqual(" +12 12 ");
102
+ })
103
+ it("should ignore sorrounded spaces ", () => {
104
+ expect(toNumber(" +1212 ")).toEqual(1212);
105
+ })
99
106
 
100
107
  });