@leancodepl/utils 8.4.0 → 8.5.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.
package/package.json CHANGED
@@ -1,24 +1,55 @@
1
1
  {
2
2
  "name": "@leancodepl/utils",
3
- "version": "8.4.0",
3
+ "version": "8.5.1",
4
4
  "license": "Apache-2.0",
5
+ "publishConfig": {
6
+ "access": "public",
7
+ "registry": "https://registry.npmjs.org/"
8
+ },
9
+ "engines": {
10
+ "node": ">=18.0.0"
11
+ },
5
12
  "dependencies": {
6
- "@leancodepl/api-date": "8.4.0",
13
+ "@leancodepl/api-date": "8.5.1",
7
14
  "tiny-invariant": ">=1.3.1"
8
15
  },
9
16
  "peerDependencies": {
10
17
  "react": "*"
11
18
  },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/leancodepl/js_corelibrary.git",
22
+ "directory": "packages/utils"
23
+ },
24
+ "homepage": "https://github.com/leancodepl/js_corelibrary",
25
+ "bugs": {
26
+ "url": "https://github.com/leancodepl/js_corelibrary/issues"
27
+ },
28
+ "description": "Common utility functions and React hooks for web applications",
29
+ "keywords": [
30
+ "utilities",
31
+ "react",
32
+ "hooks",
33
+ "helpers",
34
+ "typescript",
35
+ "javascript",
36
+ "leancode"
37
+ ],
38
+ "author": {
39
+ "name": "LeanCode",
40
+ "url": "https://leancode.co"
41
+ },
42
+ "sideEffects": false,
12
43
  "exports": {
13
44
  "./package.json": "./package.json",
14
45
  ".": {
15
46
  "module": "./index.esm.js",
16
- "types": "./index.esm.d.ts",
47
+ "types": "./index.d.ts",
17
48
  "import": "./index.cjs.mjs",
18
49
  "default": "./index.cjs.js"
19
50
  }
20
51
  },
21
52
  "module": "./index.esm.js",
22
53
  "main": "./index.cjs.js",
23
- "types": "./index.esm.d.ts"
54
+ "types": "./index.d.ts"
24
55
  }
@@ -1,5 +1,20 @@
1
1
  type PrefixWith<T, TPrefix extends string> = {
2
2
  [K in keyof T as K extends string ? `${TPrefix}${K}` : never]: T[K];
3
3
  };
4
+ /**
5
+ * Adds a prefix to all keys in an object, creating a new object with prefixed keys.
6
+ *
7
+ * @template T - The type of the input object
8
+ * @template TPrefix - The type of the prefix string
9
+ * @param object - The object whose keys will be prefixed
10
+ * @param prefix - The prefix string to add to each key
11
+ * @returns A new object with all keys prefixed
12
+ * @example
13
+ * ```typescript
14
+ * const apiData = { userId: 1, userName: 'John' };
15
+ * const prefixed = addPrefix(apiData, 'api_');
16
+ * // Result: { api_userId: 1, api_userName: 'John' }
17
+ * ```
18
+ */
4
19
  export declare function addPrefix<T extends object, TPrefix extends string>(object: T, prefix: TPrefix): PrefixWith<T, TPrefix>;
5
20
  export {};
@@ -1 +1,17 @@
1
+ /**
2
+ * Asserts that a value is not undefined. Throws an error if the value is undefined.
3
+ * This is a type assertion function that narrows the type to exclude undefined.
4
+ *
5
+ * @template T - The type of the value being checked
6
+ * @param value - The value to check for undefined
7
+ * @param message - Optional error message to use if assertion fails
8
+ * @throws {Error} When the value is undefined
9
+ * @example
10
+ * ```typescript
11
+ * function processUser(user?: User) {
12
+ * assertDefined(user);
13
+ * return user.name; // TypeScript knows user is defined
14
+ * }
15
+ * ```
16
+ */
1
17
  export declare function assertDefined<T>(value: T | undefined, message?: string): asserts value is T;
