@byloth/core 1.5.3 → 2.0.0-rc.10
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/dist/core.js +1468 -826
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +3 -3
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +8 -10
- package/src/helpers.ts +10 -0
- package/src/index.ts +33 -16
- package/src/models/aggregators/aggregated-async-iterator.ts +129 -81
- package/src/models/aggregators/aggregated-iterator.ts +128 -82
- package/src/models/aggregators/index.ts +1 -3
- package/src/models/aggregators/reduced-iterator.ts +119 -31
- package/src/models/aggregators/types.ts +15 -10
- package/src/models/callbacks/callable-object.ts +23 -0
- package/src/models/callbacks/index.ts +5 -0
- package/src/models/callbacks/publisher.ts +45 -0
- package/src/models/callbacks/switchable-callback.ts +108 -0
- package/src/models/callbacks/types.ts +1 -0
- package/src/models/exceptions/core.ts +6 -7
- package/src/models/exceptions/index.ts +50 -10
- package/src/models/game-loop.ts +83 -0
- package/src/models/index.ts +10 -7
- package/src/models/iterators/smart-async-iterator.ts +112 -24
- package/src/models/iterators/smart-iterator.ts +108 -13
- package/src/models/iterators/types.ts +17 -7
- package/src/models/json/index.ts +3 -0
- package/src/models/{json-storage.ts → json/json-storage.ts} +40 -28
- package/src/models/json/types.ts +5 -0
- package/src/models/promises/deferred-promise.ts +1 -1
- package/src/models/promises/index.ts +3 -1
- package/src/models/promises/long-running-task.ts +294 -0
- package/src/models/promises/smart-promise.ts +6 -1
- package/src/models/promises/thenable.ts +97 -0
- package/src/models/promises/timed-promise.ts +1 -1
- package/src/models/promises/types.ts +2 -0
- package/src/models/timers/clock.ts +69 -0
- package/src/models/timers/countdown.ts +117 -0
- package/src/models/timers/index.ts +4 -0
- package/src/models/types.ts +20 -9
- package/src/utils/async.ts +9 -4
- package/src/utils/date.ts +7 -4
- package/src/utils/index.ts +2 -2
- package/src/utils/random.ts +4 -3
- package/src/models/aggregators/aggregator.ts +0 -46
- package/src/models/aggregators/async-aggregator.ts +0 -56
- package/src/models/subscribers.ts +0 -35
package/src/utils/date.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
|
|
1
2
|
import { SmartIterator } from "../models/index.js";
|
|
2
3
|
|
|
3
|
-
export enum
|
|
4
|
+
export enum TimeUnit
|
|
4
5
|
{
|
|
6
|
+
/* eslint-disable @typescript-eslint/prefer-literal-enum-member */
|
|
7
|
+
|
|
5
8
|
Millisecond = 1,
|
|
6
9
|
Second = 1000,
|
|
7
10
|
Minute = 60 * Second,
|
|
@@ -12,7 +15,7 @@ export enum DateUnit
|
|
|
12
15
|
Year = 365 * Day
|
|
13
16
|
}
|
|
14
17
|
|
|
15
|
-
export function dateDifference(start: string | Date, end: string | Date, unit =
|
|
18
|
+
export function dateDifference(start: string | Date, end: string | Date, unit = TimeUnit.Day): number
|
|
16
19
|
{
|
|
17
20
|
start = new Date(start);
|
|
18
21
|
end = new Date(end);
|
|
@@ -20,7 +23,7 @@ export function dateDifference(start: string | Date, end: string | Date, unit =
|
|
|
20
23
|
return Math.floor((end.getTime() - start.getTime()) / unit);
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
export function dateRange(start: string | Date, end: string | Date, offset =
|
|
26
|
+
export function dateRange(start: string | Date, end: string | Date, offset = TimeUnit.Day): SmartIterator<Date>
|
|
24
27
|
{
|
|
25
28
|
start = new Date(start);
|
|
26
29
|
end = new Date(end);
|
|
@@ -39,7 +42,7 @@ export function dateRange(start: string | Date, end: string | Date, offset = Dat
|
|
|
39
42
|
});
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
export function dateRound(date: string | Date, unit =
|
|
45
|
+
export function dateRound(date: string | Date, unit = TimeUnit.Day): Date
|
|
43
46
|
{
|
|
44
47
|
date = new Date(date);
|
|
45
48
|
|
package/src/utils/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Random from "./random.js";
|
|
2
2
|
|
|
3
|
-
export { delay, nextAnimationFrame } from "./async.js";
|
|
4
|
-
export { dateDifference, dateRange, dateRound,
|
|
3
|
+
export { delay, nextAnimationFrame, yieldToEventLoop } from "./async.js";
|
|
4
|
+
export { dateDifference, dateRange, dateRound, TimeUnit } from "./date.js";
|
|
5
5
|
export { loadScript } from "./dom.js";
|
|
6
6
|
export { chain, count, enumerate, range, shuffle, unique, zip } from "./iterator.js";
|
|
7
7
|
export { average, hash, sum } from "./math.js";
|
package/src/utils/random.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { ValueException } from "../models/index.js";
|
|
|
2
2
|
|
|
3
3
|
export default class Random
|
|
4
4
|
{
|
|
5
|
-
public static Boolean(ratio
|
|
5
|
+
public static Boolean(ratio = 0.5): boolean
|
|
6
6
|
{
|
|
7
7
|
return (Math.random() < ratio);
|
|
8
8
|
}
|
|
@@ -38,6 +38,7 @@ export default class Random
|
|
|
38
38
|
return elements[Random.Index(elements)];
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
private constructor() { /* ... */ }
|
|
42
|
+
|
|
43
|
+
public readonly [Symbol.toStringTag]: string = "Random";
|
|
43
44
|
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import AggregatedIterator from "./aggregated-iterator.js";
|
|
2
|
-
|
|
3
|
-
import { SmartIterator } from "../iterators/index.js";
|
|
4
|
-
import type { GeneratorFunction, Iteratee, IterLike, TypeGuardIteratee } from "../iterators/types.js";
|
|
5
|
-
|
|
6
|
-
export default class Aggregator<T>
|
|
7
|
-
{
|
|
8
|
-
protected _elements: SmartIterator<T>;
|
|
9
|
-
|
|
10
|
-
public constructor(iterable: Iterable<T>);
|
|
11
|
-
public constructor(iterator: Iterator<T>);
|
|
12
|
-
public constructor(generatorFn: GeneratorFunction<T>);
|
|
13
|
-
public constructor(argument: IterLike<T>);
|
|
14
|
-
public constructor(argument: IterLike<T>)
|
|
15
|
-
{
|
|
16
|
-
this._elements = new SmartIterator(argument);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public filter(predicate: Iteratee<T, boolean>): Aggregator<T>;
|
|
20
|
-
public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): Aggregator<S>;
|
|
21
|
-
public filter(predicate: Iteratee<T, boolean>): Aggregator<T>
|
|
22
|
-
{
|
|
23
|
-
return new Aggregator(this._elements.filter(predicate));
|
|
24
|
-
}
|
|
25
|
-
public map<V>(iteratee: Iteratee<T, V>): Aggregator<V>
|
|
26
|
-
{
|
|
27
|
-
return new Aggregator(this._elements.map(iteratee));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public unique(): Aggregator<T>
|
|
31
|
-
{
|
|
32
|
-
return new Aggregator(this._elements.unique());
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public groupBy<K extends PropertyKey>(iteratee: Iteratee<T, K>): AggregatedIterator<K, T>
|
|
36
|
-
{
|
|
37
|
-
return new AggregatedIterator(this._elements.map((element, index) =>
|
|
38
|
-
{
|
|
39
|
-
const key = iteratee(element, index);
|
|
40
|
-
|
|
41
|
-
return [key, element] as [K, T];
|
|
42
|
-
}));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public get [Symbol.toStringTag]() { return "Aggregator"; }
|
|
46
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import AggregatedAsyncIterator from "./aggregated-async-iterator.js";
|
|
2
|
-
|
|
3
|
-
import { SmartAsyncIterator } from "../iterators/index.js";
|
|
4
|
-
import type {
|
|
5
|
-
AsyncGeneratorFunction,
|
|
6
|
-
GeneratorFunction,
|
|
7
|
-
MaybeAsyncIterLike,
|
|
8
|
-
MaybeAsyncIteratee,
|
|
9
|
-
MaybeAsyncTypeGuardIteratee
|
|
10
|
-
|
|
11
|
-
} from "../iterators/types.js";
|
|
12
|
-
|
|
13
|
-
export default class AsyncAggregator<T>
|
|
14
|
-
{
|
|
15
|
-
protected _elements: SmartAsyncIterator<T>;
|
|
16
|
-
|
|
17
|
-
public constructor(iterable: Iterable<T>);
|
|
18
|
-
public constructor(iterable: AsyncIterable<T>);
|
|
19
|
-
public constructor(iterator: Iterator<T>);
|
|
20
|
-
public constructor(iterator: AsyncIterator<T>);
|
|
21
|
-
public constructor(generatorFn: GeneratorFunction<T>);
|
|
22
|
-
public constructor(generatorFn: AsyncGeneratorFunction<T>);
|
|
23
|
-
public constructor(argument: MaybeAsyncIterLike<T>);
|
|
24
|
-
public constructor(argument: MaybeAsyncIterLike<T>)
|
|
25
|
-
{
|
|
26
|
-
this._elements = new SmartAsyncIterator(argument);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public filter(predicate: MaybeAsyncIteratee<T, boolean>): AsyncAggregator<T>;
|
|
30
|
-
public filter<S extends T>(predicate: MaybeAsyncTypeGuardIteratee<T, S>): AsyncAggregator<S>;
|
|
31
|
-
public filter(predicate: MaybeAsyncIteratee<T, boolean>): AsyncAggregator<T>
|
|
32
|
-
{
|
|
33
|
-
return new AsyncAggregator(this._elements.filter(predicate));
|
|
34
|
-
}
|
|
35
|
-
public map<V>(iteratee: MaybeAsyncIteratee<T, V>): AsyncAggregator<V>
|
|
36
|
-
{
|
|
37
|
-
return new AsyncAggregator(this._elements.map(iteratee));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public unique(): AsyncAggregator<T>
|
|
41
|
-
{
|
|
42
|
-
return new AsyncAggregator(this._elements.unique());
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public groupBy<K extends PropertyKey>(iteratee: MaybeAsyncIteratee<T, K>): AggregatedAsyncIterator<K, T>
|
|
46
|
-
{
|
|
47
|
-
return new AggregatedAsyncIterator(this._elements.map(async (element, index) =>
|
|
48
|
-
{
|
|
49
|
-
const key = await iteratee(element, index);
|
|
50
|
-
|
|
51
|
-
return [key, element] as [K, T];
|
|
52
|
-
}));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public get [Symbol.toStringTag]() { return "AsyncAggregator"; }
|
|
56
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { ReferenceException } from "./exceptions/index.js";
|
|
2
|
-
|
|
3
|
-
export default class Subscribers<P extends unknown[] = [], R = void, T extends (...args: P) => R = (...args: P) => R>
|
|
4
|
-
{
|
|
5
|
-
protected _subscribers: T[];
|
|
6
|
-
|
|
7
|
-
public constructor()
|
|
8
|
-
{
|
|
9
|
-
this._subscribers = [];
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public add(subscriber: T): void
|
|
13
|
-
{
|
|
14
|
-
this._subscribers.push(subscriber);
|
|
15
|
-
}
|
|
16
|
-
public remove(subscriber: T): void
|
|
17
|
-
{
|
|
18
|
-
const index = this._subscribers.indexOf(subscriber);
|
|
19
|
-
if (index < 0)
|
|
20
|
-
{
|
|
21
|
-
throw new ReferenceException("Unable to remove the requested subscriber. It was not found.");
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
this._subscribers.splice(index, 1);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public call(...args: P): R[]
|
|
28
|
-
{
|
|
29
|
-
return this._subscribers
|
|
30
|
-
.slice()
|
|
31
|
-
.map((subscriber) => subscriber(...args));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public get [Symbol.toStringTag]() { return "Subscribers"; }
|
|
35
|
-
}
|