ts-data-forge 6.2.1 → 6.3.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 (122) hide show
  1. package/README.md +35 -6
  2. package/dist/array/impl/array-utils-reducing-value.d.mts +4 -1
  3. package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -1
  4. package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
  5. package/dist/array/impl/array-utils-search.d.mts +3 -1
  6. package/dist/array/impl/array-utils-search.d.mts.map +1 -1
  7. package/dist/array/impl/array-utils-search.mjs.map +1 -1
  8. package/dist/array/impl/array-utils-transformation.d.mts +12 -5
  9. package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
  10. package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
  11. package/dist/collections/imap.d.mts +4 -1
  12. package/dist/collections/imap.d.mts.map +1 -1
  13. package/dist/collections/imap.mjs.map +1 -1
  14. package/dist/collections/iset-mapped.d.mts +2 -1
  15. package/dist/collections/iset-mapped.d.mts.map +1 -1
  16. package/dist/collections/iset-mapped.mjs.map +1 -1
  17. package/dist/collections/iset.d.mts +3 -1
  18. package/dist/collections/iset.d.mts.map +1 -1
  19. package/dist/collections/iset.mjs.map +1 -1
  20. package/dist/entry-point.mjs +1 -0
  21. package/dist/entry-point.mjs.map +1 -1
  22. package/dist/functional/optional/impl/optional-expect-to-be.d.mts +8 -2
  23. package/dist/functional/optional/impl/optional-expect-to-be.d.mts.map +1 -1
  24. package/dist/functional/optional/impl/optional-expect-to-be.mjs.map +1 -1
  25. package/dist/functional/optional/impl/optional-flat-map.d.mts +4 -1
  26. package/dist/functional/optional/impl/optional-flat-map.d.mts.map +1 -1
  27. package/dist/functional/optional/impl/optional-flat-map.mjs.map +1 -1
  28. package/dist/functional/optional/impl/optional-is-none.d.mts +1 -1
  29. package/dist/functional/optional/impl/optional-is-none.mjs +1 -1
  30. package/dist/functional/optional/impl/optional-or-else.d.mts +4 -1
  31. package/dist/functional/optional/impl/optional-or-else.d.mts.map +1 -1
  32. package/dist/functional/optional/impl/optional-or-else.mjs.map +1 -1
  33. package/dist/functional/result/impl/result-expect-to-be.d.mts +5 -1
  34. package/dist/functional/result/impl/result-expect-to-be.d.mts.map +1 -1
  35. package/dist/functional/result/impl/result-expect-to-be.mjs.map +1 -1
  36. package/dist/functional/result/impl/result-from-promise.d.mts +3 -1
  37. package/dist/functional/result/impl/result-from-promise.d.mts.map +1 -1
  38. package/dist/functional/result/impl/result-from-promise.mjs +3 -1
  39. package/dist/functional/result/impl/result-from-promise.mjs.map +1 -1
  40. package/dist/functional/result/impl/result-map-err.d.mts +3 -1
  41. package/dist/functional/result/impl/result-map-err.d.mts.map +1 -1
  42. package/dist/functional/result/impl/result-map-err.mjs.map +1 -1
  43. package/dist/functional/result/impl/result-or-else.d.mts +4 -1
  44. package/dist/functional/result/impl/result-or-else.d.mts.map +1 -1
  45. package/dist/functional/result/impl/result-or-else.mjs.map +1 -1
  46. package/dist/functional/result/impl/result-unwrap-throw.d.mts +1 -1
  47. package/dist/functional/result/impl/result-unwrap-throw.mjs +1 -1
  48. package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.d.mts +5 -1
  49. package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.d.mts.map +1 -1
  50. package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs.map +1 -1
  51. package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts +4 -1
  52. package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts.map +1 -1
  53. package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs.map +1 -1
  54. package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts +4 -5
  55. package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts.map +1 -1
  56. package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs +4 -5
  57. package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs.map +1 -1
  58. package/dist/functional/ternary-result/impl/ternary-result-map-warn.d.mts +4 -1
  59. package/dist/functional/ternary-result/impl/ternary-result-map-warn.d.mts.map +1 -1
  60. package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs.map +1 -1
  61. package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts +4 -1
  62. package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts.map +1 -1
  63. package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs +4 -1
  64. package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs.map +1 -1
  65. package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.d.mts +4 -1
  66. package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.d.mts.map +1 -1
  67. package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs.map +1 -1
  68. package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts +5 -4
  69. package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts.map +1 -1
  70. package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs +5 -4
  71. package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs.map +1 -1
  72. package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts +4 -1
  73. package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts.map +1 -1
  74. package/dist/functional/ternary-result/impl/ternary-result-zip.mjs +4 -1
  75. package/dist/functional/ternary-result/impl/ternary-result-zip.mjs.map +1 -1
  76. package/dist/guard/is-record.d.mts +1 -1
  77. package/dist/guard/is-record.mjs +1 -1
  78. package/dist/index.mjs +1 -0
  79. package/dist/index.mjs.map +1 -1
  80. package/dist/json/json.d.mts +4 -2
  81. package/dist/json/json.d.mts.map +1 -1
  82. package/dist/json/json.mjs +4 -2
  83. package/dist/json/json.mjs.map +1 -1
  84. package/dist/others/debounce-function.d.mts +11 -0
  85. package/dist/others/debounce-function.d.mts.map +1 -0
  86. package/dist/others/debounce-function.mjs +21 -0
  87. package/dist/others/debounce-function.mjs.map +1 -0
  88. package/dist/others/index.d.mts +1 -0
  89. package/dist/others/index.d.mts.map +1 -1
  90. package/dist/others/index.mjs +1 -0
  91. package/dist/others/index.mjs.map +1 -1
  92. package/package.json +24 -20
  93. package/src/array/impl/array-utils-creation.test.mts +4 -2
  94. package/src/array/impl/array-utils-reducing-value.mts +4 -1
  95. package/src/array/impl/array-utils-search.mts +3 -1
  96. package/src/array/impl/array-utils-transformation.mts +12 -5
  97. package/src/collections/imap.mts +4 -1
  98. package/src/collections/iset-mapped.mts +2 -1
  99. package/src/collections/iset.mts +3 -1
  100. package/src/collections/queue.test.mts +295 -1
  101. package/src/functional/optional/impl/optional-expect-to-be.mts +8 -2
  102. package/src/functional/optional/impl/optional-flat-map.mts +4 -1
  103. package/src/functional/optional/impl/optional-is-none.mts +1 -1
  104. package/src/functional/optional/impl/optional-or-else.mts +4 -1
  105. package/src/functional/result/impl/result-expect-to-be.mts +5 -1
  106. package/src/functional/result/impl/result-from-promise.mts +3 -1
  107. package/src/functional/result/impl/result-map-err.mts +3 -1
  108. package/src/functional/result/impl/result-or-else.mts +4 -1
  109. package/src/functional/result/impl/result-unwrap-throw.mts +1 -1
  110. package/src/functional/ternary-result/impl/ternary-result-expect-to-be.mts +5 -1
  111. package/src/functional/ternary-result/impl/ternary-result-flat-map.mts +4 -1
  112. package/src/functional/ternary-result/impl/ternary-result-is-warn.mts +4 -5
  113. package/src/functional/ternary-result/impl/ternary-result-map-warn.mts +4 -1
  114. package/src/functional/ternary-result/impl/ternary-result-to-optional.mts +4 -1
  115. package/src/functional/ternary-result/impl/ternary-result-unwrap-err-or.mts +4 -1
  116. package/src/functional/ternary-result/impl/ternary-result-unwrap-throw.mts +5 -4
  117. package/src/functional/ternary-result/impl/ternary-result-zip.mts +4 -1
  118. package/src/guard/is-record.mts +1 -1
  119. package/src/json/json.mts +4 -2
  120. package/src/others/debounce-function.mts +23 -0
  121. package/src/others/debounce-function.test.mts +81 -0
  122. package/src/others/index.mts +1 -0
