@zelgadis87/utils-core 4.0.0

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.
Files changed (134) hide show
  1. package/dist/Lazy.d.ts +21 -0
  2. package/dist/LazyAsync.d.ts +23 -0
  3. package/dist/Logger.d.ts +21 -0
  4. package/dist/Optional.d.ts +70 -0
  5. package/dist/async/CancelableDeferred.d.ts +17 -0
  6. package/dist/async/Deferred.d.ts +27 -0
  7. package/dist/async/RateThrottler.d.ts +12 -0
  8. package/dist/async/Semaphore.d.ts +10 -0
  9. package/dist/async/index.d.ts +4 -0
  10. package/dist/index.d.ts +11 -0
  11. package/dist/random/index.d.ts +2 -0
  12. package/dist/random/randomInterval.d.ts +1 -0
  13. package/dist/random/randomPercentage.d.ts +1 -0
  14. package/dist/sorting/ComparisonChain.d.ts +57 -0
  15. package/dist/sorting/Sorter.d.ts +100 -0
  16. package/dist/sorting/index.d.ts +4 -0
  17. package/dist/sorting/types.d.ts +5 -0
  18. package/dist/time/RandomTimeDuration.d.ts +9 -0
  19. package/dist/time/TimeBase.d.ts +38 -0
  20. package/dist/time/TimeDuration.d.ts +81 -0
  21. package/dist/time/TimeFrequency.d.ts +10 -0
  22. package/dist/time/TimeInstant.d.ts +137 -0
  23. package/dist/time/TimeInstantBuilder.d.ts +77 -0
  24. package/dist/time/TimeUnit.d.ts +17 -0
  25. package/dist/time/index.d.ts +6 -0
  26. package/dist/time/types.d.ts +26 -0
  27. package/dist/tsconfig.tsbuildinfo +1 -0
  28. package/dist/types/arrays.d.ts +33 -0
  29. package/dist/types/booleans.d.ts +2 -0
  30. package/dist/types/functions.d.ts +20 -0
  31. package/dist/types/index.d.ts +12 -0
  32. package/dist/types/json.d.ts +6 -0
  33. package/dist/types/nulls.d.ts +8 -0
  34. package/dist/types/numbers.d.ts +31 -0
  35. package/dist/types/promises.d.ts +6 -0
  36. package/dist/types/records.d.ts +14 -0
  37. package/dist/types/strings.d.ts +40 -0
  38. package/dist/upgrade/DataUpgrader.d.ts +22 -0
  39. package/dist/upgrade/errors.d.ts +12 -0
  40. package/dist/upgrade/getTransitionsPath.d.ts +3 -0
  41. package/dist/upgrade/index.d.ts +2 -0
  42. package/dist/upgrade/types.d.ts +31 -0
  43. package/dist/utils/asError.d.ts +1 -0
  44. package/dist/utils/bindThis.d.ts +1 -0
  45. package/dist/utils/constant.d.ts +8 -0
  46. package/dist/utils/entries.d.ts +4 -0
  47. package/dist/utils/groupBy.d.ts +7 -0
  48. package/dist/utils/iff.d.ts +8 -0
  49. package/dist/utils/index.d.ts +22 -0
  50. package/dist/utils/indexBy.d.ts +7 -0
  51. package/dist/utils/jsonCloneDeep.d.ts +2 -0
  52. package/dist/utils/math.d.ts +9 -0
  53. package/dist/utils/noop.d.ts +1 -0
  54. package/dist/utils/omit.d.ts +2 -0
  55. package/dist/utils/pad.d.ts +3 -0
  56. package/dist/utils/pluralize.d.ts +1 -0
  57. package/dist/utils/round.d.ts +8 -0
  58. package/dist/utils/sortBy.d.ts +7 -0
  59. package/dist/utils/throttle.d.ts +3 -0
  60. package/dist/utils/uniq.d.ts +1 -0
  61. package/dist/utils/uniqBy.d.ts +2 -0
  62. package/dist/utils/uniqByKey.d.ts +1 -0
  63. package/dist/utils/upsert.d.ts +0 -0
  64. package/dist/utils/withTryCatch.d.ts +2 -0
  65. package/dist/utils/withTryCatchAsync.d.ts +2 -0
  66. package/dist/utils/wrap.d.ts +1 -0
  67. package/esbuild/index.cjs +2670 -0
  68. package/esbuild/index.mjs +2518 -0
  69. package/package.json +159 -0
  70. package/src/Lazy.ts +77 -0
  71. package/src/LazyAsync.ts +100 -0
  72. package/src/Logger.ts +44 -0
  73. package/src/Optional.ts +172 -0
  74. package/src/async/CancelableDeferred.ts +36 -0
  75. package/src/async/Deferred.ts +84 -0
  76. package/src/async/RateThrottler.ts +46 -0
  77. package/src/async/Semaphore.ts +45 -0
  78. package/src/async/index.ts +6 -0
  79. package/src/index.ts +13 -0
  80. package/src/random/index.ts +3 -0
  81. package/src/random/randomInterval.ts +4 -0
  82. package/src/random/randomPercentage.ts +6 -0
  83. package/src/sorting/ComparisonChain.ts +209 -0
  84. package/src/sorting/Sorter.ts +357 -0
  85. package/src/sorting/index.ts +5 -0
  86. package/src/sorting/types.ts +7 -0
  87. package/src/time/RandomTimeDuration.ts +21 -0
  88. package/src/time/TimeBase.ts +113 -0
  89. package/src/time/TimeDuration.ts +296 -0
  90. package/src/time/TimeFrequency.ts +28 -0
  91. package/src/time/TimeInstant.ts +488 -0
  92. package/src/time/TimeInstantBuilder.ts +126 -0
  93. package/src/time/TimeUnit.ts +43 -0
  94. package/src/time/index.ts +8 -0
  95. package/src/time/types.ts +56 -0
  96. package/src/types/arrays.ts +89 -0
  97. package/src/types/booleans.ts +8 -0
  98. package/src/types/functions.ts +27 -0
  99. package/src/types/index.ts +18 -0
  100. package/src/types/json.ts +5 -0
  101. package/src/types/nulls.ts +33 -0
  102. package/src/types/numbers.ts +80 -0
  103. package/src/types/promises.ts +23 -0
  104. package/src/types/records.ts +21 -0
  105. package/src/types/strings.ts +143 -0
  106. package/src/upgrade/DataUpgrader.ts +100 -0
  107. package/src/upgrade/errors.ts +25 -0
  108. package/src/upgrade/getTransitionsPath.ts +89 -0
  109. package/src/upgrade/index.ts +4 -0
  110. package/src/upgrade/types.ts +36 -0
  111. package/src/utils/asError.ts +12 -0
  112. package/src/utils/bindThis.ts +4 -0
  113. package/src/utils/constant.ts +9 -0
  114. package/src/utils/entries.ts +13 -0
  115. package/src/utils/groupBy.ts +39 -0
  116. package/src/utils/iff.ts +26 -0
  117. package/src/utils/index.ts +24 -0
  118. package/src/utils/indexBy.ts +36 -0
  119. package/src/utils/jsonCloneDeep.ts +31 -0
  120. package/src/utils/math.ts +44 -0
  121. package/src/utils/noop.ts +2 -0
  122. package/src/utils/omit.ts +8 -0
  123. package/src/utils/pad.ts +20 -0
  124. package/src/utils/pluralize.ts +20 -0
  125. package/src/utils/round.ts +24 -0
  126. package/src/utils/sortBy.ts +27 -0
  127. package/src/utils/throttle.ts +10 -0
  128. package/src/utils/uniq.ts +6 -0
  129. package/src/utils/uniqBy.ts +15 -0
  130. package/src/utils/uniqByKey.ts +5 -0
  131. package/src/utils/upsert.ts +2 -0
  132. package/src/utils/withTryCatch.ts +10 -0
  133. package/src/utils/withTryCatchAsync.ts +6 -0
  134. package/src/utils/wrap.ts +4 -0
