@zelgadis87/utils-core 5.0.0 → 5.0.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/lazy/LazyDictionary.d.ts +9 -0
- package/dist/lazy/_index.d.ts +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/errors.d.ts +2 -0
- package/dist/utils/functions/_index.d.ts +3 -0
- package/dist/utils/functions/predicateBuilder.d.ts +7 -0
- package/dist/utils/functions.d.ts +1 -2
- package/dist/utils/records.d.ts +10 -0
- package/esbuild/index.cjs +65 -0
- package/esbuild/index.cjs.map +3 -3
- package/esbuild/index.mjs +61 -0
- package/esbuild/index.mjs.map +3 -3
- package/package.json +1 -1
- package/src/lazy/LazyDictionary.ts +33 -0
- package/src/lazy/_index.ts +2 -0
- package/src/utils/errors.ts +13 -0
- package/src/utils/functions/_index.ts +4 -0
- package/src/utils/functions/predicateBuilder.ts +25 -0
- package/src/utils/functions.ts +2 -5
- package/src/utils/records.ts +10 -0
package/package.json
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { TFunction } from "../utils/_index.ts";
|
|
2
|
+
|
|
3
|
+
export class LazyDictionary<K extends string | number | symbol, T> {
|
|
4
|
+
|
|
5
|
+
private readonly _dictionary: Partial<Record<K, T>> = {};
|
|
6
|
+
|
|
7
|
+
private constructor(
|
|
8
|
+
private readonly _generator: TFunction<K, T>
|
|
9
|
+
) { }
|
|
10
|
+
|
|
11
|
+
public static of<K extends string | number | symbol, T>( generator: TFunction<K, T> ) {
|
|
12
|
+
return new LazyDictionary<K, T>( generator );
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public getOrCreate( key: K ): T {
|
|
16
|
+
if ( key in this._dictionary ) {
|
|
17
|
+
return this._dictionary[ key ] as T;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const value = this._generator( key );
|
|
21
|
+
this._dictionary[ key ] = value;
|
|
22
|
+
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public getOrThrow( key: K , errorMessage?: "Value not initialized" ): T {
|
|
27
|
+
if ( key in this._dictionary ) {
|
|
28
|
+
return this._dictionary[ key ] as T;
|
|
29
|
+
}
|
|
30
|
+
throw new Error( errorMessage );
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
}
|
package/src/lazy/_index.ts
CHANGED
package/src/utils/errors.ts
CHANGED
|
@@ -17,3 +17,16 @@ export function isError( e: unknown ): e is Error {
|
|
|
17
17
|
return e instanceof Error;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
export function printErrorMessage( error: Error ): string {
|
|
21
|
+
const maybeCause = error.cause ? printErrorMessage( asError( error.cause ) ) : null;
|
|
22
|
+
const cause = maybeCause ? `\ncaused by: ${maybeCause}` : '';
|
|
23
|
+
return `${error.name}: ${error.message}${cause}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function printErrorStack( error: Error ): string {
|
|
27
|
+
const maybeCause = error.cause ? printErrorStack( asError( error.cause ) ) : null;
|
|
28
|
+
const cause = maybeCause ? `\ncaused by: ${maybeCause}` : '';
|
|
29
|
+
const stack = error.stack && error.stack.includes( error.message ) ? error.stack : error.message + ' ' + ( error.stack ?? '' );
|
|
30
|
+
return `${error.name}: ${stack}${cause}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
import { constantTrue, type TPredicate } from "../functions.ts";
|
|
3
|
+
|
|
4
|
+
export class PredicateBuilder<T> {
|
|
5
|
+
|
|
6
|
+
private _currentPredicate: TPredicate<T> = constantTrue;
|
|
7
|
+
|
|
8
|
+
public and( predicate: TPredicate<T> ) {
|
|
9
|
+
const curPredicate = this._currentPredicate.bind( undefined );
|
|
10
|
+
const newPredicate = ( t: T ) => curPredicate( t ) && predicate( t );
|
|
11
|
+
this._currentPredicate = newPredicate;
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public or( predicate: TPredicate<T> ) {
|
|
16
|
+
const newPredicate = ( t: T ) => this._currentPredicate( t ) || predicate( t );
|
|
17
|
+
this._currentPredicate = newPredicate;
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public toPredicate() {
|
|
22
|
+
return this._currentPredicate;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
package/src/utils/functions.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import { identity } from './
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export * from './functions/constant.js';
|
|
5
|
-
export * from './functions/iff.js';
|
|
1
|
+
import { constantTrue, identity } from './functions/constant.js';
|
|
2
|
+
export * from './functions/_index.js';
|
|
6
3
|
|
|
7
4
|
export type TFunction<A, R> = ( a: A ) => R;
|
|
8
5
|
export type TTransformer<A, R> = TFunction<A, R>;
|
package/src/utils/records.ts
CHANGED
|
@@ -38,3 +38,13 @@ export type TWithRequiredProperty<T, K extends keyof T> = T & {
|
|
|
38
38
|
* Given a type T, make all fields required instead of optional.
|
|
39
39
|
*/
|
|
40
40
|
export type TWithRequiredProperties<T> = Required<T>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Type that forces a record to contain a few properties, but allows for extraneous properties.
|
|
44
|
+
*/
|
|
45
|
+
export type TWithExtras<R extends Record<any, any>, T extends Record<any, any> = Record<string, unknown>> = R & T;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Types that improves the human readability of a Typescript type.
|
|
49
|
+
*/
|
|
50
|
+
export type TPrettify<T> = { [ K in keyof T ]: T[ K ]; } & {};
|