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