@choksheak/ts-utils 0.2.2 → 0.2.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/logging.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function stringify(u: unknown): string;
2
+ export declare function capLength(u: unknown, maxLength?: number): string;
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@choksheak/ts-utils",
3
3
  "license": "The Unlicense",
4
- "version": "0.2.2",
4
+ "version": "0.2.3",
5
5
  "description": "Random Typescript utilities with support for full tree-shaking",
6
6
  "private": false,
7
7
  "scripts": {
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
+ }