Code Duplication    Length = 182-208 lines in 3 locations

src/Subject.js 1 location

@@ 1-208 (lines=208) @@
1
var Conclusion = require('./Conclusion'),
2
    Identifiers = require('./Identifiers'),
3
    EvidenceReference = require('./EvidenceReference'),
4
    SourceReference = require('./SourceReference'),
5
    utils = require('./utils');
6
7
/**
8
 * An object identified in time and space by various conclusions.
9
 * 
10
 * @constructor
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
  Conclusion.call(this, json);
26
  
27
  if(json){
28
    this.setExtracted(json.extracted);
29
    this.setEvidence(json.evidence);
30
    this.setIdentifiers(json.identifiers);
31
    this.setMedia(json.media);
32
  }
33
};
34
35
Subject.prototype = Object.create(Conclusion.prototype);
36
37
Subject._gedxClass = Subject.prototype._gedxClass = 'GedcomX.Subject';
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
 * Is this an extracted conclusion?
51
 * 
52
 * @returns {Boolean} extracted
53
 */
54
Subject.prototype.isExtracted = function(){
55
  // Double exclamations force a boolean no matter what type the property
56
  // currently is. The main reason for this is to force undefiend into false.
57
  return !!this.isExtracted;
58
};
59
60
/**
61
 * Set the extracted property
62
 * 
63
 * @param {Boolean} extracted
64
 * @returns {Subject} This instance.
65
 */
66
Subject.prototype.setExtracted = function(extracted){
67
  // Double exclamations force a boolean
68
  this.extracted = !!extracted;
69
  return this;
70
};
71
72
/**
73
 * Get the evidence.
74
 * 
75
 * @returns {EvidenceReference[]}
76
 */
77
Subject.prototype.getEvidence = function(){
78
  return this.evidence || [];
79
};
80
81
/**
82
 * Set the evidence. This method will replace existing evidence entries with new entries.
83
 * 
84
 * @param {Object[]|EvidenceReference[]}
85
 * @returns {Subject} This instance.
86
 */
87
Subject.prototype.setEvidence = function(evidence){
88
  if(Array.isArray(evidence)){
89
    this.evidence = [];
90
    var subject = this;
91
    evidence.forEach(function(e){
92
      subject.addEvidence(e);
93
    });
94
  }
95
  return this;
96
};
97
98
/**
99
 * Add evidence
100
 * 
101
 * @param {Object|EvidenceReference}
102
 * @returns {Subject} This instance.
103
 */
104
Subject.prototype.addEvidence = function(evidence){
105
  if(evidence){
106
    if(!Array.isArray(this.evidence)){
107
      this.evidence = [];
108
    }
109
    this.evidence.push(EvidenceReference(evidence));
110
  }
111
  return this;
112
};
113
114
/**
115
 * Get the identifiers
116
 * 
117
 * @return {Identifiers}
118
 */
119
Subject.prototype.getIdentifiers = function(){
120
  return this.identifiers;
121
};
122
123
/**
124
 * Set the identifiers
125
 * 
126
 * @param {Object|Identifiers}
127
 * @returns {Subject} This instance
128
 */
129
Subject.prototype.setIdentifiers = function(identifiers){
130
  if(identifiers){
131
    this.identifiers = Identifiers(identifiers);
132
  }
133
  return this;
134
};
135
136
/**
137
 * Get the media references
138
 * 
139
 * @returns {SourceReference[]}
140
 */
141
Subject.prototype.getMedia = function(){
142
  return this.media || [];
143
};
144
145
/**
146
 * Set the media references
147
 * 
148
 * @param {Object[]|SourceReference[]}
149
 */
150
Subject.prototype.setMedia = function(media){
151
  if(Array.isArray(media)){
152
    this.media = [];
153
    var subject = this;
154
    media.forEach(function(e){
155
      subject.addMedia(e);
156
    });
157
  }
158
  return this;
159
};
160
161
/**
162
 * Add media
163
 * 
164
 * @param {Object|SourceReference}
165
 * @returns {Subject} This instance.
166
 */
167
Subject.prototype.addMedia = function(media){
168
  if(media){
169
    if(!Array.isArray(this.media)){
170
      this.media = [];
171
    }
172
    this.media.push(SourceReference(media));
173
  }
174
  return this;
175
};
176
177
/**
178
 * Export the object as JSON
179
 * 
180
 * @return {Object} JSON object
181
 */
182
Subject.prototype.toJSON = function(){
183
  var json = Conclusion.prototype.toJSON.call(this);
184
  
185
  if(this.extracted){
186
    json.extracted = this.extracted;
187
  }
188
  
189
  if(this.evidence){
190
    json.evidence = this.evidence.map(function(e){
191
      return e.toJSON();
192
    });
193
  }
194
  
195
  if(this.identifiers){
196
    json.identifiers = this.identifiers.toJSON();
197
  }
198
  
199
  if(this.media){
200
    json.media = this.media.map(function(m){
201
      return m.toJSON();
202
    });
203
  }
204
  
205
  return json;
206
};
207
208
module.exports = Subject;

