@forklaunch/common 0.2.9 → 0.2.11
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/lib/index.d.mts +35 -1
- package/lib/index.d.ts +35 -1
- package/lib/index.js +82 -0
- package/lib/index.mjs +81 -0
- package/package.json +4 -4
package/lib/index.d.mts
CHANGED
|
@@ -57,6 +57,40 @@ declare function isTrue(value: true): true;
|
|
|
57
57
|
*/
|
|
58
58
|
declare function noop(): void;
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Safely stringifies any JavaScript value, handling special cases like:
|
|
62
|
+
* - Circular references
|
|
63
|
+
* - Error objects
|
|
64
|
+
* - BigInt
|
|
65
|
+
* - Functions
|
|
66
|
+
* - Symbols
|
|
67
|
+
* - Special number values (NaN, Infinity)
|
|
68
|
+
* - Built-in objects (Date, RegExp)
|
|
69
|
+
* - Collections (Map, Set)
|
|
70
|
+
* - TypedArrays and ArrayBuffers
|
|
71
|
+
*
|
|
72
|
+
* @param arg - The value to stringify
|
|
73
|
+
* @returns A string representation of the value
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* // Handle circular references
|
|
78
|
+
* const circular = { a: 1 };
|
|
79
|
+
* circular.self = circular;
|
|
80
|
+
* safeStringify(circular); // '{"a":1,"self":"[Circular Reference]"}'
|
|
81
|
+
*
|
|
82
|
+
* // Handle Error objects
|
|
83
|
+
* safeStringify(new Error("test")); // '{"name":"Error","message":"test","stack":"..."}'
|
|
84
|
+
*
|
|
85
|
+
* // Handle special types
|
|
86
|
+
* safeStringify(BigInt(123)); // "123n"
|
|
87
|
+
* safeStringify(Symbol("test")); // "Symbol(test)"
|
|
88
|
+
* safeStringify(() => {}); // "[Function: anonymous]"
|
|
89
|
+
* safeStringify(new Map([["key", "value"]])); // '{"__type":"Map","value":[["key","value"]]}'
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare function safeStringify(arg: unknown): string;
|
|
93
|
+
|
|
60
94
|
/**
|
|
61
95
|
* Recursively sorts the keys of an object and its nested objects alphabetically.
|
|
62
96
|
* This is useful for consistent object serialization and comparison.
|
|
@@ -204,4 +238,4 @@ type Prettify<T> = {
|
|
|
204
238
|
*/
|
|
205
239
|
type RemoveTrailingSlash<T extends string> = T extends `${infer Route}/` ? Route : T;
|
|
206
240
|
|
|
207
|
-
export { type Flatten, type FlattenKeys, type FlattenValues, type IdDto, type IdsDto, type InstanceTypeRecord, type MakePropertyOptionalIfChildrenOptional, type Prettify, type RecordTimingDto, type RemoveTrailingSlash, type ReturnTypeRecord, extractArgumentNames, isNever, isRecord, isTrue, noop, sortObjectKeys, stripUndefinedProperties };
|
|
241
|
+
export { type Flatten, type FlattenKeys, type FlattenValues, type IdDto, type IdsDto, type InstanceTypeRecord, type MakePropertyOptionalIfChildrenOptional, type Prettify, type RecordTimingDto, type RemoveTrailingSlash, type ReturnTypeRecord, extractArgumentNames, isNever, isRecord, isTrue, noop, safeStringify, sortObjectKeys, stripUndefinedProperties };
|
package/lib/index.d.ts
CHANGED
|
@@ -57,6 +57,40 @@ declare function isTrue(value: true): true;
|
|
|
57
57
|
*/
|
|
58
58
|
declare function noop(): void;
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Safely stringifies any JavaScript value, handling special cases like:
|
|
62
|
+
* - Circular references
|
|
63
|
+
* - Error objects
|
|
64
|
+
* - BigInt
|
|
65
|
+
* - Functions
|
|
66
|
+
* - Symbols
|
|
67
|
+
* - Special number values (NaN, Infinity)
|
|
68
|
+
* - Built-in objects (Date, RegExp)
|
|
69
|
+
* - Collections (Map, Set)
|
|
70
|
+
* - TypedArrays and ArrayBuffers
|
|
71
|
+
*
|
|
72
|
+
* @param arg - The value to stringify
|
|
73
|
+
* @returns A string representation of the value
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* // Handle circular references
|
|
78
|
+
* const circular = { a: 1 };
|
|
79
|
+
* circular.self = circular;
|
|
80
|
+
* safeStringify(circular); // '{"a":1,"self":"[Circular Reference]"}'
|
|
81
|
+
*
|
|
82
|
+
* // Handle Error objects
|
|
83
|
+
* safeStringify(new Error("test")); // '{"name":"Error","message":"test","stack":"..."}'
|
|
84
|
+
*
|
|
85
|
+
* // Handle special types
|
|
86
|
+
* safeStringify(BigInt(123)); // "123n"
|
|
87
|
+
* safeStringify(Symbol("test")); // "Symbol(test)"
|
|
88
|
+
* safeStringify(() => {}); // "[Function: anonymous]"
|
|
89
|
+
* safeStringify(new Map([["key", "value"]])); // '{"__type":"Map","value":[["key","value"]]}'
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare function safeStringify(arg: unknown): string;
|
|
93
|
+
|
|
60
94
|
/**
|
|
61
95
|
* Recursively sorts the keys of an object and its nested objects alphabetically.
|
|
62
96
|
* This is useful for consistent object serialization and comparison.
|
|
@@ -204,4 +238,4 @@ type Prettify<T> = {
|
|
|
204
238
|
*/
|
|
205
239
|
type RemoveTrailingSlash<T extends string> = T extends `${infer Route}/` ? Route : T;
|
|
206
240
|
|
|
207
|
-
export { type Flatten, type FlattenKeys, type FlattenValues, type IdDto, type IdsDto, type InstanceTypeRecord, type MakePropertyOptionalIfChildrenOptional, type Prettify, type RecordTimingDto, type RemoveTrailingSlash, type ReturnTypeRecord, extractArgumentNames, isNever, isRecord, isTrue, noop, sortObjectKeys, stripUndefinedProperties };
|
|
241
|
+
export { type Flatten, type FlattenKeys, type FlattenValues, type IdDto, type IdsDto, type InstanceTypeRecord, type MakePropertyOptionalIfChildrenOptional, type Prettify, type RecordTimingDto, type RemoveTrailingSlash, type ReturnTypeRecord, extractArgumentNames, isNever, isRecord, isTrue, noop, safeStringify, sortObjectKeys, stripUndefinedProperties };
|
package/lib/index.js
CHANGED
|
@@ -25,6 +25,7 @@ __export(index_exports, {
|
|
|
25
25
|
isRecord: () => isRecord,
|
|
26
26
|
isTrue: () => isTrue,
|
|
27
27
|
noop: () => noop,
|
|
28
|
+
safeStringify: () => safeStringify,
|
|
28
29
|
sortObjectKeys: () => sortObjectKeys,
|
|
29
30
|
stripUndefinedProperties: () => stripUndefinedProperties
|
|
30
31
|
});
|
|
@@ -77,6 +78,86 @@ function isTrue(value) {
|
|
|
77
78
|
function noop() {
|
|
78
79
|
}
|
|
79
80
|
|
|
81
|
+
// src/safeStringify.ts
|
|
82
|
+
function safeStringify(arg) {
|
|
83
|
+
if (typeof arg === "string") {
|
|
84
|
+
return arg;
|
|
85
|
+
}
|
|
86
|
+
if (arg == null) {
|
|
87
|
+
return String(arg);
|
|
88
|
+
}
|
|
89
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
90
|
+
const replacer = (key, value) => {
|
|
91
|
+
if (value && typeof value === "object") {
|
|
92
|
+
if (seen.has(value)) {
|
|
93
|
+
return "[Circular Reference]";
|
|
94
|
+
}
|
|
95
|
+
seen.add(value);
|
|
96
|
+
}
|
|
97
|
+
if (value instanceof Error) {
|
|
98
|
+
return {
|
|
99
|
+
name: value.name,
|
|
100
|
+
message: value.message,
|
|
101
|
+
stack: value.stack,
|
|
102
|
+
cause: value.cause
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
if (typeof value === "bigint") {
|
|
106
|
+
return value.toString() + "n";
|
|
107
|
+
}
|
|
108
|
+
if (typeof value === "function") {
|
|
109
|
+
return `[Function: ${value.name || "anonymous"}]`;
|
|
110
|
+
}
|
|
111
|
+
if (typeof value === "symbol") {
|
|
112
|
+
return value.toString();
|
|
113
|
+
}
|
|
114
|
+
if (typeof value === "number") {
|
|
115
|
+
if (Number.isNaN(value)) return "NaN";
|
|
116
|
+
if (value === Infinity) return "Infinity";
|
|
117
|
+
if (value === -Infinity) return "-Infinity";
|
|
118
|
+
}
|
|
119
|
+
if (value instanceof Date) {
|
|
120
|
+
return value.toISOString();
|
|
121
|
+
}
|
|
122
|
+
if (value instanceof RegExp) {
|
|
123
|
+
return value.toString();
|
|
124
|
+
}
|
|
125
|
+
if (value instanceof Map) {
|
|
126
|
+
return {
|
|
127
|
+
__type: "Map",
|
|
128
|
+
value: Array.from(value.entries())
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
if (value instanceof Set) {
|
|
132
|
+
return {
|
|
133
|
+
__type: "Set",
|
|
134
|
+
value: Array.from(value.values())
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
if (ArrayBuffer.isView(value)) {
|
|
138
|
+
return {
|
|
139
|
+
__type: value.constructor.name,
|
|
140
|
+
value: Array.from(value)
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (value instanceof ArrayBuffer) {
|
|
144
|
+
return {
|
|
145
|
+
__type: "ArrayBuffer",
|
|
146
|
+
value: Array.from(new Uint8Array(value))
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return value;
|
|
150
|
+
};
|
|
151
|
+
try {
|
|
152
|
+
return JSON.stringify(arg, replacer);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (error instanceof Error) {
|
|
155
|
+
return `[Unserializable: ${error.message}]`;
|
|
156
|
+
}
|
|
157
|
+
return "[Unserializable: Unknown error]";
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
80
161
|
// src/sortObjectKeys.ts
|
|
81
162
|
function sortObjectKeys(obj) {
|
|
82
163
|
if (typeof obj !== "object" || obj === null) {
|
|
@@ -105,6 +186,7 @@ function stripUndefinedProperties(obj) {
|
|
|
105
186
|
isRecord,
|
|
106
187
|
isTrue,
|
|
107
188
|
noop,
|
|
189
|
+
safeStringify,
|
|
108
190
|
sortObjectKeys,
|
|
109
191
|
stripUndefinedProperties
|
|
110
192
|
});
|
package/lib/index.mjs
CHANGED
|
@@ -45,6 +45,86 @@ function isTrue(value) {
|
|
|
45
45
|
function noop() {
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
// src/safeStringify.ts
|
|
49
|
+
function safeStringify(arg) {
|
|
50
|
+
if (typeof arg === "string") {
|
|
51
|
+
return arg;
|
|
52
|
+
}
|
|
53
|
+
if (arg == null) {
|
|
54
|
+
return String(arg);
|
|
55
|
+
}
|
|
56
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
57
|
+
const replacer = (key, value) => {
|
|
58
|
+
if (value && typeof value === "object") {
|
|
59
|
+
if (seen.has(value)) {
|
|
60
|
+
return "[Circular Reference]";
|
|
61
|
+
}
|
|
62
|
+
seen.add(value);
|
|
63
|
+
}
|
|
64
|
+
if (value instanceof Error) {
|
|
65
|
+
return {
|
|
66
|
+
name: value.name,
|
|
67
|
+
message: value.message,
|
|
68
|
+
stack: value.stack,
|
|
69
|
+
cause: value.cause
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (typeof value === "bigint") {
|
|
73
|
+
return value.toString() + "n";
|
|
74
|
+
}
|
|
75
|
+
if (typeof value === "function") {
|
|
76
|
+
return `[Function: ${value.name || "anonymous"}]`;
|
|
77
|
+
}
|
|
78
|
+
if (typeof value === "symbol") {
|
|
79
|
+
return value.toString();
|
|
80
|
+
}
|
|
81
|
+
if (typeof value === "number") {
|
|
82
|
+
if (Number.isNaN(value)) return "NaN";
|
|
83
|
+
if (value === Infinity) return "Infinity";
|
|
84
|
+
if (value === -Infinity) return "-Infinity";
|
|
85
|
+
}
|
|
86
|
+
if (value instanceof Date) {
|
|
87
|
+
return value.toISOString();
|
|
88
|
+
}
|
|
89
|
+
if (value instanceof RegExp) {
|
|
90
|
+
return value.toString();
|
|
91
|
+
}
|
|
92
|
+
if (value instanceof Map) {
|
|
93
|
+
return {
|
|
94
|
+
__type: "Map",
|
|
95
|
+
value: Array.from(value.entries())
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if (value instanceof Set) {
|
|
99
|
+
return {
|
|
100
|
+
__type: "Set",
|
|
101
|
+
value: Array.from(value.values())
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (ArrayBuffer.isView(value)) {
|
|
105
|
+
return {
|
|
106
|
+
__type: value.constructor.name,
|
|
107
|
+
value: Array.from(value)
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (value instanceof ArrayBuffer) {
|
|
111
|
+
return {
|
|
112
|
+
__type: "ArrayBuffer",
|
|
113
|
+
value: Array.from(new Uint8Array(value))
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
return value;
|
|
117
|
+
};
|
|
118
|
+
try {
|
|
119
|
+
return JSON.stringify(arg, replacer);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
if (error instanceof Error) {
|
|
122
|
+
return `[Unserializable: ${error.message}]`;
|
|
123
|
+
}
|
|
124
|
+
return "[Unserializable: Unknown error]";
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
48
128
|
// src/sortObjectKeys.ts
|
|
49
129
|
function sortObjectKeys(obj) {
|
|
50
130
|
if (typeof obj !== "object" || obj === null) {
|
|
@@ -72,6 +152,7 @@ export {
|
|
|
72
152
|
isRecord,
|
|
73
153
|
isTrue,
|
|
74
154
|
noop,
|
|
155
|
+
safeStringify,
|
|
75
156
|
sortObjectKeys,
|
|
76
157
|
stripUndefinedProperties
|
|
77
158
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forklaunch/common",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"description": "Common package for base types, interfaces, implementations.",
|
|
5
5
|
"homepage": "https://github.com/forklaunch/forklaunch-js#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -28,12 +28,12 @@
|
|
|
28
28
|
"lib/**"
|
|
29
29
|
],
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@eslint/js": "^9.
|
|
31
|
+
"@eslint/js": "^9.25.0",
|
|
32
32
|
"depcheck": "^1.4.7",
|
|
33
|
-
"eslint": "^9.
|
|
33
|
+
"eslint": "^9.25.0",
|
|
34
34
|
"globals": "^16.0.0",
|
|
35
35
|
"tsup": "^8.4.0",
|
|
36
|
-
"typedoc": "^0.28.
|
|
36
|
+
"typedoc": "^0.28.3",
|
|
37
37
|
"typescript": "^5.8.3",
|
|
38
38
|
"typescript-eslint": "^8.30.1",
|
|
39
39
|
"vitest": "^3.1.1"
|