strnum 2.0.4 → 2.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.
- package/CHANGELOG.md +9 -0
- package/package.json +1 -1
- package/strnum.js +4 -4
- package/strnum.test.js +2 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/strnum.js
CHANGED
|
@@ -16,11 +16,11 @@ const consider = {
|
|
|
16
16
|
export default function toNumber(str, options = {}){
|
|
17
17
|
options = Object.assign({}, consider, options );
|
|
18
18
|
if(!str || typeof str !== "string" ) return str;
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
|
|
21
20
|
let trimmedStr = str.trim();
|
|
22
|
-
|
|
21
|
+
|
|
23
22
|
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
23
|
+
else if(str==="0") return 0;
|
|
24
24
|
else if (options.hex && hexRegex.test(trimmedStr)) {
|
|
25
25
|
return parse_int(trimmedStr, 16);
|
|
26
26
|
// }else if (options.oct && octRegex.test(str)) {
|
|
@@ -106,4 +106,4 @@ function parse_int(numStr, base){
|
|
|
106
106
|
else if(Number.parseInt) return Number.parseInt(numStr, base);
|
|
107
107
|
else if(window && window.parseInt) return window.parseInt(numStr, base);
|
|
108
108
|
else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
|
|
109
|
-
}
|
|
109
|
+
}
|
package/strnum.test.js
CHANGED
|
@@ -7,6 +7,7 @@ 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
|
+
|
|
10
11
|
});
|
|
11
12
|
it("should not parse number with spaces or comma", () => {
|
|
12
13
|
expect(toNumber("12,12")).toEqual("12,12");
|
|
@@ -139,6 +140,7 @@ describe("Should convert all the valid numeric strings to number", () => {
|
|
|
139
140
|
});
|
|
140
141
|
|
|
141
142
|
it("should skip matching pattern", () => {
|
|
143
|
+
expect(toNumber("0", { skipLike: /.*/ })).toEqual("0");
|
|
142
144
|
expect(toNumber("+12", { skipLike: /\+[0-9]{10}/} )).toEqual(12);
|
|
143
145
|
expect(toNumber("12+12", { skipLike: /\+[0-9]{10}/} )).toEqual("12+12");
|
|
144
146
|
expect(toNumber("12+1212121212", { skipLike: /\+[0-9]{10}/} )).toEqual("12+1212121212");
|