@@ -1 +1,17 @@
1
+ /**
2
+ * Asserts that a value is not null or undefined. Throws an error if the value is null or undefined.
3
+ * This is a type assertion function that narrows the type to exclude null and undefined.
4
+ *
5
+ * @template T - The type of the value being checked
6
+ * @param value - The value to check for null or undefined
7
+ * @param message - Optional error message to use if assertion fails
8
+ * @throws {Error} When the value is null or undefined
9
+ * @example
10
+ * ```typescript
11
+ * function processOptionalData(data?: string | null) {
12
+ * assertNotEmpty(data);
13
+ * return data.toUpperCase(); // TypeScript knows data is not null/undefined
14
+ * }
15
+ * ```
16
+ */
1
17
  export declare function assertNotEmpty<T>(value: T | null | undefined, message?: string): asserts value is T;
@@ -1 +1,17 @@
1
+ /**
2
+ * Asserts that a value is not null. Throws an error if the value is null.
3
+ * This is a type assertion function that narrows the type to exclude null.
4
+ *
5
+ * @template T - The type of the value being checked
6
+ * @param value - The value to check for null
7
+ * @param message - Optional error message to use if assertion fails
8
+ * @throws {Error} When the value is null
9
+ * @example
10
+ * ```typescript
11
+ * function processData(data: string | null) {
12
+ * assertNotNull(data);
13
+ * return data.toUpperCase(); // TypeScript knows data is not null
14
+ * }
15
+ * ```
16
+ */
1
17
  export declare function assertNotNull<T>(value: T | null, message?: string): asserts value is T;
@@ -1,6 +1,29 @@
1
1
  type DownloadFileOptions = {
2
2
  name?: string;
3
3
  };
4
+ /**
5
+ * Downloads a file from a URL by creating a temporary download link.
6
+ *
7
+ * @param url - The URL to download from
8
+ * @param options - Optional download options including filename
9
+ * @example
10
+ * ```typescript
11
+ * // Download from URL
12
+ * downloadFile('https://example.com/file.pdf', { name: 'document.pdf' });
13
+ * ```
14
+ */
4
15
  export declare function downloadFile(url: string, options?: DownloadFileOptions): void;
16
+ /**
17
+ * Downloads a file from a Blob or MediaSource object by creating a temporary object URL.
18
+ *
19
+ * @param obj - The Blob or MediaSource object to download
20
+ * @param options - Optional download options including filename
21
+ * @example
22
+ * ```typescript
23
+ * // Download from Blob
24
+ * const blob = new Blob(['Hello World'], { type: 'text/plain' });
25
+ * downloadFile(blob, { name: 'hello.txt' });
26
+ * ```
27
+ */
5
28
  export declare function downloadFile(obj: Blob | MediaSource, options?: DownloadFileOptions): void;
6
29
  export {};
@@ -1 +1,17 @@
1
+ /**
2
+ * Ensures that a value is defined, returning it if defined or throwing an error if undefined.
3
+ *
4
+ * @template T - The type of the value being checked
5
+ * @param value - The value to ensure is defined
6
+ * @param message - Optional error message to use if the value is undefined
7
+ * @returns The value if it is defined
8
+ * @throws {Error} When the value is undefined
9
+ * @example
10
+ * ```typescript
11
+ * function processUser(user?: User) {
12
+ * const definedUser = ensureDefined(user);
13
+ * return definedUser.name; // definedUser is guaranteed to be defined
14
+ * }
15
+ * ```
16
+ */
1
17
  export declare function ensureDefined<T>(value: T | undefined, message?: string): T;
@@ -1 +1,17 @@
1
+ /**
2
+ * Ensures that a value is not null or undefined, returning it if valid or throwing an error if empty.
3
+ *
4
+ * @template T - The type of the value being checked
5
+ * @param value - The value to ensure is not null or undefined
6
+ * @param message - Optional error message to use if the value is null or undefined
7
+ * @returns The value if it is not null or undefined
8
+ * @throws {Error} When the value is null or undefined
9
+ * @example
10
+ * ```typescript
11
+ * function processOptionalData(data?: string | null) {
12
+ * const validData = ensureNotEmpty(data);
13
+ * return validData.toUpperCase(); // validData is guaranteed to be not null/undefined
14
+ * }
15
+ * ```
16
+ */
1
17
  export declare function ensureNotEmpty<T>(value: T | null | undefined, message?: string): T;
