@siteimprove/alfa-iterable 0.89.5

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/CHANGELOG.md ADDED
@@ -0,0 +1,157 @@
1
+ # @siteimprove/alfa-iterable
2
+
3
+ ## 0.89.3
4
+
5
+ ## 0.89.2
6
+
7
+ ### Patch Changes
8
+
9
+ - **Changed:** Trying to fix a problem in generating provenance statements ([#1674](https://github.com/Siteimprove/alfa/pull/1674))
10
+
11
+ ## 0.89.1
12
+
13
+ ### Patch Changes
14
+
15
+ - **Added:** Trying to publish Alfa packages on the npm registry ([#1673](https://github.com/Siteimprove/alfa/pull/1673))
16
+
17
+ ## 0.89.0
18
+
19
+ ## 0.88.0
20
+
21
+ ### Minor Changes
22
+
23
+ - **Fixed:** The publish flow was updated to a new version. ([`a2f19cf9a6c7c72b8bf085597e4f1a95ac3e4eb2`](https://github.com/Siteimprove/alfa/commit/a2f19cf9a6c7c72b8bf085597e4f1a95ac3e4eb2))
24
+
25
+ Some 0.87.\* versions were generating uninstallable package. This should be fixed now.
26
+
27
+ ## 0.87.12
28
+
29
+ ## 0.87.11
30
+
31
+ ## 0.87.10
32
+
33
+ ## 0.87.7
34
+
35
+ ## 0.87.6
36
+
37
+ ## 0.87.5
38
+
39
+ ## 0.87.4
40
+
41
+ ## 0.87.3
42
+
43
+ ## 0.87.2
44
+
45
+ ## 0.87.1
46
+
47
+ ## 0.87.0
48
+
49
+ ### Minor Changes
50
+
51
+ - **Breaking:** Optional serialization type parameters have been removed. ([#1651](https://github.com/Siteimprove/alfa/pull/1651))
52
+
53
+ ## 0.86.2
54
+
55
+ ## 0.86.1
56
+
57
+ ## 0.86.0
58
+
59
+ ### Minor Changes
60
+
61
+ - **Breaking:** TS resolution has been changed to `Node16`, target to `es2022`. ([#1636](https://github.com/Siteimprove/alfa/pull/1636))
62
+
63
+ - **Breaking:** Alfa is now distributed as ESM rather than CJS modules; projects using it must be ESM or use dynamic `import()`. ([#1636](https://github.com/Siteimprove/alfa/pull/1636))
64
+
65
+ ⚠️ This is the last of a series of changes on the internal structure and build process of distributed packages that was started with v0.85.0.
66
+
67
+ ## 0.85.1
68
+
69
+ ## 0.85.0
70
+
71
+ ### Minor Changes
72
+
73
+ - **Breaking:** The .js files are now built in the `dist` folder rather than in `src`. ([#1628](https://github.com/Siteimprove/alfa/pull/1628))
74
+
75
+ ⚠️ This is the first of a series of changes on the internal structure and build process of distributed packages. It is probably better to not use this version and wait until more of these internal changes have been done to jump directly to the final result. We are internally releasing these changes for validation purpose only.
76
+
77
+ This should not impact consumers, the `package.json` files should be set correctly to consume these files.
78
+
79
+ ## 0.84.0
80
+
81
+ ## 0.83.1
82
+
83
+ ## 0.83.0
84
+
85
+ ## 0.82.0
86
+
87
+ ### Minor Changes
88
+
89
+ - **Added:** Serialization options are now accepted, and passed on, by `toJSON()` on these types. ([#1622](https://github.com/Siteimprove/alfa/pull/1622))
90
+
91
+ - **Breaking:** Node 18 is no longer supported. ([#1618](https://github.com/Siteimprove/alfa/pull/1618))
92
+
93
+ ## 0.81.0
94
+
95
+ ### Patch Changes
96
+
97
+ - **Added:** Each package now contains its internal dependency graph in its `docs` directory. ([#1610](https://github.com/Siteimprove/alfa/pull/1610))
98
+
99
+ ## 0.80.0
100
+
101
+ ## 0.79.1
102
+
103
+ ## 0.79.0
104
+
105
+ ## 0.78.2
106
+
107
+ ## 0.78.1
108
+
109
+ ## 0.78.0
110
+
111
+ ## 0.77.0
112
+
113
+ ## 0.76.0
114
+
115
+ ## 0.75.2
116
+
117
+ ## 0.75.1
118
+
119
+ ## 0.75.0
120
+
121
+ ## 0.74.0
122
+
123
+ ## 0.73.0
124
+
125
+ ## 0.72.0
126
+
127
+ ## 0.71.1
128
+
129
+ ## 0.71.0
130
+
131
+ ## 0.70.0
132
+
133
+ ## 0.69.0
134
+
135
+ ## 0.68.0
136
+
137
+ ## 0.67.0
138
+
139
+ ## 0.66.0
140
+
141
+ ## 0.65.1
142
+
143
+ ## 0.65.0
144
+
145
+ ## 0.64.0
146
+
147
+ ## 0.63.3
148
+
149
+ ## 0.63.2
150
+
151
+ ## 0.63.1
152
+
153
+ ## 0.63.0
154
+
155
+ ## 0.62.2
156
+
157
+ ## 0.62.1
@@ -0,0 +1,2 @@
1
+ export * from "./iterable.js";
2
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./iterable.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,95 @@
1
+ import type { Callback } from "@siteimprove/alfa-callback";
2
+ import { Comparable, type Comparer, Comparison } from "@siteimprove/alfa-comparable";
3
+ import type { Hash } from "@siteimprove/alfa-hash";
4
+ import { Serializable } from "@siteimprove/alfa-json";
5
+ import type { Mapper } from "@siteimprove/alfa-mapper";
6
+ import { Option } from "@siteimprove/alfa-option";
7
+ import { Predicate } from "@siteimprove/alfa-predicate";
8
+ import type { Reducer } from "@siteimprove/alfa-reducer";
9
+ import { Refinement } from "@siteimprove/alfa-refinement";
10
+ /**
11
+ * @remarks
12
+ * This is a re-export of the global `Iterable` interface to ensure that it
13
+ * merges with the `Iterable` namespace.
14
+ *
15
+ * @public
16
+ */
17
+ export type Iterable<T> = globalThis.Iterable<T>;
18
+ /**
19
+ * @public
20
+ */
21
+ export declare namespace Iterable {
22
+ function isIterable<T>(value: unknown): value is Iterable<T>;
23
+ function empty<T>(): Iterable<T>;
24
+ function from<T>(arrayLike: ArrayLike<T>): Iterable<T>;
25
+ function size<T>(iterable: Iterable<T>): number;
26
+ function isEmpty<T>(iterable: Iterable<T>): iterable is Iterable<never>;
27
+ function forEach<T>(iterable: Iterable<T>, callback: Callback<T, void, [index: number]>): void;
28
+ function map<T, U = T>(iterable: Iterable<T>, mapper: Mapper<T, U, [index: number]>): Iterable<U>;
29
+ function flatMap<T, U = T>(iterable: Iterable<T>, mapper: Mapper<T, Iterable<U>, [index: number]>): Iterable<U>;
30
+ function flatten<T>(iterable: Iterable<Iterable<T>>): Iterable<T>;
31
+ function reduce<T, U = T>(iterable: Iterable<T>, reducer: Reducer<T, U, [index: number]>, accumulator: U): U;
32
+ function reduceWhile<T, U = T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>, reducer: Reducer<T, U, [index: number]>, accumulator: U): U;
33
+ function reduceUntil<T, U = T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>, reducer: Reducer<T, U, [index: number]>, accumulator: U): U;
34
+ function apply<T, U>(iterable: Iterable<T>, mapper: Iterable<Mapper<T, U>>): Iterable<U>;
35
+ function filter<T, U extends T>(iterable: Iterable<T>, refinement: Refinement<T, U, [index: number]>): Iterable<U>;
36
+ function filter<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
37
+ function reject<T, U extends T>(iterable: Iterable<T>, refinement: Refinement<T, U, [index: number]>): Iterable<Exclude<T, U>>;
38
+ function reject<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
39
+ function find<T, U extends T>(iterable: Iterable<T>, refinement: Refinement<T, U, [index: number]>): Option<U>;
40
+ function find<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Option<T>;
41
+ function findLast<T, U extends T>(iterable: Iterable<T>, refinement: Refinement<T, U, [index: number]>): Option<U>;
42
+ function findLast<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Option<T>;
43
+ function includes<T>(iterable: Iterable<T>, value: T): boolean;
44
+ function collect<T, U>(iterable: Iterable<T>, mapper: Mapper<T, Option<U>, [index: number]>): Iterable<U>;
45
+ function collectFirst<T, U>(iterable: Iterable<T>, mapper: Mapper<T, Option<U>, [index: number]>): Option<U>;
46
+ function some<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): boolean;
47
+ function none<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): boolean;
48
+ function every<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): boolean;
49
+ function count<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): number;
50
+ function distinct<T>(iterable: Iterable<T>): Iterable<T>;
51
+ function get<T>(iterable: Iterable<T>, index: number): Option<T>;
52
+ function has<T>(iterable: Iterable<T>, index: number): boolean;
53
+ function set<T>(iterable: Iterable<T>, index: number, value: T): Iterable<T>;
54
+ function insert<T>(iterable: Iterable<T>, index: number, value: T): Iterable<T>;
55
+ function append<T>(iterable: Iterable<T>, value: T): Iterable<T>;
56
+ function prepend<T>(iterable: Iterable<T>, value: T): Iterable<T>;
57
+ function concat<T>(iterable: Iterable<T>, ...iterables: Array<Iterable<T>>): Iterable<T>;
58
+ function subtract<T>(iterable: Iterable<T>, ...iterables: Array<Iterable<T>>): Iterable<T>;
59
+ function intersect<T>(iterable: Iterable<T>, ...iterables: Array<Iterable<T>>): Iterable<T>;
60
+ function zip<T, U = T>(a: Iterable<T>, b: Iterable<U>): Iterable<[T, U]>;
61
+ function first<T>(iterable: Iterable<T>): Option<T>;
62
+ function last<T>(iterable: Iterable<T>): Option<T>;
63
+ function take<T>(iterable: Iterable<T>, count: number): Iterable<T>;
64
+ function takeWhile<T, U extends T>(iterable: Iterable<T>, refinement: Refinement<T, U, [index: number]>): Iterable<U>;
65
+ function takeWhile<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
66
+ function takeUntil<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
67
+ function takeLast<T>(iterable: Iterable<T>, count?: number): Iterable<T>;
68
+ function takeLastWhile<T, U extends T>(iterable: Iterable<T>, refinement: Refinement<T, U, [index: number]>): Iterable<U>;
69
+ function takeLastWhile<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
70
+ function takeLastUntil<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
71
+ function skip<T>(iterable: Iterable<T>, count: number): Iterable<T>;
72
+ function skipWhile<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
73
+ function skipUntil<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
74
+ function skipLast<T>(iterable: Iterable<T>, count?: number): Iterable<T>;
75
+ function skipLastWhile<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
76
+ function skipLastUntil<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
77
+ function trim<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
78
+ function trimLeading<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
79
+ function trimTrailing<T>(iterable: Iterable<T>, predicate: Predicate<T, [index: number]>): Iterable<T>;
80
+ function rest<T>(iterable: Iterable<T>): Iterable<T>;
81
+ function slice<T>(iterable: Iterable<T>, start: number, end?: number): Iterable<T>;
82
+ function reverse<T>(iterable: Iterable<T>): Iterable<T>;
83
+ function join<T>(iterable: Iterable<T>, separator: string): string;
84
+ function sort<T extends Comparable<T>>(iterable: Iterable<T>): Iterable<T>;
85
+ function sortWith<T>(iterable: Iterable<T>, comparer: Comparer<T>): Iterable<T>;
86
+ function sortWith<T, U extends T = T>(iterable: Iterable<U>, comparer: Comparer<T>): Iterable<U>;
87
+ function compare<T extends Comparable<U>, U = T>(a: Iterable<T>, b: Iterable<U>): Comparison;
88
+ function compareWith<T, U = T>(a: Iterable<T>, b: Iterable<U>, comparer: Comparer<T, U, [index: number]>): Comparison;
89
+ function equals<T>(a: Iterable<T>, b: Iterable<T>): boolean;
90
+ function hash<T>(iterable: Iterable<T>, hash: Hash): void;
91
+ function iterator<T>(iterable: Iterable<T>): Iterator<T>;
92
+ function groupBy<T, K>(iterable: Iterable<T>, grouper: Mapper<T, K, [index: number]>): Iterable<[K, Iterable<T>]>;
93
+ function toJSON<T>(iterable: Iterable<T>, options?: Serializable.Options): Array<Serializable.ToJSON<T>>;
94
+ }
95
+ //# sourceMappingURL=iterable.d.ts.map
@@ -0,0 +1,545 @@
1
+ import { Comparable, Comparison, } from "@siteimprove/alfa-comparable";
2
+ import { Equatable } from "@siteimprove/alfa-equatable";
3
+ import { Serializable } from "@siteimprove/alfa-json";
4
+ import { Option, None } from "@siteimprove/alfa-option";
5
+ import { Predicate } from "@siteimprove/alfa-predicate";
6
+ import { Refinement } from "@siteimprove/alfa-refinement";
7
+ const { not } = Predicate;
8
+ const { isObject } = Refinement;
9
+ const { compareComparable } = Comparable;
10
+ /**
11
+ * @public
12
+ */
13
+ export var Iterable;
14
+ (function (Iterable) {
15
+ function isIterable(value) {
16
+ return isObject(value) && Symbol.iterator in value;
17
+ }
18
+ Iterable.isIterable = isIterable;
19
+ function* empty() { }
20
+ Iterable.empty = empty;
21
+ function* from(arrayLike) {
22
+ for (let i = 0, n = arrayLike.length; i < n; i++) {
23
+ yield arrayLike[i];
24
+ }
25
+ }
26
+ Iterable.from = from;
27
+ function size(iterable) {
28
+ return reduce(iterable, (size) => size + 1, 0);
29
+ }
30
+ Iterable.size = size;
31
+ function isEmpty(iterable) {
32
+ for (const _ of iterable) {
33
+ return false;
34
+ }
35
+ return true;
36
+ }
37
+ Iterable.isEmpty = isEmpty;
38
+ function forEach(iterable, callback) {
39
+ let index = 0;
40
+ for (const value of iterable) {
41
+ callback(value, index++);
42
+ }
43
+ }
44
+ Iterable.forEach = forEach;
45
+ function* map(iterable, mapper) {
46
+ let index = 0;
47
+ for (const value of iterable) {
48
+ yield mapper(value, index++);
49
+ }
50
+ }
51
+ Iterable.map = map;
52
+ function* flatMap(iterable, mapper) {
53
+ let index = 0;
54
+ for (const value of iterable) {
55
+ yield* mapper(value, index++);
56
+ }
57
+ }
58
+ Iterable.flatMap = flatMap;
59
+ function* flatten(iterable) {
60
+ for (const value of iterable) {
61
+ yield* value;
62
+ }
63
+ }
64
+ Iterable.flatten = flatten;
65
+ function reduce(iterable, reducer, accumulator) {
66
+ let index = 0;
67
+ for (const value of iterable) {
68
+ accumulator = reducer(accumulator, value, index++);
69
+ }
70
+ return accumulator;
71
+ }
72
+ Iterable.reduce = reduce;
73
+ function reduceWhile(iterable, predicate, reducer, accumulator) {
74
+ let index = 0;
75
+ for (const value of iterable) {
76
+ if (predicate(value, index)) {
77
+ accumulator = reducer(accumulator, value, index++);
78
+ }
79
+ else {
80
+ break;
81
+ }
82
+ }
83
+ return accumulator;
84
+ }
85
+ Iterable.reduceWhile = reduceWhile;
86
+ function reduceUntil(iterable, predicate, reducer, accumulator) {
87
+ return reduceWhile(iterable, not(predicate), reducer, accumulator);
88
+ }
89
+ Iterable.reduceUntil = reduceUntil;
90
+ function apply(iterable, mapper) {
91
+ return flatMap(mapper, (mapper) => map(iterable, mapper));
92
+ }
93
+ Iterable.apply = apply;
94
+ function* filter(iterable, predicate) {
95
+ let index = 0;
96
+ for (const value of iterable) {
97
+ if (predicate(value, index++)) {
98
+ yield value;
99
+ }
100
+ }
101
+ }
102
+ Iterable.filter = filter;
103
+ function reject(iterable, predicate) {
104
+ return filter(iterable, not(predicate));
105
+ }
106
+ Iterable.reject = reject;
107
+ function find(iterable, predicate) {
108
+ let index = 0;
109
+ for (const value of iterable) {
110
+ if (predicate(value, index++)) {
111
+ return Option.of(value);
112
+ }
113
+ }
114
+ return None;
115
+ }
116
+ Iterable.find = find;
117
+ function findLast(iterable, predicate) {
118
+ let index = 0;
119
+ let result = None;
120
+ for (const value of iterable) {
121
+ if (predicate(value, index++)) {
122
+ result = Option.of(value);
123
+ }
124
+ }
125
+ return result;
126
+ }
127
+ Iterable.findLast = findLast;
128
+ function includes(iterable, value) {
129
+ return some(iterable, Predicate.equals(value));
130
+ }
131
+ Iterable.includes = includes;
132
+ function collect(iterable, mapper) {
133
+ return flatMap(iterable, mapper);
134
+ }
135
+ Iterable.collect = collect;
136
+ function collectFirst(iterable, mapper) {
137
+ return first(collect(iterable, mapper));
138
+ }
139
+ Iterable.collectFirst = collectFirst;
140
+ function some(iterable, predicate) {
141
+ let index = 0;
142
+ for (const value of iterable) {
143
+ if (predicate(value, index++)) {
144
+ return true;
145
+ }
146
+ }
147
+ return false;
148
+ }
149
+ Iterable.some = some;
150
+ function none(iterable, predicate) {
151
+ return every(iterable, not(predicate));
152
+ }
153
+ Iterable.none = none;
154
+ function every(iterable, predicate) {
155
+ let index = 0;
156
+ for (const value of iterable) {
157
+ if (!predicate(value, index++)) {
158
+ return false;
159
+ }
160
+ }
161
+ return true;
162
+ }
163
+ Iterable.every = every;
164
+ function count(iterable, predicate) {
165
+ return reduce(iterable, (count, value, index) => (predicate(value, index) ? count + 1 : count), 0);
166
+ }
167
+ Iterable.count = count;
168
+ function* distinct(iterable) {
169
+ const seen = [];
170
+ for (const value of iterable) {
171
+ if (seen.some(Predicate.equals(value))) {
172
+ continue;
173
+ }
174
+ seen.push(value);
175
+ yield value;
176
+ }
177
+ }
178
+ Iterable.distinct = distinct;
179
+ function get(iterable, index) {
180
+ return index < 0 ? None : first(skip(iterable, index));
181
+ }
182
+ Iterable.get = get;
183
+ function has(iterable, index) {
184
+ return index < 0 ? false : !isEmpty(skip(iterable, index));
185
+ }
186
+ Iterable.has = has;
187
+ function* set(iterable, index, value) {
188
+ const it = iterator(iterable);
189
+ while (index-- > 0) {
190
+ const next = it.next();
191
+ if (next.done === true) {
192
+ return;
193
+ }
194
+ yield next.value;
195
+ }
196
+ const next = it.next();
197
+ if (next.done === true) {
198
+ return;
199
+ }
200
+ yield value;
201
+ while (true) {
202
+ const next = it.next();
203
+ if (next.done === true) {
204
+ return;
205
+ }
206
+ yield next.value;
207
+ }
208
+ }
209
+ Iterable.set = set;
210
+ function* insert(iterable, index, value) {
211
+ const it = iterator(iterable);
212
+ while (index-- > 0) {
213
+ const next = it.next();
214
+ if (next.done === true) {
215
+ return;
216
+ }
217
+ yield next.value;
218
+ }
219
+ yield value;
220
+ while (true) {
221
+ const next = it.next();
222
+ if (next.done === true) {
223
+ return;
224
+ }
225
+ yield next.value;
226
+ }
227
+ }
228
+ Iterable.insert = insert;
229
+ function* append(iterable, value) {
230
+ yield* iterable;
231
+ yield value;
232
+ }
233
+ Iterable.append = append;
234
+ function* prepend(iterable, value) {
235
+ yield value;
236
+ yield* iterable;
237
+ }
238
+ Iterable.prepend = prepend;
239
+ function* concat(iterable, ...iterables) {
240
+ yield* iterable;
241
+ for (const iterable of iterables) {
242
+ yield* iterable;
243
+ }
244
+ }
245
+ Iterable.concat = concat;
246
+ function subtract(iterable, ...iterables) {
247
+ return reject(iterable, (value) => includes(flatten(iterables), value));
248
+ }
249
+ Iterable.subtract = subtract;
250
+ function intersect(iterable, ...iterables) {
251
+ return filter(iterable, (value) => includes(flatten(iterables), value));
252
+ }
253
+ Iterable.intersect = intersect;
254
+ function* zip(a, b) {
255
+ const itA = iterator(a);
256
+ const itB = iterator(b);
257
+ while (true) {
258
+ const a = itA.next();
259
+ const b = itB.next();
260
+ if (a.done === true || b.done === true) {
261
+ return;
262
+ }
263
+ yield [a.value, b.value];
264
+ }
265
+ }
266
+ Iterable.zip = zip;
267
+ function first(iterable) {
268
+ for (const value of iterable) {
269
+ return Option.of(value);
270
+ }
271
+ return None;
272
+ }
273
+ Iterable.first = first;
274
+ function last(iterable) {
275
+ let last = null;
276
+ for (const value of iterable) {
277
+ last = value;
278
+ }
279
+ return Option.from(last);
280
+ }
281
+ Iterable.last = last;
282
+ function* take(iterable, count) {
283
+ const it = iterator(iterable);
284
+ while (count-- > 0) {
285
+ const next = it.next();
286
+ if (next.done === true) {
287
+ return;
288
+ }
289
+ yield next.value;
290
+ }
291
+ }
292
+ Iterable.take = take;
293
+ function* takeWhile(iterable, predicate) {
294
+ let index = 0;
295
+ for (const value of iterable) {
296
+ if (predicate(value, index++)) {
297
+ yield value;
298
+ }
299
+ else {
300
+ break;
301
+ }
302
+ }
303
+ }
304
+ Iterable.takeWhile = takeWhile;
305
+ function takeUntil(iterable, predicate) {
306
+ return takeWhile(iterable, not(predicate));
307
+ }
308
+ Iterable.takeUntil = takeUntil;
309
+ function* takeLast(iterable, count = 1) {
310
+ if (count <= 0) {
311
+ return;
312
+ }
313
+ const last = [];
314
+ for (const value of iterable) {
315
+ last.push(value);
316
+ if (last.length > count) {
317
+ last.shift();
318
+ }
319
+ }
320
+ yield* last;
321
+ }
322
+ Iterable.takeLast = takeLast;
323
+ function* takeLastWhile(iterable, predicate) {
324
+ const values = [...iterable];
325
+ let last = values.length - 1;
326
+ while (last >= 0) {
327
+ if (predicate(values[last], last)) {
328
+ last--;
329
+ }
330
+ else {
331
+ break;
332
+ }
333
+ }
334
+ for (let i = last, n = values.length - 1; i < n; i++) {
335
+ yield values[i];
336
+ }
337
+ }
338
+ Iterable.takeLastWhile = takeLastWhile;
339
+ function takeLastUntil(iterable, predicate) {
340
+ return takeLastWhile(iterable, not(predicate));
341
+ }
342
+ Iterable.takeLastUntil = takeLastUntil;
343
+ function* skip(iterable, count) {
344
+ const it = iterator(iterable);
345
+ while (count-- > 0) {
346
+ const next = it.next();
347
+ if (next.done === true) {
348
+ return;
349
+ }
350
+ }
351
+ while (true) {
352
+ const next = it.next();
353
+ if (next.done === true) {
354
+ return;
355
+ }
356
+ yield next.value;
357
+ }
358
+ }
359
+ Iterable.skip = skip;
360
+ function* skipWhile(iterable, predicate) {
361
+ let index = 0;
362
+ let skipped = false;
363
+ for (const value of iterable) {
364
+ if (!skipped && predicate(value, index++)) {
365
+ continue;
366
+ }
367
+ else {
368
+ skipped = true;
369
+ yield value;
370
+ }
371
+ }
372
+ }
373
+ Iterable.skipWhile = skipWhile;
374
+ function skipUntil(iterable, predicate) {
375
+ return skipWhile(iterable, not(predicate));
376
+ }
377
+ Iterable.skipUntil = skipUntil;
378
+ function* skipLast(iterable, count = 1) {
379
+ const it = iterator(iterable);
380
+ const first = [];
381
+ while (count-- > 0) {
382
+ const next = it.next();
383
+ if (next.done === true) {
384
+ return;
385
+ }
386
+ first.push(next.value);
387
+ }
388
+ while (true) {
389
+ const next = it.next();
390
+ if (next.done === true) {
391
+ return;
392
+ }
393
+ first.push(next.value);
394
+ yield first.shift();
395
+ }
396
+ }
397
+ Iterable.skipLast = skipLast;
398
+ function* skipLastWhile(iterable, predicate) {
399
+ const values = [...iterable];
400
+ let last = values.length - 1;
401
+ while (last >= 0) {
402
+ if (predicate(values[last], last)) {
403
+ last--;
404
+ }
405
+ else {
406
+ break;
407
+ }
408
+ }
409
+ for (let i = 0, n = last; i < n; i++) {
410
+ yield values[i];
411
+ }
412
+ }
413
+ Iterable.skipLastWhile = skipLastWhile;
414
+ function skipLastUntil(iterable, predicate) {
415
+ return skipLastWhile(iterable, not(predicate));
416
+ }
417
+ Iterable.skipLastUntil = skipLastUntil;
418
+ function trim(iterable, predicate) {
419
+ return trimTrailing(trimLeading(iterable, predicate), predicate);
420
+ }
421
+ Iterable.trim = trim;
422
+ function trimLeading(iterable, predicate) {
423
+ return skipWhile(iterable, predicate);
424
+ }
425
+ Iterable.trimLeading = trimLeading;
426
+ function trimTrailing(iterable, predicate) {
427
+ return skipLastWhile(iterable, predicate);
428
+ }
429
+ Iterable.trimTrailing = trimTrailing;
430
+ function rest(iterable) {
431
+ return skip(iterable, 1);
432
+ }
433
+ Iterable.rest = rest;
434
+ function slice(iterable, start, end) {
435
+ iterable = skip(iterable, start);
436
+ if (end !== undefined) {
437
+ iterable = take(iterable, end - start);
438
+ }
439
+ return iterable;
440
+ }
441
+ Iterable.slice = slice;
442
+ function* reverse(iterable) {
443
+ const array = Array.from(iterable);
444
+ for (let i = array.length - 1; i >= 0; i--) {
445
+ yield array[i];
446
+ }
447
+ }
448
+ Iterable.reverse = reverse;
449
+ function join(iterable, separator) {
450
+ const it = iterator(iterable);
451
+ let next = it.next();
452
+ if (next.done === true) {
453
+ return "";
454
+ }
455
+ let result = `${next.value}`;
456
+ next = it.next();
457
+ while (next.done !== true) {
458
+ result += `${separator}${next.value}`;
459
+ next = it.next();
460
+ }
461
+ return result;
462
+ }
463
+ Iterable.join = join;
464
+ function sort(iterable) {
465
+ return sortWith(iterable, compareComparable);
466
+ }
467
+ Iterable.sort = sort;
468
+ function* sortWith(iterable, comparer) {
469
+ yield* [...iterable].sort(comparer);
470
+ }
471
+ Iterable.sortWith = sortWith;
472
+ function compare(a, b) {
473
+ return compareWith(a, b, compareComparable);
474
+ }
475
+ Iterable.compare = compare;
476
+ function compareWith(a, b, comparer) {
477
+ const itA = iterator(a);
478
+ const itB = iterator(b);
479
+ let index = 0;
480
+ while (true) {
481
+ const a = itA.next();
482
+ const b = itB.next();
483
+ if (a.done === true) {
484
+ return b.done === true ? Comparison.Equal : Comparison.Less;
485
+ }
486
+ if (b.done === true) {
487
+ return Comparison.Greater;
488
+ }
489
+ const result = comparer(a.value, b.value, index++);
490
+ if (result !== 0) {
491
+ return result;
492
+ }
493
+ }
494
+ }
495
+ Iterable.compareWith = compareWith;
496
+ function equals(a, b) {
497
+ const itA = iterator(a);
498
+ const itB = iterator(b);
499
+ while (true) {
500
+ const a = itA.next();
501
+ const b = itB.next();
502
+ if (a.done === true) {
503
+ return b.done === true;
504
+ }
505
+ if (b.done === true || !Equatable.equals(a.value, b.value)) {
506
+ return false;
507
+ }
508
+ }
509
+ }
510
+ Iterable.equals = equals;
511
+ function hash(iterable, hash) {
512
+ let size = 0;
513
+ for (const value of iterable) {
514
+ hash.writeUnknown(value);
515
+ size++;
516
+ }
517
+ hash.writeUint32(size);
518
+ }
519
+ Iterable.hash = hash;
520
+ function iterator(iterable) {
521
+ return iterable[Symbol.iterator]();
522
+ }
523
+ Iterable.iterator = iterator;
524
+ function groupBy(iterable, grouper) {
525
+ const groups = [];
526
+ let index = 0;
527
+ for (const value of iterable) {
528
+ const group = grouper(value, index++);
529
+ const existing = groups.find(([existing]) => Equatable.equals(group, existing));
530
+ if (existing === undefined) {
531
+ groups.push([group, [value]]);
532
+ }
533
+ else {
534
+ existing[1].push(value);
535
+ }
536
+ }
537
+ return groups;
538
+ }
539
+ Iterable.groupBy = groupBy;
540
+ function toJSON(iterable, options) {
541
+ return [...map(iterable, (value) => Serializable.toJSON(value, options))];
542
+ }
543
+ Iterable.toJSON = toJSON;
544
+ })(Iterable || (Iterable = {}));
545
+ //# sourceMappingURL=iterable.js.map
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "$schema": "http://json.schemastore.org/package",
3
+ "name": "@siteimprove/alfa-iterable",
4
+ "homepage": "https://alfa.siteimprove.com",
5
+ "version": "0.89.5",
6
+ "license": "MIT",
7
+ "description": "Functionality for working with structures that support iteration",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "github:Siteimprove/alfa",
11
+ "directory": "packages/alfa-iterable"
12
+ },
13
+ "bugs": "https://github.com/siteimprove/alfa/issues",
14
+ "engines": {
15
+ "node": ">=20.0.0"
16
+ },
17
+ "type": "module",
18
+ "main": "dist/index.js",
19
+ "types": "dist/index.d.ts",
20
+ "files": [
21
+ "dist/**/*.js",
22
+ "dist/**/*.d.ts"
23
+ ],
24
+ "dependencies": {
25
+ "@siteimprove/alfa-callback": "^0.89.5",
26
+ "@siteimprove/alfa-comparable": "^0.89.5",
27
+ "@siteimprove/alfa-equatable": "^0.89.5",
28
+ "@siteimprove/alfa-hash": "^0.89.5",
29
+ "@siteimprove/alfa-json": "^0.89.5",
30
+ "@siteimprove/alfa-mapper": "^0.89.5",
31
+ "@siteimprove/alfa-option": "^0.89.5",
32
+ "@siteimprove/alfa-predicate": "^0.89.5",
33
+ "@siteimprove/alfa-reducer": "^0.89.5",
34
+ "@siteimprove/alfa-refinement": "^0.89.5"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public",
38
+ "registry": "https://npm.pkg.github.com/"
39
+ }
40
+ }