src/core/Fact.js   A
last analyzed

Complexity

Total Complexity 20
Complexity/F 1.33

Size

Lines of Code 192
Function Count 15

Duplication

Duplicated Lines 189
Ratio 98.44 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 2
dl 189
loc 192
rs 10
wmc 20
mnd 1
bc 20
fnc 15
bpm 1.3333
cpm 1.3333
noi 2

15 Functions

Rating   Name   Duplication   Size   Complexity  
A Fact.isInstance 3 3 1
A Fact.getPlace 3 3 1
A Fact.setQualifiers 3 3 1
A Fact.setValue 4 4 1
A Fact.toJSON 2 3 1
A Fact.addQualifier 3 3 1
A Fact.getQualifiers 3 3 1
A Fact.setPlace 6 6 2
A Fact.getValue 3 3 1
A Fact.setType 4 4 1
A Fact.init 13 13 2
A Fact.getType 3 3 1
A Fact.setDate 6 6 2
A Fact.js ➔ Fact 14 14 3
A Fact.getDate 3 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
var GedcomX = require('../'),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    utils = require('../utils');
3
4
/**
5
 * A fact for a person or relationship.
6
 * 
7
 * @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#fact-conclusion|GEDCOM X JSON Spec}
8
 * 
9
 * @class
10
 * @extends Conclusion
11
 * @param {Object} [json]
12
 */
13
var Fact = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Fact)){
17
    return new Fact(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Fact.isInstance(json)){
22
    return json;
23
  }
24
  
25
  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...
26
};
27
28
Fact.prototype = Object.create(GedcomX.Conclusion.prototype);
29
30
Fact._gedxClass = Fact.prototype._gedxClass = 'GedcomX.Fact';
31
32
Fact.jsonProps = [
33
  'type',
34
  'date',
35
  'place',
36
  'value',
37
  'qualifiers'
38
];
39
40
/**
41
 * Check whether the given object is an instance of this class.
42
 * 
43
 * @param {Object} obj
44
 * @returns {Boolean}
45
 */
46
Fact.isInstance = function(obj){
47
  return utils.isInstance(obj, this._gedxClass);
48
};
49
50
/**
51
 * Initialize from JSON
52
 * 
53
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
54
 * @return {Fact} this
55
 */
56
Fact.prototype.init = function(json){
57
  
58
  GedcomX.Conclusion.prototype.init.call(this, json);
59
  
60
  if(json){
61
    this.setType(json.type);
62
    this.setDate(json.date);
63
    this.setPlace(json.place);
64
    this.setValue(json.value);
65
    this.setQualifiers(json.qualifiers);
66
  }
67
  return this;
68
};
69
70
/**
71
 * Get the fact type
72
 * 
73
 * @returns {String} type
74
 */
75
Fact.prototype.getType = function(){
76
  return this.type;
77
};
78
79
/**
80
 * Set the fact type
81
 * 
82
 * @param {String} type
83
 * @returns {Fact} This instance
84
 */
85
Fact.prototype.setType = function(type){
86
  this.type = type;
87
  return this;
88
};
89
90
/**
91
 * Get the date
92
 * 
93
 * @returns {Date} date
94
 */
95
Fact.prototype.getDate = function(){
96
  return this.date;
97
};
98
99
/**
100
 * Set the date
101
 * 
102
 * @param {Date|Object} date
103
 * @returns {Fact} This instance
104
 */
105
Fact.prototype.setDate = function(date){
106
  if(date){
107
    this.date = GedcomX.Date(date);
108
  }
109
  return this;
110
};
111
112
/**
113
 * Get the place reference
114
 * 
115
 * @returns {PlaceReference}
116
 */
117
Fact.prototype.getPlace = function(){
118
  return this.place;
119
};
120
121
/**
122
 * Set the place reference
123
 *
124
 * @param {PlaceReference|Object} place
125
 * @returns {Fact} This instance
126
 */
127
Fact.prototype.setPlace = function(place){
128
  if(place){
129
    this.place = GedcomX.PlaceReference(place);
130
  }
131
  return this;
132
};
133
134
/**
135
 * Get the value
136
 * 
137
 * @returns {String}
138
 */
139
Fact.prototype.getValue = function(){
140
  return this.value;
141
};
142
143
/**
144
 * Set the value
145
 * 
146
 * @param {String} value
147
 * @returns {Fact} This instance
148
 */
149
Fact.prototype.setValue = function(value){
150
  this.value = value;
151
  return this;
152
};
153
154
/**
155
 * Get qualifiers
156
 * 
157
 * @return {Qualifer[]}
158
 */
159
Fact.prototype.getQualifiers = function(){
160
  return this.qualifiers || [];
161
};
162
163
/**
164
 * Set the qualifiers
165
 * 
166
 * @param {Qualifer[]|Object[]} qualifiers
167
 * @returns {Fact} This instance
168
 */
169
Fact.prototype.setQualifiers = function(qualifiers){
170
  return this._setArray(qualifiers, 'qualifiers', 'addQualifier');
171
};
172
173
/**
174
 * Add a qualifier
175
 * 
176
 * @param {Qualifier|Object} qualifier
177
 * @returns {Fact} This instance
178
 */
179
Fact.prototype.addQualifier = function(qualifier){
180
  return this._arrayPush(qualifier, 'qualifiers', GedcomX.Qualifier);
181
};
182
183
/**
184
 * Export the object as JSON
185
 * 
186
 * @return {Object} JSON object
187
 */
188
Fact.prototype.toJSON = function(){
189
  return this._toJSON(GedcomX.Conclusion, Fact.jsonProps);
190
};
191
192
module.exports = Fact;