src/Person.js 1 location

@@ 1-204 (lines=204) @@
1
var Subject = require('./Subject'),
2
    Gender = require('./Gender'),
3
    Name = require('./Name'),
4
    Fact = require('./Fact'),
5
    utils = require('./utils');
6
    
7
/**
8
 * A person.
9
 * 
10
 * @constructor
11
 * @param {Object} [json]
12
 */
13
var Person = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Person)){
17
    return new Person(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Person.isInstance(json)){
22
    return json;
23
  }
24
  
25
  Subject.call(this, json);
26
  
27
  if(json){
28
    this.setPrivate(json.private);
29
    this.setGender(json.gender);
30
    this.setNames(json.names);
31
    this.setFacts(json.facts);
32
  }
33
};
34
35
Person.prototype = Object.create(Subject.prototype);
36
37
Person._gedxClass = Person.prototype._gedxClass = 'GedcomX.Person';
38
39
/**
40
 * Check whether the given object is an instance of this class.
41
 * 
42
 * @param {Object} obj
43
 * @returns {Boolean}
44
 */
45
Person.isInstance = function(obj){
46
  return utils.isInstance(obj, this._gedxClass);
47
};
48
49
/**
50
 * Check whether the person is marked as private
51
 * 
52
 * @returns {Boolean} private
53
 */
54
Person.prototype.isPrivate = function(){
55
  return !!this.private;
56
};
57
58
/**
59
 * Set the private flag
60
 * 
61
 * @param {Boolean} isPrivate
62
 * @returns {Person} This instance
63
 */
64
Person.prototype.setPrivate = function(isPrivate){
65
  this.private = isPrivate;
66
  return this;
67
};
68
69
/**
70
 * Get the person's gender
71
 * 
72
 * @returns {Gender} gender
73
 */
74
Person.prototype.getGender = function(){
75
  return this.gender;
76
};
77
78
/**
79
 * Set the person's gender
80
 * 
81
 * @param {Gender} gender
82
 * @returns {Person} This instance
83
 */
84
Person.prototype.setGender = function(gender){
85
  if(gender){
86
    this.gender = Gender(gender);
87
  }
88
  return this;
89
};
90
91
/**
92
 * Get the names
93
 * 
94
 * @return {Name[]}
95
 */
96
Person.prototype.getNames = function(){
97
  return this.names || [];
98
};
99
100
/**
101
 * Set the names
102
 * 
103
 * @param {Name[]|Object[]} names
104
 * @returns {Person} This instance
105
 */
106
Person.prototype.setNames = function(names){
107
  if(Array.isArray(names)){
108
    this.names = [];
109
    var person = this;
110
    names.forEach(function(n){
111
      person.addName(n);
112
    });
113
  }
114
};
115
116
/**
117
 * Add a name
118
 * 
119
 * @param {NameForm|Object} name
120
 * @returns {Person} This instance
121
 */
122
Person.prototype.addName = function(name){
123
  if(name){
124
    if(!Array.isArray(this.names)){
125
      this.names = [];
126
    }
127
    this.names.push(Name(name));
128
  }
129
  return this;
130
};
131
132
/**
133
 * Get the facts
134
 * 
135
 * @return {Fact[]}
136
 */
137
Person.prototype.getFacts = function(){
138
  return this.facts || [];
139
};
140
141
/**
142
 * Set the facts
143
 * 
144
 * @param {Fact[]|Object[]} facts
145
 * @returns {Person} This instance
146
 */
147
Person.prototype.setFacts = function(facts){
148
  if(Array.isArray(facts)){
149
    this.facts = [];
150
    var person = this;
151
    facts.forEach(function(n){
152
      person.addFact(n);
153
    });
154
  }
155
};
156
157
/**
158
 * Add a fact
159
 * 
160
 * @param {Fact|Object} fact
161
 * @returns {Person} This instance
162
 */
163
Person.prototype.addFact = function(fact){
164
  if(fact){
165
    if(!Array.isArray(this.facts)){
166
      this.facts = [];
167
    }
168
    this.facts.push(Fact(fact));
169
  }
170
  return this;
171
};
172
173
/**
174
 * Export the object as JSON
175
 * 
176
 * @return {Object} JSON object
177
 */
178
Person.prototype.toJSON = function(){
179
  var json = Subject.prototype.toJSON.call(this);
180
  
181
  if(typeof this.private === 'boolean'){
182
    json.private = this.private;
183
  }
184
  
185
  if(this.gender){
186
    json.gender = this.gender.toJSON();
187
  }
188
  
189
  if(this.names){
190
    json.names = this.names.map(function(n){
191
      return n.toJSON();
192
    });
193
  }
194
  
195
  if(this.facts){
196
    json.facts = this.facts.map(function(f){
197
      return f.toJSON();
198
    });
199
  }
200
  
201
  return json;
202
};
203
204
module.exports = Person;

