@byloth/core 1.2.0-rc.3 → 1.2.1-rc.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.
@@ -1,7 +1,18 @@
1
- import DeferredPromise from "./deferred-promise.js";
2
1
  import Exception from "./exception.js";
3
- import JsonStorage from "./json-storage.js";
4
2
  import SmartIterator from "./smart-iterator.js";
3
+ import Aggregator, { AggregatedIterator, ReducedIterator } from "./aggregators/index.js";
4
+
5
+ import DeferredPromise from "./deferred-promise.js";
6
+ import JsonStorage from "./json-storage.js";
5
7
  import Subscribers from "./subscribers.js";
6
8
 
7
- export { DeferredPromise, Exception, JsonStorage, SmartIterator, Subscribers };
9
+ export {
10
+ AggregatedIterator,
11
+ Aggregator,
12
+ DeferredPromise,
13
+ Exception,
14
+ JsonStorage,
15
+ ReducedIterator,
16
+ SmartIterator,
17
+ Subscribers
18
+ };
@@ -10,26 +10,27 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
10
10
  public constructor(iterable: Iterable<T>);
11
11
  public constructor(iterator: Iterator<T, R, N>);
12
12
  public constructor(generatorFn: GeneratorFunction<T, R, N>);
13
- public constructor(iterable: Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>)
13
+ public constructor(argument: Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>);
14
+ public constructor(argument: Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>)
14
15
  {
15
- if (iterable instanceof Function)
16
+ if (argument instanceof Function)
16
17
  {
17
- this._iterator = iterable();
18
+ this._iterator = argument();
18
19
  }
19
- else if (Symbol.iterator in iterable)
20
+ else if (Symbol.iterator in argument)
20
21
  {
21
- this._iterator = iterable[Symbol.iterator]() as Iterator<T, R, N>;
22
+ this._iterator = argument[Symbol.iterator]() as Iterator<T, R, N>;
22
23
  }
23
24
  else
24
25
  {
25
- this._iterator = iterable;
26
+ this._iterator = argument;
26
27
  }
27
28
 
28
29
  if (this._iterator.return) { this.return = (value?: R) => this._iterator.return!(value); }
29
30
  if (this._iterator.throw) { this.throw = (error?: unknown) => this._iterator.throw!(error); }
30
31
  }
31
32
 
32
- public every(iteratee: Iteratee<T, boolean>): boolean
33
+ public every(predicate: Iteratee<T, boolean>): boolean
33
34
  {
34
35
  let index = 0;
35
36
 
@@ -39,12 +40,12 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
39
40
  const result = this._iterator.next();
40
41
 
41
42
  if (result.done) { return true; }
42
- if (!(iteratee(result.value, index))) { return false; }
43
+ if (!(predicate(result.value, index))) { return false; }
43
44
 
44
45
  index += 1;
45
46
  }
46
47
  }
47
- public some(iteratee: Iteratee<T, boolean>): boolean
48
+ public some(predicate: Iteratee<T, boolean>): boolean
48
49
  {
49
50
  let index = 0;
50
51
 
@@ -54,56 +55,62 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
54
55
  const result = this._iterator.next();
55
56
 
56
57
  if (result.done) { return false; }
57
- if (iteratee(result.value, index)) { return true; }
58
+ if (predicate(result.value, index)) { return true; }
58
59
 
59
60
  index += 1;
60
61
  }
61
62
  }
62
63
 