@@ -23,7 +23,10 @@ import { type UnwrapErr, type UnwrapOk, type UnwrapWarn } from './types.mjs';
23
23
  * parse,
24
24
  * );
25
25
  *
26
- * const errPassthrough = TernaryResult.flatMap(TernaryResult.err('oops'), parse);
26
+ * const errPassthrough = TernaryResult.flatMap(
27
+ * TernaryResult.err('oops'),
28
+ * parse,
29
+ * );
27
30
  *
28
31
  * assert.deepStrictEqual(doubled, TernaryResult.ok(6));
29
32
  *
@@ -7,11 +7,10 @@ import { type NarrowToWarn } from './types.mjs';
7
7
  * @example
8
8
  *
9
9
  * ```ts
10
- * const maybeWarn = TernaryResult.warn('value', 'check logs') as TernaryResult<
11
- * string,
12
- * string,
13
- * string
14
- * >;
10
+ * const maybeWarn = TernaryResult.warn(
11
+ * 'value',
12
+ * 'check logs',
13
+ * ) as TernaryResult<string, string, string>;
15
14
  *
16
15
  * if (TernaryResult.isWarn(maybeWarn)) {
17
16
  * assert.strictEqual(maybeWarn.value, 'value');
@@ -10,7 +10,10 @@ import { type UnwrapErr, type UnwrapOk, type UnwrapWarn } from './types.mjs';
10
10
  * ```ts
