@leancodepl/utils 8.5.0 → 8.6.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.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * React hook for generating keys based on current route matches.
3
+ *
4
+ * @template TKey - The type of the route keys
5
+ * @param routeMatches - Record of route keys to match objects or arrays
6
+ * @returns Array of active route keys
7
+ * @example
8
+ * ```typescript
9
+ * function NavigationComponent() {
10
+ * const routeMatches = {
11
+ * home: useRouteMatch('/'),
12
+ * about: useRouteMatch('/about'),
13
+ * contact: useRouteMatch('/contact')
14
+ * };
15
+ *
16
+ * const activeRoutes = useKeyByRoute(routeMatches);
17
+ * // Returns ['home'] if on home page, ['about'] if on about page, etc.
18
+ *
19
+ * return (
20
+ * <nav>
21
+ * {activeRoutes.map(route => (
22
+ * <span key={route}>Active: {route}</span>
23
+ * ))}
24
+ * </nav>
25
+ * );
26
+ * }
27
+ * ```
28
+ */
29
+ export declare function useKeyByRoute<TKey extends string>(routeMatches: Record<TKey, (object | null)[] | never | object | null>): TKey[];
@@ -0,0 +1,25 @@
1
+ /**
2
+ * React hook for tracking async task execution with loading state.
3
+ * Automatically manages a loading counter and provides a wrapper function for tasks.
4
+ *
5
+ * @returns A tuple containing [isLoading: boolean, runInTask: function]
6
+ * @example
7
+ * ```typescript
8
+ * function MyComponent() {
9
+ * const [isLoading, runInTask] = useRunInTask();
10
+ *
11
+ * const handleSave = async () => {
12
+ * await runInTask(async () => {
13
+ * await saveData();
14
+ * });
15
+ * };
16
+ *
17
+ * return (
18
+ * <button onClick={handleSave} disabled={isLoading}>
19
+ * {isLoading ? 'Saving...' : 'Save'}
20
+ * </button>
21
+ * );
22
+ * }
23
+ * ```
24
+ */
25
+ export declare function useRunInTask(): readonly [boolean, <T>(task: () => Promise<T> | T) => Promise<T>];
@@ -0,0 +1,23 @@
1
+ import { Dispatch, SetStateAction } from "react";
2
+ /**
3
+ * React hook for boolean state management helpers.
4
+ *
5
+ * @param set - The state setter function from useState
6
+ * @returns A tuple containing [setTrue: function, setFalse: function]
7
+ * @example
8
+ * ```typescript
9
+ * function MyComponent() {
10
+ * const [isVisible, setIsVisible] = useState(false);
11
+ * const [show, hide] = useSetUnset(setIsVisible);
12
+ *
13
+ * return (
14
+ * <div>
15
+ * <button onClick={show}>Show</button>
16
+ * <button onClick={hide}>Hide</button>
17
+ * {isVisible && <div>Content is visible</div>}
18
+ * </div>
19
+ * );
20
+ * }
21
+ * ```
22
+ */
23
+ export declare function useSetUnset(set: Dispatch<SetStateAction<boolean>>): readonly [() => void, () => void];
@@ -0,0 +1,29 @@
1
+ import { CapitalizeDeep, UncapitalizeDeep } from "./types";
2
+ /**
3
+ * Recursively transforms all object keys to use uncapitalized (camelCase) format.
4
+ *
5
+ * @template T - The type of the input value
6
+ * @param value - The value to transform (can be object, array, or primitive)
7
+ * @returns A new object with all keys converted to camelCase
8
+ * @example
9
+ * ```typescript
10
+ * const serverData = { UserId: 1, UserName: 'John', Profile: { FirstName: 'John' } };
11
+ * const clientData = uncapitalizeDeep(serverData);
12
+ * // Result: { userId: 1, userName: 'John', profile: { firstName: 'John' } }
13
+ * ```
14
+ */
15
+ export declare function uncapitalizeDeep<T>(value: T): UncapitalizeDeep<T>;
16
+ /**
17
+ * Recursively transforms all object keys to use capitalized (PascalCase) format.
18
+ *
19
+ * @template T - The type of the input value
20
+ * @param value - The value to transform (can be object, array, or primitive)
21
+ * @returns A new object with all keys converted to PascalCase
22
+ * @example
23
+ * ```typescript
24
+ * const clientData = { userId: 1, userName: 'John', profile: { firstName: 'John' } };
25
+ * const serverData = capitalizeDeep(clientData);
26
+ * // Result: { UserId: 1, UserName: 'John', Profile: { FirstName: 'John' } }
27
+ * ```
28
+ */
29
+ export declare function capitalizeDeep<T>(value: T): CapitalizeDeep<T>;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Converts the first character of a string to lowercase.
3
+ *
4
+ * @param value - The string to transform
5
+ * @returns The string with the first character in lowercase
6
+ * @example
7
+ * ```typescript
8
+ * const result = toLowerFirst('UserName');
9
+ * // Result: 'userName'
10
+ * ```
11
+ */
12
+ export declare function toLowerFirst(value: string): string;
13
+ /**
14
+ * Converts the first character of a string to uppercase.
15
+ *
16
+ * @param value - The string to transform
17
+ * @returns The string with the first character in uppercase
18
+ * @example
19
+ * ```typescript
20
+ * const result = toUpperFirst('userName');
21
+ * // Result: 'UserName'
22
+ * ```
23
+ */
24
+ export declare function toUpperFirst(value: string): string;
@@ -0,0 +1,2 @@
1
+ export * from "./transformDeep";
2
+ export * from "./unpromisify";
@@ -0,0 +1,10 @@
1
+ import type { ApiDateOnly, ApiDateTimeOffset, ApiTimeOnly, ApiTimeSpan } from "@leancodepl/api-date";
2
+ export type Mode = "capitalize" | "uncapitalize";
3
+ export type TransformDeep<T, TMode extends Mode> = T extends Array<infer TArrayElement> ? Array<TransformDeep<TArrayElement, TMode>> : T extends object ? T extends ApiDateOnly | ApiDateTimeOffset | ApiTimeOnly | ApiTimeSpan ? T : {
4
+ [TKey in keyof T as TKey extends string ? TMode extends "uncapitalize" ? Uncapitalize<TKey> : Capitalize<TKey> : TKey]: TransformDeep<T[TKey], TMode>;
5
+ } : T extends null ? undefined : T;
6
+ export type UncapitalizeDeep<T> = TransformDeep<T, "uncapitalize">;
7
+ export type CapitalizeDeep<T> = TransformDeep<T, "capitalize">;
8
+ export type MutableDeep<T> = T extends bigint | boolean | Date | number | string | symbol | null | undefined ? T : T extends ReadonlyArray<infer ArrayType> ? Array<MutableDeep<ArrayType>> : {
9
+ -readonly [K in keyof T]: MutableDeep<T[K]>;
10
+ };
@@ -0,0 +1 @@
1
+ export type Unpromisify<T> = T extends Promise<infer TResult> ? TResult : T;