This repository was archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplace.js
More file actions
109 lines (76 loc) · 2.65 KB
/
place.js
File metadata and controls
109 lines (76 loc) · 2.65 KB
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
/**
* A class to model author card
* **/
class Place {
constructor(rawBody, config) {
// Store request body and config
this.rawBody = rawBody;
this.config = config;
// Parse author fields
this._parseBody();
};
_flatAndStoreCompositeField(key, value) {
// Collect composite object
let compositeObject = {};
let totalObject = [];
// Collect array elements in a single array
if(Array.isArray(value))
value.map(el => Object.assign(compositeObject, el));
// Or store current composite field
else
compositeObject = value;
Object.keys(compositeObject).forEach((objKey) => {
// Get and format subfield key
let subKey = objKey.toLowerCase();
subKey = subKey.charAt(0).toUpperCase() + subKey.slice(1);
// Store current subfield
if(Array.isArray(compositeObject[objKey])) {
// Trim all strings in collection
let trimmedField = compositeObject[objKey].map(el => el.trim());
// Store trimmed field
this[key + subKey] = trimmedField;
totalObject = totalObject.concat(trimmedField)
} else {
// Store trimmed field
this[key + subKey] = compositeObject[objKey].trim();
totalObject.append(compositeObject[objKey].trim());
}
});
// Store total object
this[key] = totalObject;
}
_parseName() {
// Store raw name
this.rawName = this.name;
// Parse name
if(Array.isArray(this.name))
this.name = this.name[0];
this.name = this.name.split('(')[0];
}
_parseBody() {
// Author map
let map = this.config.getInputDictionary();
// Map fields
Object.keys(map).map(key => {
if (this.rawBody[map[key]]) {
// Handle composite fields
if(this.config.isFieldComposite(key))
this._flatAndStoreCompositeField(key, this.rawBody[map[key]]);
else {
if(Array.isArray(this.rawBody[map[key]]))
this[key] = this.rawBody[map[key]].map(el => el.trim());
else
this[key] = this.rawBody[map[key]].trim()
}
if(key === 'name')
this._parseName()
} else if(!this[key])
this[key] = null;
});
};
getString() {
this.string = JSON.stringify(this);
}
}
// Exports
exports.Place = Place;