@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.
- package/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.changeset/sweet-peas-beg.md +5 -0
- package/.eslintignore +3 -0
- package/.eslintrc.json +50 -0
- package/.gitattributes +6 -0
- package/.github/workflows/main.yaml +54 -0
- package/.github/workflows/release.yaml +31 -0
- package/.prettierignore +4 -0
- package/.prettierrc.json +4 -0
- package/.vscode/settings.json +46 -0
- package/LICENSE.md +194 -0
- package/docs-api/.nojekyll +1 -0
- package/docs-api/assets/highlight.css +92 -0
- package/docs-api/assets/icons.js +18 -0
- package/docs-api/assets/icons.svg +1 -0
- package/docs-api/assets/main.js +60 -0
- package/docs-api/assets/navigation.js +1 -0
- package/docs-api/assets/search.js +1 -0
- package/docs-api/assets/style.css +1448 -0
- package/docs-api/classes/AsyncIterableCollection.html +437 -0
- package/docs-api/classes/CollectionError.html +12 -0
- package/docs-api/classes/IndexOverflowError.html +12 -0
- package/docs-api/classes/ItemNotFoundError.html +12 -0
- package/docs-api/classes/IterableCollection.html +434 -0
- package/docs-api/classes/ListCollection.html +434 -0
- package/docs-api/classes/MultipleItemsFoundError.html +12 -0
- package/docs-api/classes/UnexpectedCollectionError.html +12 -0
- package/docs-api/functions/isAsyncIterable.html +1 -0
- package/docs-api/functions/isIterable.html +1 -0
- package/docs-api/functions/range.html +4 -0
- package/docs-api/hierarchy.html +1 -0
- package/docs-api/index.html +1 -0
- package/docs-api/modules.html +60 -0
- package/docs-api/types/AsyncCollapse.html +1 -0
- package/docs-api/types/AsyncFilter.html +1 -0
- package/docs-api/types/AsyncFilter_.html +1 -0
- package/docs-api/types/AsyncFindOrSettings.html +1 -0
- package/docs-api/types/AsyncFindSettings.html +1 -0
- package/docs-api/types/AsyncForEach.html +1 -0
- package/docs-api/types/AsyncForEach_.html +1 -0
- package/docs-api/types/AsyncGroupBySettings.html +1 -0
- package/docs-api/types/AsyncIterableValue.html +1 -0
- package/docs-api/types/AsyncLazyable.html +1 -0
- package/docs-api/types/AsyncLazyable_.html +1 -0
- package/docs-api/types/AsyncMap.html +1 -0
- package/docs-api/types/AsyncMap_.html +1 -0
- package/docs-api/types/AsyncModifier.html +1 -0
- package/docs-api/types/AsyncModifier_.html +1 -0
- package/docs-api/types/AsyncReduce.html +1 -0
- package/docs-api/types/AsyncReduceSettings.html +1 -0
- package/docs-api/types/AsyncReduce_.html +1 -0
- package/docs-api/types/AsyncTap.html +1 -0
- package/docs-api/types/AsyncTap_.html +1 -0
- package/docs-api/types/AsyncTransform.html +1 -0
- package/docs-api/types/AsyncTransform_.html +1 -0
- package/docs-api/types/Collapse.html +1 -0
- package/docs-api/types/Comparator.html +1 -0
- package/docs-api/types/EnsureType.html +1 -0
- package/docs-api/types/Filter.html +1 -0
- package/docs-api/types/FilterGuard.html +1 -0
- package/docs-api/types/Filter_.html +1 -0
- package/docs-api/types/FindOrSettings.html +1 -0
- package/docs-api/types/FindSettings.html +1 -0
- package/docs-api/types/ForEach.html +1 -0
- package/docs-api/types/GroupBySettings.html +1 -0
- package/docs-api/types/IAsyncCollection.html +357 -0
- package/docs-api/types/ICollection.html +357 -0
- package/docs-api/types/JoinSettings.html +1 -0
- package/docs-api/types/Lazyable.html +1 -0
- package/docs-api/types/Map.html +1 -0
- package/docs-api/types/Modifier.html +1 -0
- package/docs-api/types/PageSettings.html +1 -0
- package/docs-api/types/RecordItem.html +1 -0
- package/docs-api/types/Reduce.html +1 -0
- package/docs-api/types/ReduceSettings.html +1 -0
- package/docs-api/types/ReverseSettings.html +1 -0
- package/docs-api/types/SliceSettings.html +1 -0
- package/docs-api/types/SlidingSettings.html +1 -0
- package/docs-api/types/Tap.html +1 -0
- package/docs-api/types/Transform.html +1 -0
- package/docs-api/types/UpdatedItem.html +1 -0
- package/package.json +47 -0
- package/src/_module.ts +2 -0
- package/src/collection/_module.ts +4 -0
- package/src/collection/_shared.ts +45 -0
- package/src/collection/async-iterable-collection/_module.ts +1 -0
- package/src/collection/async-iterable-collection/async-iterable-collection.test.ts +3195 -0
- package/src/collection/async-iterable-collection/async-iterable-collection.ts +1276 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/_module.ts +35 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-abort-iterable.ts +25 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-chunk-iterable.ts +57 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-chunk-whilte-iterable.ts +54 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-collapse-iterable.ts +40 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-count-by-iterable.ts +58 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-cross-join-iterable.ts +69 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-delay-iterable.ts +19 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-entries-iterable.ts +28 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-filter-iterable.ts +42 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-flat-map-iterable.ts +44 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-group-by-iterable.ts +48 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-insert-after-iterable.ts +57 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-insert-before-iterable.ts +57 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-map-iterable.ts +40 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-merge-iterable.ts +35 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-pad-end-iterable.ts +41 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-pad-start-iterable.ts +41 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-partion-iterable.ts +63 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-repeat-iterable.ts +40 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-reverse-iterable.ts +35 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-shuffle-iterable.ts +23 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-skip-iterable.ts +41 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-skip-until-iterable.ts +55 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-slice-iterable.ts +33 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-sliding-iterable.ts +54 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-sort-iterable.ts +18 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-split-iterable.ts +64 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-take-iterable.ts +41 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-take-until-iterable.ts +41 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-tap-iterable.ts +34 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-timeout-iterable.ts +20 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-unique-iterable.ts +49 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-update-iterable.ts +64 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-when-iterable.ts +43 -0
- package/src/collection/async-iterable-collection/async-iterable-helpers/async-zip-iterable.ts +45 -0
- package/src/collection/iterable-collection/_module.ts +1 -0
- package/src/collection/iterable-collection/iterable-collection.test.ts +2379 -0
- package/src/collection/iterable-collection/iterable-collection.ts +1317 -0
- package/src/collection/iterable-collection/iterable-helpers/_module.ts +32 -0
- package/src/collection/iterable-collection/iterable-helpers/chunk-iterable.ts +49 -0
- package/src/collection/iterable-collection/iterable-helpers/chunk-whilte-iterable.ts +51 -0
- package/src/collection/iterable-collection/iterable-helpers/collapse-iterable.ts +35 -0
- package/src/collection/iterable-collection/iterable-helpers/count-by-iterable.ts +49 -0
- package/src/collection/iterable-collection/iterable-helpers/cross-join-iterable.ts +57 -0
- package/src/collection/iterable-collection/iterable-helpers/entries-iterable.ts +42 -0
- package/src/collection/iterable-collection/iterable-helpers/filter-iterable.ts +39 -0
- package/src/collection/iterable-collection/iterable-helpers/flat-map-iterable.ts +35 -0
- package/src/collection/iterable-collection/iterable-helpers/group-by-iterable.ts +52 -0
- package/src/collection/iterable-collection/iterable-helpers/insert-after-iterable.ts +43 -0
- package/src/collection/iterable-collection/iterable-helpers/insert-before-iterable.ts +43 -0
- package/src/collection/iterable-collection/iterable-helpers/map-iterable.ts +35 -0
- package/src/collection/iterable-collection/iterable-helpers/merge-iterable.ts +31 -0
- package/src/collection/iterable-collection/iterable-helpers/pad-end-iterable.ts +52 -0
- package/src/collection/iterable-collection/iterable-helpers/pad-start-iterable.ts +52 -0
- package/src/collection/iterable-collection/iterable-helpers/partion-iterable.ts +46 -0
- package/src/collection/iterable-collection/iterable-helpers/repeat-iterable.ts +36 -0
- package/src/collection/iterable-collection/iterable-helpers/reverse-iterable.ts +46 -0
- package/src/collection/iterable-collection/iterable-helpers/shuffle-iterable.ts +36 -0
- package/src/collection/iterable-collection/iterable-helpers/skip-iterable.ts +37 -0
- package/src/collection/iterable-collection/iterable-helpers/skip-until-iterable.ts +41 -0
- package/src/collection/iterable-collection/iterable-helpers/slice-iterable.ts +47 -0
- package/src/collection/iterable-collection/iterable-helpers/sliding-iterable.ts +49 -0
- package/src/collection/iterable-collection/iterable-helpers/sort-iterable.ts +29 -0
- package/src/collection/iterable-collection/iterable-helpers/split-iterable.ts +58 -0
- package/src/collection/iterable-collection/iterable-helpers/take-iterable.ts +37 -0
- package/src/collection/iterable-collection/iterable-helpers/take-until-iterable.ts +38 -0
- package/src/collection/iterable-collection/iterable-helpers/tap-iterable.ts +31 -0
- package/src/collection/iterable-collection/iterable-helpers/unique-iterable.ts +43 -0
- package/src/collection/iterable-collection/iterable-helpers/update-iterable.ts +50 -0
- package/src/collection/iterable-collection/iterable-helpers/when-iterable.ts +37 -0
- package/src/collection/iterable-collection/iterable-helpers/zip-iterable.ts +41 -0
- package/src/collection/list-collection/_module.ts +1 -0
- package/src/collection/list-collection/list-collection.test.ts +2280 -0
- package/src/collection/list-collection/list-collection.ts +1883 -0
- package/src/contracts/_module.ts +1 -0
- package/src/contracts/collection/_module.ts +3 -0
- package/src/contracts/collection/_shared.ts +346 -0
- package/src/contracts/collection/async-collection.contract.ts +1028 -0
- package/src/contracts/collection/collection.contract.ts +978 -0
- package/src/types.ts +2 -0
- package/tsconfig.base.json +31 -0
- package/tsconfig.cjs.json +12 -0
- package/tsconfig.esm.json +12 -0
- package/tsconfig.json +10 -0
- package/tsconfig.types.json +12 -0
- package/vite.config.ts +6 -0
|
@@ -0,0 +1,40 @@
|
|
|
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 AsyncMapIterable<TInput, TOutput>
|
|
12
|
+
implements AsyncIterable<TOutput>
|
|
13
|
+
{
|
|
14
|
+
constructor(
|
|
15
|
+
private collection: IAsyncCollection<TInput>,
|
|
16
|
+
private map: AsyncMap<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
|
+
yield this.map(item, index, this.collection);
|
|
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
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-merge-iterable.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AsyncIterableValue,
|
|
3
|
+
CollectionError,
|
|
4
|
+
UnexpectedCollectionError,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export class AsyncMergeIterable<TInput, TExtended>
|
|
11
|
+
implements AsyncIterable<TInput | TExtended>
|
|
12
|
+
{
|
|
13
|
+
constructor(
|
|
14
|
+
private iterableA: AsyncIterableValue<TInput>,
|
|
15
|
+
private iterableB: AsyncIterableValue<TExtended>,
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput | TExtended> {
|
|
19
|
+
try {
|
|
20
|
+
yield* this.iterableA;
|
|
21
|
+
yield* this.iterableB;
|
|
22
|
+
} catch (error: unknown) {
|
|
23
|
+
if (
|
|
24
|
+
error instanceof CollectionError ||
|
|
25
|
+
error instanceof TypeError
|
|
26
|
+
) {
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
throw new UnexpectedCollectionError(
|
|
30
|
+
`Unexpected error "${String(error)}" occured`,
|
|
31
|
+
error,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-pad-end-iterable.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AsyncIterableValue,
|
|
3
|
+
type IAsyncCollection,
|
|
4
|
+
} from "@/contracts/collection/_module";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export class AsyncPadEndIterable<TInput, TExtended>
|
|
10
|
+
implements AsyncIterable<TInput | TExtended>
|
|
11
|
+
{
|
|
12
|
+
constructor(
|
|
13
|
+
private collection: IAsyncCollection<TInput>,
|
|
14
|
+
private maxLength: number,
|
|
15
|
+
private fillItems: AsyncIterableValue<TExtended>,
|
|
16
|
+
private makeCollection: <TInput>(
|
|
17
|
+
iterable: AsyncIterableValue<TInput>,
|
|
18
|
+
) => IAsyncCollection<TInput>,
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput | TExtended> {
|
|
22
|
+
const fillCollections = this.makeCollection(this.fillItems);
|
|
23
|
+
const fillSize = await fillCollections.size();
|
|
24
|
+
const size = await this.collection.size();
|
|
25
|
+
const repeat = Math.floor((this.maxLength - size) / fillSize);
|
|
26
|
+
let resultCollection: IAsyncCollection<TInput | TExtended> =
|
|
27
|
+
this.makeCollection<TInput | TExtended>([]);
|
|
28
|
+
for (let index = 0; index < repeat; index++) {
|
|
29
|
+
resultCollection = resultCollection.append(fillCollections);
|
|
30
|
+
}
|
|
31
|
+
const restAmount = this.maxLength - (repeat * fillSize + size);
|
|
32
|
+
resultCollection = resultCollection.append(
|
|
33
|
+
fillCollections.slice({
|
|
34
|
+
start: 0,
|
|
35
|
+
end: restAmount,
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
resultCollection = resultCollection.prepend(this.collection);
|
|
39
|
+
yield* resultCollection;
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-pad-start-iterable.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AsyncIterableValue,
|
|
3
|
+
type IAsyncCollection,
|
|
4
|
+
} from "@/contracts/collection/_module";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export class AsyncPadStartIterable<TInput, TExtended>
|
|
10
|
+
implements AsyncIterable<TInput | TExtended>
|
|
11
|
+
{
|
|
12
|
+
constructor(
|
|
13
|
+
private collection: IAsyncCollection<TInput>,
|
|
14
|
+
private maxLength: number,
|
|
15
|
+
private fillItems: AsyncIterableValue<TExtended>,
|
|
16
|
+
private makeCollection: <TInput>(
|
|
17
|
+
iterable: AsyncIterableValue<TInput>,
|
|
18
|
+
) => IAsyncCollection<TInput>,
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput | TExtended> {
|
|
22
|
+
const fillCollections = this.makeCollection(this.fillItems);
|
|
23
|
+
const fillSize = await fillCollections.size();
|
|
24
|
+
const size = await this.collection.size();
|
|
25
|
+
const repeat = Math.floor((this.maxLength - size) / fillSize);
|
|
26
|
+
let resultCollection: IAsyncCollection<TInput | TExtended> =
|
|
27
|
+
this.makeCollection<TInput | TExtended>([]);
|
|
28
|
+
for (let index = 0; index < repeat; index++) {
|
|
29
|
+
resultCollection = resultCollection.append(fillCollections);
|
|
30
|
+
}
|
|
31
|
+
const restAmount = this.maxLength - (repeat * fillSize + size);
|
|
32
|
+
resultCollection = resultCollection.append(
|
|
33
|
+
fillCollections.slice({
|
|
34
|
+
start: 0,
|
|
35
|
+
end: restAmount,
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
resultCollection = resultCollection.append(this.collection);
|
|
39
|
+
yield* resultCollection;
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-partion-iterable.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
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 AsyncPartionIterable<TInput>
|
|
14
|
+
implements AsyncIterable<IAsyncCollection<TInput>>
|
|
15
|
+
{
|
|
16
|
+
constructor(
|
|
17
|
+
private collection: IAsyncCollection<TInput>,
|
|
18
|
+
private filter: AsyncFilter<TInput, IAsyncCollection<TInput>>,
|
|
19
|
+
private throwOnNumberLimit: boolean,
|
|
20
|
+
private makeCollection: <TInput>(
|
|
21
|
+
iterable: AsyncIterableValue<TInput>,
|
|
22
|
+
) => IAsyncCollection<TInput>,
|
|
23
|
+
) {}
|
|
24
|
+
|
|
25
|
+
async *[Symbol.asyncIterator](): AsyncIterator<IAsyncCollection<TInput>> {
|
|
26
|
+
try {
|
|
27
|
+
let chunkA: IAsyncCollection<TInput> = this.makeCollection<TInput>(
|
|
28
|
+
[],
|
|
29
|
+
),
|
|
30
|
+
chunkB: IAsyncCollection<TInput> = this.makeCollection<TInput>(
|
|
31
|
+
[],
|
|
32
|
+
),
|
|
33
|
+
index = 0;
|
|
34
|
+
for await (const item of this.collection) {
|
|
35
|
+
if (
|
|
36
|
+
this.throwOnNumberLimit &&
|
|
37
|
+
index === Number.MAX_SAFE_INTEGER
|
|
38
|
+
) {
|
|
39
|
+
throw new IndexOverflowError("Index has overflowed");
|
|
40
|
+
}
|
|
41
|
+
if (await this.filter(item, index, this.collection)) {
|
|
42
|
+
chunkA = chunkA.append([item]);
|
|
43
|
+
} else {
|
|
44
|
+
chunkB = chunkB.append([item]);
|
|
45
|
+
}
|
|
46
|
+
index++;
|
|
47
|
+
}
|
|
48
|
+
yield chunkA;
|
|
49
|
+
yield chunkB;
|
|
50
|
+
} catch (error: unknown) {
|
|
51
|
+
if (
|
|
52
|
+
error instanceof CollectionError ||
|
|
53
|
+
error instanceof TypeError
|
|
54
|
+
) {
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
throw new UnexpectedCollectionError(
|
|
58
|
+
`Unexpected error "${String(error)}" occured`,
|
|
59
|
+
error,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-repeat-iterable.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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 AsyncRepeatIterable<TInput> implements AsyncIterable<TInput> {
|
|
12
|
+
constructor(
|
|
13
|
+
private collection: IAsyncCollection<TInput>,
|
|
14
|
+
private amount: number,
|
|
15
|
+
private makeAsyncCollection: <TInput>(
|
|
16
|
+
iterable: AsyncIterableValue<TInput>,
|
|
17
|
+
) => IAsyncCollection<TInput>,
|
|
18
|
+
) {}
|
|
19
|
+
|
|
20
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
|
|
21
|
+
try {
|
|
22
|
+
let collection = this.makeAsyncCollection<TInput>([]);
|
|
23
|
+
for (let index = 0; index < this.amount - 1; index++) {
|
|
24
|
+
collection = collection.append(this.collection);
|
|
25
|
+
}
|
|
26
|
+
yield* collection;
|
|
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
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-reverse-iterable.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AsyncIterableValue,
|
|
3
|
+
type IAsyncCollection,
|
|
4
|
+
} from "@/contracts/collection/_module";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export class AsyncReverseIterable<TInput> implements AsyncIterable<TInput> {
|
|
10
|
+
constructor(
|
|
11
|
+
private collection: IAsyncCollection<TInput>,
|
|
12
|
+
private chunkSize: number,
|
|
13
|
+
private throwOnNumberLimit: boolean,
|
|
14
|
+
private makeCollection: <TInput>(
|
|
15
|
+
iterable: AsyncIterableValue<TInput>,
|
|
16
|
+
) => IAsyncCollection<TInput>,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
|
|
20
|
+
const collection: IAsyncCollection<TInput> =
|
|
21
|
+
this.makeCollection<TInput>([]);
|
|
22
|
+
yield* await this.collection
|
|
23
|
+
.chunk(this.chunkSize)
|
|
24
|
+
.map(
|
|
25
|
+
async (item) =>
|
|
26
|
+
this.makeCollection([...(await item.toArray())].reverse()),
|
|
27
|
+
this.throwOnNumberLimit,
|
|
28
|
+
)
|
|
29
|
+
.reduce({
|
|
30
|
+
reduceFn: (collection, item) => collection.prepend(item),
|
|
31
|
+
initialValue: collection,
|
|
32
|
+
throwOnNumberLimit: this.throwOnNumberLimit,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-shuffle-iterable.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type IAsyncCollection } from "@/contracts/collection/_module";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export class AsyncShuffleIterable<TInput> implements AsyncIterable<TInput> {
|
|
7
|
+
constructor(private collection: IAsyncCollection<TInput>) {}
|
|
8
|
+
|
|
9
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
|
|
10
|
+
const newArray = [...(await this.collection.toArray())];
|
|
11
|
+
for (let i = newArray.length - 1; i > 0; i--) {
|
|
12
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
13
|
+
const temp = newArray[i];
|
|
14
|
+
if (newArray[j] !== undefined) {
|
|
15
|
+
newArray[i] = newArray[j];
|
|
16
|
+
}
|
|
17
|
+
if (temp !== undefined) {
|
|
18
|
+
newArray[j] = temp;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
yield* newArray;
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-skip-iterable.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type IAsyncCollection,
|
|
4
|
+
UnexpectedCollectionError,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export class AsyncSkipIterable<TInput> implements AsyncIterable<TInput> {
|
|
11
|
+
constructor(
|
|
12
|
+
private collection: IAsyncCollection<TInput>,
|
|
13
|
+
private limit: number,
|
|
14
|
+
private throwOnNumberLimit: boolean,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
|
|
18
|
+
try {
|
|
19
|
+
if (this.limit < 0) {
|
|
20
|
+
this.limit =
|
|
21
|
+
(await this.collection.size(this.throwOnNumberLimit)) +
|
|
22
|
+
this.limit;
|
|
23
|
+
}
|
|
24
|
+
yield* this.collection.skipWhile(
|
|
25
|
+
(_item, index) => index < this.limit,
|
|
26
|
+
this.throwOnNumberLimit,
|
|
27
|
+
);
|
|
28
|
+
} catch (error: unknown) {
|
|
29
|
+
if (
|
|
30
|
+
error instanceof CollectionError ||
|
|
31
|
+
error instanceof TypeError
|
|
32
|
+
) {
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
throw new UnexpectedCollectionError(
|
|
36
|
+
`Unexpected error "${String(error)}" occured`,
|
|
37
|
+
error,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-skip-until-iterable.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AsyncFilter,
|
|
3
|
+
CollectionError,
|
|
4
|
+
type IAsyncCollection,
|
|
5
|
+
IndexOverflowError,
|
|
6
|
+
UnexpectedCollectionError,
|
|
7
|
+
} from "@/contracts/collection/_module";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export class AsyncSkipUntilIterable<TInput> implements AsyncIterable<TInput> {
|
|
13
|
+
constructor(
|
|
14
|
+
private collection: IAsyncCollection<TInput>,
|
|
15
|
+
private filter: AsyncFilter<TInput, IAsyncCollection<TInput>>,
|
|
16
|
+
private throwOnNumberLimit: boolean,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
|
|
20
|
+
try {
|
|
21
|
+
let hasMatched = false,
|
|
22
|
+
index = 0;
|
|
23
|
+
for await (const item of this.collection) {
|
|
24
|
+
if (
|
|
25
|
+
this.throwOnNumberLimit &&
|
|
26
|
+
index === Number.MAX_SAFE_INTEGER
|
|
27
|
+
) {
|
|
28
|
+
throw new IndexOverflowError("Index has overflowed");
|
|
29
|
+
}
|
|
30
|
+
if (!hasMatched) {
|
|
31
|
+
hasMatched = await this.filter(
|
|
32
|
+
item,
|
|
33
|
+
index,
|
|
34
|
+
this.collection,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
if (hasMatched) {
|
|
38
|
+
yield item;
|
|
39
|
+
}
|
|
40
|
+
index++;
|
|
41
|
+
}
|
|
42
|
+
} catch (error: unknown) {
|
|
43
|
+
if (
|
|
44
|
+
error instanceof CollectionError ||
|
|
45
|
+
error instanceof TypeError
|
|
46
|
+
) {
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
throw new UnexpectedCollectionError(
|
|
50
|
+
`Unexpected error "${String(error)}" occured`,
|
|
51
|
+
error,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-slice-iterable.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type IAsyncCollection } from "@/contracts/collection/_module";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export class AsyncSliceIterable<TInput> implements AsyncIterable<TInput> {
|
|
7
|
+
constructor(
|
|
8
|
+
private collection: IAsyncCollection<TInput>,
|
|
9
|
+
private start: number | undefined,
|
|
10
|
+
private end: number | undefined,
|
|
11
|
+
private throwOnNumberLimit: boolean,
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
|
|
15
|
+
const size = await this.collection.size();
|
|
16
|
+
let { start, end } = this;
|
|
17
|
+
if (start === undefined) {
|
|
18
|
+
start = 0;
|
|
19
|
+
}
|
|
20
|
+
if (end === undefined) {
|
|
21
|
+
end = size;
|
|
22
|
+
}
|
|
23
|
+
if (start < 0) {
|
|
24
|
+
start = size + start;
|
|
25
|
+
}
|
|
26
|
+
if (end < 0) {
|
|
27
|
+
end = size + end;
|
|
28
|
+
}
|
|
29
|
+
yield* this.collection.filter((_item, index) => {
|
|
30
|
+
return start <= index && index < end;
|
|
31
|
+
}, this.throwOnNumberLimit);
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-sliding-iterable.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type IAsyncCollection,
|
|
4
|
+
UnexpectedCollectionError,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export class AsyncSlidingIteralbe<TInput>
|
|
11
|
+
implements AsyncIterable<IAsyncCollection<TInput>>
|
|
12
|
+
{
|
|
13
|
+
constructor(
|
|
14
|
+
private collection: IAsyncCollection<TInput>,
|
|
15
|
+
private chunkSize: number,
|
|
16
|
+
private step: number,
|
|
17
|
+
private throwOnNumberLimit: boolean,
|
|
18
|
+
) {}
|
|
19
|
+
|
|
20
|
+
async *[Symbol.asyncIterator](): AsyncIterator<IAsyncCollection<TInput>> {
|
|
21
|
+
try {
|
|
22
|
+
if (this.step <= 0) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const size = await this.collection.size(this.throwOnNumberLimit);
|
|
26
|
+
|
|
27
|
+
for (let index = 0; index < size; index += this.step) {
|
|
28
|
+
const start = index;
|
|
29
|
+
const end = index + this.chunkSize;
|
|
30
|
+
|
|
31
|
+
yield this.collection.slice({
|
|
32
|
+
start,
|
|
33
|
+
end,
|
|
34
|
+
throwOnNumberLimit: this.throwOnNumberLimit,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (end >= size) {
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
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
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-sort-iterable.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Comparator,
|
|
3
|
+
type IAsyncCollection,
|
|
4
|
+
} from "@/contracts/collection/_module";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export class AsyncSortIterable<TInput> implements AsyncIterable<TInput> {
|
|
10
|
+
constructor(
|
|
11
|
+
private collection: IAsyncCollection<TInput>,
|
|
12
|
+
private compare?: Comparator<TInput>,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
|
|
16
|
+
yield* [...(await this.collection.toArray())].sort(this.compare);
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-split-iterable.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
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 AsyncSplitIterable<TInput>
|
|
12
|
+
implements AsyncIterable<IAsyncCollection<TInput>>
|
|
13
|
+
{
|
|
14
|
+
constructor(
|
|
15
|
+
private collection: IAsyncCollection<TInput>,
|
|
16
|
+
private chunkAmount: number,
|
|
17
|
+
private throwOnNumberLimit: boolean,
|
|
18
|
+
private makeCollection: <TInput>(
|
|
19
|
+
iterable: AsyncIterableValue<TInput>,
|
|
20
|
+
) => IAsyncCollection<TInput>,
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
async *[Symbol.asyncIterator](): AsyncIterator<IAsyncCollection<TInput>> {
|
|
24
|
+
try {
|
|
25
|
+
const size = await this.collection.size(this.throwOnNumberLimit),
|
|
26
|
+
minChunkSize = Math.floor(size / this.chunkAmount),
|
|
27
|
+
restSize = size % this.chunkAmount,
|
|
28
|
+
chunkSizes = Array.from<number>({
|
|
29
|
+
length: this.chunkAmount,
|
|
30
|
+
}).fill(minChunkSize);
|
|
31
|
+
|
|
32
|
+
for (let i = 1; i <= restSize; i++) {
|
|
33
|
+
const chunkIndex = (i - 1) % this.chunkAmount;
|
|
34
|
+
if (chunkSizes[chunkIndex]) {
|
|
35
|
+
chunkSizes[chunkIndex] = chunkSizes[chunkIndex] + 1;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const iterator = this.collection.iterator();
|
|
40
|
+
for (const chunkSize of chunkSizes) {
|
|
41
|
+
let collection: IAsyncCollection<TInput> =
|
|
42
|
+
this.makeCollection<TInput>([]);
|
|
43
|
+
for (let i = 0; i < chunkSize; i++) {
|
|
44
|
+
const item = await iterator.next();
|
|
45
|
+
if (item.value !== undefined) {
|
|
46
|
+
collection = collection.append([item.value]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
yield collection;
|
|
50
|
+
}
|
|
51
|
+
} catch (error: unknown) {
|
|
52
|
+
if (
|
|
53
|
+
error instanceof CollectionError ||
|
|
54
|
+
error instanceof TypeError
|
|
55
|
+
) {
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
throw new UnexpectedCollectionError(
|
|
59
|
+
`Unexpected error "${String(error)}" occured`,
|
|
60
|
+
error,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-take-iterable.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type IAsyncCollection,
|
|
4
|
+
UnexpectedCollectionError,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export class AsyncTakeIterable<TInput> implements AsyncIterable<TInput> {
|
|
11
|
+
constructor(
|
|
12
|
+
private collection: IAsyncCollection<TInput>,
|
|
13
|
+
private limit: number,
|
|
14
|
+
private throwOnNumberLimit: boolean,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
|
|
18
|
+
try {
|
|
19
|
+
if (this.limit < 0) {
|
|
20
|
+
this.limit =
|
|
21
|
+
(await this.collection.size(this.throwOnNumberLimit)) +
|
|
22
|
+
this.limit;
|
|
23
|
+
}
|
|
24
|
+
yield* this.collection.takeWhile(
|
|
25
|
+
(_item, index) => index < this.limit,
|
|
26
|
+
this.throwOnNumberLimit,
|
|
27
|
+
);
|
|
28
|
+
} catch (error: unknown) {
|
|
29
|
+
if (
|
|
30
|
+
error instanceof CollectionError ||
|
|
31
|
+
error instanceof TypeError
|
|
32
|
+
) {
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
throw new UnexpectedCollectionError(
|
|
36
|
+
`Unexpected error "${String(error)}" occured`,
|
|
37
|
+
error,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/collection/async-iterable-collection/async-iterable-helpers/async-take-until-iterable.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
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 AsyncTakeUntilIterable<TInput> implements AsyncIterable<TInput> {
|
|
12
|
+
constructor(
|
|
13
|
+
private collection: IAsyncCollection<TInput>,
|
|
14
|
+
private filter: AsyncFilter<TInput, IAsyncCollection<TInput>>,
|
|
15
|
+
private throwOnNumberLimit: boolean,
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
async *[Symbol.asyncIterator](): AsyncIterator<TInput> {
|
|
19
|
+
try {
|
|
20
|
+
for await (const [index, item] of this.collection.entries(
|
|
21
|
+
this.throwOnNumberLimit,
|
|
22
|
+
)) {
|
|
23
|
+
if (await this.filter(item, index, this.collection)) {
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
yield item;
|
|
27
|
+
}
|
|
28
|
+
} catch (error: unknown) {
|
|
29
|
+
if (
|
|
30
|
+
error instanceof CollectionError ||
|
|
31
|
+
error instanceof TypeError
|
|
32
|
+
) {
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
throw new UnexpectedCollectionError(
|
|
36
|
+
`Unexpected error "${String(error)}" occured`,
|
|
37
|
+
error,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|