src/core/Subject.js   A
last analyzed

Complexity

Total Complexity 19
Complexity/F 1.36

Size

Lines of Code 183
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 4
dl 0
loc 183
rs 10
wmc 19
mnd 2
bc 19
fnc 14
bpm 1.3571
cpm 1.3571
noi 6

14 Functions

Rating   Name   Duplication   Size   Complexity  
A Subject.addEvidence 0 3 1
A Subject.setEvidence 0 3 1
A Subject.getMedia 0 3 1
A Subject.isInstance 0 3 1
A Subject.js ➔ Subject 0 14 3
A Subject.setExtracted 0 5 1
A Subject.setMedia 0 3 1
A Subject.addMedia 0 3 1
A Subject.init 0 16 3
A Subject.getEvidence 0 3 1
A Subject.isExtracted 0 5 1
A Subject.setIdentifiers 0 6 2
A Subject.toJSON 0 3 1
A Subject.getIdentifiers 0 3 1
1
var GedcomX = require('../'),
2
    utils = require('../utils');
3
4
/**
5
 * An object identified in time and space by various conclusions.
6
 * 
7
 * @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#subject|GEDCOM X JSON Spec}
8
 * 
9
 * @class
10
 * @extends Conclusion
11
 * @param {Object} [json]
12
 */
13
var Subject = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Subject)){
17
    return new Subject(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Subject.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
Subject.prototype = Object.create(GedcomX.Conclusion.prototype);
29
30
Subject._gedxClass = Subject.prototype._gedxClass = 'GedcomX.Subject';
31
32
Subject.jsonProps = [
33
  'extracted',
34
  'evidence',
35
  'identifiers',
36
  'media'
37
];
38
39
/**
40
 * Check whether the given object is an instance of this class.
41
 * 
42
 * @param {Object} obj
43
 * @returns {Boolean}
44
 */
45
Subject.isInstance = function(obj){
46
  return utils.isInstance(obj, this._gedxClass);
47
};
48
49
/**
50
 * Initialize from JSON
51
 * 
52
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
53
 * @return {Subject} this
54
 */
55
Subject.prototype.init = function(json){
56
  
57
  GedcomX.Conclusion.prototype.init.call(this, json);
58
  
59
  if(json){
60
    // setExtracted defaults to false but when the property is undefined we
61
    // want it to stay that way
62
    if(typeof json.extracted !== 'undefined'){
63
      this.setExtracted(json.extracted);
64
    }
65
    this.setEvidence(json.evidence);
66
    this.setIdentifiers(json.identifiers);
67
    this.setMedia(json.media);
68
  }
69
  return this;
70
};
71
72
/**
73
 * Is this an extracted conclusion?
74
 * 
75
 * @returns {Boolean} extracted
76
 */
77
Subject.prototype.isExtracted = function(){
78
  // Double exclamations force a boolean no matter what type the property
79
  // currently is. The main reason for this is to force undefiend into false.
80
  return !!this.isExtracted;
81
};
82
83
/**
84
 * Set the extracted property
85
 * 
86
 * @param {Boolean} extracted
87
 * @returns {Subject} This instance.
88
 */
89
Subject.prototype.setExtracted = function(extracted){
90
  // Double exclamations force a boolean
91
  this.extracted = !!extracted;
92
  return this;
93
};
94
95
/**
96
 * Get the evidence.
97
 * 
98
 * @returns {EvidenceReference[]}
99
 */
100
Subject.prototype.getEvidence = function(){
101
  return this.evidence || [];
102
};
103
104
/**
105
 * Set the evidence. This method will replace existing evidence entries with new entries.
106
 * 
107
 * @param {Object[]|EvidenceReference[]}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
108
 * @returns {Subject} This instance.
109
 */
110
Subject.prototype.setEvidence = function(evidence){
111
  return this._setArray(evidence, 'evidence', 'addEvidence');
112
};
113
114
/**
115
 * Add evidence
116
 * 
117
 * @param {Object|EvidenceReference}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
118
 * @returns {Subject} This instance.
119
 */
120
Subject.prototype.addEvidence = function(evidence){
121
  return this._arrayPush(evidence, 'evidence', GedcomX.EvidenceReference);
122
};
123
124
/**
125
 * Get the identifiers
126
 * 
127
 * @return {Identifiers}
128
 */
129
Subject.prototype.getIdentifiers = function(){
130
  return this.identifiers;
131
};
132
133
/**
134
 * Set the identifiers
135
 * 
136
 * @param {Object|Identifiers}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
137
 * @returns {Subject} This instance
138
 */
139
Subject.prototype.setIdentifiers = function(identifiers){
140
  if(identifiers){
141
    this.identifiers = GedcomX.Identifiers(identifiers);
142
  }
143
  return this;
144
};
145
146
/**
147
 * Get the media references
148
 * 
149
 * @returns {SourceReference[]}
150
 */
151
Subject.prototype.getMedia = function(){
152
  return this.media || [];
153
};
154
155
/**
156
 * Set the media references
157
 * 
158
 * @param {Object[]|SourceReference[]}
159
 */
160
Subject.prototype.setMedia = function(media){
161
  return this._setArray(media, 'media', 'addMedia');
162
};
163
164
/**
165
 * Add media
166
 * 
167
 * @param {Object|SourceReference}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
168
 * @returns {Subject} This instance.
169
 */
170
Subject.prototype.addMedia = function(media){
171
  return this._arrayPush(media, 'media', GedcomX.SourceReference);
172
};
173
174
/**
175
 * Export the object as JSON
176
 * 
177
 * @return {Object} JSON object
178
 */
179
Subject.prototype.toJSON = function(){
180
  return this._toJSON(GedcomX.Conclusion, Subject.jsonProps);
181
};
182
183
module.exports = Subject;