-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeguards.ts
146 lines (129 loc) · 3.72 KB
/
typeguards.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
import {
IEntity,
IAddress,
IDocument,
IGroup,
ITag,
IDigitalEntity,
IPhysicalEntity,
ICompilation,
IAnnotation,
IPerson,
IInstitution,
IContact,
} from './interfaces';
const isDefined = (value: any) => value != null && value != undefined;
const checkProps = (props: string[], obj: any) => {
if (!isDefined(obj)) return false;
let valid = true;
for (const prop of props) {
if (!isDefined(obj[prop])) {
valid = false;
break;
}
}
return valid;
};
/**
* Checks whether a document received from the backend is still unresolved
* @type {Boolean}
*/
const isUnresolved = (obj: any): obj is IDocument =>
Object.keys(obj).length === 1 && obj._id !== undefined;
/**
* Checks whether an object is a group entry
* @type {Boolean}
*/
const isGroup = (obj: any): obj is IGroup => checkProps(GROUP_PROPS, obj);
const GROUP_PROPS = ['name', 'creator', 'owners', 'members'];
/**
* Checks whether an object is a tag entry
* @type {Boolean}
*/
const isTag = (obj: any): obj is ITag => checkProps(TAG_PROPS, obj);
const TAG_PROPS = ['value'];
/**
* Checks whether an object is a digital/physical entity
* @type {Boolean}
*/
const isMetadataEntity = (obj: any): obj is IDigitalEntity | IPhysicalEntity =>
checkProps(META_ENTITY_PROPS, obj);
const META_ENTITY_PROPS = ['title', 'description', 'persons', 'institutions'];
/**
* Checks whether an object is a compilation
* @type {Boolean}
*/
const isCompilation = (obj: any): obj is ICompilation => checkProps(COMP_PROPS, obj);
const COMP_PROPS = ['entities', 'name', 'description'];
/**
* Checks whether an object is an entity
* @type {Boolean}
*/
const isEntity = (obj: any): obj is IEntity => checkProps(ENTITY_PROPS, obj);
const ENTITY_PROPS = ['name', 'mediaType', 'online', 'finished'];
/**
* Checks whether an <IEntity | IDocument> is fully resolved
* @type {Boolean}
*/
const isResolvedEntity = (obj: any): obj is IEntity & { relatedDigitalEntity: IDigitalEntity } =>
isEntity(obj) && isDigitalEntity(obj.relatedDigitalEntity);
/**
* Checks whether an object is an annotation
* @type {Boolean}
*/
const isAnnotation = (obj: any): obj is IAnnotation => checkProps(ANNO_PROPS, obj);
const ANNO_PROPS = ['body', 'target'];
/**
* Checks whether an object is a digital entity
* @type {Boolean}
*/
const isDigitalEntity = (obj: any): obj is IDigitalEntity =>
isMetadataEntity(obj) && checkProps(DIG_ENTITY_PROPS, obj);
const DIG_ENTITY_PROPS = ['type', 'licence'];
/**
* Checks whether an object is a physical entity
* @type {Boolean}
*/
const isPhysicalEntity = (obj: any): obj is IPhysicalEntity =>
isMetadataEntity(obj) && checkProps(PHY_ENTITY_PROPS, obj);
const PHY_ENTITY_PROPS = ['place', 'collection'];
/**
* Checks whether an object is a person
* @type {Boolean}
*/
const isPerson = (obj: any): obj is IPerson => checkProps(PERSON_PROPS, obj);
const PERSON_PROPS = ['prename', 'name'];
/**
* Checks whether an object is an institution
* @type {Boolean}
*/
const isInstitution = (obj: any): obj is IInstitution => checkProps(INST_PROPS, obj);
const INST_PROPS = ['name', 'addresses'];
/**
* Checks whether an object is an address
* @type {Boolean}
*/
const isAddress = (obj: any): obj is IAddress => checkProps(ADDR_PROPS, obj);
const ADDR_PROPS = ['building', 'city', 'country', 'number', 'postcode', 'street'];
/**
* Checks whether an object is a contact reference
* @type {Boolean}
*/
const isContact = (obj: any): obj is IContact => checkProps(CONTACT_PROPS, obj);
const CONTACT_PROPS = ['mail', 'note', 'phonenumber'];
export {
isUnresolved,
isGroup,
isTag,
isMetadataEntity,
isCompilation,
isEntity,
isResolvedEntity,
isAnnotation,
isDigitalEntity,
isPhysicalEntity,
isPerson,
isInstitution,
isAddress,
isContact,
};