Completed
Push — master ( e3ee1d...5cbb1b )
by Justin
01:36
created

src/core/Name.js   A

Complexity

Total Complexity 15
Complexity/F 1.36

Size

Lines of Code 143
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 2
dl 0
loc 143
rs 10
wmc 15
mnd 1
bc 15
fnc 11
bpm 1.3636
cpm 1.3636
noi 3

11 Functions

Rating   Name   Duplication   Size   Complexity  
A Name.init 0 11 2
A Name.js ➔ Name 0 14 3
A Name.getType 0 3 1
A Name.toJSON 0 3 1
A Name.getNameForms 0 3 1
A Name.setDate 0 6 2
A Name.getDate 0 3 1
A Name.addNameForm 0 3 1
A Name.setNameForms 0 3 1
A Name.isInstance 0 3 1
A Name.setType 0 4 1
1
var GedcomX = require('../'),
2
    utils = require('../utils');
3
4
/**
5
 * A name.
6
 * 
7
 * @constructor
8
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
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);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
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}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
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;