63
- public filter(iteratee: Iteratee<T, boolean>): SmartIterator<T, R, N>
64
+ public filter(predicate: Iteratee<T, boolean>): SmartIterator<T, R>
64
65
  {
65
- let index = 0;
66
66
  const iterator = this._iterator;
67
67
 
68
- return new SmartIterator<T, R, N>({
69
- *[Symbol.iterator]()
68
+ return new SmartIterator<T, R>(function* ()
69
+ {
70
+ let index = 0;
71
+
72
+ while (true)
70
73
  {
71
- while (true)
72
- {
73
- const result = iterator.next();
74
+ const result = iterator.next();
74
75
 
75
- if (result.done) { return result.value; }
76
- if (iteratee(result.value, index)) { yield result.value; }
76
+ if (result.done) { return result.value; }
77
+ if (predicate(result.value, index)) { yield result.value; }
77
78
 
78
- index += 1;
79
- }
79
+ index += 1;
80
80
  }
81
81
  });
82
82
  }
83
83
  public map<V>(iteratee: Iteratee<T, V>): SmartIterator<V, R>
84
84
  {
85
- let index = 0;
86
85
  const iterator = this._iterator;
87
86
 
88
- return new SmartIterator<V, R>({
89
- *[Symbol.iterator]()
87
+ return new SmartIterator<V, R>(function* ()
88
+ {
89
+ let index = 0;
90
+
91
+ while (true)
90
92
  {
91
- while (true)
92
- {
93
- const result = iterator.next();
94
- if (result.done) { return result.value; }
93
+ const result = iterator.next();
94
+ if (result.done) { return result.value; }
95
95
 
96
- yield iteratee(result.value, index);
96
+ yield iteratee(result.value, index);
97
97
 
98
- index += 1;
99
- }
98
+ index += 1;
100
99
  }
101
100
  });
102
101
  }
103
- public reduce<A>(reducer: Reducer<T, A>, initialValue: A): A
102
+ public reduce<A>(reducer: Reducer<T, A>, initialValue?: A): A
104
103
  {
105
104
  let index = 0;
106
105
  let accumulator = initialValue;
106
+ if (accumulator === undefined)
107
+ {
108
+ const result = this._iterator.next();
109
+ if (result.done) { throw new TypeError("Reduce of empty iterator with no initial value"); }
110
+
111
+ accumulator = (result.value as unknown) as A;
112
+ index += 1;
113
+ }
107
114
 
108
115
  // eslint-disable-next-line no-constant-condition
109
116
  while (true)
@@ -117,6 +124,45 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
117
124
  }
118
125
  }
119
126
 
127
+ public enumerate(): SmartIterator<[number, T], R>
128
+ {
129
+ return this.map((value, index) => [index, value]);
130
+ }
131
+ public unique(): SmartIterator<T, R>
132
+ {
133
+ const iterator = this._iterator;
134
+
135
+ return new SmartIterator<T, R>(function* ()
136
+ {
137
+ const values = new Set<T>();
138
+
139
+ while (true)
140
+ {
141
+ const result = iterator.next();
142
+
143
+ if (result.done) { return result.value; }
144
+ if (values.has(result.value)) { continue; }
145
+
146
+ values.add(result.value);
147
+
148
+ yield result.value;
149
+ }
150
+ });
151
+ }
152
+
153
+ public count(): number
154
+ {
155
+ let index = 0;
156
+
157
+ // eslint-disable-next-line no-constant-condition
158
+ while (true)
159
+ {
160
+ const result = this._iterator.next();
161
+ if (result.done) { return index; }
162
+
163
+ index += 1;
164
+ }
165
+ }
120
166
  public forEach(iteratee: Iteratee<T>): void
121
167
  {
122
168
  let index = 0;
@@ -140,7 +186,7 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
140
186
 
141
187
  public toArray(): T[]
142
188
  {
143
- return [...this];
189
+ return Array.from(this as Iterable<T>);
144
190
  }
145
191
 
146
192
  public [Symbol.iterator](): SmartIterator<T, R, N> { return this; }
package/src/types.ts CHANGED
@@ -1,4 +1,5 @@
1
- export type Constructor<T extends object, P extends unknown[] = []> = new (...args: P) => T;
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ export type Constructor<T extends object, P extends unknown[] = any[]> = new (...args: P) => T;
2
3
 
3
4
  export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R, N>;
4
5
  export type Iteratee<T, R = void> = (value: T, index: number) => R;
@@ -10,6 +10,21 @@ export function count<T>(elements: Iterable<T>): number
10
10
  return _count;
11
11
  }
12
12
 
13
+ export function enumerate<T>(elements: Iterable<T>): SmartIterator<[number, T]>
14
+ {
15
+ return new SmartIterator<[number, T]>(function* ()
16
+ {
17
+ let index = 0;
18
+
19
+ for (const element of elements)
20
+ {
21
+ yield [index, element];
22
+
23
+ index += 1;
24
+ }
25
+ });
26
+ }
27
+
13
28
  export function range(end: number): SmartIterator<number>;
14
29
  export function range(start: number, end: number): SmartIterator<number>;
15
30
  export function range(start: number, end: number, step: number): SmartIterator<number>;
@@ -31,7 +46,7 @@ export function range(start: number, end?: number, step = 1): SmartIterator<numb
31
46
 
32
47
  export function shuffle<T>(iterable: Iterable<T>): T[]
33
48
  {
34
- const array = [...iterable];
49
+ const array = Array.from(iterable);
35
50
 
36
51
  for (let index = array.length - 1; index > 0; index -= 1)
37
52
  {
@@ -47,13 +62,13 @@ export function unique<T>(elements: Iterable<T>): SmartIterator<T>
47
62
  {
48
63
  return new SmartIterator<T>(function* ()
49
64
  {
50
- const seen = new Set<T>();
65
+ const values = new Set<T>();
51
66
 
52
67
  for (const element of elements)
53
68
  {
54
- if (seen.has(element)) { continue; }
69
+ if (values.has(element)) { continue; }
55
70
 
56
- seen.add(element);
71
+ values.add(element);
57
72
 
58
73
  yield element;
59
74
  }