src/Relationship.js 1 location

@@ 1-182 (lines=182) @@
1
var Subject = require('./Subject'),
2
    Fact = require('./Fact'),
3
    ResourceReference = require('./ResourceReference'),
4
    utils = require('./utils');
5
    
6
/**
7
 * A relationship.
8
 * 
9
 * @constructor
10
 * @param {Object} [json]
11
 */
12
var Relationship = function(json){
13
  
14
  // Protect against forgetting the new keyword when calling the constructor
15
  if(!(this instanceof Relationship)){
16
    return new Relationship(json);
17
  }
18
  
19
  // If the given object is already an instance then just return it. DON'T copy it.
20
  if(Relationship.isInstance(json)){
21
    return json;
22
  }
23
  
24
  Subject.call(this, json);
25
  
26
  if(json){
27
    this.setType(json.type);
28
    this.setPerson1(json.person1);
29
    this.setPerson2(json.person2);
30
    this.setFacts(json.facts);
31
  }
32
};
33
34
Relationship.prototype = Object.create(Subject.prototype);
35
36
Relationship._gedxClass = Relationship.prototype._gedxClass = 'GedcomX.Relationship';
37
38
/**
39
 * Check whether the given object is an instance of this class.
40
 * 
41
 * @param {Object} obj
42
 * @returns {Boolean}
43
 */
44
Relationship.isInstance = function(obj){
45
  return utils.isInstance(obj, this._gedxClass);
46
};
47
48
/**
49
 * Get the relationship type
50
 * 
51
 * @returns {String} type
52
 */
53
Relationship.prototype.getType = function(){
54
  return this.type;
55
};
56
57
/**
58
 * Set the relationship type
59
 * 
60
 * @param {String} type
61
 * @returns {Relationship} This instance
62
 */
63
Relationship.prototype.setType = function(type){
64
  this.type = type;
65
  return this;
66
};
67
68
/**
69
 * Get person1 reference
70
 * 
71
 * @returns {ResourceReference}
72
 */
73
Relationship.prototype.getPerson1 = function(){
74
  return this.person1;
75
};
76
77
/**
78
 * Set the person1 reference
79
 * 
80
 * @param {ResourceReference} person1
81
 * @returns {Relationship} This instance
82
 */
83
Relationship.prototype.setPerson1 = function(person1){
84
  if(person1){
85
    this.person1 = ResourceReference(person1);
86
  }
87
  return this;
88
};
89
90
/**
91
 * Get person2 reference
92
 * 
93
 * @returns {ResourceReference}
94
 */
95
Relationship.prototype.getPerson2 = function(){
96
  return this.person2;
97
};
98
99
/**
100
 * Set the person1 reference
101
 * 
102
 * @param {ResourceReference} person2
103
 * @returns {Relationship} This instance
104
 */
105
Relationship.prototype.setPerson2 = function(person2){
106
  if(person2){
107
    this.person2 = ResourceReference(person2);
108
  }
109
  return this;
110
};
111
112
/**
113
 * Get the facts
114
 * 
115
 * @return {Fact[]}
116
 */
117
Relationship.prototype.getFacts = function(){
118
  return this.facts || [];
119
};
120
121
/**
122
 * Set the facts
123
 * 
124
 * @param {Fact[]|Object[]} facts
125
 * @returns {Relationship} This instance
126
 */
127
Relationship.prototype.setFacts = function(facts){
128
  if(Array.isArray(facts)){
129
    this.facts = [];
130
    var person = this;
131
    facts.forEach(function(n){
132
      person.addFact(n);
133
    });
134
  }
135
};
136
137
/**
138
 * Add a fact
139
 * 
140
 * @param {Fact|Object} fact
141
 * @returns {Relationship} This instance
142
 */
143
Relationship.prototype.addFact = function(fact){
144
  if(fact){
145
    if(!Array.isArray(this.facts)){
146
      this.facts = [];
147
    }
148
    this.facts.push(Fact(fact));
149
  }
150
  return this;
151
};
152
153
/**
154
 * Export the object as JSON
155
 * 
156
 * @return {Object} JSON object
157
 */
158
Relationship.prototype.toJSON = function(){
159
  var json = Subject.prototype.toJSON.call(this);
160
  
161
  if(this.type){
162
    json.type = this.type;
163
  }
164
  
165
  if(this.person1){
166
    json.person1 = this.person1.toJSON();
167
  }
168
  
169
  if(this.person2){
170
    json.person2 = this.person2.toJSON();
171
  }
172
  
173
  if(this.facts){
174
    json.facts = this.facts.map(function(f){
175
      return f.toJSON();
176
    });
177
  }
178
  
179
  return json;
180
};
181
182
module.exports = Relationship;