mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
* Add `getMilliseconds` util for safer value interpretation * Test more data types * Remove remnant * Customizable fallback with default of undefined * Clean-up * Transform getMilliseconds to named export --------- Co-authored-by: ian <licitdev@gmail.com>
13 lines
405 B
TypeScript
13 lines
405 B
TypeScript
import ms from 'ms';
|
|
|
|
/**
|
|
* Safely parse human readable time format into milliseconds
|
|
*/
|
|
export function getMilliseconds<T>(value: unknown, fallback?: T): number | T;
|
|
export function getMilliseconds(value: unknown, fallback = undefined): number | undefined {
|
|
if ((typeof value !== 'string' && typeof value !== 'number') || value === '') {
|
|
return fallback;
|
|
}
|
|
return ms(String(value)) ?? fallback;
|
|
}
|