Skip to content

Commit

Permalink
feat(utils): extra utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Oct 13, 2022
1 parent 1193f67 commit a9eeff8
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,47 @@ export function round(value: number, precision: number): number {
const multiplier = Math.pow(10, precision);
return Math.round(value * multiplier) / multiplier;
}

export function isArray<T>(value: unknown): value is T[] {
return Array.isArray(value);
}

export function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}

export function isEmpty(value: any): boolean {
if (isArray(value)) {
return value.length === 0;
} else if (isObject(value)) {
return Object.keys(value).length === 0;
} else {
return !value;
}
}

export function capitalize(value: string): string {
return value.charAt(0).toUpperCase() + value.slice(1);
}

export function uppercase(value: string): string {
return value.toUpperCase();
}

export function lowercase(value: string): string {
return value.toLowerCase();
}

export function clone<T>(value: T): T {
if (isArray(value)) {
return value.slice() as T;
} else if (isObject(value)) {
return { ...value } as T;
} else {
return value;
}
}

export function assignOptions<P, T>(defaultOptions: P, options: T): P & T {
return Object.assign({}, defaultOptions, options);
}

0 comments on commit a9eeff8

Please sign in to comment.