11
11
  * const warnValue = TernaryResult.warn(2, 'slow');
12
12
  *
13
- * const mappedWarn = TernaryResult.mapWarn(warnValue, (warning) => `${warning}!`);
13
+ * const mappedWarn = TernaryResult.mapWarn(
14
+ * warnValue,
15
+ * (warning) => `${warning}!`,
16
+ * );
14
17
  *
15
18
  * const okPassthrough = TernaryResult.mapWarn(
16
19
  * TernaryResult.ok(3),
@@ -15,7 +15,10 @@ import { type UnwrapOk } from './types.mjs';
15
15
  *
16
16
  * assert.deepStrictEqual(TernaryResult.toOptional(okValue), Optional.some(7));
17
17
  *
18
- * assert.deepStrictEqual(TernaryResult.toOptional(warnValue), Optional.some(7));
18
+ * assert.deepStrictEqual(
19
+ * TernaryResult.toOptional(warnValue),
20
+ * Optional.some(7),
21
+ * );
19
22
  * ```
20
23
  */
21
24
  export const toOptional = <R extends UnknownTernaryResult>(
@@ -9,7 +9,10 @@ import { type UnwrapErr } from './types.mjs';
9
9
  * ```ts
10
10
  * const okValue = TernaryResult.ok('value');
11
11
  *
12
- * assert.strictEqual(TernaryResult.unwrapErrOr(okValue, 'default'), 'default');
12
+ * assert.strictEqual(
13
+ * TernaryResult.unwrapErrOr(okValue, 'default'),
14
+ * 'default',
15
+ * );
13
16
  *
14
17
  * const unwrapErr = TernaryResult.unwrapErrOr('fallback error');
15
18
  *
@@ -13,10 +13,11 @@ import { type UnwrapErr, type UnwrapOk, type UnwrapWarn } from './types.mjs';
13
13
  *
14
14
  * assert.strictEqual(TernaryResult.unwrapThrow(okValue), 'ready');
15
15
  *
16
- * assert.throws(
17
- * () => TernaryResult.unwrapThrow(TernaryResult.warn('warn', 'warned')),
18
- * /Expected Ok/u,
19
- * );
16
+ * const throwTest = (): void => {
17
+ * TernaryResult.unwrapThrow(TernaryResult.warn('warn', 'warned'));
18
+ * };
19
+ *
20
+ * assert.throws(throwTest, /Expected Ok/u);
20
21
  * ```
21
22
  */
22
23
  export const unwrapThrow = <R extends UnknownTernaryResult>(
@@ -9,7 +9,10 @@ import { warn } from './ternary-result-warn.mjs';
9
9
  * @example
10
10
  *
11
11
  * ```ts
12
- * const okPair = TernaryResult.zip(TernaryResult.ok('left'), TernaryResult.ok(1));
12
+ * const okPair = TernaryResult.zip(
13
+ * TernaryResult.ok('left'),
14
+ * TernaryResult.ok(1),
15
+ * );
13
16
  *
14
17
  * const warnPair = TernaryResult.zip(
15
18
  * TernaryResult.warn('left', 'warn'),
@@ -25,7 +25,7 @@ import { isNonNullObject } from './is-non-null-object.mjs';
25
25
  * @example
26
26
  *
27
27
  * ```ts
28
- * const entries: unknown[] = [{ id: 1 }, ['tuple']];
28
+ * const entries: unknown[] = [{ id: 1 }, 'str', 0, null];
29
29
  *
30
30
  * const records = entries.filter(isRecord);
31
31
  *
package/src/json/json.mts CHANGED
@@ -291,11 +291,13 @@ export namespace Json {
291
291
  * );
292
292
  *
293
293
  * assert.isTrue(
294
- * formatted.value.indexOf('"mango"') < formatted.value.indexOf('"nested"'),
294
+ * formatted.value.indexOf('"mango"') <
295
+ * formatted.value.indexOf('"nested"'),
295
296
  * );
296
297
  *
297
298
  * assert.isTrue(
298
- * formatted.value.indexOf('"nested"') < formatted.value.indexOf('"zebra"'),
299
+ * formatted.value.indexOf('"nested"') <
300
+ * formatted.value.indexOf('"zebra"'),
299
301
  * );
300
302
  * }
301
303
  * ```
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Creates a debounced function that delays invoking `func` until after `waitMilliseconds`
3
+ * have elapsed since the last time the debounced function was invoked.
4
+ *
5
+ * @template F The type of the function to debounce.
6
+ * @param func The function to debounce.
7
+ * @param waitMilliseconds The number of milliseconds to delay.
8
+ * @returns The new debounced function.
9
+ */
10
+ export const debounce = <Args extends readonly unknown[]>(
11
+ func: (...args: Args) => void,
12
+ waitMilliseconds: number,
13
+ ): ((...args: Args) => void) => {
14
+ let mut_timeoutId: ReturnType<typeof setTimeout> | undefined;
15
+
16
+ return (...args: Args): void => {
17
+ clearTimeout(mut_timeoutId);
18
+
19
+ mut_timeoutId = setTimeout(() => {
20
+ func(...args);
21
+ }, waitMilliseconds);
22
+ };
23
+ };
@@ -0,0 +1,81 @@
1
+ /* eslint-disable vitest/no-restricted-vi-methods */
2
+
3
+ import { debounce } from './debounce-function.mjs';
4
+
5
+ describe(debounce, () => {
6
+ // eslint-disable-next-line vitest/no-hooks
7
+ beforeEach(() => {
8
+ vi.useFakeTimers();
9
+ });
10
+
11
+ // eslint-disable-next-line vitest/no-hooks
12
+ afterEach(() => {
13
+ vi.restoreAllMocks();
14
+ });
15
+
16
+ test('should delay the execution of the function', () => {
17
+ const func = vi.fn();
18
+
19
+ // eslint-disable-next-line @typescript-eslint/strict-void-return
20
+ const debouncedFunc = debounce(func, 100);
21
+
22
+ debouncedFunc();
23
+
24
+ expect(func).not.toHaveBeenCalled();
25
+
26
+ vi.advanceTimersByTime(50);
27
+
28
+ expect(func).not.toHaveBeenCalled();
29
+
30
+ vi.advanceTimersByTime(50);
31
+
32
+ expect(func).toHaveBeenCalledOnce();
33
+ });
34
+
35
+ test('should only execute the function once after multiple rapid calls', () => {
36
+ const func = vi.fn();
37
+
38
+ // eslint-disable-next-line @typescript-eslint/strict-void-return
39
+ const debouncedFunc = debounce(func, 100);
40
+
41
+ debouncedFunc();
42
+
43
+ debouncedFunc();
44
+
45
+ debouncedFunc();
46
+
47
+ expect(func).not.toHaveBeenCalled();
48
+
49
+ vi.advanceTimersByTime(100);
50
+
51
+ expect(func).toHaveBeenCalledOnce();
52
+ });
53
+
54
+ test('should pass the arguments to the original function', () => {
55
+ const func = vi.fn((_a: number, _b: string) => {});
56
+
57
+ const debouncedFunc = debounce(func, 100);
58
+
59
+ debouncedFunc(1, 'test');
60
+
61
+ vi.advanceTimersByTime(100);
62
+
63
+ expect(func).toHaveBeenCalledWith(1, 'test');
64
+ });
65
+
66
+ test('should use the latest arguments from the last call', () => {
67
+ const func = vi.fn((_a: number, _b: string) => {});
68
+
69
+ const debouncedFunc = debounce(func, 100);
70
+
71
+ debouncedFunc(1, 'a');
72
+
73
+ debouncedFunc(2, 'b');
74
+
75
+ debouncedFunc(3, 'c');
76
+
77
+ vi.advanceTimersByTime(100);
78
+
79
+ expect(func).toHaveBeenCalledExactlyOnceWith(3, 'c');
80
+ });
81
+ });
@@ -1,5 +1,6 @@
1
1
  export * from './cast-mutable.mjs';
2
2
  export * from './cast-readonly.mjs';
3
+ export * from './debounce-function.mjs';
3
4
  export * from './if-then.mjs';
4
5
  export * from './map-nullable.mjs';
5
6
  export * from './memoize-function.mjs';