@zelgadis87/utils-core 5.2.1 → 5.2.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/CHANGELOG.md +231 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/_index.d.ts +2 -1
- package/dist/utils/errors/withTryCatch.d.ts +3 -2
- package/dist/utils/errors.d.ts +13 -0
- package/dist/utils/numbers.d.ts +1 -1
- package/dist/utils/operations.d.ts +18 -0
- package/dist/utils/records.d.ts +21 -0
- package/esbuild/index.cjs +12 -1
- package/esbuild/index.cjs.map +2 -2
- package/esbuild/index.mjs +11 -1
- package/esbuild/index.mjs.map +2 -2
- package/package.json +8 -9
- package/src/utils/_index.ts +2 -1
- package/src/utils/arrays/indexBy.ts +5 -1
- package/src/utils/errors/withTryCatch.ts +3 -3
- package/src/utils/errors.ts +17 -0
- package/src/utils/operations.ts +24 -0
- package/src/utils/records.ts +20 -0
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@zelgadis87/utils-core",
|
|
4
|
-
"version": "5.2.
|
|
4
|
+
"version": "5.2.2",
|
|
5
5
|
"author": "Zelgadis87",
|
|
6
6
|
"license": "ISC",
|
|
7
7
|
"private": false,
|
|
8
8
|
"files": [
|
|
9
9
|
"/src",
|
|
10
10
|
"/dist",
|
|
11
|
-
"/esbuild"
|
|
11
|
+
"/esbuild",
|
|
12
|
+
"*.md"
|
|
12
13
|
],
|
|
13
14
|
"types": "./dist/index.d.ts",
|
|
14
15
|
"exports": {
|
|
@@ -19,17 +20,15 @@
|
|
|
19
20
|
}
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
|
-
"@stylistic/eslint-plugin-ts": "4.
|
|
23
|
-
"@typescript-eslint/eslint-plugin": "8.26.1",
|
|
24
|
-
"@typescript-eslint/parser": "8.26.1",
|
|
23
|
+
"@stylistic/eslint-plugin-ts": "4.4.1",
|
|
25
24
|
"@vitest/coverage-v8": "3.2.4",
|
|
26
25
|
"@vitest/ui": "3.2.4",
|
|
27
|
-
"esbuild": "0.25.
|
|
28
|
-
"eslint": "9.
|
|
29
|
-
"nx": "21.
|
|
26
|
+
"esbuild": "0.25.9",
|
|
27
|
+
"eslint": "9.35.0",
|
|
28
|
+
"nx": "21.5.2",
|
|
30
29
|
"rimraf": "6.0.1",
|
|
31
30
|
"typescript": "5.8.3",
|
|
32
|
-
"typescript-eslint": "8.
|
|
31
|
+
"typescript-eslint": "8.43.0",
|
|
33
32
|
"vitest": "3.2.4"
|
|
34
33
|
},
|
|
35
34
|
"dependencies": {
|
package/src/utils/_index.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
|
|
2
2
|
export * from './arrays.js';
|
|
3
3
|
export * from './booleans.js';
|
|
4
|
-
export { TCssDeclarationRulesDictionary, TCssSelectorDeclarationRulesDictionary,
|
|
4
|
+
export { cssDeclarationRulesDictionaryToCss, TCssDeclarationRulesDictionary, TCssSelectorDeclarationRulesDictionary, transformCssDictionary } from './css.js';
|
|
5
5
|
export * from './empties.js';
|
|
6
6
|
export * from './errors.js';
|
|
7
7
|
export * from './functions.js';
|
|
8
8
|
export * from './json.js';
|
|
9
9
|
export * from './nulls.js';
|
|
10
10
|
export * from './numbers.js';
|
|
11
|
+
export * from './operations.js';
|
|
11
12
|
export * from './primitives.js';
|
|
12
13
|
export * from './promises.js';
|
|
13
14
|
export * from './random.js';
|
|
@@ -28,8 +28,12 @@ export function indexBySymbolWith<V>( arr: V[], getter: TFunction<V, symbol> ):
|
|
|
28
28
|
function doIndexByWith<K extends string | number | symbol, V>( arr: V[], getter: TFunction<V, K> ): Record<K, V> {
|
|
29
29
|
return arr.reduce( ( dict, cur ) => {
|
|
30
30
|
const key = getter( cur );
|
|
31
|
-
if ( key !== null && key !== undefined )
|
|
31
|
+
if ( key !== null && key !== undefined ) {
|
|
32
|
+
if ( dict[ key ] ) {
|
|
33
|
+
throw new Error( `Multiple values indexed by same key "${ String( key ) }".` );
|
|
34
|
+
}
|
|
32
35
|
dict[ key ] = cur;
|
|
36
|
+
}
|
|
33
37
|
return dict;
|
|
34
38
|
}, {} as Record<K, V> );
|
|
35
39
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
1
|
import { asError } from "../errors.js";
|
|
3
2
|
import { identity, type TFunction, type TTransformer } from "../functions.js";
|
|
3
|
+
import type { TAsyncOperationTuple, TOperationTuple } from "../operations.js";
|
|
4
4
|
|
|
5
|
-
export function withTryCatch<R>( fn: TFunction<void, R>, errMapFn: TTransformer<Error, Error> = identity ):
|
|
5
|
+
export function withTryCatch<R>( fn: TFunction<void, R>, errMapFn: TTransformer<Error, Error> = identity ): TOperationTuple<R, Error> {
|
|
6
6
|
try {
|
|
7
7
|
return [ fn(), void 0 ] as const;
|
|
8
8
|
} catch ( e: unknown ) {
|
|
@@ -10,7 +10,7 @@ export function withTryCatch<R>( fn: TFunction<void, R>, errMapFn: TTransformer<
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export async function withTryCatchAsync<R>( fn: TFunction<void, Promise<R>>, errMapFn: TTransformer<Error, Error> = identity ):
|
|
13
|
+
export async function withTryCatchAsync<R>( fn: TFunction<void, Promise<R>>, errMapFn: TTransformer<Error, Error> = identity ): TAsyncOperationTuple<R, Error> {
|
|
14
14
|
try {
|
|
15
15
|
return [ await fn(), void 0 ] as const;
|
|
16
16
|
} catch ( e: unknown ) {
|
package/src/utils/errors.ts
CHANGED
|
@@ -39,3 +39,20 @@ export function getCauseStackFromError( error: Error ) {
|
|
|
39
39
|
return `\ncaused by: ${getStackFromError( asError( error.cause ) )}`
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Throws an error if a switch discriminated union type does not cover all the expected cases.
|
|
44
|
+
* Expected usage: `
|
|
45
|
+
* switch ( x.type ) {
|
|
46
|
+
* case "x": return doX();
|
|
47
|
+
* case "y": return doY();
|
|
48
|
+
* ...
|
|
49
|
+
* default: throw new NonExhaustiveSwitchError( x satisfies never );
|
|
50
|
+
* }`
|
|
51
|
+
*/
|
|
52
|
+
export class NonExhaustiveSwitchError extends Error {
|
|
53
|
+
|
|
54
|
+
constructor( value: unknown ) {
|
|
55
|
+
super( "Expected switch to be exhaustive, got: " + value );
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
|
|
2
|
+
export type TOperation<T, E extends Error = Error> = {
|
|
3
|
+
success: false;
|
|
4
|
+
error: E;
|
|
5
|
+
} | {
|
|
6
|
+
success: true;
|
|
7
|
+
data: T;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type TAsyncOperation<T, E extends Error = Error> = Promise<TOperation<T, E>>;
|
|
11
|
+
|
|
12
|
+
export type TOperationTuple<T, E extends Error = Error> = [ T, undefined ] | [ undefined, E ];
|
|
13
|
+
|
|
14
|
+
export type TAsyncOperationTuple<T, E extends Error = Error> = Promise<TOperationTuple<T, E>>;
|
|
15
|
+
|
|
16
|
+
export type TValidation<T, E extends Error = Error> = {
|
|
17
|
+
success: false;
|
|
18
|
+
errors: E[];
|
|
19
|
+
} | {
|
|
20
|
+
success: true;
|
|
21
|
+
data: T;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type TAsyncValidation<T, E extends Error = Error> = Promise<TValidation<T, E>>;
|
package/src/utils/records.ts
CHANGED
|
@@ -58,3 +58,23 @@ export function shallowRecordEquals( a: Record<string, unknown>, b: Record<strin
|
|
|
58
58
|
}
|
|
59
59
|
return true;
|
|
60
60
|
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A utility type that checks if any property in a record type `T` has the `never` type.
|
|
64
|
+
* This type evaluates to `true` if at least one property in `T` is of type `never`, and `false` otherwise.
|
|
65
|
+
* It's commonly used to create compile-time assertions that prevent unintended changes where properties might accidentally become `never` due to type mismatches.
|
|
66
|
+
*
|
|
67
|
+
* @example Advanced usage for type guards
|
|
68
|
+
* ```typescript
|
|
69
|
+
* type TMap = { a: string; b: number; c: boolean };
|
|
70
|
+
* type TJsonSerializable = string | number | boolean;
|
|
71
|
+
*
|
|
72
|
+
* type TFiltered = {
|
|
73
|
+
* [K in keyof TMap]: TMap[K] extends TJsonSerializable ? TMap[K] : never;
|
|
74
|
+
* };
|
|
75
|
+
*
|
|
76
|
+
* // If TMap changes and some property becomes not serializable, TFiltered will have 'never' values
|
|
77
|
+
* const _guard: THasNever<TFiltered> = false; // Compile error if any property became 'never'
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export type THasNever<T> = { [ K in keyof T ]: [ T[ K ] ] extends [ never ] ? true : false }[ keyof T ] extends false ? false : true;
|