@byloth/core 1.5.0-rc.6 → 1.5.0-rc.8

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,7 @@
1
1
  import AggregatedIterator from "./aggregated-iterator.js";
2
2
 
3
3
  import { SmartIterator } from "../iterators/index.js";
4
- import type { GeneratorFunction, Iteratee, TypeGuardIteratee } from "../iterators/types.js";
4
+ import type { GeneratorFunction, Iteratee, Iterables, TypeGuardIteratee } from "../iterators/types.js";
5
5
 
6
6
  export default class Aggregator<T>
7
7
  {
@@ -10,8 +10,8 @@ export default class Aggregator<T>
10
10
  public constructor(iterable: Iterable<T>);
11
11
  public constructor(iterator: Iterator<T>);
12
12
  public constructor(generatorFn: GeneratorFunction<T>);
13
- public constructor(argument: Iterable<T> | Iterator<T> | GeneratorFunction<T>);
14
- public constructor(argument: Iterable<T> | Iterator<T> | GeneratorFunction<T>)
13
+ public constructor(argument: Iterables<T>);
14
+ public constructor(argument: Iterables<T>)
15
15
  {
16
16
  this._elements = new SmartIterator(argument);
17
17
  }
@@ -1,17 +1,27 @@
1
1
  import AggregatedAsyncIterator from "./aggregated-async-iterator.js";
2
2
 
3
3
  import { SmartAsyncIterator } from "../iterators/index.js";
4
- import type { AsyncGeneratorFunction, MaybeAsyncIteratee, MaybeAsyncTypeGuardIteratee } from "../iterators/types.js";
4
+ import type {
5
+ AsyncGeneratorFunction,
6
+ GeneratorFunction,
7
+ MaybeAsyncIterables,
8
+ MaybeAsyncIteratee,
9
+ MaybeAsyncTypeGuardIteratee
10
+
11
+ } from "../iterators/types.js";
5
12
 
6
13
  export default class AsyncAggregator<T>
7
14
  {
8
15
  protected _elements: SmartAsyncIterator<T>;
9
16
 
17
+ public constructor(iterable: Iterable<T>);
10
18
  public constructor(iterable: AsyncIterable<T>);
19
+ public constructor(iterator: Iterator<T>);
11
20
  public constructor(iterator: AsyncIterator<T>);
21
+ public constructor(generatorFn: GeneratorFunction<T>);
12
22
  public constructor(generatorFn: AsyncGeneratorFunction<T>);
13
- public constructor(argument: AsyncIterable<T> | AsyncIterator<T> | AsyncGeneratorFunction<T>);
14
- public constructor(argument: AsyncIterable<T> | AsyncIterator<T> | AsyncGeneratorFunction<T>)
23
+ public constructor(argument: MaybeAsyncIterables<T>);
24
+ public constructor(argument: MaybeAsyncIterables<T>)
15
25
  {
16
26
  this._elements = new SmartAsyncIterator(argument);
17
27
  }
@@ -1,7 +1,8 @@
1
+ import { TypeException } from "../exceptions/index.js";
1
2
  import { SmartIterator } from "../iterators/index.js";
2
3
  import type { GeneratorFunction } from "../iterators/types.js";
3
4
 
4
- import type { KeyIteratee, KeyTypeGuardIteratee } from "./types.js";
5
+ import type { KeyIteratee, KeyReducer, KeyTypeGuardIteratee } from "./types.js";
5
6
 
6
7
  export default class ReducedIterator<K extends PropertyKey, T>
7
8
  {
@@ -45,6 +46,38 @@ export default class ReducedIterator<K extends PropertyKey, T>
45
46
  }
46
47
  });
47
48
  }
