@zelgadis87/utils-core 4.6.0 → 4.6.2
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/Logger.d.ts +21 -0
- package/dist/Optional.d.ts +70 -0
- package/dist/async/CancelableDeferred.d.ts +17 -0
- package/dist/async/Deferred.d.ts +34 -0
- package/dist/async/RateThrottler.d.ts +13 -0
- package/dist/async/Semaphore.d.ts +17 -0
- package/dist/async/index.d.ts +4 -0
- package/dist/index.d.ts +8 -0
- package/dist/lazy/Lazy.d.ts +22 -0
- package/dist/lazy/LazyAsync.d.ts +24 -0
- package/dist/lazy/index.d.ts +2 -0
- package/dist/sorting/ComparisonChain.d.ts +57 -0
- package/dist/sorting/Sorter.d.ts +100 -0
- package/dist/sorting/index.d.ts +4 -0
- package/dist/sorting/types.d.ts +5 -0
- package/dist/time/RandomTimeDuration.d.ts +9 -0
- package/dist/time/TimeBase.d.ts +38 -0
- package/dist/time/TimeDuration.d.ts +81 -0
- package/dist/time/TimeFrequency.d.ts +10 -0
- package/dist/time/TimeInstant.d.ts +175 -0
- package/dist/time/TimeInstantBuilder.d.ts +77 -0
- package/dist/time/TimeRange.d.ts +11 -0
- package/dist/time/TimeUnit.d.ts +17 -0
- package/dist/time/index.d.ts +7 -0
- package/dist/time/types.d.ts +26 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/upgrade/DataUpgrader.d.ts +22 -0
- package/dist/upgrade/errors.d.ts +12 -0
- package/dist/upgrade/getTransitionsPath.d.ts +3 -0
- package/dist/upgrade/index.d.ts +2 -0
- package/dist/upgrade/types.d.ts +31 -0
- package/dist/utils/arrays/groupBy.d.ts +9 -0
- package/dist/utils/arrays/indexBy.d.ts +7 -0
- package/dist/utils/arrays/statistics.d.ts +8 -0
- package/dist/utils/arrays/uniqBy.d.ts +4 -0
- package/dist/utils/arrays.d.ts +57 -0
- package/dist/utils/booleans.d.ts +2 -0
- package/dist/utils/empties.d.ts +17 -0
- package/dist/utils/errors/withTryCatch.d.ts +3 -0
- package/dist/utils/errors.d.ts +3 -0
- package/dist/utils/functions/constant.d.ts +10 -0
- package/dist/utils/functions/iff.d.ts +8 -0
- package/dist/utils/functions.d.ts +31 -0
- package/dist/utils/index.d.ts +13 -0
- package/dist/utils/json.d.ts +11 -0
- package/dist/utils/nulls.d.ts +8 -0
- package/dist/utils/numbers/round.d.ts +8 -0
- package/dist/utils/numbers.d.ts +34 -0
- package/dist/utils/primitives.d.ts +3 -0
- package/dist/utils/promises.d.ts +6 -0
- package/dist/utils/random.d.ts +7 -0
- package/dist/utils/records/entries.d.ts +4 -0
- package/dist/utils/records.d.ts +31 -0
- package/dist/utils/strings/StringParts.d.ts +32 -0
- package/dist/utils/strings.d.ts +17 -0
- package/esbuild/index.cjs +222 -93
- package/esbuild/index.cjs.map +7 -0
- package/esbuild/{index.js → index.mjs} +232 -90
- package/esbuild/index.mjs.map +7 -0
- package/package.json +3 -3
- package/esbuild/package.json +0 -39
package/dist/Logger.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
declare const LEVELS: readonly ["log", "debug", "info", "warn", "error"];
|
|
2
|
+
type TLogLevel = keyof Console & typeof LEVELS[number];
|
|
3
|
+
export type TLoggerOpts = {
|
|
4
|
+
console: Console;
|
|
5
|
+
enabled: boolean;
|
|
6
|
+
minLevel: Uppercase<TLogLevel>;
|
|
7
|
+
};
|
|
8
|
+
export declare class Logger {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
private enabled;
|
|
11
|
+
private console;
|
|
12
|
+
private minLevel;
|
|
13
|
+
constructor(name: string, args?: Partial<TLoggerOpts>);
|
|
14
|
+
get log(): (...args: any[]) => void;
|
|
15
|
+
get debug(): (...args: any[]) => void;
|
|
16
|
+
get info(): (...args: any[]) => void;
|
|
17
|
+
get warn(): (...args: any[]) => void;
|
|
18
|
+
get error(): (...args: any[]) => void;
|
|
19
|
+
private logWrap;
|
|
20
|
+
}
|
|
21
|
+
export default Logger;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { TConsumer, TFunction, TPredicate, TProducer, TVoidFunction } from "./utils/functions.js";
|
|
2
|
+
export declare class Optional<T> implements TOptional<T> {
|
|
3
|
+
private _present;
|
|
4
|
+
private _value;
|
|
5
|
+
private constructor();
|
|
6
|
+
get rawValue(): T | undefined;
|
|
7
|
+
get(): T | never;
|
|
8
|
+
set(t: T): void;
|
|
9
|
+
clear(): void;
|
|
10
|
+
isEmpty(): this is TEmptyOptional<T>;
|
|
11
|
+
isPresent(): this is TPresentOptional<T>;
|
|
12
|
+
ifEmpty(callback: TVoidFunction): void;
|
|
13
|
+
ifPresent(callback: TConsumer<T>): void;
|
|
14
|
+
apply(callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction): void;
|
|
15
|
+
orElse(newValue: T | null | undefined): any;
|
|
16
|
+
orElseGet(produceValue: TProducer<T | null | undefined>): TOptional<T>;
|
|
17
|
+
orElseThrow(produceError: TProducer<Error>): TPresentOptional<T>;
|
|
18
|
+
mapTo<R>(mapper: TFunction<T, R | null | undefined>): any;
|
|
19
|
+
flatMapTo<R>(mapper: TFunction<T, TOptional<R>>): any;
|
|
20
|
+
filter(predicate: TPredicate<T>): this | TEmptyOptional<T>;
|
|
21
|
+
static empty<T>(): TEmptyOptional<T>;
|
|
22
|
+
static of<T>(t: T): TPresentOptional<T>;
|
|
23
|
+
static ofNullable<T>(t: T | null | undefined): TOptional<T>;
|
|
24
|
+
}
|
|
25
|
+
export default Optional;
|
|
26
|
+
export type TOptional<T> = {
|
|
27
|
+
readonly rawValue: T | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* @returns the currently stored value, if any; throws {@link ErrorGetEmptyOptional} otherwise;
|
|
30
|
+
*/
|
|
31
|
+
get(): T | never;
|
|
32
|
+
/**
|
|
33
|
+
* Places a new value inside this optional. Throws {@link ErrorSetEmptyOptional} if t is null or undefined.
|
|
34
|
+
*/
|
|
35
|
+
set(t: T): void;
|
|
36
|
+
/**
|
|
37
|
+
* Clears the current value from this optional, if any.
|
|
38
|
+
*/
|
|
39
|
+
clear(): void;
|
|
40
|
+
isEmpty(): boolean;
|
|
41
|
+
isPresent(): boolean;
|
|
42
|
+
ifEmpty: (callback: TVoidFunction) => void;
|
|
43
|
+
ifPresent: (callback: TConsumer<T>) => void;
|
|
44
|
+
apply(callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction): void;
|
|
45
|
+
orElse: ((newValue: T | null | undefined) => TOptional<T>);
|
|
46
|
+
orElseGet: (produceValue: TProducer<T | null | undefined>) => TOptional<T>;
|
|
47
|
+
orElseThrow: (produceError: TProducer<Error>) => TPresentOptional<T>;
|
|
48
|
+
mapTo<R>(mapper: TFunction<T, R>): TOptional<R>;
|
|
49
|
+
flatMapTo<R>(mapper: TFunction<T, TOptional<R>>): TOptional<R>;
|
|
50
|
+
filter(predicate: TPredicate<T>): TOptional<T>;
|
|
51
|
+
};
|
|
52
|
+
export type TEmptyOptional<T> = TOptional<T> & {
|
|
53
|
+
readonly rawValue: undefined;
|
|
54
|
+
isEmpty(): true;
|
|
55
|
+
isPresent(): false;
|
|
56
|
+
};
|
|
57
|
+
export type TPresentOptional<T> = TOptional<T> & {
|
|
58
|
+
readonly rawValue: T;
|
|
59
|
+
isEmpty(): false;
|
|
60
|
+
isPresent(): true;
|
|
61
|
+
};
|
|
62
|
+
export declare class ErrorGetEmptyOptional extends Error {
|
|
63
|
+
constructor();
|
|
64
|
+
}
|
|
65
|
+
export declare class ErrorSetEmptyOptional extends Error {
|
|
66
|
+
constructor();
|
|
67
|
+
}
|
|
68
|
+
export declare class ErrorCannotInstantiatePresentOptionalWithEmptyValue extends Error {
|
|
69
|
+
constructor();
|
|
70
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import Deferred from "./Deferred.js";
|
|
2
|
+
interface ICancelablePromise<T> extends Promise<T> {
|
|
3
|
+
cancel(): void;
|
|
4
|
+
}
|
|
5
|
+
/** @deprecated use Deferred instead. */
|
|
6
|
+
export default class CancelableDeferred<T = void> extends Deferred<T> implements ICancelablePromise<T> {
|
|
7
|
+
private _canceled;
|
|
8
|
+
get canceled(): boolean;
|
|
9
|
+
constructor();
|
|
10
|
+
cancel(): void;
|
|
11
|
+
protected createInternals(): {
|
|
12
|
+
promise: Promise<T>;
|
|
13
|
+
resolve: (value: T) => void;
|
|
14
|
+
reject: (reason: Error) => void;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface ICancelable {
|
|
2
|
+
cancel(): void;
|
|
3
|
+
}
|
|
4
|
+
export interface ICancelablePromise<T> extends Promise<T>, ICancelable {
|
|
5
|
+
}
|
|
6
|
+
export declare class DeferredCanceledError extends Error {
|
|
7
|
+
constructor();
|
|
8
|
+
}
|
|
9
|
+
export declare class Deferred<T = void> implements ICancelablePromise<T> {
|
|
10
|
+
private _pending;
|
|
11
|
+
private readonly _internals;
|
|
12
|
+
get pending(): boolean;
|
|
13
|
+
/** @deprecated: Use resolve, then and catch directly; use asPromise to return a promise type. */
|
|
14
|
+
get promise(): Promise<T>;
|
|
15
|
+
constructor();
|
|
16
|
+
resolve(val: T): void;
|
|
17
|
+
reject(reason: Error): void;
|
|
18
|
+
cancel(): void;
|
|
19
|
+
resolveIfPending(val: T): void;
|
|
20
|
+
rejectIfPending(reason: Error): void;
|
|
21
|
+
cancelIfPending(): void;
|
|
22
|
+
then<R = T, S = T>(onFulfilled: ((value: T) => R | PromiseLike<R>) | null | undefined, onRejected?: ((reason: Error) => S | PromiseLike<S>) | null | undefined): Promise<R | S>;
|
|
23
|
+
catch<R = T>(onRejected: ((reason: Error) => R | PromiseLike<R>) | null | undefined): Promise<T | R>;
|
|
24
|
+
finally(onfinally: (() => void) | null | undefined): Promise<T>;
|
|
25
|
+
asPromise(): Promise<T>;
|
|
26
|
+
asCancelablePromise(): ICancelablePromise<T>;
|
|
27
|
+
protected createInternals(): {
|
|
28
|
+
promise: Promise<T>;
|
|
29
|
+
resolve: (value: T) => void;
|
|
30
|
+
reject: (reason: Error) => void;
|
|
31
|
+
};
|
|
32
|
+
get [Symbol.toStringTag](): string;
|
|
33
|
+
}
|
|
34
|
+
export default Deferred;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { TFunction, TPromisable } from "..";
|
|
2
|
+
import TimeFrequency from "../time/TimeFrequency";
|
|
3
|
+
type TPromiseFactory<T, R> = TFunction<T, Promise<R>>;
|
|
4
|
+
export declare class RateThrottler {
|
|
5
|
+
private readonly _frequency;
|
|
6
|
+
private readonly _availableSlots;
|
|
7
|
+
private readonly _waitingRequests;
|
|
8
|
+
constructor(_frequency: TimeFrequency);
|
|
9
|
+
get cooldown(): import("..").TimeDuration;
|
|
10
|
+
execute<T>(fn: TPromiseFactory<void, T>): Promise<T>;
|
|
11
|
+
}
|
|
12
|
+
export default RateThrottler;
|
|
13
|
+
export declare function throttle<TParams extends unknown[], R>(fn: (...params: TParams) => TPromisable<R>, frequence: TimeFrequency): (...params: TParams) => Promise<R>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TAsyncProducer, TProducer, TimeDuration } from "..";
|
|
2
|
+
export declare class Semaphore {
|
|
3
|
+
private _availableSlots;
|
|
4
|
+
private readonly _queuedRequests;
|
|
5
|
+
private _inProgress;
|
|
6
|
+
/** Creates a new Semaphore with a single slot. */
|
|
7
|
+
constructor();
|
|
8
|
+
/** Creates a new Semaphore with the specified amounts of slots. */
|
|
9
|
+
constructor(availableSlots: number);
|
|
10
|
+
private _awaitSlot;
|
|
11
|
+
private _releaseSlot;
|
|
12
|
+
execute<T>(fn: TAsyncProducer<T> | TProducer<T>, cooldown?: TimeDuration): Promise<T>;
|
|
13
|
+
get availableSlots(): number;
|
|
14
|
+
get queueSize(): number;
|
|
15
|
+
get inProgressSize(): number;
|
|
16
|
+
}
|
|
17
|
+
export default Semaphore;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './async/index.js';
|
|
2
|
+
export * from './lazy/index.js';
|
|
3
|
+
export * from './Logger.js';
|
|
4
|
+
export * from './Optional.js';
|
|
5
|
+
export * from './sorting/index.js';
|
|
6
|
+
export * from './time/index.js';
|
|
7
|
+
export * from './upgrade/index.js';
|
|
8
|
+
export * from './utils/index.js';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class that stores a value that is computationally/memory intensive to initialize.
|
|
3
|
+
* The initialization of the value is done once and only if required (lazy initialization).
|
|
4
|
+
* @author gtomberli
|
|
5
|
+
*/
|
|
6
|
+
export declare class Lazy<T> {
|
|
7
|
+
private _value;
|
|
8
|
+
private _initialized;
|
|
9
|
+
private _generator;
|
|
10
|
+
private constructor();
|
|
11
|
+
get value(): T | undefined;
|
|
12
|
+
getOrCreate(): T;
|
|
13
|
+
getOrThrow(errorMessage?: "Value not initialized"): T;
|
|
14
|
+
or(t: T): T;
|
|
15
|
+
isPresent(): boolean;
|
|
16
|
+
isEmpty(): boolean;
|
|
17
|
+
ifPresent(fn: (t: T) => void): void;
|
|
18
|
+
ifEmpty(fn: () => void): void;
|
|
19
|
+
apply(fnPresent: (t: T) => void, fnEmpty: () => void): void;
|
|
20
|
+
static of<T>(generator: () => T): Lazy<T>;
|
|
21
|
+
}
|
|
22
|
+
export default Lazy;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare class LazyAsync<T> {
|
|
2
|
+
private _promise;
|
|
3
|
+
private _pending;
|
|
4
|
+
private _resolvedValue;
|
|
5
|
+
private _error;
|
|
6
|
+
private _generator;
|
|
7
|
+
private constructor();
|
|
8
|
+
getOrCreate(): Promise<T>;
|
|
9
|
+
isPresent(): boolean;
|
|
10
|
+
isEmpty(): boolean;
|
|
11
|
+
isPending(): boolean;
|
|
12
|
+
isReady(): boolean;
|
|
13
|
+
isResolved(): boolean;
|
|
14
|
+
isError(): boolean;
|
|
15
|
+
ifPresent(fn: (t: Promise<T>) => void): void;
|
|
16
|
+
ifEmpty(fn: () => void): void;
|
|
17
|
+
ifPending(fn: (t: Promise<T>) => void): void;
|
|
18
|
+
ifReady(fn: (t: Promise<T>) => void): void;
|
|
19
|
+
ifResolved(fn: (t: T) => void): void;
|
|
20
|
+
ifError(fn: (err: Error) => void): void;
|
|
21
|
+
getOrThrow(errorMessage?: "Value not initialized"): Promise<T> | never;
|
|
22
|
+
static of<T>(generator: () => Promise<T>): LazyAsync<T>;
|
|
23
|
+
}
|
|
24
|
+
export default LazyAsync;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { TFunction, TKeysOfType, TReadableArray } from "../utils/index.js";
|
|
2
|
+
import { TComparisonDirection, TComparisonFunction, TComparisonResult } from "./types.js";
|
|
3
|
+
type TCompareValueOptions = {
|
|
4
|
+
nullsFirst: boolean;
|
|
5
|
+
direction: TComparisonDirection;
|
|
6
|
+
};
|
|
7
|
+
type TLooseCompareValueOptions = Partial<TCompareValueOptions> | TComparisonDirection;
|
|
8
|
+
declare function compareStrings(options?: TLooseCompareValueOptions & {
|
|
9
|
+
ignoreCase?: boolean;
|
|
10
|
+
}): TComparisonFunction<string>;
|
|
11
|
+
declare function compareBooleans(options: Partial<TCompareValueOptions> & {
|
|
12
|
+
truesFirst: boolean;
|
|
13
|
+
}): TComparisonFunction<boolean>;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a ComparisonChain V1.
|
|
16
|
+
* Usage: ComparisonChain.createWith<number>( c => c.compareUsingNumbers() ).sort( [ 100, 1, 10, 5 ] );
|
|
17
|
+
* @deprecated[2023-10-22]: Use createSorterFor instead.
|
|
18
|
+
**/
|
|
19
|
+
export declare class ComparisonChain<T> {
|
|
20
|
+
private readonly _fns;
|
|
21
|
+
private readonly _reversed;
|
|
22
|
+
private constructor();
|
|
23
|
+
compare: (a: T, b: T) => TComparisonResult;
|
|
24
|
+
sort: <A extends TReadableArray<T>>(arr: A) => A extends Array<T> ? Array<T> : ReadonlyArray<T>;
|
|
25
|
+
then(getFn: (compare: TChain<T>) => TComparisonFunction<T>): ComparisonChain<T>;
|
|
26
|
+
thenBy(cmp: ComparisonChain<T> | TComparisonFunction<T>): ComparisonChain<T>;
|
|
27
|
+
thenChain<R>(transform: TFunction<T, R>, getFn: (compare: TChain<R>) => TComparisonFunction<R>): ComparisonChain<T>;
|
|
28
|
+
thenChainBy<R>(transform: TFunction<T, R>, cmp: ComparisonChain<R> | TComparisonFunction<R>): ComparisonChain<T>;
|
|
29
|
+
reversed(): ComparisonChain<T>;
|
|
30
|
+
private doCompare;
|
|
31
|
+
static create<T>(): {
|
|
32
|
+
using: (getFn: (chainable: TChain<T>) => TComparisonFunction<T>) => ComparisonChain<T>;
|
|
33
|
+
usingTransformation: <R>(transform: TFunction<T, R>, getFn: (chainable: TChain<R>) => TComparisonFunction<R>) => ComparisonChain<T>;
|
|
34
|
+
};
|
|
35
|
+
static createWith<T>(getFn: (chainable: TChain<T>) => TComparisonFunction<T>): ComparisonChain<T>;
|
|
36
|
+
static createWithTransformation<T, R>(transform: TFunction<T, R>, getFn: (chainable: TChain<R>) => TComparisonFunction<R>): ComparisonChain<T>;
|
|
37
|
+
}
|
|
38
|
+
export default ComparisonChain;
|
|
39
|
+
declare function createCompare<T>(): {
|
|
40
|
+
compareUsingGetter: {
|
|
41
|
+
(getter: TFunction<T, string>, options?: TLooseCompareValueOptions): TComparisonFunction<T>;
|
|
42
|
+
(getter: TFunction<T, number>, options?: TLooseCompareValueOptions): TComparisonFunction<T>;
|
|
43
|
+
(getter: TFunction<T, Date>, options?: TLooseCompareValueOptions): TComparisonFunction<T>;
|
|
44
|
+
};
|
|
45
|
+
compareUsingField: (field: keyof T & TKeysOfType<T, string | number | Date>, options?: TLooseCompareValueOptions) => TComparisonFunction<T>;
|
|
46
|
+
compareUsingStrings: typeof compareStrings;
|
|
47
|
+
compareUsingStringsIgnoringCase: (options?: TLooseCompareValueOptions) => TComparisonFunction<string>;
|
|
48
|
+
compareUsingBooleans: typeof compareBooleans;
|
|
49
|
+
compareUsingNumbers: (options?: TLooseCompareValueOptions) => TComparisonFunction<number>;
|
|
50
|
+
compareUsingDates: (options?: TLooseCompareValueOptions) => TComparisonFunction<Date>;
|
|
51
|
+
compareUsingFunction: (fn: TComparisonFunction<T>) => TComparisonFunction<T>;
|
|
52
|
+
placeFirst: (arr: Array<T>) => TComparisonFunction<T>;
|
|
53
|
+
placeFirstInOrder: (arr: Array<T>) => TComparisonFunction<T>;
|
|
54
|
+
placeLast: (arr: Array<T>) => TComparisonFunction<T>;
|
|
55
|
+
placeLastInOrder: (arr: Array<T>) => TComparisonFunction<T>;
|
|
56
|
+
};
|
|
57
|
+
type TChain<T> = ReturnType<typeof createCompare<T>>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { TConditionalParameterOptions, TFunction, TKeysOfType, TPositiveNumber, TReadableArray } from "../utils/index.js";
|
|
2
|
+
import { TComparisonFunction } from "./types.js";
|
|
3
|
+
type TComparable<T> = {
|
|
4
|
+
compare: TComparisonFunction<T>;
|
|
5
|
+
};
|
|
6
|
+
export type TSorter<T> = {
|
|
7
|
+
get then(): TSorterStep<T, TSorter<T>>;
|
|
8
|
+
reversed: () => TSorter<T>;
|
|
9
|
+
compare: TComparisonFunction<T>;
|
|
10
|
+
sort: (arr: TReadableArray<T>) => Array<T>;
|
|
11
|
+
top: (arr: TReadableArray<T>, n: TPositiveNumber) => Array<T>;
|
|
12
|
+
bottom: (arr: TReadableArray<T>, n: TPositiveNumber) => Array<T>;
|
|
13
|
+
first: (arr: TReadableArray<T>) => T | null;
|
|
14
|
+
last: (arr: TReadableArray<T>) => T | null;
|
|
15
|
+
};
|
|
16
|
+
type TSorterStepForRecords<T, Ret> = {
|
|
17
|
+
usingStrings: (field: TKeysOfType<T, string>) => TSorterStep<string, Ret>;
|
|
18
|
+
usingNumbers: (field: TKeysOfType<T, number>) => TSorterStep<number, Ret>;
|
|
19
|
+
usingBooleans: (field: TKeysOfType<T, boolean>) => TSorterStep<boolean, Ret>;
|
|
20
|
+
usingDates: (field: TKeysOfType<T, Date>) => TSorterStep<Date, Ret>;
|
|
21
|
+
};
|
|
22
|
+
type TSorterStepForNumbers<_T, Ret> = {
|
|
23
|
+
inAscendingOrder(opts?: {
|
|
24
|
+
nullsFirst?: boolean;
|
|
25
|
+
}): Ret;
|
|
26
|
+
inDescendingOrder(opts?: {
|
|
27
|
+
nullsFirst?: boolean;
|
|
28
|
+
}): Ret;
|
|
29
|
+
};
|
|
30
|
+
type TSorterStepForStrings<_T, Ret> = {
|
|
31
|
+
inLexographicalOrder(opts?: {
|
|
32
|
+
nullsFirst?: boolean;
|
|
33
|
+
}): Ret;
|
|
34
|
+
inLexographicalOrderIgnoringCase(opts?: {
|
|
35
|
+
nullsFirst?: boolean;
|
|
36
|
+
}): Ret;
|
|
37
|
+
inReverseLexographicalOrder(opts?: {
|
|
38
|
+
nullsFirst?: boolean;
|
|
39
|
+
}): Ret;
|
|
40
|
+
inReverseLexographicalOrderIgnoringCase(opts?: {
|
|
41
|
+
nullsFirst?: boolean;
|
|
42
|
+
}): Ret;
|
|
43
|
+
};
|
|
44
|
+
type TSorterStepForBooleans<_T, Ret> = {
|
|
45
|
+
truesFirst(opts?: {
|
|
46
|
+
nullsFirst?: boolean;
|
|
47
|
+
}): Ret;
|
|
48
|
+
falsesFirst(opts?: {
|
|
49
|
+
nullsFirst?: boolean;
|
|
50
|
+
}): Ret;
|
|
51
|
+
};
|
|
52
|
+
type TSorterStepForDates<_T, Ret> = {
|
|
53
|
+
mostAncientFirst(opts?: {
|
|
54
|
+
nullsFirst?: boolean;
|
|
55
|
+
}): Ret;
|
|
56
|
+
mostRecentFirst(opts?: {
|
|
57
|
+
nullsFirst?: boolean;
|
|
58
|
+
}): Ret;
|
|
59
|
+
};
|
|
60
|
+
type TSorterStepForChainables<T, Ret> = {
|
|
61
|
+
chain: (chain: TComparable<T>) => Ret;
|
|
62
|
+
};
|
|
63
|
+
type TSorterStepForUsing<T, Ret> = {
|
|
64
|
+
using: <X>(transform: TFunction<T, X>) => TSorterStep<X, Ret>;
|
|
65
|
+
};
|
|
66
|
+
type TSorterStepForPrioritizable<T extends string | number, Ret> = {
|
|
67
|
+
prioritizing(arr: T[], options?: TConditionalParameterOptions<TPrioritizeOptions, typeof defaultPrioritizeOptions>): Ret;
|
|
68
|
+
deprioritizing(arr: Array<T>, options?: TConditionalParameterOptions<TPrioritizeOptions, typeof defaultPrioritizeOptions>): Ret;
|
|
69
|
+
prioritizingWithEqualWeight(arr: T[], options?: TConditionalParameterOptions<TPrioritizeOptions, typeof defaultPrioritizeOptions>): Ret;
|
|
70
|
+
deprioritizingWithEqualWeight(arr: Array<T>, options?: TConditionalParameterOptions<TPrioritizeOptions, typeof defaultPrioritizeOptions>): Ret;
|
|
71
|
+
};
|
|
72
|
+
type TSorterStep<T, Ret> = {} & (T extends Record<string | number | symbol, unknown> ? TSorterStepForRecords<T, Ret> : {}) & (T extends number ? TSorterStepForNumbers<T, Ret> : {}) & (T extends string ? TSorterStepForStrings<T, Ret> : {}) & (T extends boolean ? TSorterStepForBooleans<T, Ret> : {}) & (T extends Date ? TSorterStepForDates<T, Ret> : {}) & (T extends string ? TSorterStepForPrioritizable<string, Ret> : {}) & (T extends number ? TSorterStepForPrioritizable<number, Ret> : {}) & TSorterStepForChainables<T, Ret> & TSorterStepForUsing<T, Ret>;
|
|
73
|
+
type TSorterStarter<T> = TSorterStep<T, TSorter<T>>;
|
|
74
|
+
export type TSorterBuilder<T> = (builder: TSorterStarter<T>) => TSorter<T>;
|
|
75
|
+
type TPrioritizeOptions = {};
|
|
76
|
+
declare const defaultPrioritizeOptions: {};
|
|
77
|
+
/**
|
|
78
|
+
* @deprecated[2023-10-19]: Use {@link Sorter.createFor} instead.
|
|
79
|
+
**/
|
|
80
|
+
export declare function createSorterFor<T>(): TSorterStarter<T>;
|
|
81
|
+
export declare const Sorter: {
|
|
82
|
+
createFor: <T>() => TSorterStarter<T>;
|
|
83
|
+
sort: <T>(arr: TReadableArray<T>, builder: TSorterBuilder<T>) => T[];
|
|
84
|
+
top: <T>(arr: TReadableArray<T>, n: TPositiveNumber, builder: TSorterBuilder<T>) => T[];
|
|
85
|
+
bottom: <T>(arr: TReadableArray<T>, n: TPositiveNumber, builder: TSorterBuilder<T>) => T[];
|
|
86
|
+
first: <T>(arr: TReadableArray<T>, builder: TSorterBuilder<T>) => T | null;
|
|
87
|
+
last: <T>(arr: TReadableArray<T>, builder: TSorterBuilder<T>) => T | null;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* @deprecated[2023-11-01]: Use {@link Sorter} instead.
|
|
91
|
+
**/
|
|
92
|
+
export declare const Sorting: {
|
|
93
|
+
createFor: <T>() => TSorterStarter<T>;
|
|
94
|
+
sort: <T>(arr: TReadableArray<T>, builder: TSorterBuilder<T>) => T[];
|
|
95
|
+
top: <T>(arr: TReadableArray<T>, n: TPositiveNumber, builder: TSorterBuilder<T>) => T[];
|
|
96
|
+
bottom: <T>(arr: TReadableArray<T>, n: TPositiveNumber, builder: TSorterBuilder<T>) => T[];
|
|
97
|
+
first: <T>(arr: TReadableArray<T>, builder: TSorterBuilder<T>) => T | null;
|
|
98
|
+
last: <T>(arr: TReadableArray<T>, builder: TSorterBuilder<T>) => T | null;
|
|
99
|
+
};
|
|
100
|
+
export default Sorter;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { TBiFunction } from "../utils/functions.js";
|
|
2
|
+
export type TComparisonResult = -1 | 0 | 1;
|
|
3
|
+
export type TStrictComparisonResult = -1 | 1;
|
|
4
|
+
export type TComparisonFunction<T> = TBiFunction<T, T, TComparisonResult>;
|
|
5
|
+
export type TComparisonDirection = 'ASC' | 'DESC';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import TimeDuration from "./TimeDuration.js";
|
|
2
|
+
export default class RandomTimeDuration {
|
|
3
|
+
private constructor();
|
|
4
|
+
static ms: (a: number, b: number) => TimeDuration;
|
|
5
|
+
static seconds: (a: number, b: number) => TimeDuration;
|
|
6
|
+
static minutes: (a: number, b: number) => TimeDuration;
|
|
7
|
+
static hours: (a: number, b: number) => TimeDuration;
|
|
8
|
+
static days: (a: number, b: number) => TimeDuration;
|
|
9
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import TimeUnit from "./TimeUnit";
|
|
2
|
+
export default abstract class TimeBase<T> {
|
|
3
|
+
private readonly _ms;
|
|
4
|
+
protected constructor(value: number, unit: TimeUnit);
|
|
5
|
+
protected abstract create(value: number, unit: TimeUnit): T;
|
|
6
|
+
get ms(): number;
|
|
7
|
+
get seconds(): number;
|
|
8
|
+
get minutes(): number;
|
|
9
|
+
get hours(): number;
|
|
10
|
+
get days(): number;
|
|
11
|
+
addMs(milliseconds: number): T;
|
|
12
|
+
addSeconds(seconds: number): T;
|
|
13
|
+
addMinutes(minutes: number): T;
|
|
14
|
+
addHours(hours: number): T;
|
|
15
|
+
addDays(days: number): T;
|
|
16
|
+
removeDays(days: number): T;
|
|
17
|
+
addUnits(n: number, unit: TimeUnit): T;
|
|
18
|
+
removeMs(milliseconds: number): T;
|
|
19
|
+
removeSeconds(seconds: number): T;
|
|
20
|
+
removeMinutes(minutes: number): T;
|
|
21
|
+
removeHours(hours: number): T;
|
|
22
|
+
removeUnits(n: number, unit: TimeUnit): T;
|
|
23
|
+
getUnit(unit: TimeUnit): number;
|
|
24
|
+
protected toUnits(): {
|
|
25
|
+
days: number;
|
|
26
|
+
hours: number;
|
|
27
|
+
minutes: number;
|
|
28
|
+
seconds: number;
|
|
29
|
+
};
|
|
30
|
+
protected static toMs(units: {
|
|
31
|
+
days?: number;
|
|
32
|
+
hours?: number;
|
|
33
|
+
minutes?: number;
|
|
34
|
+
seconds?: number;
|
|
35
|
+
ms?: number;
|
|
36
|
+
}): number;
|
|
37
|
+
toJSON(): number;
|
|
38
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ICancelable, ICancelablePromise } from "../async/Deferred.js";
|
|
2
|
+
import { TComparisonResult } from "../sorting/index.js";
|
|
3
|
+
import { TIntervalHandle, TTimeoutHandle } from "../utils/index.js";
|
|
4
|
+
import TimeBase from "./TimeBase";
|
|
5
|
+
import TimeInstant from "./TimeInstant";
|
|
6
|
+
import TimeUnit from "./TimeUnit";
|
|
7
|
+
export declare class TimeDuration extends TimeBase<TimeDuration> {
|
|
8
|
+
protected constructor(value: number, unit: TimeUnit);
|
|
9
|
+
protected create(value: number, unit: TimeUnit): TimeDuration;
|
|
10
|
+
addDuration(duration: TimeDuration): TimeDuration;
|
|
11
|
+
removeDuration(duration: TimeDuration): TimeDuration;
|
|
12
|
+
removeUnits(n: number, unit: TimeUnit): TimeDuration;
|
|
13
|
+
/** @deprecated: Use multiplyBy instead **/
|
|
14
|
+
multiply(times: number): TimeDuration;
|
|
15
|
+
multiplyBy(times: number): TimeDuration;
|
|
16
|
+
divideBy(times: number): TimeDuration;
|
|
17
|
+
/**
|
|
18
|
+
* Returns the current duration in a human readable format, prioritizing the most significant unit of time and excluding the rest.
|
|
19
|
+
* @todo[2023-06] Should allow more customization options
|
|
20
|
+
* @todo[2023-06] By default should show the secondary unit only when actually needed (eg, 1h 20m & 3h, instead of 1h 20m & 3h 28m)
|
|
21
|
+
* @todo[2023-06] Should not allow negative durations, as this is a duration and not an instant.
|
|
22
|
+
* @returns the duration, with only the most significant units displayed as a human readable string, eg: 1d 20h, 10m 30s.
|
|
23
|
+
*/
|
|
24
|
+
get formatted(): string;
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated[2023-06] this method makes no sense, as durations are positive by default.
|
|
27
|
+
*/
|
|
28
|
+
absolute(): TimeDuration;
|
|
29
|
+
interval(cb: () => unknown): TIntervalHandle;
|
|
30
|
+
timeout(cb: () => unknown): TTimeoutHandle;
|
|
31
|
+
cancelablePromise(): ICancelablePromise<void>;
|
|
32
|
+
promise(): Promise<void>;
|
|
33
|
+
delay(cb: () => unknown): void;
|
|
34
|
+
cancelableDelay(cb: () => unknown): ICancelable;
|
|
35
|
+
debounce<T>(fn: (t: T) => void): (t: T) => void;
|
|
36
|
+
toDigitalClock(): string;
|
|
37
|
+
get asSeconds(): string;
|
|
38
|
+
fromNow(): TimeInstant;
|
|
39
|
+
differenceFrom(other: TimeDuration): TimeDuration;
|
|
40
|
+
/** @deprecated: Use fromNow instead **/
|
|
41
|
+
addCurrentTime(): TimeInstant;
|
|
42
|
+
isGreaterThan(other: TimeDuration): boolean;
|
|
43
|
+
isLessThan(other: TimeDuration): boolean;
|
|
44
|
+
compareTo(other: TimeDuration): TComparisonResult;
|
|
45
|
+
atLeast(other: TimeDuration): TimeDuration;
|
|
46
|
+
atMost(other: TimeDuration): TimeDuration;
|
|
47
|
+
isEmpty(): boolean;
|
|
48
|
+
isNotEmpty(): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* This method is used to provide a better DX when inspecting a TimeInstant object in DevTools.
|
|
51
|
+
*/
|
|
52
|
+
protected get _duration(): string;
|
|
53
|
+
toString(): string;
|
|
54
|
+
static parseHumanTime(humanTime: string): TimeDuration;
|
|
55
|
+
static compare(a: TimeDuration, b: TimeDuration): TComparisonResult;
|
|
56
|
+
static ms: IDurationConstructor;
|
|
57
|
+
static seconds: IDurationConstructor;
|
|
58
|
+
static minutes: IDurationConstructor;
|
|
59
|
+
static hours: IDurationConstructor;
|
|
60
|
+
static days: IDurationConstructor;
|
|
61
|
+
static shamefulUnref: (ms: TAllowedTimeDuration) => number;
|
|
62
|
+
static shamefulRef: (ms: TAllowedTimeDuration) => TimeDuration;
|
|
63
|
+
static unref: (ms: TValidTimeDuration) => TPredefinedTimeDuration;
|
|
64
|
+
static ref: (ms: TValidTimeDuration) => TimeDuration;
|
|
65
|
+
static fromMs(ms: number): TimeDuration;
|
|
66
|
+
static distanceFromNow(instant: TimeInstant): TimeDuration;
|
|
67
|
+
static distance(instant1: TimeInstant, instant2: TimeInstant): TimeDuration;
|
|
68
|
+
static greatest(duration1: TimeDuration, duration2: TimeDuration): TimeDuration;
|
|
69
|
+
static lowest(duration1: TimeDuration, duration2: TimeDuration): TimeDuration;
|
|
70
|
+
static ZERO: TimeDuration;
|
|
71
|
+
static fromJSON(ms: number): TimeDuration;
|
|
72
|
+
}
|
|
73
|
+
interface IDurationConstructor {
|
|
74
|
+
(a: number): TimeDuration;
|
|
75
|
+
/** @deprecated[2023-06] Use RandomTimeDuration instead */ (a: number, b?: number): TimeDuration;
|
|
76
|
+
}
|
|
77
|
+
export type TPredefinedTimeDuration = 0 | 50 | 100 | 150 | 200 | 250 | 300 | 500 | 1000 | 1500 | 2000 | 2500 | 5000;
|
|
78
|
+
export type TValidTimeDuration = TimeDuration | TPredefinedTimeDuration;
|
|
79
|
+
type TAllowedTimeDuration = TValidTimeDuration | number;
|
|
80
|
+
export declare function isAllowedTimeDuration(t: unknown): t is TAllowedTimeDuration;
|
|
81
|
+
export default TimeDuration;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { TPositiveNumber } from "../utils";
|
|
2
|
+
import TimeDuration from "./TimeDuration";
|
|
3
|
+
export default class TimeFrequency {
|
|
4
|
+
readonly times: TPositiveNumber;
|
|
5
|
+
readonly period: TimeDuration;
|
|
6
|
+
constructor(times: TPositiveNumber, period: TimeDuration);
|
|
7
|
+
get frequency(): TimeDuration;
|
|
8
|
+
static onceEvery(period: TimeDuration): TimeFrequency;
|
|
9
|
+
static twiceEvery(period: TimeDuration): TimeFrequency;
|
|
10
|
+
}
|