-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
unpack.ts
44 lines (41 loc) · 1.33 KB
/
unpack.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { Head, MetaFlatInput } from '@zhead/schema'
import { unpackToArray, unpackToString } from 'packrup'
import { changeKeyCasingDeep, fixKeyCase } from '..'
import { MetaPackingSchema, resolveMetaKeyType } from './utils'
/**
* Converts a flat meta object into an array of meta entries.
* @param input
*/
export function unpackMeta<T extends MetaFlatInput>(input: T): Required<Head>['meta'] {
return unpackToArray((input), {
key({ key }) {
return resolveMetaKeyType(key) as string
},
value({ key }) {
return key === 'charset' ? 'charset' : 'content'
},
resolveKeyData({ key }) {
return MetaPackingSchema[key]?.keyValue || fixKeyCase(key)
},
resolveValueData({ value, key }) {
if (typeof value === 'object') {
const definition = MetaPackingSchema[key]
// refresh is weird...
if (key === 'refresh')
return `${value.seconds};url=${value.url}`
return unpackToString(
changeKeyCasingDeep(value), {
entrySeparator: ', ',
keyValueSeparator: '=',
resolve({ value, key }) {
if (typeof value === 'boolean')
return `${key}`
},
...definition?.unpack,
},
)
}
return typeof value === 'number' ? value.toString() : value
},
})
}