@daiso-tech/core 0.0.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.
Files changed (176) hide show
  1. package/.changeset/README.md +8 -0
  2. package/.changeset/config.json +11 -0
  3. package/.changeset/sweet-peas-beg.md +5 -0
  4. package/.eslintignore +3 -0
  5. package/.eslintrc.json +50 -0
  6. package/.gitattributes +6 -0
  7. package/.github/workflows/main.yaml +54 -0
  8. package/.github/workflows/release.yaml +31 -0
  9. package/.prettierignore +4 -0
  10. package/.prettierrc.json +4 -0
  11. package/.vscode/settings.json +46 -0
  12. package/LICENSE.md +194 -0
  13. package/docs-api/.nojekyll +1 -0
  14. package/docs-api/assets/highlight.css +92 -0
  15. package/docs-api/assets/icons.js +18 -0
  16. package/docs-api/assets/icons.svg +1 -0
  17. package/docs-api/assets/main.js +60 -0
  18. package/docs-api/assets/navigation.js +1 -0
  19. package/docs-api/assets/search.js +1 -0
  20. package/docs-api/assets/style.css +1448 -0
  21. package/docs-api/classes/AsyncIterableCollection.html +437 -0
  22. package/docs-api/classes/CollectionError.html +12 -0
  23. package/docs-api/classes/IndexOverflowError.html +12 -0
  24. package/docs-api/classes/ItemNotFoundError.html +12 -0
  25. package/docs-api/classes/IterableCollection.html +434 -0
  26. package/docs-api/classes/ListCollection.html +434 -0
  27. package/docs-api/classes/MultipleItemsFoundError.html +12 -0
  28. package/docs-api/classes/UnexpectedCollectionError.html +12 -0
  29. package/docs-api/functions/isAsyncIterable.html +1 -0
  30. package/docs-api/functions/isIterable.html +1 -0
  31. package/docs-api/functions/range.html +4 -0
  32. package/docs-api/hierarchy.html +1 -0
  33. package/docs-api/index.html +1 -0
  34. package/docs-api/modules.html +60 -0
  35. package/docs-api/types/AsyncCollapse.html +1 -0
  36. package/docs-api/types/AsyncFilter.html +1 -0
  37. package/docs-api/types/AsyncFilter_.html +1 -0
  38. package/docs-api/types/AsyncFindOrSettings.html +1 -0
  39. package/docs-api/types/AsyncFindSettings.html +1 -0
  40. package/docs-api/types/AsyncForEach.html +1 -0
  41. package/docs-api/types/AsyncForEach_.html +1 -0
  42. package/docs-api/types/AsyncGroupBySettings.html +1 -0
  43. package/docs-api/types/AsyncIterableValue.html +1 -0
  44. package/docs-api/types/AsyncLazyable.html +1 -0
  45. package/docs-api/types/AsyncLazyable_.html +1 -0
  46. package/docs-api/types/AsyncMap.html +1 -0
  47. package/docs-api/types/AsyncMap_.html +1 -0
  48. package/docs-api/types/AsyncModifier.html +1 -0
  49. package/docs-api/types/AsyncModifier_.html +1 -0
  50. package/docs-api/types/AsyncReduce.html +1 -0
  51. package/docs-api/types/AsyncReduceSettings.html +1 -0
  52. package/docs-api/types/AsyncReduce_.html +1 -0
  53. package/docs-api/types/AsyncTap.html +1 -0
  54. package/docs-api/types/AsyncTap_.html +1 -0
  55. package/docs-api/types/AsyncTransform.html +1 -0
  56. package/docs-api/types/AsyncTransform_.html +1 -0
  57. package/docs-api/types/Collapse.html +1 -0
  58. package/docs-api/types/Comparator.html +1 -0
  59. package/docs-api/types/EnsureType.html +1 -0
  60. package/docs-api/types/Filter.html +1 -0
  61. package/docs-api/types/FilterGuard.html +1 -0
  62. package/docs-api/types/Filter_.html +1 -0
  63. package/docs-api/types/FindOrSettings.html +1 -0
  64. package/docs-api/types/FindSettings.html +1 -0
  65. package/docs-api/types/ForEach.html +1 -0
  66. package/docs-api/types/GroupBySettings.html +1 -0
  67. package/docs-api/types/IAsyncCollection.html +357 -0
  68. package/docs-api/types/ICollection.html +357 -0
  69. package/docs-api/types/JoinSettings.html +1 -0
  70. package/docs-api/types/Lazyable.html +1 -0
  71. package/docs-api/types/Map.html +1 -0
  72. package/docs-api/types/Modifier.html +1 -0
  73. package/docs-api/types/PageSettings.html +1 -0
  74. package/docs-api/types/RecordItem.html +1 -0
  75. package/docs-api/types/Reduce.html +1 -0
  76. package/docs-api/types/ReduceSettings.html +1 -0
  77. package/docs-api/types/ReverseSettings.html +1 -0
  78. package/docs-api/types/SliceSettings.html +1 -0
  79. package/docs-api/types/SlidingSettings.html +1 -0
  80. package/docs-api/types/Tap.html +1 -0
  81. package/docs-api/types/Transform.html +1 -0
  82. package/docs-api/types/UpdatedItem.html +1 -0
  83. package/package.json +47 -0
  84. package/src/_module.ts +2 -0
  85. package/src/collection/_module.ts +4 -0
  86. package/src/collection/_shared.ts +45 -0
  87. package/src/collection/async-iterable-collection/_module.ts +1 -0
  88. package/src/collection/async-iterable-collection/async-iterable-collection.test.ts +3195 -0
  89. package/src/collection/async-iterable-collection/async-iterable-collection.ts +1276 -0
  90. package/src/collection/async-iterable-collection/async-iterable-helpers/_module.ts +35 -0
  91. package/src/collection/async-iterable-collection/async-iterable-helpers/async-abort-iterable.ts +25 -0
  92. package/src/collection/async-iterable-collection/async-iterable-helpers/async-chunk-iterable.ts +57 -0
  93. package/src/collection/async-iterable-collection/async-iterable-helpers/async-chunk-whilte-iterable.ts +54 -0
  94. package/src/collection/async-iterable-collection/async-iterable-helpers/async-collapse-iterable.ts +40 -0
  95. package/src/collection/async-iterable-collection/async-iterable-helpers/async-count-by-iterable.ts +58 -0
  96. package/src/collection/async-iterable-collection/async-iterable-helpers/async-cross-join-iterable.ts +69 -0
  97. package/src/collection/async-iterable-collection/async-iterable-helpers/async-delay-iterable.ts +19 -0
  98. package/src/collection/async-iterable-collection/async-iterable-helpers/async-entries-iterable.ts +28 -0
  99. package/src/collection/async-iterable-collection/async-iterable-helpers/async-filter-iterable.ts +42 -0
  100. package/src/collection/async-iterable-collection/async-iterable-helpers/async-flat-map-iterable.ts +44 -0
  101. package/src/collection/async-iterable-collection/async-iterable-helpers/async-group-by-iterable.ts +48 -0
  102. package/src/collection/async-iterable-collection/async-iterable-helpers/async-insert-after-iterable.ts +57 -0
  103. package/src/collection/async-iterable-collection/async-iterable-helpers/async-insert-before-iterable.ts +57 -0
  104. package/src/collection/async-iterable-collection/async-iterable-helpers/async-map-iterable.ts +40 -0
  105. package/src/collection/async-iterable-collection/async-iterable-helpers/async-merge-iterable.ts +35 -0
  106. package/src/collection/async-iterable-collection/async-iterable-helpers/async-pad-end-iterable.ts +41 -0
  107. package/src/collection/async-iterable-collection/async-iterable-helpers/async-pad-start-iterable.ts +41 -0
  108. package/src/collection/async-iterable-collection/async-iterable-helpers/async-partion-iterable.ts +63 -0
  109. package/src/collection/async-iterable-collection/async-iterable-helpers/async-repeat-iterable.ts +40 -0
  110. package/src/collection/async-iterable-collection/async-iterable-helpers/async-reverse-iterable.ts +35 -0
  111. package/src/collection/async-iterable-collection/async-iterable-helpers/async-shuffle-iterable.ts +23 -0
  112. package/src/collection/async-iterable-collection/async-iterable-helpers/async-skip-iterable.ts +41 -0
  113. package/src/collection/async-iterable-collection/async-iterable-helpers/async-skip-until-iterable.ts +55 -0
  114. package/src/collection/async-iterable-collection/async-iterable-helpers/async-slice-iterable.ts +33 -0
  115. package/src/collection/async-iterable-collection/async-iterable-helpers/async-sliding-iterable.ts +54 -0
  116. package/src/collection/async-iterable-collection/async-iterable-helpers/async-sort-iterable.ts +18 -0
  117. package/src/collection/async-iterable-collection/async-iterable-helpers/async-split-iterable.ts +64 -0
  118. package/src/collection/async-iterable-collection/async-iterable-helpers/async-take-iterable.ts +41 -0
  119. package/src/collection/async-iterable-collection/async-iterable-helpers/async-take-until-iterable.ts +41 -0
  120. package/src/collection/async-iterable-collection/async-iterable-helpers/async-tap-iterable.ts +34 -0
  121. package/src/collection/async-iterable-collection/async-iterable-helpers/async-timeout-iterable.ts +20 -0
  122. package/src/collection/async-iterable-collection/async-iterable-helpers/async-unique-iterable.ts +49 -0
  123. package/src/collection/async-iterable-collection/async-iterable-helpers/async-update-iterable.ts +64 -0
  124. package/src/collection/async-iterable-collection/async-iterable-helpers/async-when-iterable.ts +43 -0
  125. package/src/collection/async-iterable-collection/async-iterable-helpers/async-zip-iterable.ts +45 -0
  126. package/src/collection/iterable-collection/_module.ts +1 -0
  127. package/src/collection/iterable-collection/iterable-collection.test.ts +2379 -0
  128. package/src/collection/iterable-collection/iterable-collection.ts +1317 -0
  129. package/src/collection/iterable-collection/iterable-helpers/_module.ts +32 -0
  130. package/src/collection/iterable-collection/iterable-helpers/chunk-iterable.ts +49 -0
  131. package/src/collection/iterable-collection/iterable-helpers/chunk-whilte-iterable.ts +51 -0
  132. package/src/collection/iterable-collection/iterable-helpers/collapse-iterable.ts +35 -0
  133. package/src/collection/iterable-collection/iterable-helpers/count-by-iterable.ts +49 -0
  134. package/src/collection/iterable-collection/iterable-helpers/cross-join-iterable.ts +57 -0
  135. package/src/collection/iterable-collection/iterable-helpers/entries-iterable.ts +42 -0
  136. package/src/collection/iterable-collection/iterable-helpers/filter-iterable.ts +39 -0
  137. package/src/collection/iterable-collection/iterable-helpers/flat-map-iterable.ts +35 -0
  138. package/src/collection/iterable-collection/iterable-helpers/group-by-iterable.ts +52 -0
  139. package/src/collection/iterable-collection/iterable-helpers/insert-after-iterable.ts +43 -0
  140. package/src/collection/iterable-collection/iterable-helpers/insert-before-iterable.ts +43 -0
  141. package/src/collection/iterable-collection/iterable-helpers/map-iterable.ts +35 -0
  142. package/src/collection/iterable-collection/iterable-helpers/merge-iterable.ts +31 -0
  143. package/src/collection/iterable-collection/iterable-helpers/pad-end-iterable.ts +52 -0
  144. package/src/collection/iterable-collection/iterable-helpers/pad-start-iterable.ts +52 -0
  145. package/src/collection/iterable-collection/iterable-helpers/partion-iterable.ts +46 -0
  146. package/src/collection/iterable-collection/iterable-helpers/repeat-iterable.ts +36 -0
  147. package/src/collection/iterable-collection/iterable-helpers/reverse-iterable.ts +46 -0
  148. package/src/collection/iterable-collection/iterable-helpers/shuffle-iterable.ts +36 -0
  149. package/src/collection/iterable-collection/iterable-helpers/skip-iterable.ts +37 -0
  150. package/src/collection/iterable-collection/iterable-helpers/skip-until-iterable.ts +41 -0
  151. package/src/collection/iterable-collection/iterable-helpers/slice-iterable.ts +47 -0
  152. package/src/collection/iterable-collection/iterable-helpers/sliding-iterable.ts +49 -0
  153. package/src/collection/iterable-collection/iterable-helpers/sort-iterable.ts +29 -0
  154. package/src/collection/iterable-collection/iterable-helpers/split-iterable.ts +58 -0
  155. package/src/collection/iterable-collection/iterable-helpers/take-iterable.ts +37 -0
  156. package/src/collection/iterable-collection/iterable-helpers/take-until-iterable.ts +38 -0
  157. package/src/collection/iterable-collection/iterable-helpers/tap-iterable.ts +31 -0
  158. package/src/collection/iterable-collection/iterable-helpers/unique-iterable.ts +43 -0
  159. package/src/collection/iterable-collection/iterable-helpers/update-iterable.ts +50 -0
  160. package/src/collection/iterable-collection/iterable-helpers/when-iterable.ts +37 -0
  161. package/src/collection/iterable-collection/iterable-helpers/zip-iterable.ts +41 -0
  162. package/src/collection/list-collection/_module.ts +1 -0
  163. package/src/collection/list-collection/list-collection.test.ts +2280 -0
  164. package/src/collection/list-collection/list-collection.ts +1883 -0
  165. package/src/contracts/_module.ts +1 -0
  166. package/src/contracts/collection/_module.ts +3 -0
  167. package/src/contracts/collection/_shared.ts +346 -0
  168. package/src/contracts/collection/async-collection.contract.ts +1028 -0
  169. package/src/contracts/collection/collection.contract.ts +978 -0
  170. package/src/types.ts +2 -0
  171. package/tsconfig.base.json +31 -0
  172. package/tsconfig.cjs.json +12 -0
  173. package/tsconfig.esm.json +12 -0
  174. package/tsconfig.json +10 -0
  175. package/tsconfig.types.json +12 -0
  176. package/vite.config.ts +6 -0
