1
|
|
|
var ExtensibleData = require('./ExtensibleData'), |
2
|
|
|
NamePart = require('./NamePart'), |
3
|
|
|
utils = require('./utils'); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A form of a name. |
7
|
|
|
* |
8
|
|
|
* @constructor |
9
|
|
|
* @param {Object} [json] |
|
|
|
|
10
|
|
|
*/ |
11
|
|
|
var NameForm = function(json){ |
12
|
|
|
|
13
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
14
|
|
|
if(!(this instanceof NameForm)){ |
15
|
|
|
return new NameForm(json); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
19
|
|
|
if(NameForm.isInstance(json)){ |
20
|
|
|
return json; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
ExtensibleData.call(this, json); |
24
|
|
|
|
25
|
|
|
if(json){ |
|
|
|
|
26
|
|
|
this.setLang(json.lang); |
27
|
|
|
this.setFullText(json.fullText); |
28
|
|
|
this.setParts(json.parts); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
NameForm.prototype = Object.create(ExtensibleData.prototype); |
33
|
|
|
|
34
|
|
|
NameForm._gedxClass = NameForm.prototype._gedxClass = 'GedcomX.NameForm'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Check whether the given object is an instance of this class. |
38
|
|
|
* |
39
|
|
|
* @param {Object} obj |
40
|
|
|
* @returns {Boolean} |
41
|
|
|
*/ |
42
|
|
|
NameForm.isInstance = function(obj){ |
43
|
|
|
return utils.isInstance(obj, this._gedxClass); |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the lang tag |
48
|
|
|
* |
49
|
|
|
* @returns {String} lang |
50
|
|
|
*/ |
51
|
|
|
NameForm.prototype.getLang = function(){ |
52
|
|
|
return this.lang; |
53
|
|
|
}; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the lang tag |
57
|
|
|
* |
58
|
|
|
* @param {String} lang |
59
|
|
|
* @returns {NameForm} This instance |
60
|
|
|
*/ |
61
|
|
|
NameForm.prototype.setLang = function(lang){ |
62
|
|
|
this.lang = lang; |
63
|
|
|
return this; |
64
|
|
|
}; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the full text |
68
|
|
|
* |
69
|
|
|
* @returns {String} fullText |
70
|
|
|
*/ |
71
|
|
|
NameForm.prototype.getFullText = function(){ |
72
|
|
|
return this.fullText; |
73
|
|
|
}; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the full text |
77
|
|
|
* |
78
|
|
|
* @param {String} fullText |
79
|
|
|
* @returns {NameForm} This instance |
80
|
|
|
*/ |
81
|
|
|
NameForm.prototype.setFullText = function(fullText){ |
82
|
|
|
this.fullText = fullText; |
83
|
|
|
return this; |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the name parts |
88
|
|
|
* |
89
|
|
|
* @returns {NamePart[]} |
90
|
|
|
*/ |
91
|
|
|
NameForm.prototype.getParts = function(){ |
92
|
|
|
return this.parts || []; |
93
|
|
|
}; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the name parts |
97
|
|
|
* |
98
|
|
|
* @param {NamePart[]|Object[]} parts |
99
|
|
|
* @returns {NameForm} This instance |
100
|
|
|
*/ |
101
|
|
|
NameForm.prototype.setParts = function(parts){ |
102
|
|
|
if(Array.isArray(parts)){ |
103
|
|
|
var nameForm = this; |
104
|
|
|
nameForm.parts = []; |
105
|
|
|
parts.forEach(function(part){ |
106
|
|
|
nameForm.addPart(part); |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
return this; |
110
|
|
|
}; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Add a name part |
114
|
|
|
* |
115
|
|
|
* @param {NamePart|Object} |
|
|
|
|
116
|
|
|
* @returns {NameForm} |
117
|
|
|
*/ |
118
|
|
|
NameForm.prototype.addPart = function(part){ |
119
|
|
|
if(part){ |
120
|
|
|
if(!Array.isArray(this.parts)){ |
121
|
|
|
this.parts = []; |
122
|
|
|
} |
123
|
|
|
this.parts.push(NamePart(part)); |
124
|
|
|
} |
125
|
|
|
return this; |
126
|
|
|
}; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Export the object as JSON |
130
|
|
|
* |
131
|
|
|
* @return {Object} JSON object |
132
|
|
|
*/ |
133
|
|
|
NameForm.prototype.toJSON = function(){ |
134
|
|
|
var json = ExtensibleData.prototype.toJSON.call(this); |
135
|
|
|
|
136
|
|
|
if(this.lang){ |
137
|
|
|
json.lang = this.lang; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if(this.fullText){ |
141
|
|
|
json.fullText = this.fullText; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if(this.parts){ |
145
|
|
|
json.parts = this.parts.map(function(p){ |
146
|
|
|
return p.toJSON(); |
147
|
|
|
}); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return json; |
151
|
|
|
}; |
152
|
|
|
|
153
|
|
|
module.exports = NameForm; |