@ls-stack/utils 3.13.0 → 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.
Files changed (42) hide show
  1. package/lib/arrayUtils.cjs +5 -2
  2. package/lib/arrayUtils.js +3 -2
  3. package/lib/assertions.cjs +42 -34
  4. package/lib/assertions.d.cts +175 -11
  5. package/lib/assertions.d.ts +175 -11
  6. package/lib/assertions.js +2 -1
  7. package/lib/cache.cjs +7 -4
  8. package/lib/cache.js +2 -1
  9. package/lib/{chunk-UTFE4P3P.js → chunk-DMW5Q4T2.js} +1 -1
  10. package/lib/{chunk-OHHF4CJZ.js → chunk-GKOTKAIV.js} +1 -1
  11. package/lib/{chunk-U44EKR2F.js → chunk-NH2LCAQS.js} +1 -1
  12. package/lib/chunk-SSKW673U.js +36 -0
  13. package/lib/chunk-WS4WEVHU.js +57 -0
  14. package/lib/concurrentCalls.cjs +20 -14
  15. package/lib/concurrentCalls.d.cts +2 -0
  16. package/lib/concurrentCalls.d.ts +2 -0
  17. package/lib/concurrentCalls.js +5 -2
  18. package/lib/createThrottleController.cjs +5 -2
  19. package/lib/createThrottleController.js +3 -2
  20. package/lib/enhancedMap.cjs +5 -2
  21. package/lib/enhancedMap.js +3 -2
  22. package/lib/getCompositeKey.cjs +5 -2
  23. package/lib/getCompositeKey.js +3 -2
  24. package/lib/getValueStableKey.cjs +5 -2
  25. package/lib/getValueStableKey.js +3 -2
  26. package/lib/interpolate.cjs +2 -2
  27. package/lib/interpolate.js +2 -1
  28. package/lib/parallelAsyncCalls.cjs +10 -7
  29. package/lib/parallelAsyncCalls.js +2 -1
  30. package/lib/serializeXML.js +3 -2
  31. package/lib/testUtils.cjs +5 -2
  32. package/lib/testUtils.js +3 -2
  33. package/lib/tsResult.cjs +9 -4
  34. package/lib/tsResult.js +2 -1
  35. package/lib/typeGuards.cjs +65 -0
  36. package/lib/typeGuards.d.cts +109 -0
  37. package/lib/typeGuards.d.ts +109 -0
  38. package/lib/typeGuards.js +16 -0
  39. package/lib/yamlStringify.cjs +17 -9
  40. package/lib/yamlStringify.js +7 -2
  41. package/package.json +5 -1
  42. package/lib/chunk-3XCS7FVO.js +0 -66
@@ -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
+ };
@@ -24,7 +24,7 @@ __export(yamlStringify_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(yamlStringify_exports);
26
26
 
27
- // src/assertions.ts
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 (isObject(obj) || Array.isArray(obj) || typeof obj === "function") {
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 (isPlainObject(value)) {
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 === "" && isObject(objVal) && prevValue && (addObjSpaces === "before" || addObjSpaces === "beforeAndAfter")) {
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 (isObject(objVal)) {
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 (isObject(objVal)) {
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) || isObject(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 || isPlainObject(value) || Array.isArray(value)) {
266
+ if (value === null || isPlainObject2(value) || Array.isArray(value)) {
259
267
  return null;
260
268
  }
261
269
  if (value instanceof Map) {
@@ -4,7 +4,8 @@ import {
4
4
  import {
5
5
  isObject,
6
6
  isPlainObject
7
- } from "./chunk-3XCS7FVO.js";
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.13.0",
4
+ "version": "3.14.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib"
@@ -150,6 +150,10 @@
150
150
  "import": "./lib/tsResult.js",
151
151
  "require": "./lib/tsResult.cjs"
152
152
  },
153
+ "./typeGuards": {
154
+ "import": "./lib/typeGuards.js",
155
+ "require": "./lib/typeGuards.cjs"
156
+ },
153
157
  "./typingFnUtils": {
154
158
  "import": "./lib/typingFnUtils.js",
155
159
  "require": "./lib/typingFnUtils.cjs"
@@ -1,66 +0,0 @@
1
- // src/assertions.ts
2
- var undefErrMsg = "not undefined assertion failed";
3
- var nullishErrMsg = "not nullish assertion failed";
4
- function notUndefined(value, errorMsg = undefErrMsg) {
5
- if (value === void 0) {
6
- throw new Error(errorMsg);
7
- }
8
- return value;
9
- }
10
- function notNullish(value, errorMsg = nullishErrMsg) {
11
- if (value === void 0 || value === null) {
12
- throw new Error(errorMsg);
13
- }
14
- return value;
15
- }
16
- function assertIsNotNullish(value, errorMsg = nullishErrMsg) {
17
- if (value === void 0 || value === null) {
18
- throw new Error(errorMsg);
19
- }
20
- }
21
- function assertIsNotUndefined(value, errorMsg = undefErrMsg) {
22
- if (value === void 0) {
23
- throw new Error(errorMsg);
24
- }
25
- }
26
- function invariant(condition, errorMsg = "Invariant violation") {
27
- if (!condition) {
28
- throw new Error(`Invariant violation: ${errorMsg}`);
29
- }
30
- }
31
- function exhaustiveCheck(narrowedType) {
32
- return new Error("This should never happen");
33
- }
34
- function isObject(value) {
35
- return typeof value === "object" && value !== null && !Array.isArray(value);
36
- }
37
- function isFunction(value) {
38
- return typeof value === "function";
39
- }
40
- function isPromise(value) {
41
- return isObject(value) && "then" in value && isFunction(value.then);
42
- }
43
- function isPlainObject(value) {
44
- if (!value || typeof value !== "object") return false;
45
- const proto = Object.getPrototypeOf(value);
46
- if (proto === null) {
47
- return true;
48
- }
49
- const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
50
- if (Ctor === Object) return true;
51
- const objectCtorString = Object.prototype.constructor.toString();
52
- return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
53
- }
54
-
55
- export {
56
- notUndefined,
57
- notNullish,
58
- assertIsNotNullish,
59
- assertIsNotUndefined,
60
- invariant,
61
- exhaustiveCheck,
62
- isObject,
63
- isFunction,
64
- isPromise,
65
- isPlainObject
66
- };