-
Notifications
You must be signed in to change notification settings - Fork 3
/
vcard4Types.ts
186 lines (177 loc) · 5.37 KB
/
vcard4Types.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { Nag, VCardNagAttributes } from './errors.js';
import { NonEmptyArray } from './nonEmptyArray.js';
import {
ADRType,
CLIENTPIDMAPType,
Flatten,
GENDERType,
keyTypeGuard,
NonEmptyStringArrayType,
NType,
NumberType,
StringType,
TypeOf,
} from './runtimeTypes.js';
// VCard4 parameters with runtime introspection capability
export const knownParameters = {
LANGUAGE: StringType,
VALUE: StringType,
PREF: NumberType,
ALTID: StringType,
PID: NonEmptyStringArrayType,
TYPE: NonEmptyStringArrayType,
MEDIATYPE: StringType,
CALSCALE: StringType,
SORT_AS: NonEmptyStringArrayType,
GEO: StringType,
TZ: StringType,
LABEL: StringType, // RFC6350, mentioned in 6.3.1 only
CC: StringType, // RFC8605
INDEX: NumberType, // RFC6715
LEVEL: StringType, // RFC6715
};
export type KnownParameterNames = keyof typeof knownParameters;
export const isKnownParameter = keyTypeGuard(knownParameters);
export type VCardParameters = Flatten<
{
[k in KnownParameterNames]?: TypeOf<typeof knownParameters[k]>;
} & { x?: Record<string, NonEmptyArray<string>> }
>;
// VCard4 properties with runtime introspection capability
export const knownProperties = {
// Cardinality: 1
BEGIN: StringType,
VERSION: StringType,
END: StringType,
// Cardinality: 1*
FN: StringType,
// Cardinality: *1
PRODID: StringType,
UID: StringType,
REV: StringType,
KIND: StringType,
N: NType,
BDAY: StringType,
ANNIVERSARY: StringType,
GENDER: GENDERType,
BIRTHPLACE: StringType, // RFC6474
DEATHPLACE: StringType, // RFC6474
DEATHDATE: StringType, // RFC6474
// Cardinality: *
CLIENTPIDMAP: CLIENTPIDMAPType,
SOURCE: StringType,
XML: StringType,
NICKNAME: NonEmptyStringArrayType,
PHOTO: StringType,
ADR: ADRType,
TEL: StringType,
EMAIL: StringType,
IMPP: StringType,
LANG: StringType,
TZ: StringType,
GEO: StringType,
TITLE: StringType,
ROLE: StringType,
LOGO: StringType,
ORG: NonEmptyStringArrayType,
MEMBER: StringType,
RELATED: StringType,
CATEGORIES: NonEmptyStringArrayType,
NOTE: StringType,
SOUND: StringType,
URL: StringType,
KEY: StringType,
FBURL: StringType,
CALADRURI: StringType,
CALURI: StringType,
CONTACT_URI: StringType, // RFC8605
EXPERTISE: StringType, // RFC6715
HOBBY: StringType, // RFC6715
INTEREST: StringType, // RFC6715
ORG_DIRECTORY: StringType, // RFC6715
};
export type KnownPropertyNames = keyof typeof knownProperties;
export const isKnownProperty = keyTypeGuard(knownProperties);
// "1" cardinality properties from RFC 6350
// Values are what is expected
export const exactlyOnceProperties = {
BEGIN: 'VCARD',
VERSION: '4.0',
END: 'VCARD',
};
export type ExactlyOncePropertyNames = keyof typeof exactlyOnceProperties;
export type ExactlyOnceProperties = Pick<
typeof knownProperties,
ExactlyOncePropertyNames
>;
export const isExactlyOnceProperty = keyTypeGuard(exactlyOnceProperties);
// "1*" cardinality properties from RFC 6350
// Value is default
export const atLeastOnceProperties = { FN: '' };
export type AtLeastOncePropertyNames = keyof typeof atLeastOnceProperties;
export type AtLeastOnceProperties = Pick<
typeof knownProperties,
AtLeastOncePropertyNames
>;
export const isAtLeastOnceProperty = keyTypeGuard(atLeastOnceProperties);
// "*1" cardinality properties from RFC 6350
export const atMostOnceProperties = {
PRODID: 0,
UID: 0,
KIND: 0,
N: 0,
BDAY: 0,
ANNIVERSARY: 0,
GENDER: 0,
REV: 0,
BIRTHPLACE: 0,
DEATHPLACE: 0,
DEATHDATE: 0,
};
export type AtMostOncePropertyNames = keyof typeof atMostOnceProperties;
export type AtMostOnceProperties = Pick<
typeof knownProperties,
AtMostOncePropertyNames
>;
export const isAtMostOnceProperty = keyTypeGuard(atMostOnceProperties);
// "*" cardinality properties from RFC 6350
export type AnyCardinalityProperties = Omit<
typeof knownProperties,
| keyof ExactlyOnceProperties
| keyof AtLeastOnceProperties
| keyof AtMostOnceProperties
>;
// AnyCardinality type guard cannot be defined, as we do not have an object
// (without, redundantly, actually creating it manually):
// isKnownProperty(x) && !isExactlyOnceProperty(x) && !isAtLeastOnceProperty(x) && !isAtMostOnceProperty(x)
// A single line, with the exception of the property name (which will become the key) to this value, anyway.
export type SingleVCardProperty<ValueType> = {
group?: string;
parameters?: VCardParameters;
value: ValueType;
};
// Compose the vCard
// Every element with its type, embedded in a SingleVCardProperty
export type VCardSingles<Selection extends Partial<typeof knownProperties>> = {
[k in keyof Selection & keyof typeof knownProperties]: SingleVCardProperty<
TypeOf<typeof knownProperties[k]>
>;
};
// Every element with its type, embedded in a NonEmptyArray of SingleVCardProperties
export type VCardMultiples<Selection extends Partial<typeof knownProperties>> =
{
[k in keyof Selection & keyof typeof knownProperties]: NonEmptyArray<
SingleVCardProperty<TypeOf<typeof knownProperties[k]>>
>;
};
export type VCard4 = Flatten<
VCardSingles<ExactlyOnceProperties> &
Partial<VCardSingles<AtMostOnceProperties>> &
VCardMultiples<AtLeastOnceProperties> &
Partial<VCardMultiples<AnyCardinalityProperties>> & {
x?: Record<string, NonEmptyArray<SingleVCardProperty<string>>>;
nags?: NonEmptyArray<Nag<VCardNagAttributes>>;
hasErrors: boolean;
unparseable?: NonEmptyArray<string>;
}
>;