@zelgadis87/utils-core 5.2.1 → 5.2.3

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/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.1",
4
+ "version": "5.2.3",
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.2.0",
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.8",
28
- "eslint": "9.22.0",
29
- "nx": "21.3.5",
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.26.1",
31
+ "typescript-eslint": "8.43.0",
33
32
  "vitest": "3.2.4"
34
33
  },
35
34
  "dependencies": {
@@ -149,7 +149,7 @@ const next = <T, R>( fns: ReadonlyArray<TComparisonFunction<T>>, transform: TFun
149
149
  return compareNumbers( fns, transform as TFunction<T, number>, { direction: 'DESC', ...opts } );
150
150
  },
151
151
  } as const satisfies TSorterStepForNumbers<R, TSorter<T>>;
152
- const retForStrings = {
152
+ const retForStringsV1 = {
153
153
  ...retAsUsing,
154
154
  inLexographicalOrder( opts: { nullsFirst?: boolean } = {} ) {
155
155
  return compareStrings( fns, transform as TFunction<T, string>, { direction: 'ASC', ignoreCase: false, ...opts } );
@@ -163,6 +163,22 @@ const next = <T, R>( fns: ReadonlyArray<TComparisonFunction<T>>, transform: TFun
163
163
  inReverseLexographicalOrderIgnoringCase( opts: { nullsFirst?: boolean } = {} ) {
164
164
  return compareStrings( fns, transform as TFunction<T, string>, { direction: 'DESC', ignoreCase: true, ...opts } );
165
165
  },
166
+ } as const;
167
+ const retForStrings = {
168
+ ...retAsUsing,
169
+ ...retForStringsV1,
170
+ inLexicographicalOrder( opts: { nullsFirst?: boolean } = {} ) {
171
+ return compareStrings( fns, transform as TFunction<T, string>, { direction: 'ASC', ignoreCase: false, ...opts } );
172
+ },
173
+ inLexicographicalOrderIgnoringCase( opts: { nullsFirst?: boolean } = {} ) {
174
+ return compareStrings( fns, transform as TFunction<T, string>, { direction: 'ASC', ignoreCase: true, ...opts } );
175
+ },
176
+ inReverseLexicographicalOrder( opts: { nullsFirst?: boolean } = {} ) {
177
+ return compareStrings( fns, transform as TFunction<T, string>, { direction: 'DESC', ignoreCase: false, ...opts } );
178
+ },
179
+ inReverseLexicographicalOrderIgnoringCase( opts: { nullsFirst?: boolean } = {} ) {
180
+ return compareStrings( fns, transform as TFunction<T, string>, { direction: 'DESC', ignoreCase: true, ...opts } );
181
+ },
166
182
  } as const satisfies TSorterStepForStrings<R, TSorter<T>>;
167
183
  const retForBooleans = {
168
184
  ...retAsUsing,
@@ -317,10 +333,14 @@ type TSorterStepForNumbers<_T, Ret> = {
317
333
  inDescendingOrder( opts?: { nullsFirst?: boolean } ): Ret;
318
334
  };
319
335
  type TSorterStepForStrings<_T, Ret> = {
320
- inLexographicalOrder( opts?: { nullsFirst?: boolean } ): Ret;
321
- inLexographicalOrderIgnoringCase( opts?: { nullsFirst?: boolean } ): Ret;
322
- inReverseLexographicalOrder( opts?: { nullsFirst?: boolean } ): Ret;
323
- inReverseLexographicalOrderIgnoringCase( opts?: { nullsFirst?: boolean } ): Ret;
336
+ /** @deprecated: Use inLexographicalOrder instead */ inLexographicalOrder( opts?: { nullsFirst?: boolean } ): Ret;
337
+ /** @deprecated: Use inLexographicalOrderIgnoringCase instead */ inLexographicalOrderIgnoringCase( opts?: { nullsFirst?: boolean } ): Ret;
338
+ /** @deprecated: Use inReverseLexographicalOrder instead */ inReverseLexographicalOrder( opts?: { nullsFirst?: boolean } ): Ret;
339
+ /** @deprecated: Use inReverseLexographicalOrderIgnoringCase instead */ inReverseLexographicalOrderIgnoringCase( opts?: { nullsFirst?: boolean } ): Ret;
340
+ inLexicographicalOrder( opts?: { nullsFirst?: boolean } ): Ret;
341
+ inLexicographicalOrderIgnoringCase( opts?: { nullsFirst?: boolean } ): Ret;
342
+ inReverseLexicographicalOrder( opts?: { nullsFirst?: boolean } ): Ret;
343
+ inReverseLexicographicalOrderIgnoringCase( opts?: { nullsFirst?: boolean } ): Ret;
324
344
  };
325
345
  type TSorterStepForBooleans<_T, Ret> = {
326
346
  truesFirst( opts?: { nullsFirst?: boolean } ): Ret;
@@ -1,13 +1,14 @@
1
1
 
2
2
  export * from './arrays.js';
3
3
  export * from './booleans.js';
4
- export { TCssDeclarationRulesDictionary, TCssSelectorDeclarationRulesDictionary, cssDeclarationRulesDictionaryToCss, transformCssDictionary } from './css.js';
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,3 +1,4 @@
1
+ import Optional, { type TOptional } from "../Optional.js";
1
2
  import type { TComparisonFunction } from "../sorting/_index.js";
2
3
  import type { TAccumulator, TBiPredicate, TPredicate, TTransformer } from "./functions.js";
3
4
  import { isNullOrUndefined, type TMaybe } from "./nulls.js";
@@ -171,3 +172,19 @@ export function shallowArrayEquals( a: unknown[], b: unknown[] ): boolean {
171
172
  }
172
173
  return true;
173
174
  }
175
+
176
+ export function findInArray<T>( arr: T[], predicate: TPredicate<T> ): TOptional<T> {
177
+ return Optional.ofNullable( arr.find( predicate ) );
178
+ }
179
+
180
+ export function zip<T, R>( ts: T[], rs: R[] ): [ T, R ][] {
181
+ if ( ts.length !== rs.length )
182
+ throw new Error( `Arrays must have the same length. Got ${ts.length} and ${rs.length}` );
183
+ return ts.map( ( t, i ) => [ t, rs[ i ]! ] as [ T, R ] );
184
+ }
185
+
186
+ export function unzip<T, R>( arr: [ T, R ][] ): [ T[], R[] ] {
187
+ return arr.reduce( ( [ ts, rs ], [ t, r ] ) => {
188
+ return [ [ ...ts, t ], [ ...rs, r ] ];
189
+ }, [ [], [] ] as [ T[], R[] ] );
190
+ }
@@ -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 ): [ R, undefined ] | [ undefined, Error ] {
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 ): Promise<[ R, undefined ] | [ undefined, Error ]> {
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 ) {
@@ -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>>;
@@ -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;