@zelgadis87/utils-core 5.2.12 → 5.3.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.
Files changed (79) hide show
  1. package/.rollup/index.cjs +3448 -0
  2. package/.rollup/index.cjs.map +1 -0
  3. package/.rollup/index.d.ts +1409 -0
  4. package/.rollup/index.mjs +3268 -0
  5. package/.rollup/index.mjs.map +1 -0
  6. package/.rollup/tsconfig.tsbuildinfo +1 -0
  7. package/CHANGELOG.md +17 -0
  8. package/package.json +6 -8
  9. package/src/lazy/Cached.ts +216 -0
  10. package/src/lazy/Lazy.ts +4 -0
  11. package/src/lazy/LazyAsync.ts +4 -0
  12. package/src/lazy/_index.ts +1 -0
  13. package/src/utils/arrays/indexBy.ts +17 -17
  14. package/src/utils/arrays.ts +3 -1
  15. package/dist/Logger.d.ts +0 -22
  16. package/dist/Optional.d.ts +0 -108
  17. package/dist/async/CancelableDeferred.d.ts +0 -17
  18. package/dist/async/Deferred.d.ts +0 -32
  19. package/dist/async/RateThrottler.d.ts +0 -13
  20. package/dist/async/Semaphore.d.ts +0 -17
  21. package/dist/async/_index.d.ts +0 -3
  22. package/dist/index.d.ts +0 -8
  23. package/dist/lazy/Lazy.d.ts +0 -22
  24. package/dist/lazy/LazyAsync.d.ts +0 -24
  25. package/dist/lazy/LazyDictionary.d.ts +0 -9
  26. package/dist/lazy/_index.d.ts +0 -3
  27. package/dist/sorting/ComparisonChain.d.ts +0 -57
  28. package/dist/sorting/Sorter.d.ts +0 -121
  29. package/dist/sorting/_index.d.ts +0 -2
  30. package/dist/sorting/types.d.ts +0 -5
  31. package/dist/time/RandomTimeDuration.d.ts +0 -9
  32. package/dist/time/TimeBase.d.ts +0 -49
  33. package/dist/time/TimeDuration.d.ts +0 -71
  34. package/dist/time/TimeFrequency.d.ts +0 -10
  35. package/dist/time/TimeInstant.d.ts +0 -174
  36. package/dist/time/TimeInstantBuilder.d.ts +0 -72
  37. package/dist/time/TimeRange.d.ts +0 -11
  38. package/dist/time/TimeUnit.d.ts +0 -15
  39. package/dist/time/_index.d.ts +0 -8
  40. package/dist/time/constants.d.ts +0 -14
  41. package/dist/time/types.d.ts +0 -29
  42. package/dist/tsconfig.tsbuildinfo +0 -1
  43. package/dist/upgrade/DataUpgrader.d.ts +0 -22
  44. package/dist/upgrade/_index.d.ts +0 -2
  45. package/dist/upgrade/errors.d.ts +0 -12
  46. package/dist/upgrade/getTransitionsPath.d.ts +0 -3
  47. package/dist/upgrade/types.d.ts +0 -31
  48. package/dist/utils/_index.d.ts +0 -15
  49. package/dist/utils/arrays/groupBy.d.ts +0 -9
  50. package/dist/utils/arrays/indexBy.d.ts +0 -7
  51. package/dist/utils/arrays/statistics.d.ts +0 -8
  52. package/dist/utils/arrays/uniqBy.d.ts +0 -4
  53. package/dist/utils/arrays.d.ts +0 -71
  54. package/dist/utils/booleans.d.ts +0 -2
  55. package/dist/utils/css.d.ts +0 -14
  56. package/dist/utils/empties.d.ts +0 -18
  57. package/dist/utils/errors/withTryCatch.d.ts +0 -4
  58. package/dist/utils/errors.d.ts +0 -20
  59. package/dist/utils/functions/_index.d.ts +0 -3
  60. package/dist/utils/functions/constant.d.ts +0 -11
  61. package/dist/utils/functions/iff.d.ts +0 -8
  62. package/dist/utils/functions/predicateBuilder.d.ts +0 -7
  63. package/dist/utils/functions.d.ts +0 -41
  64. package/dist/utils/json.d.ts +0 -11
  65. package/dist/utils/nulls.d.ts +0 -9
  66. package/dist/utils/numbers/round.d.ts +0 -8
  67. package/dist/utils/numbers.d.ts +0 -35
  68. package/dist/utils/operations.d.ts +0 -28
  69. package/dist/utils/primitives.d.ts +0 -3
  70. package/dist/utils/promises.d.ts +0 -10
  71. package/dist/utils/random.d.ts +0 -3
  72. package/dist/utils/records/entries.d.ts +0 -7
  73. package/dist/utils/records.d.ts +0 -63
  74. package/dist/utils/strings/StringParts.d.ts +0 -38
  75. package/dist/utils/strings.d.ts +0 -20
  76. package/esbuild/index.cjs +0 -3380
  77. package/esbuild/index.cjs.map +0 -7
  78. package/esbuild/index.mjs +0 -3177
  79. package/esbuild/index.mjs.map +0 -7
