-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathmigratable.ts
More file actions
29 lines (23 loc) · 983 Bytes
/
migratable.ts
File metadata and controls
29 lines (23 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type { BrowserMigratableNamespace } from '@db/types'
import { isRecord } from '@util/guard'
import { createObjectGuard, isInt, isString, TypeGuard } from 'typescript-guard'
export const isExportData = createObjectGuard<Pick<timer.backup.ExportData, '__meta__'>>({
__meta__: createObjectGuard({
version: isString,
ts: isInt,
}),
})
export const isLegacyVersion = (data: unknown): data is timer.backup.ExportData => {
if (!isExportData(data)) return false
const version = data.__meta__.version
const match = version.match(/^(\d+)\.(\d+)\.(\d+)/)
if (!match) return true
const major = parseInt(match[1])
return major < 4
}
export const extractNamespace = <T>(data: unknown, namespace: BrowserMigratableNamespace, guard: TypeGuard<T>): T | undefined => {
if (!isRecord(data)) return undefined
if (!(namespace in data)) return undefined
const nsData = data[namespace]
return guard(nsData) ? nsData : undefined
}