@ls-stack/utils 3.23.0 → 3.24.1
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/docs/testUtils.md +27 -3
- package/docs/yamlStringify.md +53 -15
- package/lib/chunk-JAPKLFIK.js +353 -0
- package/lib/testUtils.cjs +576 -0
- package/lib/testUtils.d.cts +67 -1
- package/lib/testUtils.d.ts +67 -1
- package/lib/testUtils.js +210 -1
- package/lib/yamlStringify.cjs +83 -30
- package/lib/yamlStringify.d.cts +5 -3
- package/lib/yamlStringify.d.ts +5 -3
- package/lib/yamlStringify.js +4 -291
- package/package.json +1 -1
package/lib/yamlStringify.js
CHANGED
|
@@ -1,296 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
} from "./chunk-4REIIZQY.js";
|
|
7
|
-
import {
|
|
8
|
-
isObject,
|
|
9
|
-
isPlainObject
|
|
10
|
-
} from "./chunk-C2SVCIWE.js";
|
|
2
|
+
yamlStringify
|
|
3
|
+
} from "./chunk-JAPKLFIK.js";
|
|
4
|
+
import "./chunk-IATIXMCE.js";
|
|
5
|
+
import "./chunk-4REIIZQY.js";
|
|
11
6
|
import "./chunk-JF2MDHOJ.js";
|
|
12
|
-
|
|
13
|
-
// src/yamlStringify.ts
|
|
14
|
-
function yamlStringify(obj, {
|
|
15
|
-
maxLineLength = 100,
|
|
16
|
-
showUndefined,
|
|
17
|
-
maxDepth = 50,
|
|
18
|
-
addRootObjSpaces = "beforeAndAfter"
|
|
19
|
-
} = {}) {
|
|
20
|
-
if (isObject(obj) || Array.isArray(obj) || typeof obj === "function") {
|
|
21
|
-
return `${stringifyValue(obj, "", maxLineLength, !!showUndefined, maxDepth, 0, addRootObjSpaces)}
|
|
22
|
-
`;
|
|
23
|
-
}
|
|
24
|
-
return JSON.stringify(obj) || "undefined";
|
|
25
|
-
}
|
|
26
|
-
function stringifyValue(value, indent, maxLineLength, showUndefined, maxDepth, depth, addObjSpaces) {
|
|
27
|
-
let result = "";
|
|
28
|
-
const childIndent = `${indent} `;
|
|
29
|
-
if (isPlainObject(value)) {
|
|
30
|
-
if (Object.keys(value).length === 0) {
|
|
31
|
-
return "{}";
|
|
32
|
-
}
|
|
33
|
-
let prevValue;
|
|
34
|
-
let afterSpaceWasAdded = false;
|
|
35
|
-
for (let [key, objVal] of Object.entries(value)) {
|
|
36
|
-
if (objVal === void 0 && !showUndefined) {
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
if (depth > maxDepth) {
|
|
40
|
-
objVal = `{max depth reached}`;
|
|
41
|
-
}
|
|
42
|
-
const normalizedValue2 = normalizeValue(objVal);
|
|
43
|
-
if (normalizedValue2 !== null) {
|
|
44
|
-
objVal = normalizedValue2[1];
|
|
45
|
-
key = `${key}{${normalizedValue2[0]}}`;
|
|
46
|
-
}
|
|
47
|
-
const valueString = stringifyValue(
|
|
48
|
-
objVal,
|
|
49
|
-
childIndent,
|
|
50
|
-
maxLineLength,
|
|
51
|
-
showUndefined,
|
|
52
|
-
maxDepth,
|
|
53
|
-
depth + 1,
|
|
54
|
-
addObjSpaces
|
|
55
|
-
);
|
|
56
|
-
if (!afterSpaceWasAdded && indent === "" && isObject(objVal) && prevValue && (addObjSpaces === "before" || addObjSpaces === "beforeAndAfter")) {
|
|
57
|
-
result += "\n";
|
|
58
|
-
}
|
|
59
|
-
if (Array.isArray(objVal)) {
|
|
60
|
-
const arrayIsSingleLine = valueString.split("\n").length === 1;
|
|
61
|
-
if (arrayIsSingleLine && !valueString.trim().startsWith("-")) {
|
|
62
|
-
result += `${indent}${key}: `;
|
|
63
|
-
} else {
|
|
64
|
-
result += `${indent}${key}:
|
|
65
|
-
`;
|
|
66
|
-
}
|
|
67
|
-
} else if (isObject(objVal)) {
|
|
68
|
-
if (Object.keys(objVal).length === 0) {
|
|
69
|
-
result += `${indent}${key}: `;
|
|
70
|
-
} else {
|
|
71
|
-
result += `${indent}${key}:
|
|
72
|
-
`;
|
|
73
|
-
}
|
|
74
|
-
} else {
|
|
75
|
-
result += `${indent}${key}: `;
|
|
76
|
-
}
|
|
77
|
-
result += valueString;
|
|
78
|
-
result += "\n";
|
|
79
|
-
if (indent === "") {
|
|
80
|
-
if (isObject(objVal)) {
|
|
81
|
-
if (addObjSpaces === "after" || addObjSpaces === "beforeAndAfter") {
|
|
82
|
-
result += "\n";
|
|
83
|
-
afterSpaceWasAdded = true;
|
|
84
|
-
} else {
|
|
85
|
-
afterSpaceWasAdded = false;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
prevValue = objVal;
|
|
90
|
-
}
|
|
91
|
-
return result.trimEnd();
|
|
92
|
-
}
|
|
93
|
-
if (Array.isArray(value)) {
|
|
94
|
-
let arrayWasAdded = false;
|
|
95
|
-
if (value.length === 0 || value.every(
|
|
96
|
-
(item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null || item === void 0
|
|
97
|
-
)) {
|
|
98
|
-
let line = "";
|
|
99
|
-
line += `[`;
|
|
100
|
-
line += value.map((item) => {
|
|
101
|
-
let valueToUse = item;
|
|
102
|
-
if (depth > maxDepth) {
|
|
103
|
-
valueToUse = `{max depth reached}`;
|
|
104
|
-
}
|
|
105
|
-
if (typeof valueToUse === "string" && valueToUse.includes("\n")) {
|
|
106
|
-
valueToUse = valueToUse.replace(/\n/g, "\\n");
|
|
107
|
-
}
|
|
108
|
-
return stringifyValue(
|
|
109
|
-
valueToUse,
|
|
110
|
-
"",
|
|
111
|
-
maxLineLength,
|
|
112
|
-
showUndefined,
|
|
113
|
-
maxDepth,
|
|
114
|
-
depth + 1,
|
|
115
|
-
addObjSpaces
|
|
116
|
-
);
|
|
117
|
-
}).join(", ");
|
|
118
|
-
line += "]";
|
|
119
|
-
if (line.length <= maxLineLength) {
|
|
120
|
-
result += line;
|
|
121
|
-
arrayWasAdded = true;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
if (!arrayWasAdded) {
|
|
125
|
-
for (let item of value) {
|
|
126
|
-
if (depth > maxDepth) {
|
|
127
|
-
item = `{max depth reached}`;
|
|
128
|
-
}
|
|
129
|
-
result += `${indent}- `;
|
|
130
|
-
if (Array.isArray(item) || isObject(item)) {
|
|
131
|
-
let arrayString = stringifyValue(
|
|
132
|
-
item,
|
|
133
|
-
childIndent,
|
|
134
|
-
maxLineLength,
|
|
135
|
-
showUndefined,
|
|
136
|
-
maxDepth,
|
|
137
|
-
depth + 1,
|
|
138
|
-
addObjSpaces
|
|
139
|
-
);
|
|
140
|
-
arrayString = arrayString.trimStart();
|
|
141
|
-
result += arrayString;
|
|
142
|
-
} else {
|
|
143
|
-
result += stringifyValue(
|
|
144
|
-
item,
|
|
145
|
-
childIndent,
|
|
146
|
-
maxLineLength,
|
|
147
|
-
showUndefined,
|
|
148
|
-
maxDepth,
|
|
149
|
-
depth + 1,
|
|
150
|
-
addObjSpaces
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
result += "\n";
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return result.trimEnd();
|
|
157
|
-
}
|
|
158
|
-
if (typeof value === "string") {
|
|
159
|
-
if (value.includes("\n")) {
|
|
160
|
-
const lines = value.split("\n");
|
|
161
|
-
for (const [i, line] of lines.entries()) {
|
|
162
|
-
if (i === 0) {
|
|
163
|
-
if (value.endsWith("\n")) {
|
|
164
|
-
result += `|`;
|
|
165
|
-
} else {
|
|
166
|
-
result += `|-`;
|
|
167
|
-
}
|
|
168
|
-
result += `
|
|
169
|
-
${indent}${line}
|
|
170
|
-
`;
|
|
171
|
-
} else {
|
|
172
|
-
result += `${indent}${line}
|
|
173
|
-
`;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
} else {
|
|
177
|
-
if (value.includes("'") && !value.includes('"')) {
|
|
178
|
-
result += `"${value}"`;
|
|
179
|
-
} else if (value.includes('"') && !value.includes("'")) {
|
|
180
|
-
result += `'${value}'`;
|
|
181
|
-
} else if (value.includes("'") && value.includes('"')) {
|
|
182
|
-
result += `"${value.replace(/"/g, '\\"')}"`;
|
|
183
|
-
} else {
|
|
184
|
-
result += `'${value}'`;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return result.trimEnd();
|
|
188
|
-
}
|
|
189
|
-
if (typeof value === "number" || typeof value === "boolean" || value === null || value === void 0) {
|
|
190
|
-
return String(value).trimEnd();
|
|
191
|
-
}
|
|
192
|
-
const normalizedValue = normalizeValue(value);
|
|
193
|
-
if (normalizedValue !== null) {
|
|
194
|
-
return stringifyValue(
|
|
195
|
-
{
|
|
196
|
-
[`${normalizedValue[0]}#`]: normalizedValue[1]
|
|
197
|
-
},
|
|
198
|
-
indent,
|
|
199
|
-
maxLineLength,
|
|
200
|
-
showUndefined,
|
|
201
|
-
maxDepth,
|
|
202
|
-
depth + 1,
|
|
203
|
-
addObjSpaces
|
|
204
|
-
);
|
|
205
|
-
}
|
|
206
|
-
return JSON.stringify(value);
|
|
207
|
-
}
|
|
208
|
-
function normalizeValue(value) {
|
|
209
|
-
if (value === null || isPlainObject(value) || Array.isArray(value)) {
|
|
210
|
-
return null;
|
|
211
|
-
}
|
|
212
|
-
if (value instanceof Map) {
|
|
213
|
-
const mapEntries = Array.from(value.entries());
|
|
214
|
-
let mapValue;
|
|
215
|
-
if (mapEntries.every(([key]) => typeof key === "string")) {
|
|
216
|
-
const mapObjValue = {};
|
|
217
|
-
for (const [key, val] of mapEntries) {
|
|
218
|
-
mapObjValue[key] = val;
|
|
219
|
-
}
|
|
220
|
-
mapValue = mapObjValue;
|
|
221
|
-
} else {
|
|
222
|
-
mapValue = mapEntries.map(([key, val]) => ({
|
|
223
|
-
key,
|
|
224
|
-
value: val
|
|
225
|
-
}));
|
|
226
|
-
}
|
|
227
|
-
return ["Map", mapValue];
|
|
228
|
-
}
|
|
229
|
-
if (value instanceof Set) {
|
|
230
|
-
const setValue = Array.from(value);
|
|
231
|
-
return ["Set", setValue];
|
|
232
|
-
}
|
|
233
|
-
if (value instanceof Date) {
|
|
234
|
-
return ["Date", value.toISOString()];
|
|
235
|
-
}
|
|
236
|
-
if (value instanceof RegExp) {
|
|
237
|
-
return ["RegExp", value.toString()];
|
|
238
|
-
}
|
|
239
|
-
if (value instanceof Error) {
|
|
240
|
-
return [
|
|
241
|
-
"Error",
|
|
242
|
-
{
|
|
243
|
-
message: value.message,
|
|
244
|
-
name: value.name,
|
|
245
|
-
stack: value.stack
|
|
246
|
-
}
|
|
247
|
-
];
|
|
248
|
-
}
|
|
249
|
-
if (value instanceof File) {
|
|
250
|
-
return [
|
|
251
|
-
"File",
|
|
252
|
-
{
|
|
253
|
-
name: value.name,
|
|
254
|
-
type: value.type,
|
|
255
|
-
lastModified: new Date(value.lastModified).toISOString(),
|
|
256
|
-
size: bytesToHumanReadable(value.size)
|
|
257
|
-
}
|
|
258
|
-
];
|
|
259
|
-
}
|
|
260
|
-
if (typeof value === "object") {
|
|
261
|
-
if ("toJSON" in value && typeof value.toJSON === "function") {
|
|
262
|
-
return [value.constructor.name, value.toJSON()];
|
|
263
|
-
}
|
|
264
|
-
if ("toString" in value && typeof value.toString === "function") {
|
|
265
|
-
const stringValue = value.toString();
|
|
266
|
-
if (stringValue.toString() !== "[object Object]") {
|
|
267
|
-
return [value.constructor.name, stringValue];
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
const objectValue = { ...value };
|
|
271
|
-
const displayValue = {};
|
|
272
|
-
let addedKeys = 0;
|
|
273
|
-
for (const [key, item] of Object.entries(objectValue)) {
|
|
274
|
-
if (addedKeys > 4) {
|
|
275
|
-
displayValue["...and more properties"] = Object.keys(objectValue).length - 4;
|
|
276
|
-
break;
|
|
277
|
-
}
|
|
278
|
-
if (typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null || item === void 0) {
|
|
279
|
-
displayValue[key] = item;
|
|
280
|
-
addedKeys++;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
return [String(value.constructor.name), displayValue];
|
|
284
|
-
}
|
|
285
|
-
if (typeof value === "function") {
|
|
286
|
-
const functionString = value.toString();
|
|
287
|
-
return [
|
|
288
|
-
`Function`,
|
|
289
|
-
functionString.includes("\n") ? truncateString(functionString.split("\n").join(""), 40) : functionString
|
|
290
|
-
];
|
|
291
|
-
}
|
|
292
|
-
return null;
|
|
293
|
-
}
|
|
294
7
|
export {
|
|
295
8
|
yamlStringify
|
|
296
9
|
};
|