@@ -1,40 +1,40 @@
1
1
 
2
- import { TFunction, TKeysOfType } from "../_index";
2
+ import { TFunction, TKeysOfType, identity } from "../_index";
3
3
 
4
- export function indexByString<V, K extends TKeysOfType<V, string>>( arr: V[], field: K ): Record<V[K] & string, V> {
5
- return indexByStringWith( arr, t => t[ field ] as string );
4
+ export function indexByString<V, K extends TKeysOfType<V, string>, R = V>( arr: V[], field: K, valueMapper: TFunction<V, R> = v => v as unknown as R ): Record<V[K] & string, R> {
5
+ return indexByStringWith( arr, t => t[ field ] as string, valueMapper );
6
6
  }
7
7
 
8
- export function indexByNumber<V, K extends TKeysOfType<V, number>>( arr: V[], field: K ): Record<V[K] & number, V> {
9
- return indexByNumberWith( arr, t => t[ field ] as number );
8
+ export function indexByNumber<V, K extends TKeysOfType<V, number>, R = V>( arr: V[], field: K, valueMapper: TFunction<V, R> = v => v as unknown as R ): Record<V[K] & number, R> {
9
+ return indexByNumberWith( arr, t => t[ field ] as number, valueMapper );
10
10
  }
11
11
 
12
- export function indexBySymbol<V, K extends TKeysOfType<V, symbol>>( arr: V[], field: K ): Record<V[K] & symbol, V> {
13
- return indexBySymbolWith( arr, t => t[ field ] as symbol );
12
+ export function indexBySymbol<V, K extends TKeysOfType<V, symbol>, R = V>( arr: V[], field: K, valueMapper: TFunction<V, R> = v => v as unknown as R ): Record<V[K] & symbol, R> {
13
+ return indexBySymbolWith( arr, t => t[ field ] as symbol, valueMapper );
14
14
  }
15
15
 
16
- export function indexByStringWith<V>( arr: V[], getter: TFunction<V, string> ): Record<string, V> {
17
- return doIndexByWith<string, V>( arr, getter );
16
+ export function indexByStringWith<V, R = V>( arr: V[], keyGetter: TFunction<V, string>, valueMapper: TFunction<V, R> = v => v as unknown as R ): Record<string, R> {
17
+ return doIndexByWith<string, V, R>( arr, keyGetter, valueMapper );
18
18
  }
19
19
 
20
- export function indexByNumberWith<V>( arr: V[], getter: TFunction<V, number> ): Record<number, V> {
21
- return doIndexByWith<number, V>( arr, getter );
20
+ export function indexByNumberWith<V, R = V>( arr: V[], keyGetter: TFunction<V, number>, valueMapper: TFunction<V, R> = v => v as unknown as R ): Record<number, R> {
21
+ return doIndexByWith<number, V, R>( arr, keyGetter, valueMapper );
22
22
  }
23
23
 
24
- export function indexBySymbolWith<V>( arr: V[], getter: TFunction<V, symbol> ): Record<symbol, V> {
25
- return doIndexByWith<symbol, V>( arr, getter );
24
+ export function indexBySymbolWith<V, R = V>( arr: V[], keyGetter: TFunction<V, symbol>, valueMapper: TFunction<V, R> = v => v as unknown as R ): Record<symbol, R> {
25
+ return doIndexByWith<symbol, V, R>( arr, keyGetter, valueMapper );
26
26
  }
27
27
 
28
- function doIndexByWith<K extends string | number | symbol, V>( arr: V[], getter: TFunction<V, K> ): Record<K, V> {
28
+ function doIndexByWith<K extends string | number | symbol, V, R = V>( arr: V[], keyGetter: TFunction<V, K>, valueMapper: TFunction<V, R> = v => v as unknown as R ): Record<K, R> {
29
29
  return arr.reduce( ( dict, cur ) => {
30
- const key = getter( cur );
30
+ const key = keyGetter( cur );
31
31
  if ( key !== null && key !== undefined ) {
32
32
  if ( dict[ key ] ) {
33
33
  throw new Error( `Multiple values indexed by same key "${ String( key ) }".` );
34
34
  }
35
- dict[ key ] = cur;
35
+ dict[ key ] = valueMapper( cur );
36
36
  }
37
37
  return dict;
38
- }, {} as Record<K, V> );
38
+ }, {} as Record<K, R> );
39
39
  }
40
40
 
@@ -96,9 +96,11 @@ export function sortedArray<T>( arr: TReadableArray<T>, sortFn?: TComparisonFunc
96
96
  /**
97
97
  * Custom implementation of includes which allows checking for arbitrary items inside the array.
98
98
  */
99
- export function includes<T>( arr: TReadableArray<T>, item: unknown, fromIndex?: number ) {
99
+ export function arrayIncludes<T>( arr: TReadableArray<T>, item: unknown, fromIndex?: number ) {
100
100
  return arr.includes( item as any, fromIndex );
101
101
  }
102
+ /** @deprecated[2025.11.08]: Use {@link arrayIncludes} instead. */
103
+ export const includes= arrayIncludes;
102
104
 
103
105
  export function mapTruthys<T, R>( arr: Array<T>, mapper: ( value: T, index: number, array: T[] ) => R | undefined ): Array<R> {
104
106
  return arr.map( mapper ).filter( value => value !== undefined ) as Array<R>;
package/dist/Logger.d.ts DELETED
@@ -1,22 +0,0 @@
1
- declare const LEVELS: readonly ["trace", "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 trace(): (...args: any[]) => void;
15
- get log(): (...args: any[]) => void;
16
- get debug(): (...args: any[]) => void;
17
- get info(): (...args: any[]) => void;
18
- get warn(): (...args: any[]) => void;
19
- get error(): (...args: any[]) => void;
20
- private logWrap;
21
- }
22
- export default Logger;
@@ -1,108 +0,0 @@
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
- getRawValue(): T | undefined;
7
- get(): T;
8
- getOrElseThrow(errorProducer: TProducer<Error>): T;
9
- set(t: T): void;
10
- protected setNullable(t: T | null | undefined): void | this;
11
- clear(): void;
12
- isEmpty(): this is TEmptyOptional<T>;
13
- isPresent(): this is TPresentOptional<T>;
14
- ifEmpty(callback: TVoidFunction): void;
15
- ifPresent(callback: TConsumer<T>): void;
16
- ifPresentThenClear(callback: TConsumer<T>): void;
17
- apply(callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction): any;
18
- orElseReturn(newValue: T): T;
19
- orElse: (newValue: T) => T;
20
- orElseProduce(newValueProducer: TProducer<T>): T;
21
- orElseGet: (newValueProducer: TProducer<T>) => T;
22
- orElseReturnAndApply(newValue: T): T;
23
- orElseProduceAndApply(newValueProducer: TProducer<T>): T;
24
- orElseReturnNullableAndApply(newValue: T | null | undefined): TOptional<T>;
25
- orElseProduceNullableAndApply(newValueProducer: TProducer<T | null | undefined>): TOptional<T>;
26
- orElseReturnNullable(newValue: T | null | undefined): any;
27
- orElseNullable: (newValue: T | null | undefined) => any;
28
- orElseProduceNullable(newValueProducer: TProducer<T | null | undefined>): any;
29
- orElseGetNullable: (newValueProducer: TProducer<T | null | undefined>) => any;
30
- orElseThrow(errorProducer: TProducer<Error>): TPresentOptional<T>;
31
- throwIfPresent(errorProducer: TFunction<T, Error>): any;
32
- mapTo<R>(mapper: TFunction<T, R | null | undefined>): any;
33
- flatMapTo<R>(mapper: TFunction<T, TOptional<R>>): any;
34
- filter(predicate: TPredicate<T>): this | TEmptyOptional<T>;
35
- static empty<T>(): TEmptyOptional<T>;
36
- static of<T>(t: T): TPresentOptional<T>;
37
- static ofNullable<T>(t: T | null | undefined): TOptional<T>;
38
- }
39
- export default Optional;
40
- export type TOptional<T> = {
41
- /**
42
- * @returns the currently stored value, if any; throws {@link ErrorGetEmptyOptional} otherwise;
43
- */
44
- get(): T;
45
- /**
46
- * @returns the currently stored value, if any; returns undefined otherwise;
47
- */
48
- getRawValue(): T | undefined;
49
- /**
50
- * @returns the currently stored value, if any; throws the error generated by errorProducer otherwise;
51
- */
52
- getOrElseThrow: (errorProducer: TProducer<Error>) => T;
53
- /**
54
- * Places a new value inside this optional. Throws {@link ErrorSetEmptyOptional} if t is null or undefined.
55
- */
56
- set(t: T): void;
57
- /**
58
- * Clears the current value from this optional, if any.
59
- */
60
- clear(): void;
61
- isEmpty(): boolean;
62
- isPresent(): boolean;
63
- ifEmpty(callback: TVoidFunction): void;
64
- ifPresent(callback: TConsumer<T>): void;
65
- apply<RP = void, RE = void>(callbackIfPresent: TFunction<T, RP>, callbackIfEmpty: TProducer<RE>): RP | RE;
66
- throwIfPresent: (errorGenerator: TFunction<T, Error>) => TEmptyOptional<T>;
67
- /** @deprecated[2025.07.25]: Replace with {@link orElseReturn} (drop-in replacement) */
68
- orElse: (newValue: T) => T;
69
- /** @deprecated[2025.07.25]: Replace with {@link orElseProduce} (drop-in replacement) */
70
- orElseGet: (newValueProducer: TProducer<T>) => T;
71
- /** @deprecated[2025.07.25]: Replace with {@link orElseReturnNullabe} (drop-in replacement) */
72
- orElseNullable: (newValue: T | null | undefined) => TOptional<T>;
73
- /** @deprecated[2025.07.25]: Replace with {@link orElseProduceNullable} (drop-in replacement) */
74
- orElseGetNullable: (newValueProducer: TProducer<T | null | undefined>) => TOptional<T>;
75
- orElseReturn: (newValue: T) => T;
76
- orElseProduce: (newValueProducer: TProducer<T>) => T;
77
- orElseReturnNullable: (newValue: T | null | undefined) => TOptional<T>;
78
- orElseProduceNullable: (newValueProducer: TProducer<T | null | undefined>) => TOptional<T>;
79
- orElseReturnAndApply: (newValue: T) => T;
80
- orElseProduceAndApply: (newValueProducer: TProducer<T>) => T;
81
- orElseReturnNullableAndApply: (newValue: T | null | undefined) => TOptional<T>;
82
- orElseProduceNullableAndApply: (newValueProducer: TProducer<T | null | undefined>) => TOptional<T>;
83
- orElseThrow: (errorProducer: TProducer<Error>) => TPresentOptional<T>;
84
- mapTo<R>(mapper: TFunction<T, R>): TOptional<R>;
85
- flatMapTo<R>(mapper: TFunction<T, TOptional<R>>): TOptional<R>;
86
- filter(predicate: TPredicate<T>): TOptional<T>;
87
- };
88
- export type TEmptyOptional<T> = TOptional<T> & {
89
- get(): never;
90
- isEmpty(): true;
91
- isPresent(): false;
92
- apply<RP = void, RE = void>(callbackIfPresent: TFunction<T, RP>, callbackIfEmpty: TProducer<RE>): RE;
93
- };
94
- export type TPresentOptional<T> = TOptional<T> & {
95
- get(): T;
96
- isEmpty(): false;
97
- isPresent(): true;
98
- apply<RP = void, RE = void>(callbackIfPresent: TFunction<T, RP>, callbackIfEmpty: TProducer<RE>): RP;
99
- };
100
- export declare class ErrorGetEmptyOptional extends Error {
101
- constructor();
102
- }
103
- export declare class ErrorSetEmptyOptional extends Error {
104
- constructor();
105
- }
106
- export declare class ErrorCannotInstantiatePresentOptionalWithEmptyValue extends Error {
107
- constructor();
108
- }
@@ -1,17 +0,0 @@
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 {};
@@ -1,32 +0,0 @@
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
- constructor();
14
- resolve(val: T): void;
15
- reject(reason: Error): void;
16
- cancel(): void;
17
- resolveIfPending(val: T): void;
18
- rejectIfPending(reason: Error): void;
19
- cancelIfPending(): void;
20
- then<R = T, S = T>(onFulfilled: ((value: T) => R | PromiseLike<R>) | null | undefined, onRejected?: ((reason: Error) => S | PromiseLike<S>) | null | undefined): Promise<R | S>;
21
- catch<R = T>(onRejected: ((reason: Error) => R | PromiseLike<R>) | null | undefined): Promise<T | R>;
22
- finally(onfinally: (() => void) | null | undefined): Promise<T>;
23
- asPromise(): Promise<T>;
24
- asCancelablePromise(): ICancelablePromise<T>;
25
- protected createInternals(): {
26
- promise: Promise<T>;
27
- resolve: (value: T) => void;
28
- reject: (reason: Error) => void;
29
- };
30
- get [Symbol.toStringTag](): string;
31
- }
32
- export default Deferred;
@@ -1,13 +0,0 @@
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>;
@@ -1,17 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- export * from './Deferred.ts';
2
- export * from './RateThrottler.ts';
3
- export * from './Semaphore.ts';
package/dist/index.d.ts DELETED
@@ -1,8 +0,0 @@
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';
@@ -1,22 +0,0 @@
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;
@@ -1,24 +0,0 @@
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;
@@ -1,9 +0,0 @@
1
- import type { TFunction } from "../utils/_index.ts";
2
- export declare class LazyDictionary<K extends string | number | symbol, T> {
3
- private readonly _generator;
4
- private readonly _dictionary;
5
- private constructor();
6
- static of<K extends string | number | symbol, T>(generator: TFunction<K, T>): LazyDictionary<K, T>;
7
- getOrCreate(key: K): T;
8
- getOrThrow(key: K, errorMessage?: "Value not initialized"): T;
9
- }
@@ -1,3 +0,0 @@
1
- export * from './Lazy.js';
2
- export * from './LazyAsync.js';
3
- export * from './LazyDictionary.js';
@@ -1,57 +0,0 @@
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>>;
@@ -1,121 +0,0 @@
1
- import type TimeDuration from "../time/TimeDuration.js";
2
- import type TimeInstant from "../time/TimeInstant.js";
3
- import { TConditionalParameterOptions, TFunction, TKeysOfType, TPositiveNumber, TReadableArray } from "../utils/_index.js";
4
- import { TComparisonFunction } from "./types.js";
5
- type TComparable<T> = {
6
- compare: TComparisonFunction<T>;
7
- };
8
- export type TSorter<T> = {
9
- get then(): TSorterStep<T, TSorter<T>>;
10
- reversed: () => TSorter<T>;
11
- compare: TComparisonFunction<T>;
12
- sort: (arr: TReadableArray<T>) => Array<T>;
13
- top: (arr: TReadableArray<T>, n: TPositiveNumber) => Array<T>;
14
- bottom: (arr: TReadableArray<T>, n: TPositiveNumber) => Array<T>;
15
- first: (arr: TReadableArray<T>) => T | null;
16
- last: (arr: TReadableArray<T>) => T | null;
17
- };
18
- type TSorterStepForRecords<T, Ret> = {
19
- usingStrings: (field: TKeysOfType<T, string>) => TSorterStepUsing<string, Ret>;
20
- usingNumbers: (field: TKeysOfType<T, number>) => TSorterStepUsing<number, Ret>;
21
- usingBooleans: (field: TKeysOfType<T, boolean>) => TSorterStepUsing<boolean, Ret>;
22
- usingDates: (field: TKeysOfType<T, Date>) => TSorterStepUsing<Date, Ret>;
23
- usingTimeInstant: (field: TKeysOfType<T, TimeInstant>) => TSorterStepUsing<TimeInstant, Ret>;
24
- usingTimeDuration: (field: TKeysOfType<T, TimeDuration>) => TSorterStepUsing<TimeDuration, Ret>;
25
- };
26
- type TSorterStepForNumbers<_T, Ret> = {
27
- inAscendingOrder(opts?: {
28
- nullsFirst?: boolean;
29
- }): Ret;
30
- inDescendingOrder(opts?: {
31
- nullsFirst?: boolean;
32
- }): Ret;
33
- };
34
- type TSorterStepForStrings<_T, Ret> = {
35
- /** @deprecated: Use inLexographicalOrder instead */ inLexographicalOrder(opts?: {
36
- nullsFirst?: boolean;
37
- }): Ret;
38
- /** @deprecated: Use inLexographicalOrderIgnoringCase instead */ inLexographicalOrderIgnoringCase(opts?: {
39
- nullsFirst?: boolean;
40
- }): Ret;
41
- /** @deprecated: Use inReverseLexographicalOrder instead */ inReverseLexographicalOrder(opts?: {
42
- nullsFirst?: boolean;
43
- }): Ret;
44
- /** @deprecated: Use inReverseLexographicalOrderIgnoringCase instead */ inReverseLexographicalOrderIgnoringCase(opts?: {
45
- nullsFirst?: boolean;
46
- }): Ret;
47
- inLexicographicalOrder(opts?: {
48
- nullsFirst?: boolean;
49
- }): Ret;
50
- inLexicographicalOrderIgnoringCase(opts?: {
51
- nullsFirst?: boolean;
52
- }): Ret;
53
- inReverseLexicographicalOrder(opts?: {
54
- nullsFirst?: boolean;
55
- }): Ret;
56
- inReverseLexicographicalOrderIgnoringCase(opts?: {
57
- nullsFirst?: boolean;
58
- }): Ret;
59
- };
60
- type TSorterStepForBooleans<_T, Ret> = {
61
- truesFirst(opts?: {
62
- nullsFirst?: boolean;
63
- }): Ret;
64
- falsesFirst(opts?: {
65
- nullsFirst?: boolean;
66
- }): Ret;
67
- };
68
- type TSorterStepForDates<_T, Ret> = {
69
- oldestDateFirst(opts?: {
70
- nullsFirst?: boolean;
71
- }): Ret;
72
- newestDateFirst(opts?: {
73
- nullsFirst?: boolean;
74
- }): Ret;
75
- };
76
- type TSorterStepForTimeInstant<_T, Ret> = {
77
- oldestFirst(opts?: {
78
- nullsFirst?: boolean;
79
- }): Ret;
80
- newestFirst(opts?: {
81
- nullsFirst?: boolean;
82
- }): Ret;
83
- };
84
- type TSorterStepForTimeDuration<_T, Ret> = {
85
- longestFirst(opts?: {
86
- nullsFirst?: boolean;
87
- }): Ret;
88
- shortestFirst(opts?: {
89
- nullsFirst?: boolean;
90
- }): Ret;
91
- };
92
- type TSorterStepForChainables<T, Ret> = {
93
- chain: (chain: TComparable<T>) => Ret;
94
- chainFunction: (fn: TComparisonFunction<T>) => Ret;
95
- };
96
- type TSorterStepForUsing<T, Ret> = {
97
- using: <X>(transform: TFunction<T, X>) => TSorterStepUsing<X, Ret>;
98
- };
99
- type TSorterStepForPrioritizable<T extends string | number, Ret> = {
100
- prioritizing(arr: T[], options?: TConditionalParameterOptions<TPrioritizeOptions, typeof defaultPrioritizeOptions>): Ret;
101
- deprioritizing(arr: Array<T>, options?: TConditionalParameterOptions<TPrioritizeOptions, typeof defaultPrioritizeOptions>): Ret;
102
- prioritizingWithEqualWeight(arr: T[], options?: TConditionalParameterOptions<TPrioritizeOptions, typeof defaultPrioritizeOptions>): Ret;
103
- deprioritizingWithEqualWeight(arr: Array<T>, options?: TConditionalParameterOptions<TPrioritizeOptions, typeof defaultPrioritizeOptions>): Ret;
104
- };
105
- type TSorterStepUsing<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 TimeInstant ? TSorterStepForTimeInstant<T, Ret> : {}) & (T extends TimeDuration ? TSorterStepForTimeDuration<T, Ret> : {}) & (T extends string ? TSorterStepForPrioritizable<string, Ret> : {}) & (T extends number ? TSorterStepForPrioritizable<number, Ret> : {});
106
- type TSorterStep<T, Ret> = TSorterStepUsing<T, Ret> & TSorterStepForChainables<T, Ret> & TSorterStepForUsing<T, Ret>;
107
- type TSorterStarter<T> = TSorterStepUsing<T, TSorter<T>> & TSorterStepForUsing<T, TSorter<T>> & {
108
- usingFunction: (fn: TComparisonFunction<T>) => TSorter<T>;
109
- };
110
- export type TSorterBuilder<T> = (builder: TSorterStarter<T>) => TSorter<T>;
111
- type TPrioritizeOptions = {};
112
- declare const defaultPrioritizeOptions: {};
113
- export declare const Sorter: {
114
- createFor: <T>(_template?: T) => TSorterStarter<T>;
115
- sort: <T>(arr: TReadableArray<T>, builder: TSorterBuilder<T>) => T[];
116
- top: <T>(arr: TReadableArray<T>, n: TPositiveNumber, builder: TSorterBuilder<T>) => T[];
117
- bottom: <T>(arr: TReadableArray<T>, n: TPositiveNumber, builder: TSorterBuilder<T>) => T[];
118
- first: <T>(arr: TReadableArray<T>, builder: TSorterBuilder<T>) => T | null;
119
- last: <T>(arr: TReadableArray<T>, builder: TSorterBuilder<T>) => T | null;
120
- };
121
- export default Sorter;
@@ -1,2 +0,0 @@
1
- export * from './Sorter.js';
2
- export * from './types.js';
@@ -1,5 +0,0 @@
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';
@@ -1,9 +0,0 @@
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
- }
@@ -1,49 +0,0 @@
1
- import { TimeUnit } from "./TimeUnit";
2
- export type TTimeInUnits = {
3
- milliseconds: number;
4
- seconds: number;
5
- minutes: number;
6
- hours: number;
7
- days: number;
8
- };
9
- export default abstract class TimeBase<T> {
10
- private readonly _ms;
11
- protected constructor(value: number, unit: TimeUnit);
12
- protected abstract create(value: number, unit: TimeUnit): T;
13
- /**
14
- * Total number of milliseconds, rounded down.
15
- */
16
- get ms(): number;
17
- /**
18
- * Total number of seconds, rounded down.
19
- */
20
- get seconds(): number;
21
- /**
22
- * Total number of minutes, rounded down.
23
- */
24
- get minutes(): number;
25
- /**
26
- * Total number of hours, rounded down.
27
- */
28
- get hours(): number;
29
- /**
30
- * Total number of days, rounded down.
31
- */
32
- get days(): number;
33
- addMs(milliseconds: number): T;
34
- addSeconds(seconds: number): T;
35
- addMinutes(minutes: number): T;
36
- addHours(hours: number): T;
37
- addDays(days: number): T;
38
- removeDays(days: number): T;
39
- addUnits(n: number, unit: TimeUnit): T;
40
- removeMs(milliseconds: number): T;
41
- removeSeconds(seconds: number): T;
42
- removeMinutes(minutes: number): T;
43
- removeHours(hours: number): T;
44
- removeUnits(n: number, unit: TimeUnit): T;
45
- getUnit(unit: TimeUnit): number;
46
- protected toUnits(): TTimeInUnits;
47
- protected static unitsToMs(units: Partial<TTimeInUnits>): number;
48
- toJSON(): number;
49
- }