mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-28 07:16:53 +05:00
feat(core): add shared utilities and types
Add shared layer: - Result<T,E> type for error handling - BaseError and error subclasses - Guards for runtime type checking - Application constants
This commit is contained in:
44
packages/core/src/shared/utils/Guards.ts
Normal file
44
packages/core/src/shared/utils/Guards.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user