-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperson_or_organization.ts
87 lines (81 loc) · 3.14 KB
/
person_or_organization.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
export interface PersonOrOrganizationInterface {
fromObject(obj: object): boolean;
toObject(): object;
}
export class PersonOrOrganization implements PersonOrOrganizationInterface {
// ORCID is use for person id
id: string = "";
type: string = "";
// Name is used by organizations
name: string = "";
// Given/Family are used by individual persons
givenName: string = "";
familyName: string = "";
affiliation: PersonOrOrganization | undefined;
email: string = "";
toObject(): object {
let obj: {[key: string]: any} = {};
(this.id === undefined || this.id.length === 0) ? '': obj["id"] = this.id;
(this.type === undefined || this.type.trim().length === 0) ? '': obj["type"] = this.type;
(this.name === undefined || this.name.trim().length === 0) ? '': obj["name"] = this.name;
(this.givenName === undefined || this.givenName.length === 0) ? '': obj["givenName"] = this.givenName;
(this.familyName === undefined || this.familyName.length === 0) ? '': obj["familyName"] = this.familyName;
(this.affiliation === undefined) ? '': obj["affiliation"] = this.affiliation;
(this.email === undefined || this.email.length === 0) ? '': obj['email'] = this.email;
return obj;
}
fromObject(obj: { [key: string]: any }): boolean {
(obj["id"] === undefined) ? "" : this.id = obj["id"].trim();
if (this.id === "") {
(obj["@id"] === undefined) ? "" : this.id = obj["@id"].trim();
}
(obj["type"] === undefined) ? "" : this.type = obj["type"].trim();
if (this.type === "") {
(obj["@type"] === undefined) ? "" : this.type = obj["@type"].trim();
}
(obj["name"] === undefined) ? "" : this.name = obj["name"].trim();
(obj["givenName"] === undefined) ? "" : this.givenName = obj["givenName"].trim();
(obj["familyName"] === undefined) ? "" : this.familyName = obj["familyName"].trim();
(obj["affiliation"] === undefined) ? '' : this.affiliation = obj["affiliation"];
(obj["email"] === undefined) ? "" : this.email = obj["email"].trim();
return true;
}
}
export function normalizePersonOrOrgList(obj: object | string): PersonOrOrganization[] {
const oType = Object.prototype.toString.call(obj);
let p: PersonOrOrganization = new PersonOrOrganization();
let l: {[key: string]: any}[] = [];
switch (oType) {
case "[object Array]":
for (let item of Object.values(obj)) {
p = new PersonOrOrganization();
p.fromObject(item);
if (p.type === "") {
if (p.name === "") {
p.type = "Person";
} else if (p.familyName === "" && p.givenName === "" ){
p.type = "Organization";
}
}
l.push(p.toObject());
}
break;
case "[object Object]":
p.fromObject(obj as Object);
if (p.type === "") {
if (p.name === "") {
p.type = "Person";
} else if (p.familyName === "" && p.givenName === "" ){
p.type = "Organization";
}
}
l.push(p.toObject());
break;
case "[object String]":
p.type = "Person";
p.name = obj.toString().trim();
l.push(p.toObject());
break;
}
return l as unknown as PersonOrOrganization[];
}