package/dist/Lazy.d.ts ADDED
@@ -0,0 +1,21 @@
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 default 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
+ }
@@ -0,0 +1,23 @@
1
+ export default 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
+ }
@@ -0,0 +1,21 @@
1
+ declare const LEVELS: readonly ["log", "debug", "info", "warn", "error"];
2
+ type TLogLevel = keyof Console & typeof LEVELS[number];
3
+ type TLoggerOpts = {
4
+ console: Console;
5
+ enabled: boolean;
6
+ minLevel: Uppercase<TLogLevel>;
7
+ };
8
+ export default 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 {};
@@ -0,0 +1,70 @@
1
+ import { TConsumer, TFunction, TPredicate, TProducer, TVoidFunction } from "./types/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 CancelableDeferredV2 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,27 @@
1
+ export interface ICancelablePromise<T> extends Promise<T> {
2
+ cancel(): void;
3
+ }
4
+ export declare class DeferredCanceledError extends Error {
5
+ }
6
+ export declare class Deferred<T = void> implements Promise<T>, ICancelablePromise<T> {
7
+ private _pending;
8
+ private readonly _internals;
9
+ get pending(): boolean;
10
+ /** @deprecated: Use resolve, then and catch directly; use asPromise to return a promise type. */ get promise(): Promise<T>;
11
+ constructor();
12
+ resolve(val: T): void;
13
+ reject(reason: Error): void;
14
+ then<R = T, S = T>(onFulfilled: ((value: T) => R | PromiseLike<R>) | null | undefined, onRejected?: ((reason: Error) => S | PromiseLike<S>) | null | undefined): Promise<R | S>;
15
+ catch<R = T>(onRejected: ((reason: Error) => R | PromiseLike<R>) | null | undefined): Promise<T | R>;
16
+ finally(onfinally: (() => void) | null | undefined): Promise<T>;
17
+ cancel(): void;
18
+ get asPromise(): Promise<T>;
19
+ get asCancelablePromise(): ICancelablePromise<T>;
20
+ protected createInternals(): {
21
+ promise: Promise<T>;
22
+ resolve: (value: T) => void;
23
+ reject: (reason: Error) => void;
24
+ };
25
+ get [Symbol.toStringTag](): string;
26
+ }
27
+ export default Deferred;
@@ -0,0 +1,12 @@
1
+ import { TFunction } 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;
@@ -0,0 +1,10 @@
1
+ import { TAsyncProducer, TProducer, TimeDuration } from "..";
2
+ export declare class Semaphore {
3
+ private _availableSlots;
4
+ private readonly _waitingRequests;
5
+ constructor(_availableSlots?: number);
6
+ private _awaitSlot;
7
+ private _releaseSlot;
8
+ execute<T>(fn: TAsyncProducer<T> | TProducer<T>, cooldown?: TimeDuration): Promise<T>;
9
+ }
10
+ export default Semaphore;
@@ -0,0 +1,4 @@
1
+ export * from './CancelableDeferred.ts';
2
+ export * from './Deferred';
3
+ export * from './RateThrottler';
4
+ export * from './Semaphore';
@@ -0,0 +1,11 @@
1
+ export { default as Lazy } from './Lazy';
2
+ export { default as LazyAsync } from './LazyAsync';
3
+ export { default as Logger } from './Logger';
4
+ export * from './Optional.js';
5
+ export * from './async';
6
+ export * from './random';
7
+ export * from './sorting/index.js';
8
+ export * from './time';
9
+ export * from './types';
10
+ export * from './upgrade';
11
+ export * from './utils';
@@ -0,0 +1,2 @@
1
+ export { default as randomInterval } from './randomInterval';
2
+ export { default as randomPercentage } from './randomPercentage';
@@ -0,0 +1 @@
1
+ export default function randomInterval(min: number, max: number): number;
@@ -0,0 +1 @@
1
+ export default function randomPercentage(min: number, max: number): number;
@@ -0,0 +1,57 @@
1
+ import { TFunction, TKeysOfType, TReadableArray } from "../types";
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 "../types";
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,4 @@
1
+ export * from './types.js';
2
+ export { default as ComparisonChain } from './ComparisonChain.js';
3
+ export * from './Sorter.js';
4
+ export * from './types.js';
@@ -0,0 +1,5 @@
1
+ import { TBiFunction } from "../types/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 { ICancelablePromise } from "../async/Deferred.js";
2
+ import { TComparisonResult } from "../sorting/index.js";
3
+ import { TIntervalHandle, TTimeoutHandle, TVoidFunction } from "../types";
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
+ promise(): ICancelablePromise<void>;
32
+ delay(cb: () => unknown): {
33
+ cancel: TVoidFunction;
34
+ };
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 "../types";
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
+ }