@ls-stack/utils 3.12.3 → 3.14.0
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/arrayUtils.cjs +5 -2
- package/lib/arrayUtils.js +3 -2
- package/lib/assertions.cjs +42 -34
- package/lib/assertions.d.cts +175 -11
- package/lib/assertions.d.ts +175 -11
- package/lib/assertions.js +2 -1
- package/lib/cache.cjs +7 -4
- package/lib/cache.js +2 -1
- package/lib/{chunk-UTFE4P3P.js → chunk-DMW5Q4T2.js} +1 -1
- package/lib/{chunk-OHHF4CJZ.js → chunk-GKOTKAIV.js} +1 -1
- package/lib/chunk-NH2LCAQS.js +56 -0
- package/lib/chunk-SSKW673U.js +36 -0
- package/lib/chunk-WS4WEVHU.js +57 -0
- package/lib/concurrentCalls.cjs +20 -14
- package/lib/concurrentCalls.d.cts +2 -0
- package/lib/concurrentCalls.d.ts +2 -0
- package/lib/concurrentCalls.js +5 -2
- package/lib/createThrottleController.cjs +5 -2
- package/lib/createThrottleController.js +3 -2
- package/lib/enhancedMap.cjs +5 -2
- package/lib/enhancedMap.js +3 -2
- package/lib/getCompositeKey.cjs +86 -0
- package/lib/getCompositeKey.d.cts +10 -0
- package/lib/getCompositeKey.d.ts +10 -0
- package/lib/getCompositeKey.js +8 -0
- package/lib/getValueStableKey.cjs +10 -4
- package/lib/getValueStableKey.d.cts +5 -1
- package/lib/getValueStableKey.d.ts +5 -1
- package/lib/getValueStableKey.js +5 -49
- package/lib/interpolate.cjs +2 -2
- package/lib/interpolate.js +2 -1
- package/lib/objUtils.d.cts +1 -0
- package/lib/objUtils.d.ts +1 -0
- package/lib/parallelAsyncCalls.cjs +10 -7
- package/lib/parallelAsyncCalls.js +2 -1
- package/lib/saferTyping.d.cts +11 -1
- package/lib/saferTyping.d.ts +11 -1
- package/lib/serializeXML.js +3 -2
- package/lib/testUtils.cjs +5 -2
- package/lib/testUtils.js +3 -2
- package/lib/tsResult.cjs +9 -4
- package/lib/tsResult.js +2 -1
- package/lib/typeGuards.cjs +65 -0
- package/lib/typeGuards.d.cts +109 -0
- package/lib/typeGuards.d.ts +109 -0
- package/lib/typeGuards.js +16 -0
- package/lib/yamlStringify.cjs +17 -9
- package/lib/yamlStringify.js +7 -2
- package/package.json +9 -1
- package/lib/chunk-3XCS7FVO.js +0 -66
package/lib/saferTyping.d.cts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
/** Use this only when you have 100% of certainty that this will not break the types */
|
|
2
2
|
type __LEGIT_ANY__ = any;
|
|
3
|
+
type __LEGIT_ANY_FUNCTION__ = (...params: any) => __LEGIT_ANY__;
|
|
4
|
+
/**
|
|
5
|
+
* An empty object type, equivalent to `{}` but with safer typing
|
|
6
|
+
*/
|
|
7
|
+
type EmptyObject = Record<string, never>;
|
|
3
8
|
/**
|
|
4
9
|
* Cast a value to `any` type. Use this when you have legit usage of `any` casting.
|
|
5
10
|
*
|
|
@@ -23,5 +28,10 @@ declare function __REFINE_CAST__<T>(value: T): <R extends T>() => R;
|
|
|
23
28
|
declare function __FIX_THIS_CASTING__<T>(value: unknown): T;
|
|
24
29
|
type __FIX_THIS_TYPING__ = any;
|
|
25
30
|
declare function __FIX_THIS_TYPING__(value: unknown): __LEGIT_ANY__;
|
|
31
|
+
/**
|
|
32
|
+
* Any type that is not a primitive (number, string, boolean, null, undefined, symbol, bigint, ...)
|
|
33
|
+
* Equivalent to `object` type
|
|
34
|
+
*/
|
|
35
|
+
type AnyNonPrimitiveValue = object;
|
|
26
36
|
|
|
27
|
-
export { __FIX_THIS_CASTING__, __FIX_THIS_TYPING__, __LEGIT_ANY_CAST__, type __LEGIT_ANY__, __LEGIT_CAST__, __REFINE_CAST__ };
|
|
37
|
+
export { type AnyNonPrimitiveValue, type EmptyObject, __FIX_THIS_CASTING__, __FIX_THIS_TYPING__, __LEGIT_ANY_CAST__, type __LEGIT_ANY_FUNCTION__, type __LEGIT_ANY__, __LEGIT_CAST__, __REFINE_CAST__ };
|
package/lib/saferTyping.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
/** Use this only when you have 100% of certainty that this will not break the types */
|
|
2
2
|
type __LEGIT_ANY__ = any;
|
|
3
|
+
type __LEGIT_ANY_FUNCTION__ = (...params: any) => __LEGIT_ANY__;
|
|
4
|
+
/**
|
|
5
|
+
* An empty object type, equivalent to `{}` but with safer typing
|
|
6
|
+
*/
|
|
7
|
+
type EmptyObject = Record<string, never>;
|
|
3
8
|
/**
|
|
4
9
|
* Cast a value to `any` type. Use this when you have legit usage of `any` casting.
|
|
5
10
|
*
|
|
@@ -23,5 +28,10 @@ declare function __REFINE_CAST__<T>(value: T): <R extends T>() => R;
|
|
|
23
28
|
declare function __FIX_THIS_CASTING__<T>(value: unknown): T;
|
|
24
29
|
type __FIX_THIS_TYPING__ = any;
|
|
25
30
|
declare function __FIX_THIS_TYPING__(value: unknown): __LEGIT_ANY__;
|
|
31
|
+
/**
|
|
32
|
+
* Any type that is not a primitive (number, string, boolean, null, undefined, symbol, bigint, ...)
|
|
33
|
+
* Equivalent to `object` type
|
|
34
|
+
*/
|
|
35
|
+
type AnyNonPrimitiveValue = object;
|
|
26
36
|
|
|
27
|
-
export { __FIX_THIS_CASTING__, __FIX_THIS_TYPING__, __LEGIT_ANY_CAST__, type __LEGIT_ANY__, __LEGIT_CAST__, __REFINE_CAST__ };
|
|
37
|
+
export { type AnyNonPrimitiveValue, type EmptyObject, __FIX_THIS_CASTING__, __FIX_THIS_TYPING__, __LEGIT_ANY_CAST__, type __LEGIT_ANY_FUNCTION__, type __LEGIT_ANY__, __LEGIT_CAST__, __REFINE_CAST__ };
|
package/lib/serializeXML.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
filterAndMap
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
3
|
+
} from "./chunk-DMW5Q4T2.js";
|
|
4
|
+
import "./chunk-WS4WEVHU.js";
|
|
5
|
+
import "./chunk-SSKW673U.js";
|
|
5
6
|
|
|
6
7
|
// src/serializeXML.ts
|
|
7
8
|
var XML_TAG_NAME_REGEX = /^[a-zA-Z_][a-zA-Z0-9._-]*$/;
|
package/lib/testUtils.cjs
CHANGED
|
@@ -26,11 +26,14 @@ __export(testUtils_exports, {
|
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(testUtils_exports);
|
|
28
28
|
|
|
29
|
-
// src/
|
|
29
|
+
// src/typeGuards.ts
|
|
30
30
|
function isObject(value) {
|
|
31
31
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
// src/assertions.ts
|
|
35
|
+
var isObject2 = isObject;
|
|
36
|
+
|
|
34
37
|
// src/arrayUtils.ts
|
|
35
38
|
function filterAndMap(array, mapFilter) {
|
|
36
39
|
const result = [];
|
|
@@ -175,7 +178,7 @@ function createLoggerStore({
|
|
|
175
178
|
startTime = Date.now();
|
|
176
179
|
}
|
|
177
180
|
function add(render) {
|
|
178
|
-
if (!
|
|
181
|
+
if (!isObject2(render)) {
|
|
179
182
|
for (const [i, r] of render.entries()) {
|
|
180
183
|
logs.push({
|
|
181
184
|
i: i + 1,
|
package/lib/testUtils.js
CHANGED
|
@@ -14,10 +14,11 @@ import {
|
|
|
14
14
|
import {
|
|
15
15
|
arrayWithPrevAndIndex,
|
|
16
16
|
filterAndMap
|
|
17
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-DMW5Q4T2.js";
|
|
18
18
|
import {
|
|
19
19
|
isObject
|
|
20
|
-
} from "./chunk-
|
|
20
|
+
} from "./chunk-WS4WEVHU.js";
|
|
21
|
+
import "./chunk-SSKW673U.js";
|
|
21
22
|
|
|
22
23
|
// src/testUtils.ts
|
|
23
24
|
function createLoggerStore({
|
package/lib/tsResult.cjs
CHANGED
|
@@ -28,7 +28,7 @@ __export(tsResult_exports, {
|
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(tsResult_exports);
|
|
30
30
|
|
|
31
|
-
// src/
|
|
31
|
+
// src/typeGuards.ts
|
|
32
32
|
function isObject(value) {
|
|
33
33
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
34
34
|
}
|
|
@@ -39,6 +39,11 @@ function isPromise(value) {
|
|
|
39
39
|
return isObject(value) && "then" in value && isFunction(value.then);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
// src/assertions.ts
|
|
43
|
+
var isFunction2 = isFunction;
|
|
44
|
+
var isObject2 = isObject;
|
|
45
|
+
var isPromise2 = isPromise;
|
|
46
|
+
|
|
42
47
|
// src/safeJson.ts
|
|
43
48
|
function safeJsonStringify(value) {
|
|
44
49
|
try {
|
|
@@ -163,7 +168,7 @@ var Result = {
|
|
|
163
168
|
getOkErr
|
|
164
169
|
};
|
|
165
170
|
function resultify(fn, errorNormalizer) {
|
|
166
|
-
if (!
|
|
171
|
+
if (!isFunction2(fn)) {
|
|
167
172
|
return fn.then((value) => ok(value)).catch(
|
|
168
173
|
(error) => err(
|
|
169
174
|
errorNormalizer ? errorNormalizer(error) : unknownToError(error)
|
|
@@ -172,7 +177,7 @@ function resultify(fn, errorNormalizer) {
|
|
|
172
177
|
}
|
|
173
178
|
try {
|
|
174
179
|
const result = fn();
|
|
175
|
-
if (
|
|
180
|
+
if (isPromise2(result)) {
|
|
176
181
|
return result.then((value) => ok(value)).catch(
|
|
177
182
|
(error) => err(
|
|
178
183
|
errorNormalizer ? errorNormalizer(error) : unknownToError(error)
|
|
@@ -191,7 +196,7 @@ function unknownToError(error) {
|
|
|
191
196
|
if (typeof error === "string") {
|
|
192
197
|
return new Error(error);
|
|
193
198
|
}
|
|
194
|
-
if (
|
|
199
|
+
if (isObject2(error)) {
|
|
195
200
|
return new Error(
|
|
196
201
|
"message" in error && error.message && typeof error.message === "string" ? error.message : safeJsonStringify(error) ?? "unknown",
|
|
197
202
|
{ cause: error }
|
package/lib/tsResult.js
CHANGED
|
@@ -0,0 +1,65 @@
|
|
|
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/typeGuards.ts
|
|
21
|
+
var typeGuards_exports = {};
|
|
22
|
+
__export(typeGuards_exports, {
|
|
23
|
+
arrayHasAtLeastXItems: () => arrayHasAtLeastXItems,
|
|
24
|
+
isFunction: () => isFunction,
|
|
25
|
+
isNonEmptyArray: () => isNonEmptyArray,
|
|
26
|
+
isObject: () => isObject,
|
|
27
|
+
isPlainObject: () => isPlainObject,
|
|
28
|
+
isPromise: () => isPromise
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(typeGuards_exports);
|
|
31
|
+
function isObject(value) {
|
|
32
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
33
|
+
}
|
|
34
|
+
function isFunction(value) {
|
|
35
|
+
return typeof value === "function";
|
|
36
|
+
}
|
|
37
|
+
function isPromise(value) {
|
|
38
|
+
return isObject(value) && "then" in value && isFunction(value.then);
|
|
39
|
+
}
|
|
40
|
+
function isPlainObject(value) {
|
|
41
|
+
if (!value || typeof value !== "object") return false;
|
|
42
|
+
const proto = Object.getPrototypeOf(value);
|
|
43
|
+
if (proto === null) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|
47
|
+
if (Ctor === Object) return true;
|
|
48
|
+
const objectCtorString = Object.prototype.constructor.toString();
|
|
49
|
+
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
50
|
+
}
|
|
51
|
+
function isNonEmptyArray(value) {
|
|
52
|
+
return value.length > 0;
|
|
53
|
+
}
|
|
54
|
+
function arrayHasAtLeastXItems(array, minLength) {
|
|
55
|
+
return array.length >= minLength;
|
|
56
|
+
}
|
|
57
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
58
|
+
0 && (module.exports = {
|
|
59
|
+
arrayHasAtLeastXItems,
|
|
60
|
+
isFunction,
|
|
61
|
+
isNonEmptyArray,
|
|
62
|
+
isObject,
|
|
63
|
+
isPlainObject,
|
|
64
|
+
isPromise
|
|
65
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard to check if a value is a plain object (not null, not an array).
|
|
3
|
+
*
|
|
4
|
+
* Returns true if the value is an object that is not null and not an array.
|
|
5
|
+
* This is useful for distinguishing between objects and other types.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The value to check
|
|
8
|
+
* @returns True if the value is a plain object, false otherwise
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* if (isObject(value)) {
|
|
13
|
+
* // TypeScript knows value is Record<string, unknown>
|
|
14
|
+
* console.log(value.someProperty);
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* isObject({}); // true
|
|
18
|
+
* isObject({ a: 1 }); // true
|
|
19
|
+
* isObject(null); // false
|
|
20
|
+
* isObject([]); // false
|
|
21
|
+
* isObject('string'); // false
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
25
|
+
/**
|
|
26
|
+
* Type guard to check if a value is a function.
|
|
27
|
+
*
|
|
28
|
+
* Returns true if the value is a function of any kind (regular function,
|
|
29
|
+
* arrow function, method, constructor, etc.).
|
|
30
|
+
*
|
|
31
|
+
* @param value - The value to check
|
|
32
|
+
* @returns True if the value is a function, false otherwise
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* if (isFunction(value)) {
|
|
37
|
+
* // TypeScript knows value is (...args: any[]) => any
|
|
38
|
+
* const result = value();
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* isFunction(() => {}); // true
|
|
42
|
+
* isFunction(function() {}); // true
|
|
43
|
+
* isFunction(Math.max); // true
|
|
44
|
+
* isFunction('string'); // false
|
|
45
|
+
* isFunction({}); // false
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function isFunction(value: unknown): value is (...args: any[]) => any;
|
|
49
|
+
/**
|
|
50
|
+
* Type guard to check if a value is a Promise or thenable object.
|
|
51
|
+
*
|
|
52
|
+
* Returns true if the value is an object with a `then` method that is a function.
|
|
53
|
+
* This covers both native Promises and thenable objects that implement the
|
|
54
|
+
* Promise interface.
|
|
55
|
+
*
|
|
56
|
+
* @param value - The value to check
|
|
57
|
+
* @returns True if the value is a Promise or thenable, false otherwise
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* if (isPromise(value)) {
|
|
62
|
+
* // TypeScript knows value is Promise<unknown>
|
|
63
|
+
* const result = await value;
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* isPromise(Promise.resolve()); // true
|
|
67
|
+
* isPromise(new Promise(() => {})); // true
|
|
68
|
+
* isPromise({ then: () => {} }); // true
|
|
69
|
+
* isPromise({ then: 'not a function' }); // false
|
|
70
|
+
* isPromise('string'); // false
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare function isPromise(value: unknown): value is Promise<unknown>;
|
|
74
|
+
/**
|
|
75
|
+
* Type guard to check if a value is a plain object (created by Object literal or Object constructor).
|
|
76
|
+
*
|
|
77
|
+
* Returns true if the value is a plain object - an object created by the Object
|
|
78
|
+
* constructor or object literal syntax. This excludes instances of classes,
|
|
79
|
+
* built-in objects like Date, RegExp, etc.
|
|
80
|
+
*
|
|
81
|
+
* @param value - The value to check
|
|
82
|
+
* @returns True if the value is a plain object, false otherwise
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* if (isPlainObject(value)) {
|
|
87
|
+
* // TypeScript knows value is Record<string, unknown>
|
|
88
|
+
* console.log(Object.keys(value));
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* isPlainObject({}); // true
|
|
92
|
+
* isPlainObject({ a: 1 }); // true
|
|
93
|
+
* isPlainObject(Object.create(null)); // true
|
|
94
|
+
* isPlainObject(new Date()); // false
|
|
95
|
+
* isPlainObject(/regex/); // false
|
|
96
|
+
* isPlainObject(new MyClass()); // false
|
|
97
|
+
* isPlainObject([]); // false
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function isPlainObject(value: any): value is Record<string, unknown>;
|
|
101
|
+
type NonEmptyArray<T> = [T, ...T[]];
|
|
102
|
+
declare function isNonEmptyArray<T>(value: T[] | readonly T[]): value is NonEmptyArray<T>;
|
|
103
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 1): array is [T, ...T[]];
|
|
104
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 2): array is [T, T, ...T[]];
|
|
105
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 3): array is [T, T, T, ...T[]];
|
|
106
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 4): array is [T, T, T, T, ...T[]];
|
|
107
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 5): array is [T, T, T, T, T, ...T[]];
|
|
108
|
+
|
|
109
|
+
export { type NonEmptyArray, arrayHasAtLeastXItems, isFunction, isNonEmptyArray, isObject, isPlainObject, isPromise };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard to check if a value is a plain object (not null, not an array).
|
|
3
|
+
*
|
|
4
|
+
* Returns true if the value is an object that is not null and not an array.
|
|
5
|
+
* This is useful for distinguishing between objects and other types.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The value to check
|
|
8
|
+
* @returns True if the value is a plain object, false otherwise
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* if (isObject(value)) {
|
|
13
|
+
* // TypeScript knows value is Record<string, unknown>
|
|
14
|
+
* console.log(value.someProperty);
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* isObject({}); // true
|
|
18
|
+
* isObject({ a: 1 }); // true
|
|
19
|
+
* isObject(null); // false
|
|
20
|
+
* isObject([]); // false
|
|
21
|
+
* isObject('string'); // false
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
25
|
+
/**
|
|
26
|
+
* Type guard to check if a value is a function.
|
|
27
|
+
*
|
|
28
|
+
* Returns true if the value is a function of any kind (regular function,
|
|
29
|
+
* arrow function, method, constructor, etc.).
|
|
30
|
+
*
|
|
31
|
+
* @param value - The value to check
|
|
32
|
+
* @returns True if the value is a function, false otherwise
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* if (isFunction(value)) {
|
|
37
|
+
* // TypeScript knows value is (...args: any[]) => any
|
|
38
|
+
* const result = value();
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* isFunction(() => {}); // true
|
|
42
|
+
* isFunction(function() {}); // true
|
|
43
|
+
* isFunction(Math.max); // true
|
|
44
|
+
* isFunction('string'); // false
|
|
45
|
+
* isFunction({}); // false
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function isFunction(value: unknown): value is (...args: any[]) => any;
|
|
49
|
+
/**
|
|
50
|
+
* Type guard to check if a value is a Promise or thenable object.
|
|
51
|
+
*
|
|
52
|
+
* Returns true if the value is an object with a `then` method that is a function.
|
|
53
|
+
* This covers both native Promises and thenable objects that implement the
|
|
54
|
+
* Promise interface.
|
|
55
|
+
*
|
|
56
|
+
* @param value - The value to check
|
|
57
|
+
* @returns True if the value is a Promise or thenable, false otherwise
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* if (isPromise(value)) {
|
|
62
|
+
* // TypeScript knows value is Promise<unknown>
|
|
63
|
+
* const result = await value;
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* isPromise(Promise.resolve()); // true
|
|
67
|
+
* isPromise(new Promise(() => {})); // true
|
|
68
|
+
* isPromise({ then: () => {} }); // true
|
|
69
|
+
* isPromise({ then: 'not a function' }); // false
|
|
70
|
+
* isPromise('string'); // false
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare function isPromise(value: unknown): value is Promise<unknown>;
|
|
74
|
+
/**
|
|
75
|
+
* Type guard to check if a value is a plain object (created by Object literal or Object constructor).
|
|
76
|
+
*
|
|
77
|
+
* Returns true if the value is a plain object - an object created by the Object
|
|
78
|
+
* constructor or object literal syntax. This excludes instances of classes,
|
|
79
|
+
* built-in objects like Date, RegExp, etc.
|
|
80
|
+
*
|
|
81
|
+
* @param value - The value to check
|
|
82
|
+
* @returns True if the value is a plain object, false otherwise
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* if (isPlainObject(value)) {
|
|
87
|
+
* // TypeScript knows value is Record<string, unknown>
|
|
88
|
+
* console.log(Object.keys(value));
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* isPlainObject({}); // true
|
|
92
|
+
* isPlainObject({ a: 1 }); // true
|
|
93
|
+
* isPlainObject(Object.create(null)); // true
|
|
94
|
+
* isPlainObject(new Date()); // false
|
|
95
|
+
* isPlainObject(/regex/); // false
|
|
96
|
+
* isPlainObject(new MyClass()); // false
|
|
97
|
+
* isPlainObject([]); // false
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function isPlainObject(value: any): value is Record<string, unknown>;
|
|
101
|
+
type NonEmptyArray<T> = [T, ...T[]];
|
|
102
|
+
declare function isNonEmptyArray<T>(value: T[] | readonly T[]): value is NonEmptyArray<T>;
|
|
103
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 1): array is [T, ...T[]];
|
|
104
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 2): array is [T, T, ...T[]];
|
|
105
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 3): array is [T, T, T, ...T[]];
|
|
106
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 4): array is [T, T, T, T, ...T[]];
|
|
107
|
+
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 5): array is [T, T, T, T, T, ...T[]];
|
|
108
|
+
|
|
109
|
+
export { type NonEmptyArray, arrayHasAtLeastXItems, isFunction, isNonEmptyArray, isObject, isPlainObject, isPromise };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
arrayHasAtLeastXItems,
|
|
3
|
+
isFunction,
|
|
4
|
+
isNonEmptyArray,
|
|
5
|
+
isObject,
|
|
6
|
+
isPlainObject,
|
|
7
|
+
isPromise
|
|
8
|
+
} from "./chunk-SSKW673U.js";
|
|
9
|
+
export {
|
|
10
|
+
arrayHasAtLeastXItems,
|
|
11
|
+
isFunction,
|
|
12
|
+
isNonEmptyArray,
|
|
13
|
+
isObject,
|
|
14
|
+
isPlainObject,
|
|
15
|
+
isPromise
|
|
16
|
+
};
|
package/lib/yamlStringify.cjs
CHANGED
|
@@ -24,7 +24,7 @@ __export(yamlStringify_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(yamlStringify_exports);
|
|
26
26
|
|
|
27
|
-
// src/
|
|
27
|
+
// src/typeGuards.ts
|
|
28
28
|
function isObject(value) {
|
|
29
29
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
30
30
|
}
|
|
@@ -40,6 +40,10 @@ function isPlainObject(value) {
|
|
|
40
40
|
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
// src/assertions.ts
|
|
44
|
+
var isObject2 = isObject;
|
|
45
|
+
var isPlainObject2 = isPlainObject;
|
|
46
|
+
|
|
43
47
|
// src/conversions.ts
|
|
44
48
|
function bytesToHumanReadable(bytes) {
|
|
45
49
|
if (bytes < 1024) {
|
|
@@ -70,7 +74,7 @@ function yamlStringify(obj, {
|
|
|
70
74
|
maxDepth = 50,
|
|
71
75
|
addRootObjSpaces = "beforeAndAfter"
|
|
72
76
|
} = {}) {
|
|
73
|
-
if (
|
|
77
|
+
if (isObject2(obj) || Array.isArray(obj) || typeof obj === "function") {
|
|
74
78
|
return `${stringifyValue(obj, "", maxLineLength, !!showUndefined, maxDepth, 0, addRootObjSpaces)}
|
|
75
79
|
`;
|
|
76
80
|
}
|
|
@@ -79,7 +83,7 @@ function yamlStringify(obj, {
|
|
|
79
83
|
function stringifyValue(value, indent, maxLineLength, showUndefined, maxDepth, depth, addObjSpaces) {
|
|
80
84
|
let result = "";
|
|
81
85
|
const childIndent = `${indent} `;
|
|
82
|
-
if (
|
|
86
|
+
if (isPlainObject2(value)) {
|
|
83
87
|
if (Object.keys(value).length === 0) {
|
|
84
88
|
return "{}";
|
|
85
89
|
}
|
|
@@ -106,7 +110,7 @@ function stringifyValue(value, indent, maxLineLength, showUndefined, maxDepth, d
|
|
|
106
110
|
depth + 1,
|
|
107
111
|
addObjSpaces
|
|
108
112
|
);
|
|
109
|
-
if (!afterSpaceWasAdded && indent === "" &&
|
|
113
|
+
if (!afterSpaceWasAdded && indent === "" && isObject2(objVal) && prevValue && (addObjSpaces === "before" || addObjSpaces === "beforeAndAfter")) {
|
|
110
114
|
result += "\n";
|
|
111
115
|
}
|
|
112
116
|
if (Array.isArray(objVal)) {
|
|
@@ -117,7 +121,7 @@ function stringifyValue(value, indent, maxLineLength, showUndefined, maxDepth, d
|
|
|
117
121
|
result += `${indent}${key}:
|
|
118
122
|
`;
|
|
119
123
|
}
|
|
120
|
-
} else if (
|
|
124
|
+
} else if (isObject2(objVal)) {
|
|
121
125
|
if (Object.keys(objVal).length === 0) {
|
|
122
126
|
result += `${indent}${key}: `;
|
|
123
127
|
} else {
|
|
@@ -130,7 +134,7 @@ function stringifyValue(value, indent, maxLineLength, showUndefined, maxDepth, d
|
|
|
130
134
|
result += valueString;
|
|
131
135
|
result += "\n";
|
|
132
136
|
if (indent === "") {
|
|
133
|
-
if (
|
|
137
|
+
if (isObject2(objVal)) {
|
|
134
138
|
if (addObjSpaces === "after" || addObjSpaces === "beforeAndAfter") {
|
|
135
139
|
result += "\n";
|
|
136
140
|
afterSpaceWasAdded = true;
|
|
@@ -180,7 +184,7 @@ function stringifyValue(value, indent, maxLineLength, showUndefined, maxDepth, d
|
|
|
180
184
|
item = `{max depth reached}`;
|
|
181
185
|
}
|
|
182
186
|
result += `${indent}- `;
|
|
183
|
-
if (Array.isArray(item) ||
|
|
187
|
+
if (Array.isArray(item) || isObject2(item)) {
|
|
184
188
|
let arrayString = stringifyValue(
|
|
185
189
|
item,
|
|
186
190
|
childIndent,
|
|
@@ -227,8 +231,12 @@ ${indent}${line}
|
|
|
227
231
|
}
|
|
228
232
|
}
|
|
229
233
|
} else {
|
|
230
|
-
if (value.includes("'")) {
|
|
234
|
+
if (value.includes("'") && !value.includes('"')) {
|
|
231
235
|
result += `"${value}"`;
|
|
236
|
+
} else if (value.includes('"') && !value.includes("'")) {
|
|
237
|
+
result += `'${value}'`;
|
|
238
|
+
} else if (value.includes("'") && value.includes('"')) {
|
|
239
|
+
result += `"${value.replace(/"/g, '\\"')}"`;
|
|
232
240
|
} else {
|
|
233
241
|
result += `'${value}'`;
|
|
234
242
|
}
|
|
@@ -255,7 +263,7 @@ ${indent}${line}
|
|
|
255
263
|
return JSON.stringify(value);
|
|
256
264
|
}
|
|
257
265
|
function normalizeValue(value) {
|
|
258
|
-
if (value === null ||
|
|
266
|
+
if (value === null || isPlainObject2(value) || Array.isArray(value)) {
|
|
259
267
|
return null;
|
|
260
268
|
}
|
|
261
269
|
if (value instanceof Map) {
|
package/lib/yamlStringify.js
CHANGED
|
@@ -4,7 +4,8 @@ import {
|
|
|
4
4
|
import {
|
|
5
5
|
isObject,
|
|
6
6
|
isPlainObject
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-WS4WEVHU.js";
|
|
8
|
+
import "./chunk-SSKW673U.js";
|
|
8
9
|
import {
|
|
9
10
|
bytesToHumanReadable
|
|
10
11
|
} from "./chunk-IATIXMCE.js";
|
|
@@ -173,8 +174,12 @@ ${indent}${line}
|
|
|
173
174
|
}
|
|
174
175
|
}
|
|
175
176
|
} else {
|
|
176
|
-
if (value.includes("'")) {
|
|
177
|
+
if (value.includes("'") && !value.includes('"')) {
|
|
177
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, '\\"')}"`;
|
|
178
183
|
} else {
|
|
179
184
|
result += `'${value}'`;
|
|
180
185
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ls-stack/utils",
|
|
3
3
|
"description": "Typescript utils",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.14.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib"
|
|
@@ -74,6 +74,10 @@
|
|
|
74
74
|
"import": "./lib/exhaustiveMatch.js",
|
|
75
75
|
"require": "./lib/exhaustiveMatch.cjs"
|
|
76
76
|
},
|
|
77
|
+
"./getCompositeKey": {
|
|
78
|
+
"import": "./lib/getCompositeKey.js",
|
|
79
|
+
"require": "./lib/getCompositeKey.cjs"
|
|
80
|
+
},
|
|
77
81
|
"./getValueStableKey": {
|
|
78
82
|
"import": "./lib/getValueStableKey.js",
|
|
79
83
|
"require": "./lib/getValueStableKey.cjs"
|
|
@@ -146,6 +150,10 @@
|
|
|
146
150
|
"import": "./lib/tsResult.js",
|
|
147
151
|
"require": "./lib/tsResult.cjs"
|
|
148
152
|
},
|
|
153
|
+
"./typeGuards": {
|
|
154
|
+
"import": "./lib/typeGuards.js",
|
|
155
|
+
"require": "./lib/typeGuards.cjs"
|
|
156
|
+
},
|
|
149
157
|
"./typingFnUtils": {
|
|
150
158
|
"import": "./lib/typingFnUtils.js",
|
|
151
159
|
"require": "./lib/typingFnUtils.cjs"
|