@@ -0,0 +1,35 @@
1
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-chunk-iterable";
2
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-chunk-whilte-iterable";
3
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-collapse-iterable";
4
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-count-by-iterable";
5
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-cross-join-iterable";
6
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-entries-iterable";
7
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-filter-iterable";
8
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-flat-map-iterable";
9
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-group-by-iterable";
10
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-insert-after-iterable";
11
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-insert-before-iterable";
12
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-map-iterable";
13
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-merge-iterable";
14
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-pad-end-iterable";
15
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-pad-start-iterable";
16
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-partion-iterable";
17
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-repeat-iterable";
18
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-reverse-iterable";
19
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-shuffle-iterable";
20
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-skip-iterable";
21
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-skip-until-iterable";
22
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-slice-iterable";
23
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-sliding-iterable";
24
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-sort-iterable";
25
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-split-iterable";
26
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-take-iterable";
27
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-take-until-iterable";
28
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-tap-iterable";
29
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-unique-iterable";
30
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-update-iterable";
31
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-when-iterable";
32
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-zip-iterable";
33
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-timeout-iterable";
34
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-delay-iterable";
35
+ export * from "@/collection/async-iterable-collection/async-iterable-helpers/async-abort-iterable";
@@ -0,0 +1,25 @@
1
+ import { type IAsyncCollection } from "@/contracts/collection/_module";
2
+
3
+ /**
4
+ * @internal
5
+ */
6
+ export class AsyncAbortIterable<TInput> implements AsyncIterable<TInput> {
7
+ constructor(
8
+ private collection: IAsyncCollection<TInput>,
9
+ private signal: AbortSignal,
10
+ ) {}
11
+
12
+ async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
13
+ let hasAborted = false;
14
+ this.signal.addEventListener("abort", () => {
15
+ hasAborted = true;
16
+ });
17
+ for await (const item of this.collection) {
18
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
19
+ if (hasAborted) {
20
+ break;
21
+ }
22
+ yield item;
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,57 @@
1
+ import {
2
+ type AsyncIterableValue,
3
+ CollectionError,
4
+ type IAsyncCollection,
5
+ UnexpectedCollectionError,
6
+ } from "@/contracts/collection/_module";
7
+
8
+ /**
9
+ * @internal
10
+ */
11
+ export class AsyncChunkIterable<TInput>
12
+ implements AsyncIterable<IAsyncCollection<TInput>>
13
+ {
14
+ constructor(
15
+ private collection: IAsyncCollection<TInput>,
16
+ private chunkSize: number,
17
+ private makeCollection: <TInput>(
18
+ iterable: AsyncIterableValue<TInput>,
19
+ ) => IAsyncCollection<TInput>,
20
+ ) {}
21
+
22
+ async *[Symbol.asyncIterator](): AsyncIterator<IAsyncCollection<TInput>> {
23
+ try {
24
+ let chunk: IAsyncCollection<TInput> = this.makeCollection<TInput>(
25
+ [],
26
+ ),
27
+ currentChunkSize = 0,
28
+ isFirstIteration = true;
29
+ for await (const item of this.collection) {
30
+ currentChunkSize %= this.chunkSize;
31
+ const isFilled = currentChunkSize === 0;
32
+ if (!isFirstIteration && isFilled) {
33
+ yield chunk;
34
+ chunk = this.makeCollection<TInput>([]);
35
+ }
36
+ chunk = chunk.append([item]);
37
+ currentChunkSize++;
38
+ isFirstIteration = false;
39
+ }
40
+ const hasRest = currentChunkSize !== 0;
41
+ if (hasRest) {
42
+ yield chunk;
43
+ }
44
+ } catch (error: unknown) {
45
+ if (
46
+ error instanceof CollectionError ||
47
+ error instanceof TypeError
48
+ ) {
49
+ throw error;
50
+ }
51
+ throw new UnexpectedCollectionError(
52
+ `Unexpected error "${String(error)}" occured`,
53
+ error,
54
+ );
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,54 @@
1
+ import {
2
+ type AsyncFilter,
3
+ type AsyncIterableValue,
4
+ CollectionError,
5
+ type IAsyncCollection,
6
+ UnexpectedCollectionError,
7
+ } from "@/contracts/collection/_module";
8
+
9
+ /**
10
+ * @internal
11
+ */
12
+ export class AsyncChunkWhileIterable<TInput>
13
+ implements AsyncIterable<IAsyncCollection<TInput>>
14
+ {
15
+ constructor(
16
+ private collection: IAsyncCollection<TInput>,
17
+ private filter: AsyncFilter<TInput, IAsyncCollection<TInput>>,
18
+ private throwOnNumberLimit: boolean,
19
+ private makeCollection: <TInput>(
20
+ iterable: AsyncIterableValue<TInput>,
21
+ ) => IAsyncCollection<TInput>,
22
+ ) {}
23
+
24
+ async *[Symbol.asyncIterator](): AsyncIterator<IAsyncCollection<TInput>> {
25
+ try {
26
+ let collection: IAsyncCollection<TInput> =
27
+ this.makeCollection<TInput>([]);
28
+ for await (const [index, item] of this.collection.entries(
29
+ this.throwOnNumberLimit,
30
+ )) {
31
+ if (index === 0) {
32
+ collection = collection.append([item]);
33
+ } else if (await this.filter(item, index, collection)) {
34
+ collection = collection.append([item]);
35
+ } else {
36
+ yield collection;
37
+ collection = this.makeCollection<TInput>([item]);
38
+ }
39
+ }
40
+ yield collection;
41
+ } catch (error: unknown) {
42
+ if (
43
+ error instanceof CollectionError ||
44
+ error instanceof TypeError
45
+ ) {
46
+ throw error;
47
+ }
48
+ throw new UnexpectedCollectionError(
49
+ `Unexpected error "${String(error)}" occured`,
50
+ error,
51
+ );
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,40 @@
1
+ import { isAsyncIterable, isIterable } from "@/collection/_shared";
2
+ import {
3
+ type AsyncCollapse,
4
+ CollectionError,
5
+ type IAsyncCollection,
6
+ UnexpectedCollectionError,
7
+ } from "@/contracts/collection/_module";
8
+
9
+ /**
10
+ * @internal
11
+ */
12
+ export class AsyncCollapseIterable<TInput>
13
+ implements AsyncIterable<AsyncCollapse<TInput>>
14
+ {
15
+ constructor(private collection: IAsyncCollection<TInput>) {}
16
+
17
+ async *[Symbol.asyncIterator](): AsyncIterator<AsyncCollapse<TInput>> {
18
+ try {
19
+ for await (const item of this.collection) {
20
+ if (isIterable<TInput>(item) || isAsyncIterable<TInput>(item)) {
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ yield* item as any;
23
+ } else {
24
+ yield item as AsyncCollapse<TInput>;
25
+ }
26
+ }
27
+ } catch (error: unknown) {
28
+ if (
29
+ error instanceof CollectionError ||
30
+ error instanceof TypeError
31
+ ) {
32
+ throw error;
33
+ }
34
+ throw new UnexpectedCollectionError(
35
+ `Unexpected error "${String(error)}" occured`,
36
+ error,
37
+ );
38
+ }
39
+ }
40
+ }
@@ -0,0 +1,58 @@
1
+ import {
2
+ type AsyncMap,
3
+ CollectionError,
4
+ type IAsyncCollection,
5
+ type RecordItem,
6
+ UnexpectedCollectionError,
7
+ } from "@/contracts/collection/_module";
8
+
9
+ /**
10
+ * @internal
11
+ */
12
+ export class AsyncCountByIterable<TInput, TOutput = TInput>
13
+ implements AsyncIterable<RecordItem<TOutput, number>>
14
+ {
15
+ constructor(
16
+ private collection: IAsyncCollection<TInput>,
17
+ private callback: AsyncMap<
18
+ TInput,
19
+ IAsyncCollection<TInput>,
20
+ TOutput
21
+ > = (item) =>
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return
23
+ item as any,
24
+ private throwOnNumberLimit: boolean,
25
+ ) {}
26
+
27
+ async *[Symbol.asyncIterator](): AsyncIterator<
28
+ RecordItem<TOutput, number>
29
+ > {
30
+ try {
31
+ const map = new Map<TOutput, number>();
32
+ for await (const [index, item] of this.collection.entries(
33
+ this.throwOnNumberLimit,
34
+ )) {
35
+ const key = await this.callback(item, index, this.collection);
36
+ if (!map.has(key)) {
37
+ map.set(key, 0);
38
+ }
39
+ const counter = map.get(key);
40
+ if (counter !== undefined) {
41
+ map.set(key, counter + 1);
42
+ }
43
+ }
44
+ yield* map;
45
+ } catch (error: unknown) {
46
+ if (
47
+ error instanceof CollectionError ||
48
+ error instanceof TypeError
49
+ ) {
50
+ throw error;
51
+ }
52
+ throw new UnexpectedCollectionError(
53
+ `Unexpected error "${String(error)}" occured`,
54
+ error,
55
+ );
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,69 @@
1
+ import {
2
+ type AsyncIterableValue,
3
+ CollectionError,
4
+ type IAsyncCollection,
5
+ UnexpectedCollectionError,
6
+ } from "@/contracts/collection/_module";
7
+
8
+ /**
9
+ * @internal
10
+ */
11
+ export class AsyncCrossJoinIterable<TInput, TExtended>
12
+ implements AsyncIterable<IAsyncCollection<TInput | TExtended>>
13
+ {
14
+ constructor(
15
+ private collection: IAsyncCollection<TInput>,
16
+ private iterables: Array<AsyncIterableValue<TExtended>>,
17
+ private makeCollection: <TInput>(
18
+ iterable: AsyncIterableValue<TInput>,
19
+ ) => IAsyncCollection<TInput>,
20
+ ) {}
21
+
22
+ async *[Symbol.asyncIterator](): AsyncIterator<
23
+ IAsyncCollection<TInput | TExtended>
24
+ > {
25
+ try {
26
+ yield* await this.makeCollection<
27
+ IAsyncCollection<TInput | TExtended>
28
+ >([
29
+ this.collection,
30
+ ...this.iterables.map<IAsyncCollection<TExtended>>((iterable) =>
31
+ this.makeCollection(iterable),
32
+ ),
33
+ ]).reduce<IAsyncCollection<IAsyncCollection<TInput | TExtended>>>({
34
+ reduceFn: (a, b) => {
35
+ return a
36
+ .map((x) =>
37
+ b.map((y) => {
38
+ return x.append([y]);
39
+ }),
40
+ )
41
+ .reduce<
42
+ IAsyncCollection<
43
+ IAsyncCollection<TInput | TExtended>
44
+ >
45
+ >({
46
+ reduceFn: (c, b) => c.append(b),
47
+ initialValue: this.makeCollection<
48
+ IAsyncCollection<TInput | TExtended>
49
+ >([]),
50
+ });
51
+ },
52
+ initialValue: this.makeCollection<
53
+ IAsyncCollection<TInput | TExtended>
54
+ >([this.makeCollection<TInput | TExtended>([])]),
55
+ });
56
+ } catch (error: unknown) {
57
+ if (
58
+ error instanceof CollectionError ||
59
+ error instanceof TypeError
60
+ ) {
61
+ throw error;
62
+ }
63
+ throw new UnexpectedCollectionError(
64
+ `Unexpected error "${String(error)}" occured`,
65
+ error,
66
+ );
67
+ }
68
+ }
69
+ }
@@ -0,0 +1,19 @@
1
+ import { type IAsyncCollection } from "@/contracts/collection/_module";
2
+
3
+ /**
4
+ * @internal
5
+ */
6
+ export class AsyncDelayIterable<TInput> implements AsyncIterable<TInput> {
7
+ constructor(
8
+ private collection: IAsyncCollection<TInput>,
9
+ private timeInMs: number,
10
+ ) {}
11
+
12
+ async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
13
+ for await (const item of this.collection) {
14
+ yield item;
15
+
16
+ await new Promise((resolve) => setTimeout(resolve, this.timeInMs));
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,28 @@
1
+ import {
2
+ type RecordItem,
3
+ IndexOverflowError,
4
+ } from "@/contracts/collection/_shared";
5
+ import type { AsyncIterableValue } from "@/contracts/collection/async-collection.contract";
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export class AsyncEntriesIterable<TInput>
11
+ implements AsyncIterable<RecordItem<number, TInput>>
12
+ {
13
+ constructor(
14
+ private iterable: AsyncIterableValue<TInput>,
15
+ private throwOnNumberLimit: boolean,
16
+ ) {}
17
+
18
+ async *[Symbol.asyncIterator](): AsyncIterator<RecordItem<number, TInput>> {
19
+ let index = 0;
20
+ for await (const item of this.iterable) {
21
+ if (this.throwOnNumberLimit && index === Number.MAX_SAFE_INTEGER) {
22
+ throw new IndexOverflowError("Index has overflowed");
23
+ }
24
+ yield [index, item];
25
+ index++;
26
+ }
27
+ }
28
+ }
@@ -0,0 +1,42 @@
1
+ import {
2
+ type AsyncFilter,
3
+ CollectionError,
4
+ type IAsyncCollection,
5
+ UnexpectedCollectionError,
6
+ } from "@/contracts/collection/_module";
7
+
8
+ /**
9
+ * @internal
10
+ */
11
+ export class AsyncFilterIterable<TInput, TOutput extends TInput>
12
+ implements AsyncIterable<TOutput>
13
+ {
14
+ constructor(
15
+ private collection: IAsyncCollection<TInput>,
16
+ private filter: AsyncFilter<TInput, IAsyncCollection<TInput>, TOutput>,
17
+ private throwOnNumberLimit: boolean,
18
+ ) {}
19
+
20
+ async *[Symbol.asyncIterator](): AsyncIterator<TOutput> {
21
+ try {
22
+ for await (const [index, item] of this.collection.entries(
23
+ this.throwOnNumberLimit,
24
+ )) {
25
+ if (await this.filter(item, index, this.collection)) {
26
+ yield item as TOutput;
27
+ }
28
+ }
29
+ } catch (error: unknown) {
30
+ if (
31
+ error instanceof CollectionError ||
32
+ error instanceof TypeError
33
+ ) {
34
+ throw error;
35
+ }
36
+ throw new UnexpectedCollectionError(
37
+ `Unexpected error "${String(error)}" occured`,
38
+ error,
39
+ );
40
+ }
41
+ }
42
+ }
@@ -0,0 +1,44 @@
1
+ import {
2
+ type AsyncMap,
3
+ CollectionError,
4
+ type IAsyncCollection,
5
+ UnexpectedCollectionError,
6
+ } from "@/contracts/collection/_module";
7
+
8
+ /**
9
+ * @internal
10
+ */
11
+ export class AsyncFlatMapIterable<TInput, TOutput>
12
+ implements AsyncIterable<TOutput>
13
+ {
14
+ constructor(
15
+ private collection: IAsyncCollection<TInput>,
16
+ private map: AsyncMap<
17
+ TInput,
18
+ IAsyncCollection<TInput>,
19
+ Iterable<TOutput>
20
+ >,
21
+ private throwOnNumberLimit: boolean,
22
+ ) {}
23
+
24
+ async *[Symbol.asyncIterator](): AsyncIterator<TOutput> {
25
+ try {
26
+ for await (const [index, item] of this.collection.entries(
27
+ this.throwOnNumberLimit,
28
+ )) {
29
+ yield* await this.map(item, index, this.collection);
30
+ }
31
+ } catch (error: unknown) {
32
+ if (
33
+ error instanceof CollectionError ||
34
+ error instanceof TypeError
35
+ ) {
36
+ throw error;
37
+ }
38
+ throw new UnexpectedCollectionError(
39
+ `Unexpected error "${String(error)}" occured`,
40
+ error,
41
+ );
42
+ }
43
+ }
44
+ }
@@ -0,0 +1,48 @@
1
+ import {
2
+ type AsyncIterableValue,
3
+ type AsyncMap,
4
+ type IAsyncCollection,
5
+ type RecordItem,
6
+ } from "@/contracts/collection/_module";
7
+
8
+ /**
9
+ * @internal
10
+ */
11
+ export class AsyncGroupByIterable<TInput, TOutput = TInput>
12
+ implements AsyncIterable<RecordItem<TOutput, IAsyncCollection<TInput>>>
13
+ {
14
+ constructor(
15
+ private collection: IAsyncCollection<TInput>,
16
+ private callback: AsyncMap<
17
+ TInput,
18
+ IAsyncCollection<TInput>,
19
+ TOutput
20
+ > = (item) =>
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return
22
+ item as any,
23
+ private throwOnNumberLimit: boolean,
24
+ private makeCollection: <TInput>(
25
+ iterable: AsyncIterableValue<TInput>,
26
+ ) => IAsyncCollection<TInput>,
27
+ ) {}
28
+
29
+ async *[Symbol.asyncIterator](): AsyncIterator<
30
+ RecordItem<TOutput, IAsyncCollection<TInput>>
31
+ > {
32
+ const map = new Map<TOutput, IAsyncCollection<TInput>>();
33
+
34
+ for await (const [index, item] of this.collection.entries(
35
+ this.throwOnNumberLimit,
36
+ )) {
37
+ const key = await this.callback(item, index, this.collection);
38
+ let collection: IAsyncCollection<TInput> | undefined = map.get(key);
39
+ if (collection === undefined) {
40
+ collection = this.makeCollection<TInput>([]);
41
+ map.set(key, collection);
42
+ }
43
+
44
+ map.set(key, collection.append([item]));
45
+ }
46
+ yield* map;
47
+ }
48
+ }
@@ -0,0 +1,57 @@
1
+ import {
2
+ type AsyncFilter,
3
+ type AsyncIterableValue,
4
+ CollectionError,
5
+ type IAsyncCollection,
6
+ IndexOverflowError,
7
+ UnexpectedCollectionError,
8
+ } from "@/contracts/collection/_module";
9
+
10
+ /**
11
+ * @internal
12
+ */
13
+ export class AsyncInsertAfterIterable<TInput, TExtended>
14
+ implements AsyncIterable<TInput | TExtended>
15
+ {
16
+ constructor(
17
+ private collection: IAsyncCollection<TInput>,
18
+ private filter: AsyncFilter<TInput, IAsyncCollection<TInput>>,
19
+ private iterable: AsyncIterableValue<TInput | TExtended>,
20
+ private throwOnNumberLimit: boolean,
21
+ ) {}
22
+
23
+ async *[Symbol.asyncIterator](): AsyncIterator<TInput | TExtended> {
24
+ try {
25
+ let hasMatched = false,
26
+ index = 0;
27
+ for await (const item of this.collection) {
28
+ if (
29
+ this.throwOnNumberLimit &&
30
+ index === Number.MAX_SAFE_INTEGER
31
+ ) {
32
+ throw new IndexOverflowError("Index has overflowed");
33
+ }
34
+ yield item;
35
+ if (
36
+ !hasMatched &&
37
+ (await this.filter(item, index, this.collection))
38
+ ) {
39
+ yield* this.iterable;
40
+ hasMatched = true;
41
+ }
42
+ index++;
43
+ }
44
+ } catch (error: unknown) {
45
+ if (
46
+ error instanceof CollectionError ||
47
+ error instanceof TypeError
48
+ ) {
49
+ throw error;
50
+ }
51
+ throw new UnexpectedCollectionError(
52
+ `Unexpected error "${String(error)}" occured`,
53
+ error,
54
+ );
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,57 @@
1
+ import {
2
+ type AsyncFilter,
3
+ type AsyncIterableValue,
4
+ CollectionError,
5
+ type IAsyncCollection,
6
+ IndexOverflowError,
7
+ UnexpectedCollectionError,
8
+ } from "@/contracts/collection/_module";
9
+
10
+ /**
11
+ * @internal
12
+ */
13
+ export class AsyncInsertBeforeIterable<TInput, TExtended>
14
+ implements AsyncIterable<TInput | TExtended>
15
+ {
16
+ constructor(
17
+ private collection: IAsyncCollection<TInput>,
18
+ private filter: AsyncFilter<TInput, IAsyncCollection<TInput>>,
19
+ private iterable: AsyncIterableValue<TInput | TExtended>,
20
+ private throwOnNumberLimit: boolean,
21
+ ) {}
22
+
23
+ async *[Symbol.asyncIterator](): AsyncIterator<TInput | TExtended> {
24
+ try {
25
+ let hasMatched = false,
26
+ index = 0;
27
+ for await (const item of this.collection) {
28
+ if (
29
+ this.throwOnNumberLimit &&
30
+ index === Number.MAX_SAFE_INTEGER
31
+ ) {
32
+ throw new IndexOverflowError("Index has overflowed");
33
+ }
34
+ if (
35
+ !hasMatched &&
36
+ (await this.filter(item, index, this.collection))
37
+ ) {
38
+ yield* this.iterable;
39
+ hasMatched = true;
40
+ }
41
+ yield item;
42
+ index++;
43
+ }
44
+ } catch (error: unknown) {
45
+ if (
46
+ error instanceof CollectionError ||
47
+ error instanceof TypeError
48
+ ) {
49
+ throw error;
50
+ }
51
+ throw new UnexpectedCollectionError(
52
+ `Unexpected error "${String(error)}" occured`,
53
+ error,
54
+ );
55
+ }
56
+ }
57
+ }