@choksheak/ts-utils 0.2.3 → 0.2.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/package.json +1 -1
- package/round.d.ts +12 -0
- package/round.js +39 -0
- package/round.js.map +1 -0
- package/safeParseFloat.d.ts +4 -0
- package/safeParseFloat.js +34 -0
- package/safeParseFloat.js.map +1 -0
- package/safeParseInt.d.ts +2 -5
- package/safeParseInt.js +2 -8
- package/safeParseInt.js.map +1 -1
- package/src/round.ts +18 -0
- package/src/safeParseFloat.ts +10 -0
- package/src/safeParseInt.ts +6 -14
package/package.json
CHANGED
package/round.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simplifies rounding of floating point numbers. Note that if your number ends
|
|
3
|
+
* with zeros, and you convert the number to string, you will lose the zeroes
|
|
4
|
+
* at the end. To show the exact number of decimal places, you'll need to use
|
|
5
|
+
* toFixed(). E.g. round(1.20, 2).toFixed(2).
|
|
6
|
+
*/
|
|
7
|
+
export declare function round(n: number, numDecimalPlaces?: number): number;
|
|
8
|
+
/**
|
|
9
|
+
* Returns a string with the number in the exact number of decimal places
|
|
10
|
+
* specified, in case the number ends with zeroes.
|
|
11
|
+
*/
|
|
12
|
+
export declare function roundS(n: number, numDecimalPlaces?: number): string;
|
package/round.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/round.ts
|
|
21
|
+
var round_exports = {};
|
|
22
|
+
__export(round_exports, {
|
|
23
|
+
round: () => round,
|
|
24
|
+
roundS: () => roundS
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(round_exports);
|
|
27
|
+
function round(n, numDecimalPlaces = 0) {
|
|
28
|
+
const multipler = Math.pow(10, numDecimalPlaces);
|
|
29
|
+
return Math.round(n * multipler) / multipler;
|
|
30
|
+
}
|
|
31
|
+
function roundS(n, numDecimalPlaces = 0) {
|
|
32
|
+
return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);
|
|
33
|
+
}
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
round,
|
|
37
|
+
roundS
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=round.js.map
|
package/round.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/round.ts"],"sourcesContent":["/**\n * Simplifies rounding of floating point numbers. Note that if your number ends\n * with zeros, and you convert the number to string, you will lose the zeroes\n * at the end. To show the exact number of decimal places, you'll need to use\n * toFixed(). E.g. round(1.20, 2).toFixed(2).\n */\nexport function round(n: number, numDecimalPlaces = 0): number {\n const multipler = Math.pow(10, numDecimalPlaces);\n return Math.round(n * multipler) / multipler;\n}\n\n/**\n * Returns a string with the number in the exact number of decimal places\n * specified, in case the number ends with zeroes.\n */\nexport function roundS(n: number, numDecimalPlaces = 0): string {\n return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMO,SAAS,MAAM,GAAW,mBAAmB,GAAW;AAC7D,QAAM,YAAY,KAAK,IAAI,IAAI,gBAAgB;AAC/C,SAAO,KAAK,MAAM,IAAI,SAAS,IAAI;AACrC;AAMO,SAAS,OAAO,GAAW,mBAAmB,GAAW;AAC9D,SAAO,MAAM,GAAG,gBAAgB,EAAE,QAAQ,gBAAgB;AAC5D;","names":[]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/safeParseFloat.ts
|
|
21
|
+
var safeParseFloat_exports = {};
|
|
22
|
+
__export(safeParseFloat_exports, {
|
|
23
|
+
safeParseFloat: () => safeParseFloat
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(safeParseFloat_exports);
|
|
26
|
+
function safeParseFloat(s, defaultValue = 0) {
|
|
27
|
+
const i = Number(s);
|
|
28
|
+
return isNaN(i) ? defaultValue : i;
|
|
29
|
+
}
|
|
30
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
31
|
+
0 && (module.exports = {
|
|
32
|
+
safeParseFloat
|
|
33
|
+
});
|
|
34
|
+
//# sourceMappingURL=safeParseFloat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/safeParseFloat.ts"],"sourcesContent":["/**\n * Returns 0 or the given defaultValue if the string is not a valid number.\n */\nexport function safeParseFloat<T>(\n s: string,\n defaultValue: T | number = 0,\n): T | number {\n const i = Number(s);\n return isNaN(i) ? defaultValue : i;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,SAAS,eACd,GACA,eAA2B,GACf;AACZ,QAAM,IAAI,OAAO,CAAC;AAClB,SAAO,MAAM,CAAC,IAAI,eAAe;AACnC;","names":[]}
|
package/safeParseInt.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Returns 0 if the string is not a valid number.
|
|
3
|
-
*
|
|
4
|
-
* @param logError Log a console error if the given string is not a valid
|
|
5
|
-
* number. Defaults to false (don't log anything).
|
|
2
|
+
* Returns 0 or the given defaultValue if the string is not a valid number.
|
|
6
3
|
*/
|
|
7
|
-
export declare function safeParseInt(s: string,
|
|
4
|
+
export declare function safeParseInt<T>(s: string, defaultValue?: T | number): T | number;
|
package/safeParseInt.js
CHANGED
|
@@ -23,15 +23,9 @@ __export(safeParseInt_exports, {
|
|
|
23
23
|
safeParseInt: () => safeParseInt
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(safeParseInt_exports);
|
|
26
|
-
function safeParseInt(s,
|
|
26
|
+
function safeParseInt(s, defaultValue = 0) {
|
|
27
27
|
const i = Number(s);
|
|
28
|
-
|
|
29
|
-
if (logError) {
|
|
30
|
-
console.error(`Not a number: "${s}"`);
|
|
31
|
-
}
|
|
32
|
-
return 0;
|
|
33
|
-
}
|
|
34
|
-
return Math.floor(i);
|
|
28
|
+
return isNaN(i) ? defaultValue : Math.floor(i);
|
|
35
29
|
}
|
|
36
30
|
// Annotate the CommonJS export names for ESM import in node:
|
|
37
31
|
0 && (module.exports = {
|
package/safeParseInt.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/safeParseInt.ts"],"sourcesContent":["/**\n * Returns 0
|
|
1
|
+
{"version":3,"sources":["../src/safeParseInt.ts"],"sourcesContent":["/**\n * Returns 0 or the given defaultValue if the string is not a valid number.\n */\nexport function safeParseInt<T>(\n s: string,\n defaultValue: T | number = 0,\n): T | number {\n const i = Number(s);\n return isNaN(i) ? defaultValue : Math.floor(i);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,SAAS,aACd,GACA,eAA2B,GACf;AACZ,QAAM,IAAI,OAAO,CAAC;AAClB,SAAO,MAAM,CAAC,IAAI,eAAe,KAAK,MAAM,CAAC;AAC/C;","names":[]}
|
package/src/round.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simplifies rounding of floating point numbers. Note that if your number ends
|
|
3
|
+
* with zeros, and you convert the number to string, you will lose the zeroes
|
|
4
|
+
* at the end. To show the exact number of decimal places, you'll need to use
|
|
5
|
+
* toFixed(). E.g. round(1.20, 2).toFixed(2).
|
|
6
|
+
*/
|
|
7
|
+
export function round(n: number, numDecimalPlaces = 0): number {
|
|
8
|
+
const multipler = Math.pow(10, numDecimalPlaces);
|
|
9
|
+
return Math.round(n * multipler) / multipler;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Returns a string with the number in the exact number of decimal places
|
|
14
|
+
* specified, in case the number ends with zeroes.
|
|
15
|
+
*/
|
|
16
|
+
export function roundS(n: number, numDecimalPlaces = 0): string {
|
|
17
|
+
return round(n, numDecimalPlaces).toFixed(numDecimalPlaces);
|
|
18
|
+
}
|
package/src/safeParseInt.ts
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Returns 0 if the string is not a valid number.
|
|
3
|
-
*
|
|
4
|
-
* @param logError Log a console error if the given string is not a valid
|
|
5
|
-
* number. Defaults to false (don't log anything).
|
|
2
|
+
* Returns 0 or the given defaultValue if the string is not a valid number.
|
|
6
3
|
*/
|
|
7
|
-
export function safeParseInt(
|
|
4
|
+
export function safeParseInt<T>(
|
|
5
|
+
s: string,
|
|
6
|
+
defaultValue: T | number = 0,
|
|
7
|
+
): T | number {
|
|
8
8
|
const i = Number(s);
|
|
9
|
-
|
|
10
|
-
if (isNaN(i)) {
|
|
11
|
-
if (logError) {
|
|
12
|
-
console.error(`Not a number: "${s}"`);
|
|
13
|
-
}
|
|
14
|
-
return 0;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return Math.floor(i);
|
|
9
|
+
return isNaN(i) ? defaultValue : Math.floor(i);
|
|
18
10
|
}
|