@choksheak/ts-utils 0.2.2 → 0.2.4
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/logging.d.ts +2 -0
- package/logging.js +48 -0
- package/logging.js.map +1 -0
- package/package.json +1 -1
- 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/logging.ts +26 -0
- package/src/safeParseFloat.ts +10 -0
- package/src/safeParseInt.ts +6 -14
package/logging.d.ts
ADDED
package/logging.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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/logging.ts
|
|
21
|
+
var logging_exports = {};
|
|
22
|
+
__export(logging_exports, {
|
|
23
|
+
capLength: () => capLength,
|
|
24
|
+
stringify: () => stringify
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(logging_exports);
|
|
27
|
+
function stringify(u) {
|
|
28
|
+
if (typeof u === "string") {
|
|
29
|
+
return u;
|
|
30
|
+
}
|
|
31
|
+
if (u !== null && typeof u === "object" && u.toString !== Object.prototype.toString) {
|
|
32
|
+
return u.toString();
|
|
33
|
+
}
|
|
34
|
+
return JSON.stringify(u);
|
|
35
|
+
}
|
|
36
|
+
function capLength(u, maxLength = 400) {
|
|
37
|
+
const s = stringify(u);
|
|
38
|
+
if (s.length <= maxLength) {
|
|
39
|
+
return s;
|
|
40
|
+
}
|
|
41
|
+
return s.slice(0, maxLength) + ` ... (${s.length - maxLength} more)`;
|
|
42
|
+
}
|
|
43
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
44
|
+
0 && (module.exports = {
|
|
45
|
+
capLength,
|
|
46
|
+
stringify
|
|
47
|
+
});
|
|
48
|
+
//# sourceMappingURL=logging.js.map
|
package/logging.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/logging.ts"],"sourcesContent":["export function stringify(u: unknown): string {\n if (typeof u === \"string\") {\n return u;\n }\n\n // If the object has a custom toString(), then use it.\n if (\n u !== null &&\n typeof u === \"object\" &&\n u.toString !== Object.prototype.toString\n ) {\n return u.toString();\n }\n\n return JSON.stringify(u);\n}\n\nexport function capLength(u: unknown, maxLength = 400): string {\n const s = stringify(u);\n\n if (s.length <= maxLength) {\n return s;\n }\n\n return s.slice(0, maxLength) + ` ... (${s.length - maxLength} more)`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,SAAS,UAAU,GAAoB;AAC5C,MAAI,OAAO,MAAM,UAAU;AACzB,WAAO;AAAA,EACT;AAGA,MACE,MAAM,QACN,OAAO,MAAM,YACb,EAAE,aAAa,OAAO,UAAU,UAChC;AACA,WAAO,EAAE,SAAS;AAAA,EACpB;AAEA,SAAO,KAAK,UAAU,CAAC;AACzB;AAEO,SAAS,UAAU,GAAY,YAAY,KAAa;AAC7D,QAAM,IAAI,UAAU,CAAC;AAErB,MAAI,EAAE,UAAU,WAAW;AACzB,WAAO;AAAA,EACT;AAEA,SAAO,EAAE,MAAM,GAAG,SAAS,IAAI,SAAS,EAAE,SAAS,SAAS;AAC9D;","names":[]}
|
package/package.json
CHANGED
|
@@ -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/logging.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function stringify(u: unknown): string {
|
|
2
|
+
if (typeof u === "string") {
|
|
3
|
+
return u;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// If the object has a custom toString(), then use it.
|
|
7
|
+
if (
|
|
8
|
+
u !== null &&
|
|
9
|
+
typeof u === "object" &&
|
|
10
|
+
u.toString !== Object.prototype.toString
|
|
11
|
+
) {
|
|
12
|
+
return u.toString();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return JSON.stringify(u);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function capLength(u: unknown, maxLength = 400): string {
|
|
19
|
+
const s = stringify(u);
|
|
20
|
+
|
|
21
|
+
if (s.length <= maxLength) {
|
|
22
|
+
return s;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return s.slice(0, maxLength) + ` ... (${s.length - maxLength} more)`;
|
|
26
|
+
}
|
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
|
}
|