@@ -1 +1,18 @@
1
+ /**
2
+ * Ensures that a value is not null, returning it if not null or throwing an error if null.
3
+ * Unlike assertNotNull, this function returns the value for use in expressions.
4
+ *
5
+ * @template T - The type of the value being checked
6
+ * @param value - The value to ensure is not null
7
+ * @param message - Optional error message to use if the value is null
8
+ * @returns The value if it is not null
9
+ * @throws {Error} When the value is null
10
+ * @example
11
+ * ```typescript
12
+ * function processData(data: string | null) {
13
+ * const nonNullData = ensureNotNull(data);
14
+ * return nonNullData.toUpperCase(); // nonNullData is guaranteed to be not null
15
+ * }
16
+ * ```
17
+ */
1
18
  export declare function ensureNotNull<T>(value: T | null, message?: string): T;
@@ -1,4 +1,30 @@
1
1
  type AnyFunction = (...args: any[]) => any;
2
+ /**
3
+ * React hook for bound task execution with loading state.
4
+ * Creates a wrapped version of a function that automatically tracks loading state.
5
+ *
6
+ * @template T - The type of the function being wrapped
7
+ * @param block - The function to wrap with task tracking
8
+ * @returns A tuple containing [isLoading: boolean, wrappedFunction: T]
9
+ * @example
10
+ * ```typescript
11
+ * function UserProfile({ userId }: { userId: string }) {
12
+ * const [user, setUser] = useState<User | null>(null);
13
+ *
14
+ * const [isLoading, loadUser] = useBoundRunInTask(async () => {
15
+ * const userData = await fetchUser(userId);
16
+ * setUser(userData);
17
+ * });
18
+ *
19
+ * useEffect(() => {
20
+ * loadUser();
21
+ * }, [userId, loadUser]);
22
+ *
23
+ * if (isLoading) return <div>Loading...</div>;
24
+ * return <div>{user?.name}</div>;
25
+ * }
26
+ * ```
27
+ */
2
28
  export declare function useBoundRunInTask<T extends AnyFunction>(block: T): [boolean, T];
3
29
  export declare function useBoundRunInTask<T extends AnyFunction>(block: T | undefined): [boolean, T | undefined];
4
30
  export {};
@@ -1,3 +1,25 @@
1
+ /**
2
+ * React hook for managing dialog state with optional callback after closing.
3
+ * Provides convenient open/close functions and tracks the dialog's open state.
4
+ *
5
+ * @param onAfterClose - Optional callback function to execute after the dialog closes
6
+ * @returns Object containing dialog state and control functions
7
+ * @example
8
+ * ```typescript
9
+ * function MyComponent() {
10
+ * const { isDialogOpen, openDialog, closeDialog } = useDialog(() => {
11
+ * console.log('Dialog closed');
12
+ * });
13
+ *
14
+ * return (
15
+ * <div>
16
+ * <button onClick={openDialog}>Open Dialog</button>
17
+ * {isDialogOpen && <Dialog onClose={closeDialog} />}
18
+ * </div>
19
+ * );
20
+ * }
21
+ * ```
22
+ */
1
23
  export declare function useDialog(onAfterClose?: () => void): {
2
24
  isDialogOpen: boolean;
3
25
  openDialog: () => void;
@@ -1 +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
+ */
1
29
  export declare function useKeyByRoute<TKey extends string>(routeMatches: Record<TKey, (object | null)[] | never | object | null>): TKey[];
@@ -1 +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
+ */
1
25
  export declare function useRunInTask(): readonly [boolean, <T>(task: () => Promise<T> | T) => Promise<T>];
@@ -1,2 +1,23 @@
1
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
+ */
2
23
  export declare function useSetUnset(set: Dispatch<SetStateAction<boolean>>): readonly [() => void, () => void];
@@ -1,3 +1,29 @@
1
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
+ */
2
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
+ */
3
29
  export declare function capitalizeDeep<T>(value: T): CapitalizeDeep<T>;
@@ -1,2 +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
+ */
1
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
+ */
2
24
  export declare function toUpperFirst(value: string): string;
package/index.esm.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/index";
File without changes