mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-28 07:16:53 +05:00
Add shared layer: - Result<T,E> type for error handling - BaseError and error subclasses - Guards for runtime type checking - Application constants
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
/**
|
|
* Type guard utilities for runtime type checking
|
|
*/
|
|
export class Guards {
|
|
public static isNullOrUndefined(value: unknown): value is null | undefined {
|
|
return value === null || value === undefined;
|
|
}
|
|
|
|
public static isString(value: unknown): value is string {
|
|
return typeof value === 'string';
|
|
}
|
|
|
|
public static isNumber(value: unknown): value is number {
|
|
return typeof value === 'number' && !isNaN(value);
|
|
}
|
|
|
|
public static isBoolean(value: unknown): value is boolean {
|
|
return typeof value === 'boolean';
|
|
}
|
|
|
|
public static isObject(value: unknown): value is object {
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
public static isArray<T>(value: unknown): value is T[] {
|
|
return Array.isArray(value);
|
|
}
|
|
|
|
public static isEmpty(value: string | unknown[] | object | null | undefined): boolean {
|
|
if (Guards.isNullOrUndefined(value)) {
|
|
return true;
|
|
}
|
|
|
|
if (Guards.isString(value) || Guards.isArray(value)) {
|
|
return value.length === 0;
|
|
}
|
|
|
|
if (Guards.isObject(value)) {
|
|
return Object.keys(value).length === 0;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|