@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,2280 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CollectionError,
|
|
5
|
+
ItemNotFoundError,
|
|
6
|
+
MultipleItemsFoundError,
|
|
7
|
+
type RecordItem,
|
|
8
|
+
} from "@/contracts/collection/_module";
|
|
9
|
+
import { ListCollection } from "@/collection/list-collection/_module";
|
|
10
|
+
|
|
11
|
+
describe("class: ListCollection", () => {
|
|
12
|
+
describe("method: filter", () => {
|
|
13
|
+
test(`Should filter out all "a" of ["a", "bc", "c", "a", "d", "a"]`, () => {
|
|
14
|
+
const arr = ["a", "bc", "c", "a", "d", "a"],
|
|
15
|
+
collection = new ListCollection(arr),
|
|
16
|
+
filterFunction = (item: string): boolean => item === "a",
|
|
17
|
+
newCollection = collection.filter(filterFunction);
|
|
18
|
+
expect(newCollection.toArray()).toEqual(arr.filter(filterFunction));
|
|
19
|
+
});
|
|
20
|
+
test("Should input correct indexes to filter function", () => {
|
|
21
|
+
const collection = new ListCollection([
|
|
22
|
+
"a",
|
|
23
|
+
"bc",
|
|
24
|
+
"c",
|
|
25
|
+
"a",
|
|
26
|
+
"d",
|
|
27
|
+
"a",
|
|
28
|
+
]),
|
|
29
|
+
indexes: number[] = [],
|
|
30
|
+
filterFunction = (item: string, index: number): boolean => {
|
|
31
|
+
indexes.push(index);
|
|
32
|
+
return item === "a";
|
|
33
|
+
};
|
|
34
|
+
collection.filter(filterFunction).toArray();
|
|
35
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4, 5]);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
describe("method: map", () => {
|
|
39
|
+
test("Should apply power by 2 for all items", () => {
|
|
40
|
+
const arr = [2, 3, 4, 5],
|
|
41
|
+
collection = new ListCollection(arr),
|
|
42
|
+
mapFunction = (item: number): number => item ** 2,
|
|
43
|
+
newCollection = collection.map(mapFunction);
|
|
44
|
+
expect(newCollection.toArray()).toEqual(arr.map(mapFunction));
|
|
45
|
+
});
|
|
46
|
+
test("Should input correct indexes to map function", () => {
|
|
47
|
+
const collection = new ListCollection([2, 3, 4, 5]),
|
|
48
|
+
indexes: number[] = [],
|
|
49
|
+
mapFunction = (item: number, index: number): number => {
|
|
50
|
+
indexes.push(index);
|
|
51
|
+
return item ** 2;
|
|
52
|
+
};
|
|
53
|
+
collection.map(mapFunction).toArray();
|
|
54
|
+
expect(indexes).toEqual([0, 1, 2, 3]);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe("method: reduce", () => {
|
|
58
|
+
test("Should join all string items without initial values", () => {
|
|
59
|
+
const arr = ["a", "b", "c", "d"],
|
|
60
|
+
collection = new ListCollection(arr),
|
|
61
|
+
seperator = "_#_",
|
|
62
|
+
result = collection.reduce({
|
|
63
|
+
reduceFn(firstItem, item) {
|
|
64
|
+
return firstItem + seperator + item;
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
expect(result).toBe(arr.join(seperator));
|
|
68
|
+
});
|
|
69
|
+
test(`Should join all string items initial value "_#_"`, () => {
|
|
70
|
+
const arr = ["a", "b", "c", "d"],
|
|
71
|
+
collection = new ListCollection(arr),
|
|
72
|
+
initialValue = "!",
|
|
73
|
+
result = collection.reduce({
|
|
74
|
+
reduceFn(initialValue, item) {
|
|
75
|
+
return initialValue + item;
|
|
76
|
+
},
|
|
77
|
+
initialValue,
|
|
78
|
+
});
|
|
79
|
+
expect(result).toBe(initialValue + arr.join(""));
|
|
80
|
+
});
|
|
81
|
+
test("Should input correct indexes to reduce function", () => {
|
|
82
|
+
const arr = ["a", "b", "c", "d"],
|
|
83
|
+
collection = new ListCollection(arr),
|
|
84
|
+
initialValue = "!",
|
|
85
|
+
indexes: number[] = [];
|
|
86
|
+
collection.reduce({
|
|
87
|
+
reduceFn(initialValue, item, index) {
|
|
88
|
+
indexes.push(index);
|
|
89
|
+
return initialValue + item;
|
|
90
|
+
},
|
|
91
|
+
initialValue,
|
|
92
|
+
});
|
|
93
|
+
expect(indexes).toEqual([0, 1, 2, 3]);
|
|
94
|
+
});
|
|
95
|
+
test("Should throw TypeError when given an empty array without initial value", () => {
|
|
96
|
+
const collection = new ListCollection<string>([]);
|
|
97
|
+
expect(() => {
|
|
98
|
+
collection.reduce({
|
|
99
|
+
reduceFn: (a, b) => a + b,
|
|
100
|
+
});
|
|
101
|
+
}).toThrowError(TypeError);
|
|
102
|
+
});
|
|
103
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
104
|
+
const arr = ["a", "b", "c", "d"],
|
|
105
|
+
collection = new ListCollection(arr),
|
|
106
|
+
seperator = "_#_";
|
|
107
|
+
expect(
|
|
108
|
+
collection.reduce({
|
|
109
|
+
reduceFn(firstItem, item) {
|
|
110
|
+
return firstItem + seperator + item;
|
|
111
|
+
},
|
|
112
|
+
}),
|
|
113
|
+
).toBe(arr.join(seperator));
|
|
114
|
+
expect(
|
|
115
|
+
collection.reduce({
|
|
116
|
+
reduceFn(firstItem, item) {
|
|
117
|
+
return firstItem + seperator + item;
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
).toBe(arr.join(seperator));
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
describe("method: join", () => {
|
|
124
|
+
test(`Should join iterable of ["a", "b", "c"] to "a,b,c"`, () => {
|
|
125
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
126
|
+
expect(collection.join()).toBe("a,b,c");
|
|
127
|
+
});
|
|
128
|
+
test(`Should join iterable of ["a", "b", "c"] to "a,b,c" with seperator "_#_"`, () => {
|
|
129
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
130
|
+
expect(
|
|
131
|
+
collection.join({
|
|
132
|
+
seperator: "_#_",
|
|
133
|
+
}),
|
|
134
|
+
).toBe("a_#_b_#_c");
|
|
135
|
+
});
|
|
136
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
137
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
138
|
+
expect(collection.join()).toBe("a,b,c");
|
|
139
|
+
expect(collection.join()).toBe("a,b,c");
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
describe("method: flatMap", () => {
|
|
143
|
+
test("Should apply flatmap", () => {
|
|
144
|
+
const collection = new ListCollection(["a", "ab", "b", "ba"]),
|
|
145
|
+
newCollection = collection.flatMap((item, index) => [
|
|
146
|
+
index,
|
|
147
|
+
item,
|
|
148
|
+
item.length,
|
|
149
|
+
]);
|
|
150
|
+
expect(newCollection.toArray()).toEqual([
|
|
151
|
+
0,
|
|
152
|
+
"a",
|
|
153
|
+
1,
|
|
154
|
+
1,
|
|
155
|
+
"ab",
|
|
156
|
+
2,
|
|
157
|
+
2,
|
|
158
|
+
"b",
|
|
159
|
+
1,
|
|
160
|
+
3,
|
|
161
|
+
"ba",
|
|
162
|
+
2,
|
|
163
|
+
]);
|
|
164
|
+
});
|
|
165
|
+
test("Should input correct indexes to map function", () => {
|
|
166
|
+
const collection = new ListCollection(["a", "ab", "b", "ba"]),
|
|
167
|
+
indexes: number[] = [],
|
|
168
|
+
mapFunction = (
|
|
169
|
+
item: string,
|
|
170
|
+
index: number,
|
|
171
|
+
): [number, string, number] => {
|
|
172
|
+
indexes.push(index);
|
|
173
|
+
return [index, item, item.length];
|
|
174
|
+
};
|
|
175
|
+
collection.flatMap(mapFunction).toArray();
|
|
176
|
+
expect(indexes).toEqual([0, 1, 2, 3]);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
describe("method: update", () => {
|
|
180
|
+
test("Should change all the items that match the filter function", () => {
|
|
181
|
+
const collection = new ListCollection([
|
|
182
|
+
"a",
|
|
183
|
+
"aa",
|
|
184
|
+
"b",
|
|
185
|
+
"bbb",
|
|
186
|
+
"c",
|
|
187
|
+
"cccc",
|
|
188
|
+
]),
|
|
189
|
+
newCollection = collection.update(
|
|
190
|
+
(item) => item.length >= 2,
|
|
191
|
+
(item) => item.slice(0, -1),
|
|
192
|
+
);
|
|
193
|
+
expect(newCollection.toArray()).toEqual([
|
|
194
|
+
"a",
|
|
195
|
+
"a",
|
|
196
|
+
"b",
|
|
197
|
+
"bb",
|
|
198
|
+
"c",
|
|
199
|
+
"ccc",
|
|
200
|
+
]);
|
|
201
|
+
});
|
|
202
|
+
test("Should input correct indexes to filter function", () => {
|
|
203
|
+
const collection = new ListCollection([
|
|
204
|
+
"a",
|
|
205
|
+
"aa",
|
|
206
|
+
"b",
|
|
207
|
+
"bbb",
|
|
208
|
+
"c",
|
|
209
|
+
"cccc",
|
|
210
|
+
]),
|
|
211
|
+
indexes: number[] = [];
|
|
212
|
+
collection
|
|
213
|
+
.update(
|
|
214
|
+
(item, index) => {
|
|
215
|
+
indexes.push(index);
|
|
216
|
+
return item.length >= 2;
|
|
217
|
+
},
|
|
218
|
+
(item) => item.slice(0, -1),
|
|
219
|
+
)
|
|
220
|
+
.toArray();
|
|
221
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4, 5]);
|
|
222
|
+
});
|
|
223
|
+
test("Should input correct indexes to map function", () => {
|
|
224
|
+
const collection = new ListCollection([
|
|
225
|
+
"a",
|
|
226
|
+
"aa",
|
|
227
|
+
"b",
|
|
228
|
+
"bbb",
|
|
229
|
+
"c",
|
|
230
|
+
"cccc",
|
|
231
|
+
]),
|
|
232
|
+
indexes: number[] = [];
|
|
233
|
+
collection
|
|
234
|
+
.update(
|
|
235
|
+
(item) => item.length >= 2,
|
|
236
|
+
(item, index) => {
|
|
237
|
+
indexes.push(index);
|
|
238
|
+
return item.slice(0, -1);
|
|
239
|
+
},
|
|
240
|
+
)
|
|
241
|
+
.toArray();
|
|
242
|
+
expect(indexes).toEqual([1, 3, 5]);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
describe("method: page", () => {
|
|
246
|
+
test("Should return the first 4 items when page is 1 and pageSize 4", () => {
|
|
247
|
+
const arr = ["a", "b", "c", "d", "e", "f", "g", "h"],
|
|
248
|
+
collection = new ListCollection(arr),
|
|
249
|
+
newCollection = collection.page({
|
|
250
|
+
page: 1,
|
|
251
|
+
pageSize: 4,
|
|
252
|
+
});
|
|
253
|
+
expect(newCollection.toArray()).toEqual(arr.slice(0, 4));
|
|
254
|
+
});
|
|
255
|
+
test("Should return the last 4 items when page is 2 and pageSize 4", () => {
|
|
256
|
+
const arr = ["a", "b", "c", "d", "e", "f", "g", "h"],
|
|
257
|
+
collection = new ListCollection(arr),
|
|
258
|
+
newCollection = collection.page({
|
|
259
|
+
page: 2,
|
|
260
|
+
pageSize: 4,
|
|
261
|
+
});
|
|
262
|
+
expect(newCollection.toArray()).toEqual(arr.slice(-4));
|
|
263
|
+
});
|
|
264
|
+
test("Should return the last 4 items when page is -1 and pageSize 4", () => {
|
|
265
|
+
const arr = ["a", "b", "c", "d", "e", "f", "g", "h"],
|
|
266
|
+
collection = new ListCollection(arr),
|
|
267
|
+
newCollection = collection.page({
|
|
268
|
+
page: -1,
|
|
269
|
+
pageSize: 4,
|
|
270
|
+
});
|
|
271
|
+
expect(newCollection.toArray()).toEqual(arr.slice(-4));
|
|
272
|
+
});
|
|
273
|
+
test("Should return the first 2 items when page is 1 and pageSize 2", () => {
|
|
274
|
+
const arr = ["a", "b", "c", "d", "e", "f", "g", "h"],
|
|
275
|
+
collection = new ListCollection(arr),
|
|
276
|
+
newCollection = collection.page({
|
|
277
|
+
page: 1,
|
|
278
|
+
pageSize: 2,
|
|
279
|
+
});
|
|
280
|
+
expect(newCollection.toArray()).toEqual(arr.slice(0, 2));
|
|
281
|
+
});
|
|
282
|
+
test("Should return the last 2 items when page is 4 and pageSize 2", () => {
|
|
283
|
+
const arr = ["a", "b", "c", "d", "e", "f", "g", "h"],
|
|
284
|
+
collection = new ListCollection(arr),
|
|
285
|
+
newCollection = collection.page({
|
|
286
|
+
page: 4,
|
|
287
|
+
pageSize: 2,
|
|
288
|
+
});
|
|
289
|
+
expect(newCollection.toArray()).toEqual(arr.slice(-2));
|
|
290
|
+
});
|
|
291
|
+
test("Should return the last 2 items when page is -1 and pageSize 2", () => {
|
|
292
|
+
const arr = ["a", "b", "c", "d", "e", "f", "g", "h"],
|
|
293
|
+
collection = new ListCollection(arr),
|
|
294
|
+
newCollection = collection.page({
|
|
295
|
+
page: -1,
|
|
296
|
+
pageSize: 2,
|
|
297
|
+
});
|
|
298
|
+
expect(newCollection.toArray()).toEqual(arr.slice(-2));
|
|
299
|
+
});
|
|
300
|
+
test("Should return the last 2 items when page is -4 and pageSize -2", () => {
|
|
301
|
+
const arr = ["a", "b", "c", "d", "e", "f", "g", "h"],
|
|
302
|
+
collection = new ListCollection(arr),
|
|
303
|
+
newCollection = collection.page({
|
|
304
|
+
page: -2,
|
|
305
|
+
pageSize: 2,
|
|
306
|
+
});
|
|
307
|
+
expect(newCollection.toArray()).toEqual(arr.slice(-4, -2));
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
describe("method: sum", () => {
|
|
311
|
+
test("Should calculate sum iterable of [1, 2, 3, 4] to 10", () => {
|
|
312
|
+
const collection = new ListCollection([1, 2, 3, 4]);
|
|
313
|
+
expect(collection.sum()).toBe(10);
|
|
314
|
+
});
|
|
315
|
+
test("Should throw TypeError when containg a none number item", () => {
|
|
316
|
+
const collection = new ListCollection([1, 2, 3, 4, "a"]);
|
|
317
|
+
expect(() => {
|
|
318
|
+
collection.sum();
|
|
319
|
+
}).toThrowError(TypeError);
|
|
320
|
+
});
|
|
321
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
322
|
+
const collection = new ListCollection([1, 2, 3, 4]);
|
|
323
|
+
expect(collection.sum()).toBe(10);
|
|
324
|
+
expect(collection.sum()).toBe(10);
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
describe("method: average", () => {
|
|
328
|
+
test("Should calculate average iterable of [1, 2, 3, 4] to 2.5", () => {
|
|
329
|
+
const collection = new ListCollection([1, 2, 3, 4]);
|
|
330
|
+
expect(collection.average()).toBe(2.5);
|
|
331
|
+
});
|
|
332
|
+
test("Should throw TypeError when containg a none number item", () => {
|
|
333
|
+
const collection = new ListCollection([1, 2, 3, 4, "a"]);
|
|
334
|
+
expect(() => {
|
|
335
|
+
collection.average();
|
|
336
|
+
}).toThrowError(TypeError);
|
|
337
|
+
});
|
|
338
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
339
|
+
const collection = new ListCollection([1, 2, 3, 4]);
|
|
340
|
+
expect(collection.average()).toBe(2.5);
|
|
341
|
+
expect(collection.average()).toBe(2.5);
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
describe("method: median", () => {
|
|
345
|
+
test("Should calculate median iterable of [1, 2, 3, 4, 5] to 3", () => {
|
|
346
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
347
|
+
expect(collection.median()).toBe(3);
|
|
348
|
+
});
|
|
349
|
+
test("Should calculate median iterable of [1, 2, 4, 5] to 3", () => {
|
|
350
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
351
|
+
expect(collection.median()).toBe(3);
|
|
352
|
+
});
|
|
353
|
+
test("Should throw TypeError when containg a none number item", () => {
|
|
354
|
+
const collection = new ListCollection([1, 2, 3, 4, "a"]);
|
|
355
|
+
expect(() => {
|
|
356
|
+
collection.median();
|
|
357
|
+
}).toThrowError(TypeError);
|
|
358
|
+
});
|
|
359
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
360
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
361
|
+
expect(collection.median()).toBe(3);
|
|
362
|
+
expect(collection.median()).toBe(3);
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
describe("method: min", () => {
|
|
366
|
+
test("Should return the smallest number", () => {
|
|
367
|
+
const collection = new ListCollection([2, 1, 3, -2, 4]);
|
|
368
|
+
expect(collection.min()).toBe(-2);
|
|
369
|
+
});
|
|
370
|
+
test("Should throw TypeError when containg a none number item", () => {
|
|
371
|
+
const collection = new ListCollection([2, 1, 3, -2, 4, "-4"]);
|
|
372
|
+
expect(() => {
|
|
373
|
+
collection.min();
|
|
374
|
+
}).toThrowError(TypeError);
|
|
375
|
+
});
|
|
376
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
377
|
+
const collection = new ListCollection([2, 1, 3, -2, 4]);
|
|
378
|
+
expect(collection.min()).toBe(-2);
|
|
379
|
+
expect(collection.min()).toBe(-2);
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
describe("method: max", () => {
|
|
383
|
+
test("Should return the largest number", () => {
|
|
384
|
+
const collection = new ListCollection([2, 1, 3, -2, 4]);
|
|
385
|
+
expect(collection.max()).toBe(4);
|
|
386
|
+
});
|
|
387
|
+
test("Should throw TypeError when containg a none number item", () => {
|
|
388
|
+
const collection = new ListCollection([2, 1, 3, -2, 4, "-4"]);
|
|
389
|
+
expect(() => {
|
|
390
|
+
collection.max();
|
|
391
|
+
}).toThrowError(TypeError);
|
|
392
|
+
});
|
|
393
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
394
|
+
const collection = new ListCollection([2, 1, 3, -2, 4]);
|
|
395
|
+
expect(collection.max()).toBe(4);
|
|
396
|
+
expect(collection.max()).toBe(4);
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
describe("method: percentage", () => {
|
|
400
|
+
test(`Should return 50 when filtering "a" of ["a", "b", "a", "c"]`, () => {
|
|
401
|
+
const collection = new ListCollection(["a", "b", "a", "b"]);
|
|
402
|
+
expect(collection.percentage((item) => item === "a")).toBe(50);
|
|
403
|
+
});
|
|
404
|
+
test("Should input correct indexes to filter function", () => {
|
|
405
|
+
const collection = new ListCollection([
|
|
406
|
+
"a",
|
|
407
|
+
"bc",
|
|
408
|
+
"c",
|
|
409
|
+
"a",
|
|
410
|
+
"d",
|
|
411
|
+
"a",
|
|
412
|
+
]),
|
|
413
|
+
indexes: number[] = [],
|
|
414
|
+
filterFunction = (item: string, index: number): boolean => {
|
|
415
|
+
indexes.push(index);
|
|
416
|
+
return item === "a";
|
|
417
|
+
};
|
|
418
|
+
collection.percentage(filterFunction);
|
|
419
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4, 5]);
|
|
420
|
+
});
|
|
421
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
422
|
+
const collection = new ListCollection(["a", "b", "a", "b"]);
|
|
423
|
+
expect(collection.percentage((item) => item === "a")).toBe(50);
|
|
424
|
+
expect(collection.percentage((item) => item === "a")).toBe(50);
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
describe("method: some", () => {
|
|
428
|
+
test("Should return true when at least 1 item match the filter function", () => {
|
|
429
|
+
const collection = new ListCollection(["a", "b", "c", "c", "a"]);
|
|
430
|
+
expect(collection.some((item) => item === "b")).toBe(true);
|
|
431
|
+
});
|
|
432
|
+
test("Should return false when all of the items does not match the filter function", () => {
|
|
433
|
+
const collection = new ListCollection(["a", "b", "c", "c", "a"]);
|
|
434
|
+
expect(collection.some((item) => item === "d")).toBe(false);
|
|
435
|
+
});
|
|
436
|
+
test("Should input correct indexes to filter function", () => {
|
|
437
|
+
const collection = new ListCollection([
|
|
438
|
+
"a",
|
|
439
|
+
"bc",
|
|
440
|
+
"c",
|
|
441
|
+
"a",
|
|
442
|
+
"d",
|
|
443
|
+
"a",
|
|
444
|
+
]),
|
|
445
|
+
indexes: number[] = [],
|
|
446
|
+
filterFunction = (item: string, index: number): boolean => {
|
|
447
|
+
indexes.push(index);
|
|
448
|
+
return item === "a";
|
|
449
|
+
};
|
|
450
|
+
collection.every(filterFunction);
|
|
451
|
+
expect(indexes).toEqual([0, 1]);
|
|
452
|
+
});
|
|
453
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
454
|
+
const collection = new ListCollection(["a", "b", "c", "c", "a"]);
|
|
455
|
+
expect(collection.some((item) => item === "b")).toBe(true);
|
|
456
|
+
expect(collection.some((item) => item === "b")).toBe(true);
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
describe("method: every", () => {
|
|
460
|
+
test("Should return true when all items match the filter function", () => {
|
|
461
|
+
const collection = new ListCollection(["a", "b", "c", "c", "a"]);
|
|
462
|
+
expect(collection.every((item) => item.length === 1)).toBe(true);
|
|
463
|
+
});
|
|
464
|
+
test("Should return false when one item does not match the filter function", () => {
|
|
465
|
+
const collection = new ListCollection(["a", "b", "c", "c", "aa"]);
|
|
466
|
+
expect(collection.every((item) => item.length === 1)).toBe(false);
|
|
467
|
+
});
|
|
468
|
+
test("Should input correct indexes to filter function", () => {
|
|
469
|
+
const collection = new ListCollection(["a", "b", "c", "c", "aa"]),
|
|
470
|
+
indexes: number[] = [];
|
|
471
|
+
collection.every((item, index) => {
|
|
472
|
+
indexes.push(index);
|
|
473
|
+
return item.length === 1;
|
|
474
|
+
});
|
|
475
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4]);
|
|
476
|
+
});
|
|
477
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
478
|
+
const collection = new ListCollection(["a", "b", "c", "c", "a"]);
|
|
479
|
+
expect(collection.every((item) => item.length === 1)).toBe(true);
|
|
480
|
+
expect(collection.every((item) => item.length === 1)).toBe(true);
|
|
481
|
+
});
|
|
482
|
+
});
|
|
483
|
+
describe("method: take", () => {
|
|
484
|
+
test("Should take first item when input is 1", () => {
|
|
485
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
|
486
|
+
collection = new ListCollection(arr),
|
|
487
|
+
newCollection = collection.take(1);
|
|
488
|
+
expect(newCollection.toArray()).toEqual(arr.slice(0, 1));
|
|
489
|
+
});
|
|
490
|
+
test("Should take 5 first items when input is 5", () => {
|
|
491
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
|
492
|
+
collection = new ListCollection(arr),
|
|
493
|
+
newCollection = collection.take(5);
|
|
494
|
+
expect(newCollection.toArray()).toEqual(arr.slice(0, 5));
|
|
495
|
+
});
|
|
496
|
+
test("Should take 8 first items when input is -2", () => {
|
|
497
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
|
498
|
+
collection = new ListCollection(arr),
|
|
499
|
+
newCollection = collection.take(-2);
|
|
500
|
+
expect(newCollection.toArray()).toEqual(arr.slice(0, -2));
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
describe("method: takeUntil", () => {
|
|
504
|
+
test("Should take all items until item is larger or equal to 3", () => {
|
|
505
|
+
const collection = new ListCollection([1, 2, 3, 4]),
|
|
506
|
+
newCollection = collection.takeUntil((item) => item >= 3);
|
|
507
|
+
expect(newCollection.toArray()).toEqual([1, 2]);
|
|
508
|
+
});
|
|
509
|
+
test("Should input correct indexes to filter function", () => {
|
|
510
|
+
const collection = new ListCollection([1, 2, 3, 4]),
|
|
511
|
+
indexes: number[] = [];
|
|
512
|
+
collection
|
|
513
|
+
.takeUntil((item, index) => {
|
|
514
|
+
indexes.push(index);
|
|
515
|
+
return item >= 3;
|
|
516
|
+
})
|
|
517
|
+
.toArray();
|
|
518
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
describe("method: takeWhile", () => {
|
|
522
|
+
test("Should take all items while item is less than 3", () => {
|
|
523
|
+
const collection = new ListCollection([1, 2, 3, 4]),
|
|
524
|
+
newCollection = collection.takeWhile((item) => item < 3);
|
|
525
|
+
expect(newCollection.toArray()).toEqual([1, 2]);
|
|
526
|
+
});
|
|
527
|
+
test("Should input correct indexes to filter function", () => {
|
|
528
|
+
const collection = new ListCollection([1, 2, 3, 4]),
|
|
529
|
+
indexes: number[] = [];
|
|
530
|
+
collection
|
|
531
|
+
.takeWhile((item, index) => {
|
|
532
|
+
indexes.push(index);
|
|
533
|
+
return item < 3;
|
|
534
|
+
})
|
|
535
|
+
.toArray();
|
|
536
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
describe("method: skip", () => {
|
|
540
|
+
test("Should skip first item when input is 1", () => {
|
|
541
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
|
542
|
+
collection = new ListCollection(arr),
|
|
543
|
+
newCollection = collection.skip(1);
|
|
544
|
+
expect(newCollection.toArray()).toEqual(arr.slice(1));
|
|
545
|
+
});
|
|
546
|
+
test("Should skip 5 first items when input is 5", () => {
|
|
547
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
|
548
|
+
collection = new ListCollection(arr),
|
|
549
|
+
newCollection = collection.skip(5);
|
|
550
|
+
expect(newCollection.toArray()).toEqual(arr.slice(5));
|
|
551
|
+
});
|
|
552
|
+
test("Should skip 8 first items when input is -2", () => {
|
|
553
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
|
554
|
+
collection = new ListCollection(arr),
|
|
555
|
+
newCollection = collection.skip(-2);
|
|
556
|
+
expect(newCollection.toArray()).toEqual(arr.slice(-2));
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
describe("method: skipUntil", () => {
|
|
560
|
+
test("Should skip all items until item is larger or equal to 3", () => {
|
|
561
|
+
const collection = new ListCollection([1, 2, 3, 4]),
|
|
562
|
+
newCollection = collection.skipUntil((item) => item >= 3);
|
|
563
|
+
expect(newCollection.toArray()).toEqual([3, 4]);
|
|
564
|
+
});
|
|
565
|
+
test("Should input correct indexes to filter function", () => {
|
|
566
|
+
const collection = new ListCollection([1, 2, 3, 4]),
|
|
567
|
+
indexes: number[] = [];
|
|
568
|
+
collection
|
|
569
|
+
.skipUntil((item, index) => {
|
|
570
|
+
indexes.push(index);
|
|
571
|
+
return item >= 3;
|
|
572
|
+
})
|
|
573
|
+
.toArray();
|
|
574
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
575
|
+
});
|
|
576
|
+
});
|
|
577
|
+
describe("method: skipWhile", () => {
|
|
578
|
+
test("Should skipp all items while item is less than 3", () => {
|
|
579
|
+
const collection = new ListCollection([1, 2, 3, 4]),
|
|
580
|
+
newCollection = collection.skipWhile((item) => item <= 3);
|
|
581
|
+
expect(newCollection.toArray()).toEqual([4]);
|
|
582
|
+
});
|
|
583
|
+
test("Should input correct indexes to filter function", () => {
|
|
584
|
+
const collection = new ListCollection([1, 2, 3, 4]),
|
|
585
|
+
indexes: number[] = [];
|
|
586
|
+
collection
|
|
587
|
+
.skipWhile((item, index) => {
|
|
588
|
+
indexes.push(index);
|
|
589
|
+
return item <= 3;
|
|
590
|
+
})
|
|
591
|
+
.toArray();
|
|
592
|
+
expect(indexes).toEqual([0, 1, 2, 3]);
|
|
593
|
+
});
|
|
594
|
+
});
|
|
595
|
+
describe("method: when", () => {
|
|
596
|
+
test("Should append items when statement is true", () => {
|
|
597
|
+
const arr1 = ["a", "b", "c"],
|
|
598
|
+
collection = new ListCollection(arr1),
|
|
599
|
+
arr2 = [1, 2, 3],
|
|
600
|
+
newCollection = collection.when(true, (collection) =>
|
|
601
|
+
collection.append(arr2),
|
|
602
|
+
);
|
|
603
|
+
expect(newCollection.toArray()).toEqual([...arr1, ...arr2]);
|
|
604
|
+
});
|
|
605
|
+
test("Should not append items when statement is false", () => {
|
|
606
|
+
const arr1 = ["a", "b", "c"],
|
|
607
|
+
collection = new ListCollection(arr1),
|
|
608
|
+
arr2 = [1, 2, 3],
|
|
609
|
+
newCollection = collection.when(false, (collection) =>
|
|
610
|
+
collection.append(arr2),
|
|
611
|
+
);
|
|
612
|
+
expect(newCollection.toArray()).toEqual(arr1);
|
|
613
|
+
});
|
|
614
|
+
});
|
|
615
|
+
describe("method: whenEmpty", () => {
|
|
616
|
+
test("Should append items when empty", () => {
|
|
617
|
+
const collection = new ListCollection<string>([]),
|
|
618
|
+
arr2 = [1, 2, 3],
|
|
619
|
+
newCollection = collection.whenEmpty((collection) =>
|
|
620
|
+
collection.append(arr2),
|
|
621
|
+
);
|
|
622
|
+
expect(newCollection.toArray()).toEqual(arr2);
|
|
623
|
+
});
|
|
624
|
+
test("Should not append items when not empty", () => {
|
|
625
|
+
const arr1 = ["a", "b", "c"],
|
|
626
|
+
collection = new ListCollection(arr1),
|
|
627
|
+
arr2 = [1, 2, 3],
|
|
628
|
+
newCollection = collection.whenEmpty((collection) =>
|
|
629
|
+
collection.append(arr2),
|
|
630
|
+
);
|
|
631
|
+
expect(newCollection.toArray()).toEqual(arr1);
|
|
632
|
+
});
|
|
633
|
+
});
|
|
634
|
+
describe("method: whenNot", () => {
|
|
635
|
+
test("Should append items when statement is false", () => {
|
|
636
|
+
const arr1 = ["a", "b", "c"],
|
|
637
|
+
collection = new ListCollection(arr1),
|
|
638
|
+
arr2 = [1, 2, 3],
|
|
639
|
+
newCollection = collection.whenNot(false, (collection) =>
|
|
640
|
+
collection.append(arr2),
|
|
641
|
+
);
|
|
642
|
+
expect(newCollection.toArray()).toEqual([...arr1, ...arr2]);
|
|
643
|
+
});
|
|
644
|
+
test("Should not append items when statement is true", () => {
|
|
645
|
+
const arr1 = ["a", "b", "c"],
|
|
646
|
+
collection = new ListCollection(arr1),
|
|
647
|
+
arr2 = [1, 2, 3],
|
|
648
|
+
newCollection = collection.whenNot(true, (collection) =>
|
|
649
|
+
collection.append(arr2),
|
|
650
|
+
);
|
|
651
|
+
expect(newCollection.toArray()).toEqual(arr1);
|
|
652
|
+
});
|
|
653
|
+
});
|
|
654
|
+
describe("method: whenNotEmpty", () => {
|
|
655
|
+
test("Should append items when not empty", () => {
|
|
656
|
+
const arr1 = ["a", "b", "c"],
|
|
657
|
+
collection = new ListCollection(arr1),
|
|
658
|
+
arr2 = [1, 2, 3],
|
|
659
|
+
newCollection = collection.whenNotEmpty((collection) =>
|
|
660
|
+
collection.append(arr2),
|
|
661
|
+
);
|
|
662
|
+
expect(newCollection.toArray()).toEqual([...arr1, ...arr2]);
|
|
663
|
+
});
|
|
664
|
+
test("Should not append items when empty", () => {
|
|
665
|
+
const collection = new ListCollection([]),
|
|
666
|
+
arr2 = [1, 2, 3],
|
|
667
|
+
newCollection = collection.whenNotEmpty((collection) =>
|
|
668
|
+
collection.append(arr2),
|
|
669
|
+
);
|
|
670
|
+
expect(newCollection.toArray()).toEqual([]);
|
|
671
|
+
});
|
|
672
|
+
});
|
|
673
|
+
describe("method: pipe", () => {
|
|
674
|
+
test("Should pipe multiple functions", () => {
|
|
675
|
+
const collection = new ListCollection(["a", "ab", "abc", "abcd"]),
|
|
676
|
+
result = collection
|
|
677
|
+
.pipe((collection) =>
|
|
678
|
+
collection.map((item) =>
|
|
679
|
+
new ListCollection(item).map((char) =>
|
|
680
|
+
char.charCodeAt(0),
|
|
681
|
+
),
|
|
682
|
+
),
|
|
683
|
+
)
|
|
684
|
+
.pipe((collection) =>
|
|
685
|
+
collection.map((collection) => collection.sum()),
|
|
686
|
+
)
|
|
687
|
+
.pipe((collection) => collection.sum());
|
|
688
|
+
expect(result).toBeTypeOf("number");
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
describe("method: tap", () => {
|
|
692
|
+
test("Should change the original collection", () => {
|
|
693
|
+
const arr = ["a", "ab", "abc"],
|
|
694
|
+
collection = new ListCollection(arr).tap((collection) =>
|
|
695
|
+
collection.map((item) => item.length),
|
|
696
|
+
);
|
|
697
|
+
expect(collection.toArray()).toEqual(arr);
|
|
698
|
+
});
|
|
699
|
+
});
|
|
700
|
+
describe("method: chunk", () => {
|
|
701
|
+
test("Should group items into groups of size 1", () => {
|
|
702
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8],
|
|
703
|
+
collection = new ListCollection(arr),
|
|
704
|
+
newCollection = collection.chunk(1);
|
|
705
|
+
expect(
|
|
706
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
707
|
+
).toEqual(arr.map((item) => [item]));
|
|
708
|
+
});
|
|
709
|
+
test("Should group items into groups of size 4", () => {
|
|
710
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8],
|
|
711
|
+
collection = new ListCollection(arr),
|
|
712
|
+
newCollection = collection.chunk(4);
|
|
713
|
+
expect(
|
|
714
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
715
|
+
).toEqual([arr.slice(0, 4), arr.slice(4)]);
|
|
716
|
+
});
|
|
717
|
+
});
|
|
718
|
+
describe("method: chunkWhile", () => {
|
|
719
|
+
test("Should group items by checking if next item is the same as the current item", () => {
|
|
720
|
+
const collection = new ListCollection("AABBCCCD".split("")),
|
|
721
|
+
newCollection = collection.chunkWhile(
|
|
722
|
+
(value, _index, chunk) => value === chunk.last(),
|
|
723
|
+
);
|
|
724
|
+
expect(
|
|
725
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
726
|
+
).toEqual([["A", "A"], ["B", "B"], ["C", "C", "C"], ["D"]]);
|
|
727
|
+
});
|
|
728
|
+
});
|
|
729
|
+
describe("method: split", () => {
|
|
730
|
+
test("Should split items into 3 groups in equal size when size is 9", () => {
|
|
731
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
732
|
+
collection = new ListCollection(arr),
|
|
733
|
+
newCollection = collection.split(3);
|
|
734
|
+
expect(
|
|
735
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
736
|
+
).toEqual([arr.slice(0, 3), arr.slice(3, 6), arr.slice(6, 9)]);
|
|
737
|
+
});
|
|
738
|
+
test("Should split items into 3 groups where the first group have on item more when size is 10", () => {
|
|
739
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
|
740
|
+
collection = new ListCollection(arr),
|
|
741
|
+
newCollection = collection.split(3);
|
|
742
|
+
expect(
|
|
743
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
744
|
+
).toEqual([arr.slice(0, 4), arr.slice(4, 7), arr.slice(7, 10)]);
|
|
745
|
+
});
|
|
746
|
+
test("Should split items into 3 groups where the first and second group have on item more when size is 11", () => {
|
|
747
|
+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
|
|
748
|
+
collection = new ListCollection(arr),
|
|
749
|
+
newCollection = collection.split(3);
|
|
750
|
+
expect(
|
|
751
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
752
|
+
).toEqual([arr.slice(0, 4), arr.slice(4, 8), arr.slice(8, 11)]);
|
|
753
|
+
});
|
|
754
|
+
});
|
|
755
|
+
describe("method: partition", () => {
|
|
756
|
+
test("Should group items into strings and number", () => {
|
|
757
|
+
const arr = ["a", 1, "b", 2, "c", 3, "d", 4, "e", 5],
|
|
758
|
+
collection = new ListCollection(arr),
|
|
759
|
+
newCollection = collection.partition(
|
|
760
|
+
(item) => typeof item === "string",
|
|
761
|
+
);
|
|
762
|
+
expect(
|
|
763
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
764
|
+
).toEqual([
|
|
765
|
+
arr.filter((item) => typeof item === "string"),
|
|
766
|
+
arr.filter((item) => typeof item === "number"),
|
|
767
|
+
]);
|
|
768
|
+
});
|
|
769
|
+
test("Should input correct indexes to filter function", () => {
|
|
770
|
+
const arr = ["a", 1, "b", 2, "c", 3, "d", 4, "e", 5],
|
|
771
|
+
collection = new ListCollection(arr),
|
|
772
|
+
indexes: number[] = [];
|
|
773
|
+
collection
|
|
774
|
+
.partition((item, index) => {
|
|
775
|
+
indexes.push(index);
|
|
776
|
+
return typeof item === "string";
|
|
777
|
+
})
|
|
778
|
+
.toArray();
|
|
779
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
780
|
+
});
|
|
781
|
+
});
|
|
782
|
+
describe("method: sliding", () => {
|
|
783
|
+
test("Should group items into 7 groups when size is 2", () => {
|
|
784
|
+
const collection = new ListCollection([
|
|
785
|
+
"a",
|
|
786
|
+
"b",
|
|
787
|
+
"c",
|
|
788
|
+
"d",
|
|
789
|
+
"e",
|
|
790
|
+
"f",
|
|
791
|
+
"g",
|
|
792
|
+
"h",
|
|
793
|
+
]),
|
|
794
|
+
newCollection = collection.sliding({ chunkSize: 2 });
|
|
795
|
+
expect(
|
|
796
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
797
|
+
).toEqual([
|
|
798
|
+
["a", "b"],
|
|
799
|
+
["b", "c"],
|
|
800
|
+
["c", "d"],
|
|
801
|
+
["d", "e"],
|
|
802
|
+
["e", "f"],
|
|
803
|
+
["f", "g"],
|
|
804
|
+
["g", "h"],
|
|
805
|
+
]);
|
|
806
|
+
});
|
|
807
|
+
test("Should group items into 4 groups when size is 3", () => {
|
|
808
|
+
const collection = new ListCollection([
|
|
809
|
+
"a",
|
|
810
|
+
"b",
|
|
811
|
+
"c",
|
|
812
|
+
"d",
|
|
813
|
+
"e",
|
|
814
|
+
"f",
|
|
815
|
+
"g",
|
|
816
|
+
"h",
|
|
817
|
+
]),
|
|
818
|
+
newCollection = collection.sliding({ chunkSize: 3 });
|
|
819
|
+
expect(
|
|
820
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
821
|
+
).toEqual([
|
|
822
|
+
["a", "b", "c"],
|
|
823
|
+
["c", "d", "e"],
|
|
824
|
+
["e", "f", "g"],
|
|
825
|
+
["g", "h"],
|
|
826
|
+
]);
|
|
827
|
+
});
|
|
828
|
+
test("Should group items into 6 groups when size is 3 and step is 1", () => {
|
|
829
|
+
const collection = new ListCollection([
|
|
830
|
+
"a",
|
|
831
|
+
"b",
|
|
832
|
+
"c",
|
|
833
|
+
"d",
|
|
834
|
+
"e",
|
|
835
|
+
"f",
|
|
836
|
+
"g",
|
|
837
|
+
"h",
|
|
838
|
+
]),
|
|
839
|
+
newCollection = collection.sliding({ chunkSize: 3, step: 1 });
|
|
840
|
+
expect(
|
|
841
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
842
|
+
).toEqual([
|
|
843
|
+
["a", "b", "c"],
|
|
844
|
+
["b", "c", "d"],
|
|
845
|
+
["c", "d", "e"],
|
|
846
|
+
["d", "e", "f"],
|
|
847
|
+
["e", "f", "g"],
|
|
848
|
+
["f", "g", "h"],
|
|
849
|
+
]);
|
|
850
|
+
});
|
|
851
|
+
test("Should group items into 6 groups when size is 4 and step is 2", () => {
|
|
852
|
+
const collection = new ListCollection([
|
|
853
|
+
"a",
|
|
854
|
+
"b",
|
|
855
|
+
"c",
|
|
856
|
+
"d",
|
|
857
|
+
"e",
|
|
858
|
+
"f",
|
|
859
|
+
"g",
|
|
860
|
+
"h",
|
|
861
|
+
]),
|
|
862
|
+
newCollection = collection.sliding({ chunkSize: 4, step: 2 });
|
|
863
|
+
expect(
|
|
864
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
865
|
+
).toEqual([
|
|
866
|
+
["a", "b", "c", "d"],
|
|
867
|
+
["c", "d", "e", "f"],
|
|
868
|
+
["e", "f", "g", "h"],
|
|
869
|
+
]);
|
|
870
|
+
});
|
|
871
|
+
test("Should group items into 4 groups when size is 1 and step is 2", () => {
|
|
872
|
+
const collection = new ListCollection([
|
|
873
|
+
"a",
|
|
874
|
+
"b",
|
|
875
|
+
"c",
|
|
876
|
+
"d",
|
|
877
|
+
"e",
|
|
878
|
+
"f",
|
|
879
|
+
"g",
|
|
880
|
+
"h",
|
|
881
|
+
]),
|
|
882
|
+
newCollection = collection.sliding({ chunkSize: 1, step: 2 });
|
|
883
|
+
expect(
|
|
884
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
885
|
+
).toEqual([["a"], ["c"], ["e"], ["g"]]);
|
|
886
|
+
});
|
|
887
|
+
test("Should group items into 3 groups when size is 1 and step is 3", () => {
|
|
888
|
+
const collection = new ListCollection([
|
|
889
|
+
"a",
|
|
890
|
+
"b",
|
|
891
|
+
"c",
|
|
892
|
+
"d",
|
|
893
|
+
"e",
|
|
894
|
+
"f",
|
|
895
|
+
"g",
|
|
896
|
+
"h",
|
|
897
|
+
]),
|
|
898
|
+
newCollection = collection.sliding({ chunkSize: 1, step: 3 });
|
|
899
|
+
expect(
|
|
900
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
901
|
+
).toEqual([["a"], ["d"], ["g"]]);
|
|
902
|
+
});
|
|
903
|
+
test("Should group items into 2 groups when size is 1 and step is 4", () => {
|
|
904
|
+
const collection = new ListCollection([
|
|
905
|
+
"a",
|
|
906
|
+
"b",
|
|
907
|
+
"c",
|
|
908
|
+
"d",
|
|
909
|
+
"e",
|
|
910
|
+
"f",
|
|
911
|
+
"g",
|
|
912
|
+
"h",
|
|
913
|
+
]),
|
|
914
|
+
newCollection = collection.sliding({ chunkSize: 1, step: 3 });
|
|
915
|
+
expect(
|
|
916
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
917
|
+
).toEqual([["a"], ["d"], ["g"]]);
|
|
918
|
+
});
|
|
919
|
+
test("Should group items into 1 groups when size is 2 and step is 1 and array size is 2", () => {
|
|
920
|
+
const collection = new ListCollection(["a", "b"]),
|
|
921
|
+
newCollection = collection.sliding({ chunkSize: 2, step: 1 });
|
|
922
|
+
expect(
|
|
923
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
924
|
+
).toEqual([["a", "b"]]);
|
|
925
|
+
});
|
|
926
|
+
test("Should group items into 1 groups when size is 2 and step is 2 and array size is 2", () => {
|
|
927
|
+
const collection = new ListCollection(["a", "b"]),
|
|
928
|
+
newCollection = collection.sliding({ chunkSize: 2, step: 2 });
|
|
929
|
+
expect(
|
|
930
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
931
|
+
).toEqual([["a", "b"]]);
|
|
932
|
+
});
|
|
933
|
+
test("Should group items into 1 groups when size is 3 and step is 2 and array size is 2", () => {
|
|
934
|
+
const collection = new ListCollection(["a", "b"]),
|
|
935
|
+
newCollection = collection.sliding({ chunkSize: 2, step: 3 });
|
|
936
|
+
expect(
|
|
937
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
938
|
+
).toEqual([["a", "b"]]);
|
|
939
|
+
});
|
|
940
|
+
test("Should group items into 1 groups when size is 2 and step is 3 and array size is 2", () => {
|
|
941
|
+
const collection = new ListCollection(["a", "b"]),
|
|
942
|
+
newCollection = collection.sliding({ chunkSize: 3, step: 2 });
|
|
943
|
+
expect(
|
|
944
|
+
newCollection.map((item) => item.toArray()).toArray(),
|
|
945
|
+
).toEqual([["a", "b"]]);
|
|
946
|
+
});
|
|
947
|
+
test("Should return empty collection when size is 1", () => {
|
|
948
|
+
const collection = new ListCollection([
|
|
949
|
+
"a",
|
|
950
|
+
"b",
|
|
951
|
+
"c",
|
|
952
|
+
"d",
|
|
953
|
+
"e",
|
|
954
|
+
"f",
|
|
955
|
+
"g",
|
|
956
|
+
"h",
|
|
957
|
+
]),
|
|
958
|
+
newCollection = collection.sliding({ chunkSize: 1 });
|
|
959
|
+
expect(newCollection.toArray()).toEqual([]);
|
|
960
|
+
});
|
|
961
|
+
});
|
|
962
|
+
describe("method: groupBy", () => {
|
|
963
|
+
test("Should group by with default map function", () => {
|
|
964
|
+
const arr = ["a", "b", "c", "a", "b", "c", "b", "d"],
|
|
965
|
+
collection = new ListCollection(arr),
|
|
966
|
+
newCollection = collection.groupBy();
|
|
967
|
+
expect(
|
|
968
|
+
newCollection
|
|
969
|
+
.map(
|
|
970
|
+
([key, item]): RecordItem<string, string[]> => [
|
|
971
|
+
key,
|
|
972
|
+
item.toArray(),
|
|
973
|
+
],
|
|
974
|
+
)
|
|
975
|
+
.toArray(),
|
|
976
|
+
).toEqual([
|
|
977
|
+
["a", arr.filter((item) => item === "a")],
|
|
978
|
+
["b", arr.filter((item) => item === "b")],
|
|
979
|
+
["c", arr.filter((item) => item === "c")],
|
|
980
|
+
["d", arr.filter((item) => item === "d")],
|
|
981
|
+
]);
|
|
982
|
+
});
|
|
983
|
+
test("Should group by with custom map function", () => {
|
|
984
|
+
type Person = {
|
|
985
|
+
name: string;
|
|
986
|
+
age: number;
|
|
987
|
+
};
|
|
988
|
+
const arr: Person[] = [
|
|
989
|
+
{
|
|
990
|
+
name: "Abra",
|
|
991
|
+
age: 20,
|
|
992
|
+
},
|
|
993
|
+
{
|
|
994
|
+
name: "Asmail",
|
|
995
|
+
age: 34,
|
|
996
|
+
},
|
|
997
|
+
{
|
|
998
|
+
name: "Ibra",
|
|
999
|
+
age: 50,
|
|
1000
|
+
},
|
|
1001
|
+
{
|
|
1002
|
+
name: "Asmail",
|
|
1003
|
+
age: 21,
|
|
1004
|
+
},
|
|
1005
|
+
{
|
|
1006
|
+
name: "Abra",
|
|
1007
|
+
age: 32,
|
|
1008
|
+
},
|
|
1009
|
+
{
|
|
1010
|
+
name: "Abra",
|
|
1011
|
+
age: 67,
|
|
1012
|
+
},
|
|
1013
|
+
],
|
|
1014
|
+
collection = new ListCollection<Person>(arr),
|
|
1015
|
+
newCollection = collection.groupBy({
|
|
1016
|
+
mapFn(item) {
|
|
1017
|
+
return item.name;
|
|
1018
|
+
},
|
|
1019
|
+
});
|
|
1020
|
+
expect(
|
|
1021
|
+
newCollection
|
|
1022
|
+
.map(
|
|
1023
|
+
([key, item]): RecordItem<string, Person[]> => [
|
|
1024
|
+
key,
|
|
1025
|
+
item.toArray(),
|
|
1026
|
+
],
|
|
1027
|
+
)
|
|
1028
|
+
.toArray(),
|
|
1029
|
+
).toEqual([
|
|
1030
|
+
["Abra", arr.filter((item) => item.name === "Abra")],
|
|
1031
|
+
["Asmail", arr.filter((item) => item.name === "Asmail")],
|
|
1032
|
+
["Ibra", arr.filter((item) => item.name === "Ibra")],
|
|
1033
|
+
]);
|
|
1034
|
+
});
|
|
1035
|
+
test("Should input correct indexes to filter function", () => {
|
|
1036
|
+
const collection = new ListCollection([
|
|
1037
|
+
"a",
|
|
1038
|
+
"b",
|
|
1039
|
+
"c",
|
|
1040
|
+
"a",
|
|
1041
|
+
"b",
|
|
1042
|
+
"c",
|
|
1043
|
+
"b",
|
|
1044
|
+
"d",
|
|
1045
|
+
]),
|
|
1046
|
+
indexes: number[] = [];
|
|
1047
|
+
collection
|
|
1048
|
+
.groupBy({
|
|
1049
|
+
mapFn: (item, index) => {
|
|
1050
|
+
indexes.push(index);
|
|
1051
|
+
return item;
|
|
1052
|
+
},
|
|
1053
|
+
})
|
|
1054
|
+
.toArray();
|
|
1055
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4, 5, 6, 7]);
|
|
1056
|
+
});
|
|
1057
|
+
});
|
|
1058
|
+
describe("method: countBy", () => {
|
|
1059
|
+
test("Should count by with default map function", () => {
|
|
1060
|
+
const arr = ["a", "b", "c", "a", "b", "c", "b", "d"],
|
|
1061
|
+
collection = new ListCollection(arr),
|
|
1062
|
+
newCollection = collection.countBy();
|
|
1063
|
+
expect(newCollection.toArray()).toEqual([
|
|
1064
|
+
["a", arr.filter((item) => item === "a").length],
|
|
1065
|
+
["b", arr.filter((item) => item === "b").length],
|
|
1066
|
+
["c", arr.filter((item) => item === "c").length],
|
|
1067
|
+
["d", arr.filter((item) => item === "d").length],
|
|
1068
|
+
]);
|
|
1069
|
+
});
|
|
1070
|
+
test("Should count by with custom map function", () => {
|
|
1071
|
+
type Person = {
|
|
1072
|
+
name: string;
|
|
1073
|
+
age: number;
|
|
1074
|
+
};
|
|
1075
|
+
const arr: Person[] = [
|
|
1076
|
+
{
|
|
1077
|
+
name: "Abra",
|
|
1078
|
+
age: 20,
|
|
1079
|
+
},
|
|
1080
|
+
{
|
|
1081
|
+
name: "Asmail",
|
|
1082
|
+
age: 34,
|
|
1083
|
+
},
|
|
1084
|
+
{
|
|
1085
|
+
name: "Ibra",
|
|
1086
|
+
age: 50,
|
|
1087
|
+
},
|
|
1088
|
+
{
|
|
1089
|
+
name: "Asmail",
|
|
1090
|
+
age: 21,
|
|
1091
|
+
},
|
|
1092
|
+
{
|
|
1093
|
+
name: "Abra",
|
|
1094
|
+
age: 32,
|
|
1095
|
+
},
|
|
1096
|
+
{
|
|
1097
|
+
name: "Abra",
|
|
1098
|
+
age: 67,
|
|
1099
|
+
},
|
|
1100
|
+
],
|
|
1101
|
+
collection = new ListCollection<Person>(arr),
|
|
1102
|
+
newCollection = collection.countBy({
|
|
1103
|
+
mapFn(item) {
|
|
1104
|
+
return item.name;
|
|
1105
|
+
},
|
|
1106
|
+
});
|
|
1107
|
+
expect(newCollection.toArray()).toEqual([
|
|
1108
|
+
["Abra", arr.filter((item) => item.name === "Abra").length],
|
|
1109
|
+
["Asmail", arr.filter((item) => item.name === "Asmail").length],
|
|
1110
|
+
["Ibra", arr.filter((item) => item.name === "Ibra").length],
|
|
1111
|
+
]);
|
|
1112
|
+
});
|
|
1113
|
+
test("Should input correct indexes to filter function", () => {
|
|
1114
|
+
const collection = new ListCollection([
|
|
1115
|
+
"a",
|
|
1116
|
+
"b",
|
|
1117
|
+
"c",
|
|
1118
|
+
"a",
|
|
1119
|
+
"b",
|
|
1120
|
+
"c",
|
|
1121
|
+
"b",
|
|
1122
|
+
"d",
|
|
1123
|
+
]),
|
|
1124
|
+
indexes: number[] = [];
|
|
1125
|
+
collection
|
|
1126
|
+
.countBy({
|
|
1127
|
+
mapFn: (item, index) => {
|
|
1128
|
+
indexes.push(index);
|
|
1129
|
+
return item;
|
|
1130
|
+
},
|
|
1131
|
+
})
|
|
1132
|
+
.toArray();
|
|
1133
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4, 5, 6, 7]);
|
|
1134
|
+
});
|
|
1135
|
+
});
|
|
1136
|
+
describe("method: unique", () => {
|
|
1137
|
+
test("Should return unique items with default map function", () => {
|
|
1138
|
+
const collection = new ListCollection([
|
|
1139
|
+
"a",
|
|
1140
|
+
"b",
|
|
1141
|
+
"c",
|
|
1142
|
+
"a",
|
|
1143
|
+
"b",
|
|
1144
|
+
"c",
|
|
1145
|
+
]),
|
|
1146
|
+
newCollection = collection.unique();
|
|
1147
|
+
expect(newCollection.toArray()).toEqual(["a", "b", "c"]);
|
|
1148
|
+
});
|
|
1149
|
+
test("Should return unique items with custom map function", () => {
|
|
1150
|
+
const collection = new ListCollection([
|
|
1151
|
+
"a",
|
|
1152
|
+
"bb",
|
|
1153
|
+
"cc",
|
|
1154
|
+
"acc",
|
|
1155
|
+
"b",
|
|
1156
|
+
"cccc",
|
|
1157
|
+
]),
|
|
1158
|
+
newCollection = collection.unique({
|
|
1159
|
+
mapFn(item) {
|
|
1160
|
+
return item.length;
|
|
1161
|
+
},
|
|
1162
|
+
});
|
|
1163
|
+
expect(newCollection.toArray()).toEqual(["a", "bb", "acc", "cccc"]);
|
|
1164
|
+
});
|
|
1165
|
+
test("Should input correct indexes to filter function", () => {
|
|
1166
|
+
const collection = new ListCollection([
|
|
1167
|
+
"a",
|
|
1168
|
+
"b",
|
|
1169
|
+
"c",
|
|
1170
|
+
"a",
|
|
1171
|
+
"b",
|
|
1172
|
+
"c",
|
|
1173
|
+
"b",
|
|
1174
|
+
"d",
|
|
1175
|
+
]),
|
|
1176
|
+
indexes: number[] = [];
|
|
1177
|
+
collection
|
|
1178
|
+
.unique({
|
|
1179
|
+
mapFn: (item, index) => {
|
|
1180
|
+
indexes.push(index);
|
|
1181
|
+
return item;
|
|
1182
|
+
},
|
|
1183
|
+
})
|
|
1184
|
+
.toArray();
|
|
1185
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4, 5, 6, 7]);
|
|
1186
|
+
});
|
|
1187
|
+
});
|
|
1188
|
+
describe("method: difference", () => {
|
|
1189
|
+
test("Should remove all elements matches the given iterable elements", () => {
|
|
1190
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1191
|
+
const difference = collection.difference([2, 4, 6, 8]);
|
|
1192
|
+
expect(difference.toArray()).toEqual([1, 3, 5]);
|
|
1193
|
+
});
|
|
1194
|
+
});
|
|
1195
|
+
describe("method: repeat", () => {
|
|
1196
|
+
test("Should repeath all elements 2 times when input is 3", () => {
|
|
1197
|
+
const arr = [1, 2, 3];
|
|
1198
|
+
const collection = new ListCollection(arr);
|
|
1199
|
+
const newCollection = collection.repeat(4);
|
|
1200
|
+
expect(newCollection.toArray()).toEqual([...arr, ...arr, ...arr]);
|
|
1201
|
+
});
|
|
1202
|
+
});
|
|
1203
|
+
describe("method: padStart", () => {
|
|
1204
|
+
test(`Should retuern "foofoofabc" when maxLength is 10 and fillItems "foo"`, () => {
|
|
1205
|
+
const result = new ListCollection("abc").padStart(10, "foo").join({
|
|
1206
|
+
seperator: "",
|
|
1207
|
+
});
|
|
1208
|
+
expect(result).toBe("foofoofabc");
|
|
1209
|
+
});
|
|
1210
|
+
test(`Should retuern "123abc" when maxLength is 6 and fillItems "abc"`, () => {
|
|
1211
|
+
const result = new ListCollection("abc")
|
|
1212
|
+
.padStart(6, "123465")
|
|
1213
|
+
.join({ seperator: "" });
|
|
1214
|
+
expect(result).toBe("123abc");
|
|
1215
|
+
});
|
|
1216
|
+
test(`Should retuern "00000abc" when maxLength is 8 and fillItems "00000abc"`, () => {
|
|
1217
|
+
const result = new ListCollection("abc")
|
|
1218
|
+
.padStart(8, "0")
|
|
1219
|
+
.join({ seperator: "" });
|
|
1220
|
+
expect(result).toBe("00000abc");
|
|
1221
|
+
});
|
|
1222
|
+
test(`Should retuern "abc" when maxLength is 1 and fillItems "_"`, () => {
|
|
1223
|
+
const result = new ListCollection("abc")
|
|
1224
|
+
.padStart(1, "_")
|
|
1225
|
+
.join({ seperator: "" });
|
|
1226
|
+
expect(result).toBe("abc");
|
|
1227
|
+
});
|
|
1228
|
+
});
|
|
1229
|
+
describe("method: padEnd", () => {
|
|
1230
|
+
test(`Should retuern "abcfoofoof" when maxLength is 10 and fillItems "foo"`, () => {
|
|
1231
|
+
const result = new ListCollection("abc").padEnd(10, "foo").join({
|
|
1232
|
+
seperator: "",
|
|
1233
|
+
});
|
|
1234
|
+
expect(result).toBe("abcfoofoof");
|
|
1235
|
+
});
|
|
1236
|
+
test(`Should retuern "abc123" when maxLength is 6 and fillItems "abc"`, () => {
|
|
1237
|
+
const result = new ListCollection("abc")
|
|
1238
|
+
.padEnd(6, "123465")
|
|
1239
|
+
.join({ seperator: "" });
|
|
1240
|
+
expect(result).toBe("abc123");
|
|
1241
|
+
});
|
|
1242
|
+
test(`Should retuern "abc00000" when maxLength is 8 and fillItems "00000abc"`, () => {
|
|
1243
|
+
const result = new ListCollection("abc")
|
|
1244
|
+
.padEnd(8, "0")
|
|
1245
|
+
.join({ seperator: "" });
|
|
1246
|
+
expect(result).toBe("abc00000");
|
|
1247
|
+
});
|
|
1248
|
+
test(`Should retuern "abc" when maxLength is 1 and fillItems "_"`, () => {
|
|
1249
|
+
const result = new ListCollection("abc")
|
|
1250
|
+
.padEnd(1, "_")
|
|
1251
|
+
.join({ seperator: "" });
|
|
1252
|
+
expect(result).toBe("abc");
|
|
1253
|
+
});
|
|
1254
|
+
});
|
|
1255
|
+
describe("method: slice", () => {
|
|
1256
|
+
test("Should return [1] when start is 0, end is 1 and array is [1, 2, 3, 4, 5]", () => {
|
|
1257
|
+
const arr = [1, 2, 3, 4, 5];
|
|
1258
|
+
const collection = new ListCollection(arr);
|
|
1259
|
+
const newCollection = collection.slice({ start: 0, end: 1 });
|
|
1260
|
+
expect(newCollection.toArray()).toEqual(arr.slice(0, 1));
|
|
1261
|
+
});
|
|
1262
|
+
test("Should return [4, 5] when start is -2 and array is [1, 2, 3, 4, 5]", () => {
|
|
1263
|
+
const arr = [1, 2, 3, 4, 5];
|
|
1264
|
+
const collection = new ListCollection(arr);
|
|
1265
|
+
const newCollection = collection.slice({ start: -2 });
|
|
1266
|
+
expect(newCollection.toArray()).toEqual(arr.slice(-2));
|
|
1267
|
+
});
|
|
1268
|
+
test("Should return [1, 2, 3, 4] when start is 0, end is -1 and array is [1, 2, 3, 4, 5]", () => {
|
|
1269
|
+
const arr = [1, 2, 3, 4, 5];
|
|
1270
|
+
const collection = new ListCollection(arr);
|
|
1271
|
+
const newCollection = collection.slice({ start: 0, end: -1 });
|
|
1272
|
+
expect(newCollection.toArray()).toEqual(arr.slice(0, -1));
|
|
1273
|
+
});
|
|
1274
|
+
test("Should return [3, 4] when start is 2, end is -1 and array is [1, 2, 3, 4, 5]", () => {
|
|
1275
|
+
const arr = [1, 2, 3, 4, 5];
|
|
1276
|
+
const collection = new ListCollection(arr);
|
|
1277
|
+
const newCollection = collection.slice({ start: 2, end: -1 });
|
|
1278
|
+
expect(newCollection.toArray()).toEqual(arr.slice(2, -1));
|
|
1279
|
+
});
|
|
1280
|
+
test("Should return [2, 3, 4] when start is 1, end is 4 and array is [1, 2, 3, 4, 5]", () => {
|
|
1281
|
+
const arr = [1, 2, 3, 4, 5];
|
|
1282
|
+
const collection = new ListCollection(arr);
|
|
1283
|
+
const newCollection = collection.slice({ start: 1, end: 4 });
|
|
1284
|
+
expect(newCollection.toArray()).toEqual(arr.slice(1, 4));
|
|
1285
|
+
});
|
|
1286
|
+
test("Should return [3, 4] when start is 2, end is 4 and array is [1, 2, 3, 4, 5]", () => {
|
|
1287
|
+
const arr = [1, 2, 3, 4, 5];
|
|
1288
|
+
const collection = new ListCollection(arr);
|
|
1289
|
+
const newCollection = collection.slice({ start: 2, end: 4 });
|
|
1290
|
+
expect(newCollection.toArray()).toEqual(arr.slice(2, 4));
|
|
1291
|
+
});
|
|
1292
|
+
});
|
|
1293
|
+
describe("method: prepend", () => {
|
|
1294
|
+
test("Should prepend iterable", () => {
|
|
1295
|
+
const arr1 = ["a", "b", "c"],
|
|
1296
|
+
collection = new ListCollection(arr1),
|
|
1297
|
+
arr2 = [1, 2, 3],
|
|
1298
|
+
prependedCollection = collection.prepend(arr2);
|
|
1299
|
+
expect(prependedCollection.toArray()).toEqual([...arr2, ...arr1]);
|
|
1300
|
+
});
|
|
1301
|
+
});
|
|
1302
|
+
describe("method: append", () => {
|
|
1303
|
+
test("Should append iterable", () => {
|
|
1304
|
+
const arr1 = ["a", "b", "c"],
|
|
1305
|
+
collection = new ListCollection(arr1),
|
|
1306
|
+
arr2 = [1, 2, 3],
|
|
1307
|
+
appendedCollection = collection.append(arr2);
|
|
1308
|
+
expect(appendedCollection.toArray()).toEqual([...arr1, ...arr2]);
|
|
1309
|
+
});
|
|
1310
|
+
});
|
|
1311
|
+
describe("method: insertBefore", () => {
|
|
1312
|
+
test("Should insert iterable before first item", () => {
|
|
1313
|
+
const arr1 = ["a", "b", "c"],
|
|
1314
|
+
collection = new ListCollection(arr1),
|
|
1315
|
+
arr2 = [1, 2, 3],
|
|
1316
|
+
newCollection = collection.insertBefore(
|
|
1317
|
+
(item) => item === "a",
|
|
1318
|
+
arr2,
|
|
1319
|
+
);
|
|
1320
|
+
expect(newCollection.toArray()).toEqual([...arr2, ...arr1]);
|
|
1321
|
+
});
|
|
1322
|
+
test("Should insert iterable before last item", () => {
|
|
1323
|
+
const arr1 = ["a", "b", "c"],
|
|
1324
|
+
collection = new ListCollection(arr1),
|
|
1325
|
+
arr2 = [1, 2, 3],
|
|
1326
|
+
newCollection = collection.insertBefore(
|
|
1327
|
+
(item) => item === "c",
|
|
1328
|
+
arr2,
|
|
1329
|
+
);
|
|
1330
|
+
expect(newCollection.toArray()).toEqual([
|
|
1331
|
+
...arr1.slice(0, -1),
|
|
1332
|
+
...arr2,
|
|
1333
|
+
...arr1.slice(-1),
|
|
1334
|
+
]);
|
|
1335
|
+
});
|
|
1336
|
+
test("Should not insert iterable if filter item not found", () => {
|
|
1337
|
+
const arr1 = ["a", "b", "c"],
|
|
1338
|
+
collection = new ListCollection(arr1),
|
|
1339
|
+
arr2 = [1, 2, 3],
|
|
1340
|
+
newCollection = collection.insertBefore(
|
|
1341
|
+
(item) => item === "d",
|
|
1342
|
+
arr2,
|
|
1343
|
+
);
|
|
1344
|
+
expect(newCollection.toArray()).toEqual(arr1);
|
|
1345
|
+
});
|
|
1346
|
+
});
|
|
1347
|
+
describe("method: insertAfter", () => {
|
|
1348
|
+
test("Should insert iterable after last item", () => {
|
|
1349
|
+
const arr1 = ["a", "b", "c"],
|
|
1350
|
+
collection = new ListCollection(arr1),
|
|
1351
|
+
arr2 = [1, 2, 3],
|
|
1352
|
+
newCollection = collection.insertAfter(
|
|
1353
|
+
(item) => item === "c",
|
|
1354
|
+
arr2,
|
|
1355
|
+
);
|
|
1356
|
+
expect(newCollection.toArray()).toEqual([...arr1, ...arr2]);
|
|
1357
|
+
});
|
|
1358
|
+
test("Should insert iterable after first item", () => {
|
|
1359
|
+
const arr1 = ["a", "b", "c"],
|
|
1360
|
+
collection = new ListCollection(arr1),
|
|
1361
|
+
arr2 = [1, 2, 3],
|
|
1362
|
+
newCollection = collection.insertAfter(
|
|
1363
|
+
(item) => item === "a",
|
|
1364
|
+
arr2,
|
|
1365
|
+
);
|
|
1366
|
+
expect(newCollection.toArray()).toEqual([
|
|
1367
|
+
...arr1.slice(0, 1),
|
|
1368
|
+
...arr2,
|
|
1369
|
+
...arr1.slice(-2),
|
|
1370
|
+
]);
|
|
1371
|
+
});
|
|
1372
|
+
test("Should not insert iterable if filter item not found", () => {
|
|
1373
|
+
const arr1 = ["a", "b", "c"],
|
|
1374
|
+
collection = new ListCollection(arr1),
|
|
1375
|
+
arr2 = [1, 2, 3],
|
|
1376
|
+
newCollection = collection.insertAfter(
|
|
1377
|
+
(item) => item === "d",
|
|
1378
|
+
arr2,
|
|
1379
|
+
);
|
|
1380
|
+
expect(newCollection.toArray()).toEqual(arr1);
|
|
1381
|
+
});
|
|
1382
|
+
});
|
|
1383
|
+
describe("method: crossJoin", () => {
|
|
1384
|
+
test(`Should return 4 combinations when input iterable is [1, 2] and ["a", "b"]`, () => {
|
|
1385
|
+
const collection = new ListCollection([1, 2]);
|
|
1386
|
+
const matrix = collection.crossJoin(["a", "b"]);
|
|
1387
|
+
expect(
|
|
1388
|
+
matrix.map((collection) => collection.toArray()).toArray(),
|
|
1389
|
+
).toEqual([
|
|
1390
|
+
[1, "a"],
|
|
1391
|
+
[1, "b"],
|
|
1392
|
+
[2, "a"],
|
|
1393
|
+
[2, "b"],
|
|
1394
|
+
]);
|
|
1395
|
+
});
|
|
1396
|
+
test(`Should return 8 combinations when input iterable is [1, 2], ["a", "b"] and ["I", "II"]`, () => {
|
|
1397
|
+
const collection = new ListCollection([1, 2]);
|
|
1398
|
+
const matrix = collection.crossJoin(["a", "b"], ["I", "II"]);
|
|
1399
|
+
expect(
|
|
1400
|
+
matrix.map((collection) => collection.toArray()).toArray(),
|
|
1401
|
+
).toEqual([
|
|
1402
|
+
[1, "a", "I"],
|
|
1403
|
+
[1, "a", "II"],
|
|
1404
|
+
[1, "b", "I"],
|
|
1405
|
+
[1, "b", "II"],
|
|
1406
|
+
[2, "a", "I"],
|
|
1407
|
+
[2, "a", "II"],
|
|
1408
|
+
[2, "b", "I"],
|
|
1409
|
+
[2, "b", "II"],
|
|
1410
|
+
]);
|
|
1411
|
+
});
|
|
1412
|
+
});
|
|
1413
|
+
describe("method: zip", () => {
|
|
1414
|
+
test("Should zip iterable", () => {
|
|
1415
|
+
const arr1 = ["a", "b", "c"],
|
|
1416
|
+
collection = new ListCollection(arr1),
|
|
1417
|
+
arr2 = [1, 2, 3],
|
|
1418
|
+
newCollection = collection.zip(arr2);
|
|
1419
|
+
expect(newCollection.toArray()).toEqual([
|
|
1420
|
+
[arr1[0], arr2[0]],
|
|
1421
|
+
[arr1[1], arr2[1]],
|
|
1422
|
+
[arr1[2], arr2[2]],
|
|
1423
|
+
]);
|
|
1424
|
+
});
|
|
1425
|
+
test("Should have the length of collection", () => {
|
|
1426
|
+
const arr1 = ["a", "b", "c", "d"],
|
|
1427
|
+
collection = new ListCollection(arr1),
|
|
1428
|
+
arr2 = [1, 2, 3],
|
|
1429
|
+
newCollection = collection.zip(arr2);
|
|
1430
|
+
expect(newCollection.toArray()).toEqual([
|
|
1431
|
+
[arr1[0], arr2[0]],
|
|
1432
|
+
[arr1[1], arr2[1]],
|
|
1433
|
+
[arr1[2], arr2[2]],
|
|
1434
|
+
]);
|
|
1435
|
+
});
|
|
1436
|
+
test("Should have the length of input iterable", () => {
|
|
1437
|
+
const arr1 = ["a", "b", "c"],
|
|
1438
|
+
collection = new ListCollection(arr1),
|
|
1439
|
+
arr2 = [1, 2, 3, 4],
|
|
1440
|
+
newCollection = collection.zip(arr2);
|
|
1441
|
+
expect(newCollection.toArray()).toEqual([
|
|
1442
|
+
[arr1[0], arr2[0]],
|
|
1443
|
+
[arr1[1], arr2[1]],
|
|
1444
|
+
[arr1[2], arr2[2]],
|
|
1445
|
+
]);
|
|
1446
|
+
});
|
|
1447
|
+
});
|
|
1448
|
+
describe("method: sort", () => {
|
|
1449
|
+
test("Sort numbers from smallest to largest with custom comparator function", () => {
|
|
1450
|
+
const arr = [-1, 2, 1, -3, 4, 20, 15, -5, -3],
|
|
1451
|
+
collection = new ListCollection(arr),
|
|
1452
|
+
newCollection = collection.sort((a, b) => a - b);
|
|
1453
|
+
expect(newCollection.toArray()).toEqual(
|
|
1454
|
+
[...arr].sort((a, b) => a - b),
|
|
1455
|
+
);
|
|
1456
|
+
});
|
|
1457
|
+
test("Sort numbers from smallest to largest with default compartor function", () => {
|
|
1458
|
+
const arr = [-1, 2, 1, -3, 4, 20, 15, -5, -3],
|
|
1459
|
+
collection = new ListCollection(arr),
|
|
1460
|
+
newCollection = collection.sort();
|
|
1461
|
+
expect(newCollection.toArray()).toEqual([...arr].sort());
|
|
1462
|
+
});
|
|
1463
|
+
});
|
|
1464
|
+
describe("method: reverse", () => {
|
|
1465
|
+
test("Should reverse iterable", () => {
|
|
1466
|
+
const arr = ["a", "b", "c", "d", "e", "f"],
|
|
1467
|
+
collection = new ListCollection(arr),
|
|
1468
|
+
newCollection = collection.reverse();
|
|
1469
|
+
expect(newCollection.toArray()).toEqual([...arr].reverse());
|
|
1470
|
+
});
|
|
1471
|
+
});
|
|
1472
|
+
describe("method: first", () => {
|
|
1473
|
+
test("Should return first item that matches the filter function", () => {
|
|
1474
|
+
type Person = {
|
|
1475
|
+
name: string;
|
|
1476
|
+
age: number;
|
|
1477
|
+
};
|
|
1478
|
+
const persons: Person[] = [
|
|
1479
|
+
{
|
|
1480
|
+
name: "Joe",
|
|
1481
|
+
age: 20,
|
|
1482
|
+
},
|
|
1483
|
+
{
|
|
1484
|
+
name: "Jhon",
|
|
1485
|
+
age: 23,
|
|
1486
|
+
},
|
|
1487
|
+
{
|
|
1488
|
+
name: "Joe",
|
|
1489
|
+
age: 30,
|
|
1490
|
+
},
|
|
1491
|
+
{
|
|
1492
|
+
name: "Jhon",
|
|
1493
|
+
age: 50,
|
|
1494
|
+
},
|
|
1495
|
+
],
|
|
1496
|
+
collection = new ListCollection(persons),
|
|
1497
|
+
item = collection.first({
|
|
1498
|
+
filterFn: (person) => person.name === "Joe",
|
|
1499
|
+
});
|
|
1500
|
+
expect(item).toEqual(persons[0]);
|
|
1501
|
+
});
|
|
1502
|
+
test("Should return first item when found", () => {
|
|
1503
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1504
|
+
item = collection.first();
|
|
1505
|
+
expect(item).toBe(1);
|
|
1506
|
+
});
|
|
1507
|
+
test("Should return null when item not found", () => {
|
|
1508
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1509
|
+
item = collection.first({
|
|
1510
|
+
filterFn: (item) => item === 6,
|
|
1511
|
+
});
|
|
1512
|
+
expect(item).toBe(null);
|
|
1513
|
+
});
|
|
1514
|
+
test("Should input correct indexes to filter function", () => {
|
|
1515
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1516
|
+
indexes: number[] = [];
|
|
1517
|
+
collection.first({
|
|
1518
|
+
filterFn: (item, index) => {
|
|
1519
|
+
indexes.push(index);
|
|
1520
|
+
return item === 6;
|
|
1521
|
+
},
|
|
1522
|
+
});
|
|
1523
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4]);
|
|
1524
|
+
});
|
|
1525
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
1526
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1527
|
+
expect(collection.first()).toBe(1);
|
|
1528
|
+
expect(collection.first()).toBe(1);
|
|
1529
|
+
});
|
|
1530
|
+
});
|
|
1531
|
+
describe("method: firstOr", () => {
|
|
1532
|
+
test("Should return first item that matches the filter function", () => {
|
|
1533
|
+
type Person = {
|
|
1534
|
+
name: string;
|
|
1535
|
+
age: number;
|
|
1536
|
+
};
|
|
1537
|
+
const persons: Person[] = [
|
|
1538
|
+
{
|
|
1539
|
+
name: "Joe",
|
|
1540
|
+
age: 20,
|
|
1541
|
+
},
|
|
1542
|
+
{
|
|
1543
|
+
name: "Jhon",
|
|
1544
|
+
age: 23,
|
|
1545
|
+
},
|
|
1546
|
+
{
|
|
1547
|
+
name: "Joe",
|
|
1548
|
+
age: 30,
|
|
1549
|
+
},
|
|
1550
|
+
{
|
|
1551
|
+
name: "Jhon",
|
|
1552
|
+
age: 50,
|
|
1553
|
+
},
|
|
1554
|
+
],
|
|
1555
|
+
collection = new ListCollection(persons),
|
|
1556
|
+
item = collection.firstOr({
|
|
1557
|
+
defaultValue: null,
|
|
1558
|
+
filterFn: (person) => person.name === "Joe",
|
|
1559
|
+
});
|
|
1560
|
+
expect(item).toEqual(persons[0]);
|
|
1561
|
+
});
|
|
1562
|
+
test("Should return first item when found", () => {
|
|
1563
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1564
|
+
item = collection.firstOr({
|
|
1565
|
+
defaultValue: "a",
|
|
1566
|
+
});
|
|
1567
|
+
expect(item).toBe(1);
|
|
1568
|
+
});
|
|
1569
|
+
test("Should return default value when item not found", () => {
|
|
1570
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1571
|
+
item = collection.firstOr({
|
|
1572
|
+
defaultValue: "a",
|
|
1573
|
+
filterFn: (item) => item === 6,
|
|
1574
|
+
});
|
|
1575
|
+
expect(item).toBe("a");
|
|
1576
|
+
});
|
|
1577
|
+
test("Should input correct indexes to filter function", () => {
|
|
1578
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1579
|
+
indexes: number[] = [];
|
|
1580
|
+
collection.firstOr({
|
|
1581
|
+
defaultValue: null,
|
|
1582
|
+
filterFn: (item, index) => {
|
|
1583
|
+
indexes.push(index);
|
|
1584
|
+
return item === 6;
|
|
1585
|
+
},
|
|
1586
|
+
});
|
|
1587
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4]);
|
|
1588
|
+
});
|
|
1589
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
1590
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1591
|
+
expect(
|
|
1592
|
+
collection.firstOr({
|
|
1593
|
+
defaultValue: "a",
|
|
1594
|
+
}),
|
|
1595
|
+
).toBe(1);
|
|
1596
|
+
expect(
|
|
1597
|
+
collection.firstOr({
|
|
1598
|
+
defaultValue: "a",
|
|
1599
|
+
}),
|
|
1600
|
+
).toBe(1);
|
|
1601
|
+
});
|
|
1602
|
+
});
|
|
1603
|
+
describe("method: firstOrFail", () => {
|
|
1604
|
+
test("Should return first item that matches the filter function", () => {
|
|
1605
|
+
type Person = {
|
|
1606
|
+
name: string;
|
|
1607
|
+
age: number;
|
|
1608
|
+
};
|
|
1609
|
+
const persons: Person[] = [
|
|
1610
|
+
{
|
|
1611
|
+
name: "Joe",
|
|
1612
|
+
age: 20,
|
|
1613
|
+
},
|
|
1614
|
+
{
|
|
1615
|
+
name: "Jhon",
|
|
1616
|
+
age: 23,
|
|
1617
|
+
},
|
|
1618
|
+
{
|
|
1619
|
+
name: "Joe",
|
|
1620
|
+
age: 30,
|
|
1621
|
+
},
|
|
1622
|
+
{
|
|
1623
|
+
name: "Jhon",
|
|
1624
|
+
age: 50,
|
|
1625
|
+
},
|
|
1626
|
+
],
|
|
1627
|
+
collection = new ListCollection(persons),
|
|
1628
|
+
item = collection.firstOrFail({
|
|
1629
|
+
filterFn: (person) => person.name === "Joe",
|
|
1630
|
+
});
|
|
1631
|
+
expect(item).toEqual(persons[0]);
|
|
1632
|
+
});
|
|
1633
|
+
test("Should return first item when found", () => {
|
|
1634
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1635
|
+
item = collection.firstOrFail();
|
|
1636
|
+
expect(item).toBe(1);
|
|
1637
|
+
});
|
|
1638
|
+
test("Should throw CollectionError when item not found", () => {
|
|
1639
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1640
|
+
expect(() => {
|
|
1641
|
+
collection.firstOrFail({
|
|
1642
|
+
filterFn: (item) => item === 6,
|
|
1643
|
+
});
|
|
1644
|
+
}).toThrowError(CollectionError);
|
|
1645
|
+
});
|
|
1646
|
+
test("Should throw ItemNotFoundError when item not found", () => {
|
|
1647
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1648
|
+
expect(() => {
|
|
1649
|
+
collection.firstOrFail({
|
|
1650
|
+
filterFn: (item) => item === 6,
|
|
1651
|
+
});
|
|
1652
|
+
}).toThrowError(ItemNotFoundError);
|
|
1653
|
+
});
|
|
1654
|
+
test("Should input correct indexes to filter function", () => {
|
|
1655
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1656
|
+
indexes: number[] = [];
|
|
1657
|
+
try {
|
|
1658
|
+
collection.firstOrFail({
|
|
1659
|
+
filterFn: (item, index) => {
|
|
1660
|
+
indexes.push(index);
|
|
1661
|
+
return item === 6;
|
|
1662
|
+
},
|
|
1663
|
+
});
|
|
1664
|
+
} catch {
|
|
1665
|
+
/* Empty */
|
|
1666
|
+
}
|
|
1667
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4]);
|
|
1668
|
+
});
|
|
1669
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
1670
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1671
|
+
expect(collection.firstOrFail()).toBe(1);
|
|
1672
|
+
expect(collection.firstOrFail()).toBe(1);
|
|
1673
|
+
});
|
|
1674
|
+
});
|
|
1675
|
+
describe("method: last", () => {
|
|
1676
|
+
test("Should return last item that matches the filter function", () => {
|
|
1677
|
+
type Person = {
|
|
1678
|
+
name: string;
|
|
1679
|
+
age: number;
|
|
1680
|
+
};
|
|
1681
|
+
const persons: Person[] = [
|
|
1682
|
+
{
|
|
1683
|
+
name: "Joe",
|
|
1684
|
+
age: 20,
|
|
1685
|
+
},
|
|
1686
|
+
{
|
|
1687
|
+
name: "Jhon",
|
|
1688
|
+
age: 23,
|
|
1689
|
+
},
|
|
1690
|
+
{
|
|
1691
|
+
name: "Joe",
|
|
1692
|
+
age: 30,
|
|
1693
|
+
},
|
|
1694
|
+
{
|
|
1695
|
+
name: "Jhon",
|
|
1696
|
+
age: 50,
|
|
1697
|
+
},
|
|
1698
|
+
],
|
|
1699
|
+
collection = new ListCollection(persons),
|
|
1700
|
+
item = collection.last({
|
|
1701
|
+
filterFn: (person) => person.name === "Joe",
|
|
1702
|
+
});
|
|
1703
|
+
expect(item).toEqual(persons[2]);
|
|
1704
|
+
});
|
|
1705
|
+
test("Should return last item when found", () => {
|
|
1706
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1707
|
+
item = collection.last();
|
|
1708
|
+
expect(item).toBe(5);
|
|
1709
|
+
});
|
|
1710
|
+
test("Should return null when item not found", () => {
|
|
1711
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1712
|
+
item = collection.last({
|
|
1713
|
+
filterFn: (item) => item === 6,
|
|
1714
|
+
});
|
|
1715
|
+
expect(item).toBe(null);
|
|
1716
|
+
});
|
|
1717
|
+
test("Should input correct indexes to filter function", () => {
|
|
1718
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1719
|
+
indexes: number[] = [];
|
|
1720
|
+
collection.last({
|
|
1721
|
+
filterFn: (item, index) => {
|
|
1722
|
+
indexes.push(index);
|
|
1723
|
+
return item === 6;
|
|
1724
|
+
},
|
|
1725
|
+
});
|
|
1726
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4]);
|
|
1727
|
+
});
|
|
1728
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
1729
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1730
|
+
expect(collection.last()).toBe(5);
|
|
1731
|
+
expect(collection.last()).toBe(5);
|
|
1732
|
+
});
|
|
1733
|
+
});
|
|
1734
|
+
describe("method: lastOr", () => {
|
|
1735
|
+
test("Should return last item that matches the filter function", () => {
|
|
1736
|
+
type Person = {
|
|
1737
|
+
name: string;
|
|
1738
|
+
age: number;
|
|
1739
|
+
};
|
|
1740
|
+
const persons: Person[] = [
|
|
1741
|
+
{
|
|
1742
|
+
name: "Joe",
|
|
1743
|
+
age: 20,
|
|
1744
|
+
},
|
|
1745
|
+
{
|
|
1746
|
+
name: "Jhon",
|
|
1747
|
+
age: 23,
|
|
1748
|
+
},
|
|
1749
|
+
{
|
|
1750
|
+
name: "Joe",
|
|
1751
|
+
age: 30,
|
|
1752
|
+
},
|
|
1753
|
+
{
|
|
1754
|
+
name: "Jhon",
|
|
1755
|
+
age: 50,
|
|
1756
|
+
},
|
|
1757
|
+
],
|
|
1758
|
+
collection = new ListCollection(persons),
|
|
1759
|
+
item = collection.lastOr({
|
|
1760
|
+
defaultValue: null,
|
|
1761
|
+
filterFn: (person) => person.name === "Joe",
|
|
1762
|
+
});
|
|
1763
|
+
expect(item).toEqual(persons[2]);
|
|
1764
|
+
});
|
|
1765
|
+
test("Should return last item when found", () => {
|
|
1766
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1767
|
+
item = collection.lastOr({
|
|
1768
|
+
defaultValue: "a",
|
|
1769
|
+
});
|
|
1770
|
+
expect(item).toBe(5);
|
|
1771
|
+
});
|
|
1772
|
+
test("Should return default value when item not found", () => {
|
|
1773
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1774
|
+
item = collection.lastOr({
|
|
1775
|
+
defaultValue: "a",
|
|
1776
|
+
filterFn: (item) => item === 6,
|
|
1777
|
+
});
|
|
1778
|
+
expect(item).toBe("a");
|
|
1779
|
+
});
|
|
1780
|
+
test("Should input correct indexes to filter function", () => {
|
|
1781
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1782
|
+
indexes: number[] = [];
|
|
1783
|
+
collection.lastOr({
|
|
1784
|
+
defaultValue: null,
|
|
1785
|
+
filterFn: (item, index) => {
|
|
1786
|
+
indexes.push(index);
|
|
1787
|
+
return item === 6;
|
|
1788
|
+
},
|
|
1789
|
+
});
|
|
1790
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4]);
|
|
1791
|
+
});
|
|
1792
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
1793
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1794
|
+
expect(
|
|
1795
|
+
collection.lastOr({
|
|
1796
|
+
defaultValue: "a",
|
|
1797
|
+
}),
|
|
1798
|
+
).toBe(5);
|
|
1799
|
+
expect(
|
|
1800
|
+
collection.lastOr({
|
|
1801
|
+
defaultValue: "a",
|
|
1802
|
+
}),
|
|
1803
|
+
).toBe(5);
|
|
1804
|
+
});
|
|
1805
|
+
});
|
|
1806
|
+
describe("method: lastOrFail", () => {
|
|
1807
|
+
test("Should return last item that matches the filter function", () => {
|
|
1808
|
+
type Person = {
|
|
1809
|
+
name: string;
|
|
1810
|
+
age: number;
|
|
1811
|
+
};
|
|
1812
|
+
const persons: Person[] = [
|
|
1813
|
+
{
|
|
1814
|
+
name: "Joe",
|
|
1815
|
+
age: 20,
|
|
1816
|
+
},
|
|
1817
|
+
{
|
|
1818
|
+
name: "Jhon",
|
|
1819
|
+
age: 23,
|
|
1820
|
+
},
|
|
1821
|
+
{
|
|
1822
|
+
name: "Joe",
|
|
1823
|
+
age: 30,
|
|
1824
|
+
},
|
|
1825
|
+
{
|
|
1826
|
+
name: "Jhon",
|
|
1827
|
+
age: 50,
|
|
1828
|
+
},
|
|
1829
|
+
],
|
|
1830
|
+
collection = new ListCollection(persons),
|
|
1831
|
+
item = collection.lastOrFail({
|
|
1832
|
+
filterFn: (person) => person.name === "Joe",
|
|
1833
|
+
});
|
|
1834
|
+
expect(item).toEqual(persons[2]);
|
|
1835
|
+
});
|
|
1836
|
+
test("Should return last item when found", () => {
|
|
1837
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1838
|
+
item = collection.lastOrFail();
|
|
1839
|
+
expect(item).toBe(5);
|
|
1840
|
+
});
|
|
1841
|
+
test("Should throw CollectionError when item not found", () => {
|
|
1842
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1843
|
+
expect(() => {
|
|
1844
|
+
collection.lastOrFail({
|
|
1845
|
+
filterFn: (item) => item === 6,
|
|
1846
|
+
});
|
|
1847
|
+
}).toThrowError(CollectionError);
|
|
1848
|
+
});
|
|
1849
|
+
test("Should throw ItemNotFoundError when item not found", () => {
|
|
1850
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1851
|
+
expect(() => {
|
|
1852
|
+
collection.lastOrFail({
|
|
1853
|
+
filterFn: (item) => item === 6,
|
|
1854
|
+
});
|
|
1855
|
+
}).toThrowError(ItemNotFoundError);
|
|
1856
|
+
});
|
|
1857
|
+
test("Should input correct indexes to filter function", () => {
|
|
1858
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]),
|
|
1859
|
+
indexes: number[] = [];
|
|
1860
|
+
try {
|
|
1861
|
+
collection.lastOrFail({
|
|
1862
|
+
filterFn: (item, index) => {
|
|
1863
|
+
indexes.push(index);
|
|
1864
|
+
return item === 6;
|
|
1865
|
+
},
|
|
1866
|
+
});
|
|
1867
|
+
} catch {
|
|
1868
|
+
/* Empty */
|
|
1869
|
+
}
|
|
1870
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4]);
|
|
1871
|
+
});
|
|
1872
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
1873
|
+
const collection = new ListCollection([1, 2, 3, 4, 5]);
|
|
1874
|
+
expect(collection.lastOrFail()).toBe(5);
|
|
1875
|
+
expect(collection.lastOrFail()).toBe(5);
|
|
1876
|
+
});
|
|
1877
|
+
});
|
|
1878
|
+
describe("method: before", () => {
|
|
1879
|
+
test(`Should return "a" when searching for string "b" of ["a", "b", "c"]`, () => {
|
|
1880
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1881
|
+
item = collection.before((item) => item === "b");
|
|
1882
|
+
expect(item).toBe("a");
|
|
1883
|
+
});
|
|
1884
|
+
test(`Should return "b" when searching for string "c" of ["a", "b", "c"]`, () => {
|
|
1885
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1886
|
+
item = collection.before((item) => item === "c");
|
|
1887
|
+
expect(item).toBe("b");
|
|
1888
|
+
});
|
|
1889
|
+
test(`Should return null when searching for string "a" of ["a", "b", "c"]`, () => {
|
|
1890
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1891
|
+
item = collection.before((item) => item === "a");
|
|
1892
|
+
expect(item).toBe(null);
|
|
1893
|
+
});
|
|
1894
|
+
test(`Should return null when searching for string "d" of ["a", "b", "c"]`, () => {
|
|
1895
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1896
|
+
item = collection.before((item) => item === "d");
|
|
1897
|
+
expect(item).toBe(null);
|
|
1898
|
+
});
|
|
1899
|
+
test("Should input correct indexes to filter function", () => {
|
|
1900
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1901
|
+
indexes: number[] = [];
|
|
1902
|
+
collection.before((item, index) => {
|
|
1903
|
+
indexes.push(index);
|
|
1904
|
+
return item === "c";
|
|
1905
|
+
});
|
|
1906
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
1907
|
+
});
|
|
1908
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
1909
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
1910
|
+
expect(collection.before((item) => item === "c")).toBe("b");
|
|
1911
|
+
expect(collection.before((item) => item === "c")).toBe("b");
|
|
1912
|
+
});
|
|
1913
|
+
});
|
|
1914
|
+
describe("method: beforeOr", () => {
|
|
1915
|
+
test(`Should return "a" when searching for string "b" of ["a", "b", "c"]`, () => {
|
|
1916
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1917
|
+
item = collection.beforeOr(-1, (item) => item === "b");
|
|
1918
|
+
expect(item).toBe("a");
|
|
1919
|
+
});
|
|
1920
|
+
test(`Should return "b" when searching for string "c" of ["a", "b", "c"]`, () => {
|
|
1921
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1922
|
+
item = collection.beforeOr(-1, (item) => item === "c");
|
|
1923
|
+
expect(item).toBe("b");
|
|
1924
|
+
});
|
|
1925
|
+
test(`Should return default value when searching for string "a" of ["a", "b", "c"]`, () => {
|
|
1926
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1927
|
+
item = collection.beforeOr(-1, (item) => item === "a");
|
|
1928
|
+
expect(item).toBe(-1);
|
|
1929
|
+
});
|
|
1930
|
+
test(`Should return default value when searching for string "d" of ["a", "b", "c"]`, () => {
|
|
1931
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1932
|
+
item = collection.beforeOr(-1, (item) => item === "d");
|
|
1933
|
+
expect(item).toBe(-1);
|
|
1934
|
+
});
|
|
1935
|
+
test("Should input correct indexes to filter function", () => {
|
|
1936
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1937
|
+
indexes: number[] = [];
|
|
1938
|
+
collection.beforeOr(null, (item, index) => {
|
|
1939
|
+
indexes.push(index);
|
|
1940
|
+
return item === "c";
|
|
1941
|
+
});
|
|
1942
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
1943
|
+
});
|
|
1944
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
1945
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
1946
|
+
expect(collection.beforeOr(-1, (item) => item === "c")).toBe("b");
|
|
1947
|
+
expect(collection.beforeOr(-1, (item) => item === "c")).toBe("b");
|
|
1948
|
+
});
|
|
1949
|
+
});
|
|
1950
|
+
describe("method: beforeOrFail", () => {
|
|
1951
|
+
test(`Should return "a" when searching for string "b" of ["a", "b", "c"]`, () => {
|
|
1952
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1953
|
+
item = collection.beforeOrFail((item) => item === "b");
|
|
1954
|
+
expect(item).toBe("a");
|
|
1955
|
+
});
|
|
1956
|
+
test(`Should return "b" when searching for string "c" of ["a", "b", "c"]`, () => {
|
|
1957
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1958
|
+
item = collection.beforeOrFail((item) => item === "c");
|
|
1959
|
+
expect(item).toBe("b");
|
|
1960
|
+
});
|
|
1961
|
+
test(`Should throw CollectionError when searching for string "a" of ["a", "b", "c"]`, () => {
|
|
1962
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
1963
|
+
expect(() => {
|
|
1964
|
+
collection.beforeOrFail((item) => item === "a");
|
|
1965
|
+
}).toThrowError(CollectionError);
|
|
1966
|
+
});
|
|
1967
|
+
test(`Should throw ItemNotFoundError when searching for string "d" of ["a", "b", "c"]`, () => {
|
|
1968
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
1969
|
+
expect(() => {
|
|
1970
|
+
collection.beforeOrFail((item) => item === "d");
|
|
1971
|
+
}).toThrowError(ItemNotFoundError);
|
|
1972
|
+
});
|
|
1973
|
+
test("Should input correct indexes to filter function", () => {
|
|
1974
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1975
|
+
indexes: number[] = [];
|
|
1976
|
+
try {
|
|
1977
|
+
collection.beforeOrFail((item, index) => {
|
|
1978
|
+
indexes.push(index);
|
|
1979
|
+
return item === "c";
|
|
1980
|
+
});
|
|
1981
|
+
} catch {
|
|
1982
|
+
/* Empty */
|
|
1983
|
+
}
|
|
1984
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
1985
|
+
});
|
|
1986
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
1987
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
1988
|
+
expect(collection.beforeOrFail((item) => item === "c")).toBe("b");
|
|
1989
|
+
expect(collection.beforeOrFail((item) => item === "c")).toBe("b");
|
|
1990
|
+
});
|
|
1991
|
+
});
|
|
1992
|
+
describe("method: after", () => {
|
|
1993
|
+
test(`Should return "c" when searching for string "b" of ["a", "b", "c"]`, () => {
|
|
1994
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
1995
|
+
item = collection.after((item) => item === "b");
|
|
1996
|
+
expect(item).toBe("c");
|
|
1997
|
+
});
|
|
1998
|
+
test(`Should return "b" when searching for string "a" of ["a", "b", "c"]`, () => {
|
|
1999
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2000
|
+
item = collection.after((item) => item === "a");
|
|
2001
|
+
expect(item).toBe("b");
|
|
2002
|
+
});
|
|
2003
|
+
test(`Should return null when searching for string "c" of ["a", "b", "c"]`, () => {
|
|
2004
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2005
|
+
item = collection.after((item) => item === "c");
|
|
2006
|
+
expect(item).toBe(null);
|
|
2007
|
+
});
|
|
2008
|
+
test(`Should return null when searching for string "d" of ["a", "b", "c"]`, () => {
|
|
2009
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2010
|
+
item = collection.after((item) => item === "d");
|
|
2011
|
+
expect(item).toBe(null);
|
|
2012
|
+
});
|
|
2013
|
+
test("Should input correct indexes to filter function", () => {
|
|
2014
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2015
|
+
indexes: number[] = [];
|
|
2016
|
+
collection.after((item, index) => {
|
|
2017
|
+
indexes.push(index);
|
|
2018
|
+
return item === "c";
|
|
2019
|
+
});
|
|
2020
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
2021
|
+
});
|
|
2022
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2023
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2024
|
+
expect(collection.after((item) => item === "a")).toBe("b");
|
|
2025
|
+
expect(collection.after((item) => item === "a")).toBe("b");
|
|
2026
|
+
});
|
|
2027
|
+
});
|
|
2028
|
+
describe("method: afterOr", () => {
|
|
2029
|
+
test(`Should return "c" when searching for string "b" of ["a", "b", "c"]`, () => {
|
|
2030
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2031
|
+
item = collection.afterOr(-1, (item) => item === "b");
|
|
2032
|
+
expect(item).toBe("c");
|
|
2033
|
+
});
|
|
2034
|
+
test(`Should return "b" when searching for string "a" of ["a", "b", "c"]`, () => {
|
|
2035
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2036
|
+
item = collection.afterOr(-1, (item) => item === "a");
|
|
2037
|
+
expect(item).toBe("b");
|
|
2038
|
+
});
|
|
2039
|
+
test(`Should return default value when searching for string "c" of ["a", "b", "c"]`, () => {
|
|
2040
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2041
|
+
item = collection.afterOr(-1, (item) => item === "c");
|
|
2042
|
+
expect(item).toBe(-1);
|
|
2043
|
+
});
|
|
2044
|
+
test(`Should return default value when searching for string "d" of ["a", "b", "c"]`, () => {
|
|
2045
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2046
|
+
item = collection.afterOr(-1, (item) => item === "d");
|
|
2047
|
+
expect(item).toBe(-1);
|
|
2048
|
+
});
|
|
2049
|
+
test("Should input correct indexes to filter function", () => {
|
|
2050
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2051
|
+
indexes: number[] = [];
|
|
2052
|
+
collection.afterOr(null, (item, index) => {
|
|
2053
|
+
indexes.push(index);
|
|
2054
|
+
return item === "c";
|
|
2055
|
+
});
|
|
2056
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
2057
|
+
});
|
|
2058
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2059
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2060
|
+
expect(collection.afterOr(-1, (item) => item === "a")).toBe("b");
|
|
2061
|
+
expect(collection.afterOr(-1, (item) => item === "a")).toBe("b");
|
|
2062
|
+
});
|
|
2063
|
+
});
|
|
2064
|
+
describe("method: afterOrFail", () => {
|
|
2065
|
+
test(`Should return "c" when searching for string "b" of ["a", "b", "c"]`, () => {
|
|
2066
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2067
|
+
item = collection.afterOrFail((item) => item === "b");
|
|
2068
|
+
expect(item).toBe("c");
|
|
2069
|
+
});
|
|
2070
|
+
test(`Should return "b" when searching for string "a" of ["a", "b", "c"]`, () => {
|
|
2071
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2072
|
+
item = collection.afterOrFail((item) => item === "a");
|
|
2073
|
+
expect(item).toBe("b");
|
|
2074
|
+
});
|
|
2075
|
+
test(`Should throw CollectionError when searching for string "c" of ["a", "b", "c"]`, () => {
|
|
2076
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2077
|
+
expect(() => {
|
|
2078
|
+
collection.afterOrFail((item) => item === "c");
|
|
2079
|
+
}).toThrowError(CollectionError);
|
|
2080
|
+
});
|
|
2081
|
+
test(`Should throw ItemNotFoundError when searching for string "d" of ["a", "b", "c"]`, () => {
|
|
2082
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2083
|
+
expect(() => {
|
|
2084
|
+
collection.afterOrFail((item) => item === "d");
|
|
2085
|
+
}).toThrowError(ItemNotFoundError);
|
|
2086
|
+
});
|
|
2087
|
+
test("Should input correct indexes to filter function", () => {
|
|
2088
|
+
const collection = new ListCollection(["a", "b", "c"]),
|
|
2089
|
+
indexes: number[] = [];
|
|
2090
|
+
try {
|
|
2091
|
+
collection.afterOrFail((item, index) => {
|
|
2092
|
+
indexes.push(index);
|
|
2093
|
+
return item === "c";
|
|
2094
|
+
});
|
|
2095
|
+
} catch {
|
|
2096
|
+
/* Empty */
|
|
2097
|
+
}
|
|
2098
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
2099
|
+
});
|
|
2100
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2101
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2102
|
+
expect(collection.afterOrFail((item) => item === "a")).toBe("b");
|
|
2103
|
+
expect(collection.afterOrFail((item) => item === "a")).toBe("b");
|
|
2104
|
+
});
|
|
2105
|
+
});
|
|
2106
|
+
describe("method: sole", () => {
|
|
2107
|
+
test("Should throw ItemNotFoundError when item does not exist", () => {
|
|
2108
|
+
const collection = new ListCollection(["a", "a", "b", "c", "b"]);
|
|
2109
|
+
expect(() => {
|
|
2110
|
+
collection.sole((item) => item === "f");
|
|
2111
|
+
}).toThrowError(ItemNotFoundError);
|
|
2112
|
+
});
|
|
2113
|
+
test("Should throw MultipleItemsFoundError when multiple item of same sort does exist", () => {
|
|
2114
|
+
const collection = new ListCollection(["a", "a", "b", "c", "b"]);
|
|
2115
|
+
expect(() => {
|
|
2116
|
+
collection.sole((item) => item === "a");
|
|
2117
|
+
}).toThrowError(MultipleItemsFoundError);
|
|
2118
|
+
});
|
|
2119
|
+
test("Should return item when only one item of the same sort exist", () => {
|
|
2120
|
+
const collection = new ListCollection(["a", "a", "b", "c", "b"]);
|
|
2121
|
+
expect(collection.sole((item) => item === "c")).toBe("c");
|
|
2122
|
+
});
|
|
2123
|
+
test("Should input correct indexes to filter function", () => {
|
|
2124
|
+
const collection = new ListCollection(["a", "a", "b", "c", "b"]),
|
|
2125
|
+
indexes: number[] = [];
|
|
2126
|
+
collection.sole((item, index) => {
|
|
2127
|
+
indexes.push(index);
|
|
2128
|
+
return item === "c";
|
|
2129
|
+
});
|
|
2130
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4]);
|
|
2131
|
+
});
|
|
2132
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2133
|
+
const collection = new ListCollection(["a", "a", "b", "c", "b"]);
|
|
2134
|
+
expect(collection.sole((item) => item === "c")).toBe("c");
|
|
2135
|
+
expect(collection.sole((item) => item === "c")).toBe("c");
|
|
2136
|
+
});
|
|
2137
|
+
});
|
|
2138
|
+
describe("method: nth", () => {
|
|
2139
|
+
test("Should filter the 4:th items", () => {
|
|
2140
|
+
const collection = new ListCollection([
|
|
2141
|
+
"a",
|
|
2142
|
+
"b",
|
|
2143
|
+
"c",
|
|
2144
|
+
"d",
|
|
2145
|
+
"e",
|
|
2146
|
+
"f",
|
|
2147
|
+
]),
|
|
2148
|
+
newCollection = collection.nth(4);
|
|
2149
|
+
expect(newCollection.toArray()).toEqual(["a", "e"]);
|
|
2150
|
+
});
|
|
2151
|
+
});
|
|
2152
|
+
describe("method: count", () => {
|
|
2153
|
+
test(`Should return number 0 when filtering all string "a" of ["b", "b"]`, () => {
|
|
2154
|
+
const collection = new ListCollection(["b", "b"]);
|
|
2155
|
+
expect(collection.count((item) => item === "a")).toBe(0);
|
|
2156
|
+
});
|
|
2157
|
+
test(`Should return number 3 when filtering all string "a" of ["a", "b", "a", "b", "a"]`, () => {
|
|
2158
|
+
const collection = new ListCollection(["a", "b", "a", "b", "a"]);
|
|
2159
|
+
expect(collection.count((item) => item === "a")).toBe(3);
|
|
2160
|
+
});
|
|
2161
|
+
test("Should input correct indexes to filter function", () => {
|
|
2162
|
+
const collection = new ListCollection(["a", "a", "b", "c", "b"]),
|
|
2163
|
+
indexes: number[] = [];
|
|
2164
|
+
collection.count((_item, index) => {
|
|
2165
|
+
indexes.push(index);
|
|
2166
|
+
return true;
|
|
2167
|
+
});
|
|
2168
|
+
expect(indexes).toEqual([0, 1, 2, 3, 4]);
|
|
2169
|
+
});
|
|
2170
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2171
|
+
const collection = new ListCollection(["a", "b", "a", "b", "a"]);
|
|
2172
|
+
expect(collection.count((item) => item === "a")).toBe(3);
|
|
2173
|
+
expect(collection.count((item) => item === "a")).toBe(3);
|
|
2174
|
+
});
|
|
2175
|
+
});
|
|
2176
|
+
describe("method: size", () => {
|
|
2177
|
+
test("Should return 0 when empty", () => {
|
|
2178
|
+
const collection = new ListCollection([]);
|
|
2179
|
+
expect(collection.size()).toBe(0);
|
|
2180
|
+
});
|
|
2181
|
+
test("Should return number larger than 0 when empty", () => {
|
|
2182
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2183
|
+
expect(collection.size()).toBeGreaterThan(0);
|
|
2184
|
+
});
|
|
2185
|
+
test("Should return 3 when contains 3 items", () => {
|
|
2186
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2187
|
+
expect(collection.size()).toBe(3);
|
|
2188
|
+
});
|
|
2189
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2190
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2191
|
+
expect(collection.size()).toBe(3);
|
|
2192
|
+
expect(collection.size()).toBe(3);
|
|
2193
|
+
});
|
|
2194
|
+
});
|
|
2195
|
+
describe("method: empty", () => {
|
|
2196
|
+
test("Should return true when empty", () => {
|
|
2197
|
+
const collection = new ListCollection([]);
|
|
2198
|
+
expect(collection.empty()).toBe(true);
|
|
2199
|
+
});
|
|
2200
|
+
test("Should return false when not empty", () => {
|
|
2201
|
+
const collection = new ListCollection([""]);
|
|
2202
|
+
expect(collection.empty()).toBe(false);
|
|
2203
|
+
});
|
|
2204
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2205
|
+
const collection = new ListCollection([]);
|
|
2206
|
+
expect(collection.empty()).toBe(true);
|
|
2207
|
+
expect(collection.empty()).toBe(true);
|
|
2208
|
+
});
|
|
2209
|
+
});
|
|
2210
|
+
describe("method: notEmpty", () => {
|
|
2211
|
+
test("Should return true when not empty", () => {
|
|
2212
|
+
const collection = new ListCollection([""]);
|
|
2213
|
+
expect(collection.notEmpty()).toBe(true);
|
|
2214
|
+
});
|
|
2215
|
+
test("Should return false when empty", () => {
|
|
2216
|
+
const collection = new ListCollection([]);
|
|
2217
|
+
expect(collection.notEmpty()).toBe(false);
|
|
2218
|
+
});
|
|
2219
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2220
|
+
const collection = new ListCollection([""]);
|
|
2221
|
+
expect(collection.notEmpty()).toBe(true);
|
|
2222
|
+
expect(collection.notEmpty()).toBe(true);
|
|
2223
|
+
});
|
|
2224
|
+
});
|
|
2225
|
+
describe("method: search", () => {
|
|
2226
|
+
test("Should return -1 when searching for value that does not exist in collection", () => {
|
|
2227
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2228
|
+
expect(collection.search((item) => item === "d")).toBe(-1);
|
|
2229
|
+
});
|
|
2230
|
+
test(`Should return 1 when searching for string "b" of ["a", "b", "c"]`, () => {
|
|
2231
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2232
|
+
expect(collection.search((item) => item === "b")).toBe(1);
|
|
2233
|
+
});
|
|
2234
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2235
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2236
|
+
expect(collection.search((item) => item === "b")).toBe(1);
|
|
2237
|
+
expect(collection.search((item) => item === "b")).toBe(1);
|
|
2238
|
+
});
|
|
2239
|
+
});
|
|
2240
|
+
describe("method: forEach", () => {
|
|
2241
|
+
test("Should iterate all items", () => {
|
|
2242
|
+
const arr1 = [1, 2, 3],
|
|
2243
|
+
collection = new ListCollection(arr1),
|
|
2244
|
+
arr2: number[] = [];
|
|
2245
|
+
collection.forEach((item) => arr2.push(item));
|
|
2246
|
+
expect(arr2).toEqual(arr1);
|
|
2247
|
+
});
|
|
2248
|
+
test("Should input correct indexes to filter function", () => {
|
|
2249
|
+
const collection = new ListCollection([1, 2, 3]),
|
|
2250
|
+
indexes: number[] = [];
|
|
2251
|
+
collection.forEach((_item, index) => {
|
|
2252
|
+
indexes.push(index);
|
|
2253
|
+
});
|
|
2254
|
+
expect(indexes).toEqual([0, 1, 2]);
|
|
2255
|
+
});
|
|
2256
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2257
|
+
const arr1 = [1, 2, 3],
|
|
2258
|
+
collection = new ListCollection(arr1),
|
|
2259
|
+
arr2: number[] = [];
|
|
2260
|
+
collection.forEach((item) => arr2.push(item));
|
|
2261
|
+
expect(arr2).toEqual(arr1);
|
|
2262
|
+
expect(arr2).toEqual(arr1);
|
|
2263
|
+
});
|
|
2264
|
+
});
|
|
2265
|
+
describe("method: toArray", () => {
|
|
2266
|
+
test("Should return array with 0 items when empty", () => {
|
|
2267
|
+
const collection = new ListCollection([]);
|
|
2268
|
+
expect(collection.toArray()).toEqual([]);
|
|
2269
|
+
});
|
|
2270
|
+
test("Should return array with items when that match collection items", () => {
|
|
2271
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2272
|
+
expect(collection.toArray()).toEqual(["a", "b", "c"]);
|
|
2273
|
+
});
|
|
2274
|
+
test("Should return the same value when called more than 1 times", () => {
|
|
2275
|
+
const collection = new ListCollection(["a", "b", "c"]);
|
|
2276
|
+
expect(collection.toArray()).toEqual(["a", "b", "c"]);
|
|
2277
|
+
expect(collection.toArray()).toEqual(["a", "b", "c"]);
|
|
2278
|
+
});
|
|
2279
|
+
});
|
|
2280
|
+
});
|