49
+ public reduce(reducer: KeyReducer<K, T, T>): T;
50
+ public reduce<A>(reducer: KeyReducer<K, T, A>, initialValue: A): A;
51
+ public reduce<A>(reducer: KeyReducer<K, T, A>, initialValue?: A): A
52
+ {
53
+ let index = 0;
54
+ let accumulator: A;
55
+
56
+ if (initialValue !== undefined)
57
+ {
58
+ accumulator = initialValue;
59
+ }
60
+ else
61
+ {
62
+ const firstElement = this._elements.next();
63
+ if (firstElement.done)
64
+ {
65
+ throw new TypeException("Reduce of empty iterator with no initial value");
66
+ }
67
+
68
+ index += 1;
69
+ accumulator = (firstElement.value[1] as unknown) as A;
70
+ }
71
+
72
+ for (const [key, element] of this._elements)
73
+ {
74
+ accumulator = reducer(key, accumulator, element, index);
75
+
76
+ index += 1;
77
+ }
78
+
79
+ return accumulator;
80
+ }
48
81
 
49
82
  public keys(): SmartIterator<K>
50
83
  {
@@ -7,7 +7,7 @@ export type MaybeAsyncKeyIteratee<K extends PropertyKey, T, R = void> = (key: K,
7
7
  export type KeyTypeGuardIteratee<K extends PropertyKey, T, R extends T> =
8
8
  (key: K, value: T, index: number) => value is R;
9
9
  export type MaybeAsyncKeyTypeGuardIteratee<K extends PropertyKey, T, R extends T> =
10
- (key: K, value: MaybePromise<T>, index: number) => value is MaybePromise<R>;
10
+ (key: K, value: MaybePromise<T>, index: number) => value is Awaited<R>;
11
11
 
12
12
  export type KeyReducer<K extends PropertyKey, T, A> = (key: K, accumulator: A, value: T, index: number) => A;
13
13
  export type MaybeAsyncKeyReducer<K extends PropertyKey, T, A> =
@@ -41,3 +41,34 @@ export default class Exception extends Error
41
41
 
42
42
  public get [Symbol.toStringTag]() { return "Exception"; }
43
43
  }
44
+
45
+ export class FatalErrorException extends Exception
46
+ {
47
+ public constructor(message?: string, cause?: unknown, name = "FatalErrorException")
48
+ {
49
+ if (message === undefined)
50
+ {
51
+ message = "The routine has encountered an unrecoverable error and cannot continue as expected. " +
52
+ "Please, refresh the page and try again. If the problem persists, contact the support team.";
53
+ }
54
+
55
+ super(message, cause, name);
56
+ }
57
+
58
+ public get [Symbol.toStringTag]() { return "FatalErrorException"; }
59
+ }
60
+
61
+ export class NotImplementedException extends Exception
62
+ {
63
+ public constructor(message?: string, cause?: unknown, name = "NotImplementedException")
64
+ {
65
+ if (message === undefined)
66
+ {
67
+ message = "This feature is not implemented yet. Please, try again later.";
68
+ }
69
+
70
+ super(message, cause, name);
71
+ }
72
+
73
+ public get [Symbol.toStringTag]() { return "NotImplementedException"; }
74
+ }
@@ -1,5 +1,32 @@
1
1
  import Exception from "./core.js";
2
2
 
3
+ export class FileNotFoundException extends Exception
4
+ {
5
+ public constructor(message: string, cause?: unknown, name = "FileNotFoundException")
6
+ {
7
+ super(message, cause, name);
8
+ }
9
+
10
+ public get [Symbol.toStringTag]() { return "FileNotFoundException"; }
11
+ }
12
+ export class NetworkException extends Exception
13
+ {
14
+ public constructor(message: string, cause?: unknown, name = "NetworkException")
15
+ {
16
+ super(message, cause, name);
17
+ }
18
+
19
+ public get [Symbol.toStringTag]() { return "NetworkException"; }
20
+ }
21
+ export class PermissionException extends Exception
22
+ {
23
+ public constructor(message: string, cause?: unknown, name = "PermissionException")
24
+ {
25
+ super(message, cause, name);
26
+ }
27
+
28
+ public get [Symbol.toStringTag]() { return "PermissionException"; }
29
+ }
3
30
  export class ReferenceException extends Exception
4
31
  {
5
32
  public constructor(message: string, cause?: unknown, name = "ReferenceException")
@@ -9,6 +36,15 @@ export class ReferenceException extends Exception
9
36
 
10
37
  public get [Symbol.toStringTag]() { return "ReferenceException"; }
11
38
  }
39
+ export class RuntimeException extends Exception
40
+ {
41
+ public constructor(message: string, cause?: unknown, name = "RuntimeException")
42
+ {
43
+ super(message, cause, name);
44
+ }
45
+
46
+ public get [Symbol.toStringTag]() { return "RuntimeException"; }
47
+ }
12
48
  export class TimeoutException extends Exception
13
49
  {
14
50
  public constructor(message: string, cause?: unknown, name = "TimeoutException")
@@ -18,6 +54,15 @@ export class TimeoutException extends Exception
18
54
 
19
55
  public get [Symbol.toStringTag]() { return "TimeoutException"; }
20
56
  }
57
+ export class TypeException extends Exception
58
+ {
59
+ public constructor(message: string, cause?: unknown, name = "TypeException")
60
+ {
61
+ super(message, cause, name);
62
+ }
63
+
64
+ public get [Symbol.toStringTag]() { return "TypeException"; }
65
+ }
21
66
  export class ValueException extends Exception
22
67
  {
23
68
  public constructor(message: string, cause?: unknown, name = "ValueException")
@@ -29,3 +74,4 @@ export class ValueException extends Exception
29
74
  }
30
75
 
31
76
  export { Exception };
77
+ export { FatalErrorException, NotImplementedException } from "./core.js";
@@ -7,7 +7,21 @@ export {
7
7
 
8
8
  } from "./aggregators/index.js";
9
9
 
10
- export { Exception, ReferenceException, TimeoutException, ValueException } from "./exceptions/index.js";
10
+ export {
11
+ Exception,
12
+ FatalErrorException,
13
+ NotImplementedException,
14
+ FileNotFoundException,
15
+ NetworkException,
16
+ PermissionException,
17
+ ReferenceException,
18
+ RuntimeException,
19
+ TimeoutException,
20
+ TypeException,
21
+ ValueException
22
+
23
+ } from "./exceptions/index.js";
24
+
11
25
  export { SmartIterator, SmartAsyncIterator } from "./iterators/index.js";
12
26
 
13
27
  import JsonStorage from "./json-storage.js";
@@ -1,4 +1,12 @@
1
- import type { AsyncGeneratorFunction, MaybeAsyncIteratee, MaybeAsyncReducer, TypeGuardIteratee } from "./types.js";
1
+ import type {
2
+ AsyncGeneratorFunction,
3
+ GeneratorFunction,
4
+ MaybeAsyncIteratee,
5
+ MaybeAsyncReducer,
6
+ MaybeAsyncIterables,
7
+ MaybeAsyncTypeGuardIteratee
8
+
9
+ } from "./types.js";
2
10
 
3
11
  export default class SmartAsyncIterator<T, R = void, N = undefined> implements AsyncIterator<T, R, N>
4
12
  {
@@ -7,23 +15,73 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
7
15
  public return?: (value?: R) => Promise<IteratorResult<T, R>>;
8
16
  public throw?: (error?: unknown) => Promise<IteratorResult<T, R>>;
9
17
 
18
+ public constructor(iterable: Iterable<T>);
10
19
  public constructor(iterable: AsyncIterable<T>);
20
+ public constructor(iterator: Iterator<T, R, N>);
11
21
  public constructor(iterator: AsyncIterator<T, R, N>);
12
- public constructor(generatorFn: () => AsyncGenerator<T, R, N>);
13
- public constructor(argument: AsyncIterable<T> | AsyncIterator<T, R, N> | AsyncGeneratorFunction<T, R, N>);
14
- public constructor(argument: AsyncIterable<T> | AsyncIterator<T, R, N> | AsyncGeneratorFunction<T, R, N>)
22
+ public constructor(generatorFn: GeneratorFunction<T, R, N>);
23
+ public constructor(generatorFn: AsyncGeneratorFunction<T, R, N>);
24
+ public constructor(argument: MaybeAsyncIterables<T, R, N>);
25
+ public constructor(argument: MaybeAsyncIterables<T, R, N>)
15
26
  {
16
27
  if (argument instanceof Function)
17
28
  {
18
- this._iterator = argument();
29
+ const generator = argument();
30
+ if (Symbol.asyncIterator in generator)
31
+ {
32
+ this._iterator = generator;
33
+ }
34
+ else
35
+ {
36
+ this._iterator = (async function* ()
37
+ {
38
+ let next: [] | [N] = [];
39
+
40
+ while (true)
41
+ {
42
+ const result = generator.next(...next);
43
+ if (result.done) { return result.value; }
44
+
45
+ next = [yield result.value];
46
+ }
47
+
48
+ })();
49
+ }
19
50
  }
20
51
  else if (Symbol.asyncIterator in argument)
21
52
  {
22
53
  this._iterator = argument[Symbol.asyncIterator]() as AsyncIterator<T, R, N>;
23
54
  }
55
+ else if (Symbol.iterator in argument)
56
+ {
57
+ const iterator = argument[Symbol.iterator]();
58
+ this._iterator = (async function* ()
59
+ {
60
+ while (true)
61
+ {
62
+ const result = iterator.next();
63
+ if (result.done) { return result.value; }
64
+
65
+ yield result.value;
66
+ }
67
+
68
+ })();
69
+ }
24
70
  else
25
71
  {
26
- this._iterator = argument;
72
+ this._iterator = (async function* ()
73
+ {
74
+ let next: [] | [N] = [];
75
+
76
+ while (true)
77
+ {
78
+ const result: IteratorResult<T, R> = await argument.next(...next);
79
+ if (result.done) { return result.value; }
80
+
81
+ next = [yield result.value];
82
+ }
83
+
84
+ })();
27
85
  }
28
86
 
29
87
  if (this._iterator.return) { this.return = (value?: R) => this._iterator.return!(value); }
@@ -62,7 +120,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
62
120
  }
63
121
 
64
122
  public filter(predicate: MaybeAsyncIteratee<T, boolean>): SmartAsyncIterator<T, R>;
65
- public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): SmartAsyncIterator<T, S>;
123
+ public filter<S extends T>(predicate: MaybeAsyncTypeGuardIteratee<T, S>): SmartAsyncIterator<S, R>;
66
124
  public filter(predicate: MaybeAsyncIteratee<T, boolean>): SmartAsyncIterator<T, R>
67
125
  {
68
126
  const iterator = this._iterator;
@@ -1,4 +1,4 @@
1
- import type { GeneratorFunction, Iteratee, TypeGuardIteratee, Reducer } from "./types.js";
1
+ import type { GeneratorFunction, Iteratee, TypeGuardIteratee, Reducer, Iterables } from "./types.js";
2
2
 
3
3
  export default class SmartIterator<T, R = void, N = undefined> implements Iterator<T, R, N>
4
4
  {
@@ -10,8 +10,8 @@ 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(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>)
13
+ public constructor(argument: Iterables<T, R, N>);
14
+ public constructor(argument: Iterables<T, R, N>)
15
15
  {
16
16
  if (argument instanceof Function)
17
17
  {
@@ -62,7 +62,7 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
62
62
  }
63
63
 
64
64
  public filter(predicate: Iteratee<T, boolean>): SmartIterator<T, R>;
65
- public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): SmartIterator<T, S>;
65
+ public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): SmartIterator<S, R>;
66
66
  public filter(predicate: Iteratee<T, boolean>): SmartIterator<T, R>
67
67
  {
68
68
  const iterator = this._iterator;
@@ -12,3 +12,9 @@ export type MaybeAsyncTypeGuardIteratee<T, R extends T> = (value: MaybePromise<T
12
12
 
13
13
  export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
14
14
  export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number) => MaybePromise<A>;
15
+
16
+ export type Iterables<T, R = void, N = undefined> = Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>;
17
+ export type AsyncIterables<T, R = void, N = undefined> =
18
+ AsyncIterable<T> | AsyncIterator<T, R, N> | AsyncGeneratorFunction<T, R, N>;
19
+
20
+ export type MaybeAsyncIterables<T, R = void, N = undefined> = Iterables<T, R, N> | AsyncIterables<T, R, N>;