| 189 | /** |
| 190 | * Converts the first character of a string to uppercase. |
| 191 | * Note: This is NOT full title-casing (e.g., "hello world" → "Hello world", not "Hello World") |
| 192 | */ |
| 193 | export function capitalize(s: string): string { |
| 194 | return s.charAt(0).toUpperCase() + s.slice(1); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Asserts that a value is not null or undefined and narrows its type. |
| 199 | * Throws an error if the value is null or undefined. |
| 200 | * This version provides type narrowing of the original parameter. |
| 201 | * |
| 202 | * @param value - The value to check for null/undefined |
| 203 | * @param message - Optional custom error message |
| 204 | * @throws Error if the value is null or undefined |
| 205 | */ |
| 206 | export function assertNotNullish<T>(value: T, message?: string): asserts value is NonNullable<T> { |
| 207 | if (value === null || value === undefined) { |
| 208 | throw new Error(message || 'Value must not be null or undefined'); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Asserts that a value is not null or undefined and returns it with proper type narrowing. |
| 214 | * Throws an error if the value is null or undefined. |
| 215 | * This version returns the non-null value for use in expressions. |
| 216 | * |
| 217 | * @param value - The value to check for null/undefined |
| 218 | * @param message - Optional custom error message |
| 219 | * @returns The value with null and undefined removed from its type |
| 220 | * @throws Error if the value is null or undefined |
| 221 | */ |
| 222 | export function toNonNullish<T>(value: T, message?: string): NonNullable<T> { |
| 223 | assertNotNullish(value, message); |
| 224 | return value; |