mirror of
https://github.com/supabase/supabase.git
synced 2026-05-23 01:39:34 +08:00
* wip: added logic for migration checking * updated null check * updated to return undefined * updated use memo check * updated migration text * updated text based on migrations data * re-added tabbed deleted code * Updated insertedAt date logic * Fit missing gap + add tooltip * Niot --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
30 lines
1.0 KiB
TypeScript
30 lines
1.0 KiB
TypeScript
import dayjs, { Dayjs } from 'dayjs'
|
|
|
|
/**
|
|
* Safely parses a migration version string as a date.
|
|
* Migration versions are typically in the format YYYYMMDDHHmmss (e.g., "20231128095400").
|
|
* However, some projects may have custom version formats (e.g., "001", "002") that cannot be parsed as dates.
|
|
*
|
|
* @param version - Migration version string
|
|
* @returns Dayjs object if the version is a valid datetime, null otherwise
|
|
*
|
|
* @example
|
|
* const parsed = parseMigrationVersion('20231128095400')
|
|
* if (parsed) {
|
|
* console.log(parsed.fromNow()) // "2 hours ago"
|
|
* console.log(parsed.format('DD MMM YYYY')) // "28 Nov 2023"
|
|
* }
|
|
*
|
|
* @example
|
|
* const invalid = parseMigrationVersion('001') // returns null
|
|
*/
|
|
export function parseMigrationVersion(version: string | null | undefined): Dayjs | undefined {
|
|
if (!version) return undefined
|
|
|
|
// Must contain only digits
|
|
if (!/^\d{14}$/.test(version)) return undefined
|
|
|
|
const parsed = dayjs(version, 'YYYYMMDDHHmmss', true)
|
|
return parsed.isValid() ? parsed : undefined
|
|
}
|