forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigratable.ts
More file actions
29 lines (24 loc) · 1014 Bytes
/
Copy pathmigratable.ts
File metadata and controls
29 lines (24 loc) · 1014 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 { isRecord } from '@util/guard'
import { createObjectGuard, isInt, isString, TypeGuard } from 'typescript-guard'
import type { BrowserMigratableNamespace } from '../types'
export const isExportData = createObjectGuard<Pick<tt4b.backup.ExportData, '__meta__'>>({
__meta__: createObjectGuard({
version: isString,
ts: isInt,
}),
})
export const isLegacyVersion = (data: unknown): data is tt4b.backup.ExportData => {
if (!isExportData(data)) return false
const version = data.__meta__.version
const match = version.match(/^(\d+)\.(\d+)\.(\d+)/)
const majorStr = match?.[1]
if (!majorStr) return true
const major = parseInt(majorStr)
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
}