@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,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
UnexpectedCollectionError,
|
|
4
|
+
type ICollection,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
export class PadStartIterable<TInput, TExtended>
|
|
8
|
+
implements Iterable<TInput | TExtended>
|
|
9
|
+
{
|
|
10
|
+
constructor(
|
|
11
|
+
private collection: ICollection<TInput>,
|
|
12
|
+
private maxLength: number,
|
|
13
|
+
private fillItems: Iterable<TExtended>,
|
|
14
|
+
private makeCollection: <TInput>(
|
|
15
|
+
iterable: Iterable<TInput | TExtended>,
|
|
16
|
+
) => ICollection<TInput | TExtended>,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
*[Symbol.iterator](): Iterator<TInput | TExtended> {
|
|
20
|
+
try {
|
|
21
|
+
const fillCollections = this.makeCollection<TInput>(this.fillItems);
|
|
22
|
+
const fillSize = fillCollections.size();
|
|
23
|
+
const size = this.collection.size();
|
|
24
|
+
const repeat = Math.floor((this.maxLength - size) / fillSize);
|
|
25
|
+
let resultCollection: ICollection<TInput | TExtended> =
|
|
26
|
+
this.makeCollection<TInput>([]);
|
|
27
|
+
for (let index = 0; index < repeat; index++) {
|
|
28
|
+
resultCollection = resultCollection.append(fillCollections);
|
|
29
|
+
}
|
|
30
|
+
const restAmount = this.maxLength - (repeat * fillSize + size);
|
|
31
|
+
resultCollection = resultCollection.append(
|
|
32
|
+
fillCollections.slice({
|
|
33
|
+
start: 0,
|
|
34
|
+
end: restAmount,
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
37
|
+
resultCollection = resultCollection.append(this.collection);
|
|
38
|
+
yield* resultCollection;
|
|
39
|
+
} catch (error: unknown) {
|
|
40
|
+
if (
|
|
41
|
+
error instanceof CollectionError ||
|
|
42
|
+
error instanceof TypeError
|
|
43
|
+
) {
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
throw new UnexpectedCollectionError(
|
|
47
|
+
`Unexpected error "${String(error)}" occured`,
|
|
48
|
+
error,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type Filter,
|
|
4
|
+
type ICollection,
|
|
5
|
+
UnexpectedCollectionError,
|
|
6
|
+
} from "@/contracts/collection/_module";
|
|
7
|
+
|
|
8
|
+
export class PartionIterable<TInput> implements Iterable<ICollection<TInput>> {
|
|
9
|
+
constructor(
|
|
10
|
+
private collection: ICollection<TInput>,
|
|
11
|
+
private filter: Filter<TInput, ICollection<TInput>>,
|
|
12
|
+
private throwOnNumberLimit: boolean,
|
|
13
|
+
private makeCollection: <TInput>(
|
|
14
|
+
iterable: Iterable<TInput>,
|
|
15
|
+
) => ICollection<TInput>,
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
*[Symbol.iterator](): Iterator<ICollection<TInput>> {
|
|
19
|
+
try {
|
|
20
|
+
let chunkA: ICollection<TInput> = this.makeCollection<TInput>([]),
|
|
21
|
+
chunkB: ICollection<TInput> = this.makeCollection<TInput>([]);
|
|
22
|
+
for (const [index, item] of this.collection.entries(
|
|
23
|
+
this.throwOnNumberLimit,
|
|
24
|
+
)) {
|
|
25
|
+
if (this.filter(item, index, this.collection)) {
|
|
26
|
+
chunkA = chunkA.append([item]);
|
|
27
|
+
} else {
|
|
28
|
+
chunkB = chunkB.append([item]);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
yield chunkA;
|
|
32
|
+
yield chunkB;
|
|
33
|
+
} catch (error: unknown) {
|
|
34
|
+
if (
|
|
35
|
+
error instanceof CollectionError ||
|
|
36
|
+
error instanceof TypeError
|
|
37
|
+
) {
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
throw new UnexpectedCollectionError(
|
|
41
|
+
`Unexpected error "${String(error)}" occured`,
|
|
42
|
+
error,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
UnexpectedCollectionError,
|
|
4
|
+
type ICollection,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
export class RepeatIterable<TInput> implements Iterable<TInput> {
|
|
8
|
+
constructor(
|
|
9
|
+
private collection: ICollection<TInput>,
|
|
10
|
+
private amount: number,
|
|
11
|
+
private makeCollection: <TInput>(
|
|
12
|
+
iterable: Iterable<TInput>,
|
|
13
|
+
) => ICollection<TInput>,
|
|
14
|
+
) {}
|
|
15
|
+
|
|
16
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
17
|
+
try {
|
|
18
|
+
let collection = this.makeCollection<TInput>([]);
|
|
19
|
+
for (let index = 0; index < this.amount - 1; index++) {
|
|
20
|
+
collection = collection.append(this.collection);
|
|
21
|
+
}
|
|
22
|
+
yield* collection;
|
|
23
|
+
} catch (error: unknown) {
|
|
24
|
+
if (
|
|
25
|
+
error instanceof CollectionError ||
|
|
26
|
+
error instanceof TypeError
|
|
27
|
+
) {
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
throw new UnexpectedCollectionError(
|
|
31
|
+
`Unexpected error "${String(error)}" occured`,
|
|
32
|
+
error,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
UnexpectedCollectionError,
|
|
4
|
+
type ICollection,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
export class ReverseIterable<TInput> implements Iterable<TInput> {
|
|
8
|
+
constructor(
|
|
9
|
+
private collection: ICollection<TInput>,
|
|
10
|
+
private chunkSize: number,
|
|
11
|
+
private throwOnNumberLimit: boolean,
|
|
12
|
+
private makeCollection: <TInput>(
|
|
13
|
+
iterable: Iterable<TInput>,
|
|
14
|
+
) => ICollection<TInput>,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
18
|
+
try {
|
|
19
|
+
const collection: ICollection<TInput> = this.makeCollection<TInput>(
|
|
20
|
+
[],
|
|
21
|
+
);
|
|
22
|
+
yield* this.collection
|
|
23
|
+
.chunk(this.chunkSize)
|
|
24
|
+
.map(
|
|
25
|
+
(item) => this.makeCollection<TInput>([...item].reverse()),
|
|
26
|
+
this.throwOnNumberLimit,
|
|
27
|
+
)
|
|
28
|
+
.reduce({
|
|
29
|
+
reduceFn: (collection, item) => collection.prepend(item),
|
|
30
|
+
initialValue: collection,
|
|
31
|
+
throwOnNumberLimit: this.throwOnNumberLimit,
|
|
32
|
+
});
|
|
33
|
+
} catch (error: unknown) {
|
|
34
|
+
if (
|
|
35
|
+
error instanceof CollectionError ||
|
|
36
|
+
error instanceof TypeError
|
|
37
|
+
) {
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
throw new UnexpectedCollectionError(
|
|
41
|
+
`Unexpected error "${String(error)}" occured`,
|
|
42
|
+
error,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
UnexpectedCollectionError,
|
|
4
|
+
} from "@/contracts/collection/_shared";
|
|
5
|
+
|
|
6
|
+
export class ShuffleIterable<TInput> implements Iterable<TInput> {
|
|
7
|
+
constructor(private iterable: Iterable<TInput>) {}
|
|
8
|
+
|
|
9
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
10
|
+
try {
|
|
11
|
+
const newArray = [...this.iterable];
|
|
12
|
+
for (let i = newArray.length - 1; i > 0; i--) {
|
|
13
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
14
|
+
const temp = newArray[i];
|
|
15
|
+
if (newArray[j] !== undefined) {
|
|
16
|
+
newArray[i] = newArray[j];
|
|
17
|
+
}
|
|
18
|
+
if (temp !== undefined) {
|
|
19
|
+
newArray[j] = temp;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
yield* newArray;
|
|
23
|
+
} catch (error: unknown) {
|
|
24
|
+
if (
|
|
25
|
+
error instanceof CollectionError ||
|
|
26
|
+
error instanceof TypeError
|
|
27
|
+
) {
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
throw new UnexpectedCollectionError(
|
|
31
|
+
`Unexpected error "${String(error)}" occured`,
|
|
32
|
+
error,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type ICollection,
|
|
4
|
+
UnexpectedCollectionError,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
export class SkipIterable<TInput> implements Iterable<TInput> {
|
|
8
|
+
constructor(
|
|
9
|
+
private collection: ICollection<TInput>,
|
|
10
|
+
private offset: number,
|
|
11
|
+
private throwOnNumberLimit: boolean,
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
15
|
+
try {
|
|
16
|
+
if (this.offset < 0) {
|
|
17
|
+
this.offset =
|
|
18
|
+
this.collection.size(this.throwOnNumberLimit) + this.offset;
|
|
19
|
+
}
|
|
20
|
+
yield* this.collection.skipWhile(
|
|
21
|
+
(_item, index) => index < this.offset,
|
|
22
|
+
this.throwOnNumberLimit,
|
|
23
|
+
);
|
|
24
|
+
} catch (error: unknown) {
|
|
25
|
+
if (
|
|
26
|
+
error instanceof CollectionError ||
|
|
27
|
+
error instanceof TypeError
|
|
28
|
+
) {
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
throw new UnexpectedCollectionError(
|
|
32
|
+
`Unexpected error "${String(error)}" occured`,
|
|
33
|
+
error,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type Filter,
|
|
4
|
+
type ICollection,
|
|
5
|
+
UnexpectedCollectionError,
|
|
6
|
+
} from "@/contracts/collection/_module";
|
|
7
|
+
|
|
8
|
+
export class SkipUntilIterable<TInput> implements Iterable<TInput> {
|
|
9
|
+
constructor(
|
|
10
|
+
private collection: ICollection<TInput>,
|
|
11
|
+
private filter: Filter<TInput, ICollection<TInput>>,
|
|
12
|
+
private throwOnNumberLimit: boolean,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
16
|
+
try {
|
|
17
|
+
let hasMatched = false;
|
|
18
|
+
for (const [index, item] of this.collection.entries(
|
|
19
|
+
this.throwOnNumberLimit,
|
|
20
|
+
)) {
|
|
21
|
+
if (!hasMatched) {
|
|
22
|
+
hasMatched = this.filter(item, index, this.collection);
|
|
23
|
+
}
|
|
24
|
+
if (hasMatched) {
|
|
25
|
+
yield item;
|
|
26
|
+
}
|
|
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
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
UnexpectedCollectionError,
|
|
4
|
+
type ICollection,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
export class SliceIterable<TInput> implements Iterable<TInput> {
|
|
8
|
+
constructor(
|
|
9
|
+
private collection: ICollection<TInput>,
|
|
10
|
+
private start: number | undefined,
|
|
11
|
+
private end: number | undefined,
|
|
12
|
+
private throwOnNumberLimit: boolean,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
16
|
+
try {
|
|
17
|
+
const size = this.collection.size();
|
|
18
|
+
let { start, end } = this;
|
|
19
|
+
if (start === undefined) {
|
|
20
|
+
start = 0;
|
|
21
|
+
}
|
|
22
|
+
if (end === undefined) {
|
|
23
|
+
end = size;
|
|
24
|
+
}
|
|
25
|
+
if (start < 0) {
|
|
26
|
+
start = size + start;
|
|
27
|
+
}
|
|
28
|
+
if (end < 0) {
|
|
29
|
+
end = size + end;
|
|
30
|
+
}
|
|
31
|
+
yield* this.collection.filter((_item, index) => {
|
|
32
|
+
return start <= index && index < end;
|
|
33
|
+
}, this.throwOnNumberLimit);
|
|
34
|
+
} catch (error: unknown) {
|
|
35
|
+
if (
|
|
36
|
+
error instanceof CollectionError ||
|
|
37
|
+
error instanceof TypeError
|
|
38
|
+
) {
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
throw new UnexpectedCollectionError(
|
|
42
|
+
`Unexpected error "${String(error)}" occured`,
|
|
43
|
+
error,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type ICollection,
|
|
4
|
+
UnexpectedCollectionError,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
export class SlidingIteralbe<TInput> implements Iterable<ICollection<TInput>> {
|
|
8
|
+
constructor(
|
|
9
|
+
private collection: ICollection<TInput>,
|
|
10
|
+
private chunkSize: number,
|
|
11
|
+
private step: number,
|
|
12
|
+
private throwOnNumberLimit: boolean,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
*[Symbol.iterator](): Iterator<ICollection<TInput>> {
|
|
16
|
+
try {
|
|
17
|
+
if (this.step <= 0) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const size = this.collection.size(this.throwOnNumberLimit);
|
|
21
|
+
|
|
22
|
+
for (let index = 0; index < size; index += this.step) {
|
|
23
|
+
const start = index;
|
|
24
|
+
const end = index + this.chunkSize;
|
|
25
|
+
|
|
26
|
+
yield this.collection.slice({
|
|
27
|
+
start,
|
|
28
|
+
end,
|
|
29
|
+
throwOnNumberLimit: this.throwOnNumberLimit,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (end >= size) {
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
} catch (error: unknown) {
|
|
37
|
+
if (
|
|
38
|
+
error instanceof CollectionError ||
|
|
39
|
+
error instanceof TypeError
|
|
40
|
+
) {
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
throw new UnexpectedCollectionError(
|
|
44
|
+
`Unexpected error "${String(error)}" occured`,
|
|
45
|
+
error,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
UnexpectedCollectionError,
|
|
4
|
+
type Comparator,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
export class SortIterable<TInput> implements Iterable<TInput> {
|
|
8
|
+
constructor(
|
|
9
|
+
private iterable: Iterable<TInput>,
|
|
10
|
+
private compare?: Comparator<TInput>,
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
14
|
+
try {
|
|
15
|
+
yield* [...this.iterable].sort(this.compare);
|
|
16
|
+
} catch (error: unknown) {
|
|
17
|
+
if (
|
|
18
|
+
error instanceof CollectionError ||
|
|
19
|
+
error instanceof TypeError
|
|
20
|
+
) {
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
throw new UnexpectedCollectionError(
|
|
24
|
+
`Unexpected error "${String(error)}" occured`,
|
|
25
|
+
error,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type ICollection,
|
|
4
|
+
UnexpectedCollectionError,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
export class SplitIterable<TInput> implements Iterable<ICollection<TInput>> {
|
|
8
|
+
constructor(
|
|
9
|
+
private collection: ICollection<TInput>,
|
|
10
|
+
private chunkAmount: number,
|
|
11
|
+
private throwOnNumberLimit: boolean,
|
|
12
|
+
private makeCollection: <TInput>(
|
|
13
|
+
iterable: Iterable<TInput>,
|
|
14
|
+
) => ICollection<TInput>,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
*[Symbol.iterator](): Iterator<ICollection<TInput>> {
|
|
18
|
+
try {
|
|
19
|
+
const size = this.collection.size(this.throwOnNumberLimit),
|
|
20
|
+
minChunkSize = Math.floor(size / this.chunkAmount),
|
|
21
|
+
restSize = size % this.chunkAmount,
|
|
22
|
+
chunkSizes = Array.from<number>({
|
|
23
|
+
length: this.chunkAmount,
|
|
24
|
+
}).fill(minChunkSize);
|
|
25
|
+
|
|
26
|
+
for (let i = 1; i <= restSize; i++) {
|
|
27
|
+
const chunkIndex = (i - 1) % this.chunkAmount;
|
|
28
|
+
if (chunkSizes[chunkIndex]) {
|
|
29
|
+
chunkSizes[chunkIndex] = chunkSizes[chunkIndex] + 1;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const iterator = this.collection.iterator();
|
|
34
|
+
for (const chunkSize of chunkSizes) {
|
|
35
|
+
let collection: ICollection<TInput> =
|
|
36
|
+
this.makeCollection<TInput>([]);
|
|
37
|
+
for (let i = 0; i < chunkSize; i++) {
|
|
38
|
+
const item = iterator.next();
|
|
39
|
+
if (item.value !== undefined) {
|
|
40
|
+
collection = collection.append([item.value]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
yield collection;
|
|
44
|
+
}
|
|
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,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type ICollection,
|
|
4
|
+
UnexpectedCollectionError,
|
|
5
|
+
} from "@/contracts/collection/_module";
|
|
6
|
+
|
|
7
|
+
export class TakeIterable<TInput> implements Iterable<TInput> {
|
|
8
|
+
constructor(
|
|
9
|
+
private collection: ICollection<TInput>,
|
|
10
|
+
private limit: number,
|
|
11
|
+
private throwOnNumberLimit: boolean,
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
15
|
+
try {
|
|
16
|
+
if (this.limit < 0) {
|
|
17
|
+
this.limit =
|
|
18
|
+
this.collection.size(this.throwOnNumberLimit) + this.limit;
|
|
19
|
+
}
|
|
20
|
+
yield* this.collection.takeWhile(
|
|
21
|
+
(_item, index) => index < this.limit,
|
|
22
|
+
this.throwOnNumberLimit,
|
|
23
|
+
);
|
|
24
|
+
} catch (error: unknown) {
|
|
25
|
+
if (
|
|
26
|
+
error instanceof CollectionError ||
|
|
27
|
+
error instanceof TypeError
|
|
28
|
+
) {
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
throw new UnexpectedCollectionError(
|
|
32
|
+
`Unexpected error "${String(error)}" occured`,
|
|
33
|
+
error,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type Filter,
|
|
4
|
+
type ICollection,
|
|
5
|
+
UnexpectedCollectionError,
|
|
6
|
+
} from "@/contracts/collection/_module";
|
|
7
|
+
|
|
8
|
+
export class TakeUntilIterable<TInput> implements Iterable<TInput> {
|
|
9
|
+
constructor(
|
|
10
|
+
private collection: ICollection<TInput>,
|
|
11
|
+
private filter: Filter<TInput, ICollection<TInput>>,
|
|
12
|
+
private throwOnNumberLimit: boolean,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
16
|
+
try {
|
|
17
|
+
for (const [index, item] of this.collection.entries(
|
|
18
|
+
this.throwOnNumberLimit,
|
|
19
|
+
)) {
|
|
20
|
+
if (this.filter(item, index, this.collection)) {
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
yield item;
|
|
24
|
+
}
|
|
25
|
+
} catch (error: unknown) {
|
|
26
|
+
if (
|
|
27
|
+
error instanceof CollectionError ||
|
|
28
|
+
error instanceof TypeError
|
|
29
|
+
) {
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
throw new UnexpectedCollectionError(
|
|
33
|
+
`Unexpected error "${String(error)}" occured`,
|
|
34
|
+
error,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type ICollection,
|
|
4
|
+
type Tap,
|
|
5
|
+
UnexpectedCollectionError,
|
|
6
|
+
} from "@/contracts/collection/_module";
|
|
7
|
+
|
|
8
|
+
export class TapIterable<TInput> implements Iterable<TInput> {
|
|
9
|
+
constructor(
|
|
10
|
+
private collection: ICollection<TInput>,
|
|
11
|
+
private callback: Tap<ICollection<TInput>>,
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
15
|
+
try {
|
|
16
|
+
this.callback(this.collection);
|
|
17
|
+
yield* this.collection;
|
|
18
|
+
} catch (error: unknown) {
|
|
19
|
+
if (
|
|
20
|
+
error instanceof CollectionError ||
|
|
21
|
+
error instanceof TypeError
|
|
22
|
+
) {
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
throw new UnexpectedCollectionError(
|
|
26
|
+
`Unexpected error "${String(error)}" occured`,
|
|
27
|
+
error,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type ICollection,
|
|
4
|
+
type Map,
|
|
5
|
+
UnexpectedCollectionError,
|
|
6
|
+
} from "@/contracts/collection/_module";
|
|
7
|
+
|
|
8
|
+
export class UniqueIterable<TInput, TOutput> implements Iterable<TInput> {
|
|
9
|
+
constructor(
|
|
10
|
+
private collection: ICollection<TInput>,
|
|
11
|
+
private callback: Map<TInput, ICollection<TInput>, TOutput> = (item) =>
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return
|
|
13
|
+
item as any,
|
|
14
|
+
private throwOnNumberLimit: boolean,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
*[Symbol.iterator](): Iterator<TInput> {
|
|
18
|
+
try {
|
|
19
|
+
const set = new Set<TOutput>([]);
|
|
20
|
+
|
|
21
|
+
for (const [index, item] of this.collection.entries(
|
|
22
|
+
this.throwOnNumberLimit,
|
|
23
|
+
)) {
|
|
24
|
+
const item_ = this.callback(item, index, this.collection);
|
|
25
|
+
if (!set.has(item_)) {
|
|
26
|
+
yield item;
|
|
27
|
+
}
|
|
28
|
+
set.add(item_);
|
|
29
|
+
}
|
|
30
|
+
} catch (error: unknown) {
|
|
31
|
+
if (
|
|
32
|
+
error instanceof CollectionError ||
|
|
33
|
+
error instanceof TypeError
|
|
34
|
+
) {
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
throw new UnexpectedCollectionError(
|
|
38
|
+
`Unexpected error "${String(error)}" occured`,
|
|
39
|
+
error,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CollectionError,
|
|
3
|
+
type Filter,
|
|
4
|
+
type ICollection,
|
|
5
|
+
type Map,
|
|
6
|
+
UnexpectedCollectionError,
|
|
7
|
+
type UpdatedItem,
|
|
8
|
+
} from "@/contracts/collection/_module";
|
|
9
|
+
|
|
10
|
+
export class UpdateIterable<TInput, TFilterOutput extends TInput, TMapOutput>
|
|
11
|
+
implements Iterable<UpdatedItem<TInput, TFilterOutput, TMapOutput>>
|
|
12
|
+
{
|
|
13
|
+
constructor(
|
|
14
|
+
private collection: ICollection<TInput>,
|
|
15
|
+
private filter: Filter<TInput, ICollection<TInput>, TFilterOutput>,
|
|
16
|
+
private map: Map<TFilterOutput, ICollection<TInput>, TMapOutput>,
|
|
17
|
+
private throwOnNumberLimit: boolean,
|
|
18
|
+
) {}
|
|
19
|
+
|
|
20
|
+
*[Symbol.iterator](): Iterator<
|
|
21
|
+
UpdatedItem<TInput, TFilterOutput, TMapOutput>
|
|
22
|
+
> {
|
|
23
|
+
try {
|
|
24
|
+
for (const [index, item] of this.collection.entries(
|
|
25
|
+
this.throwOnNumberLimit,
|
|
26
|
+
)) {
|
|
27
|
+
if (this.filter(item, index, this.collection)) {
|
|
28
|
+
yield this.map(
|
|
29
|
+
item as TFilterOutput,
|
|
30
|
+
index,
|
|
31
|
+
this.collection,
|
|
32
|
+
);
|
|
33
|
+
} else {
|
|
34
|
+
yield item;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
} catch (error: unknown) {
|
|
38
|
+
if (
|
|
39
|
+
error instanceof CollectionError ||
|
|
40
|
+
error instanceof TypeError
|
|
41
|
+
) {
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
throw new UnexpectedCollectionError(
|
|
45
|
+
`Unexpected error "${String(error)}" occured`,
|
|
46
|
+
error,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|