Completed
Push — master ( 2281e8...8b163e )
by Justin
01:35
created

Agent.js ➔ Agent   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 26
rs 8.5806
1
var ExtensibleData = require('./ExtensibleData'),
2
    ResourceReference = require('./ResourceReference'),
3
    OnlineAccount = require('./OnlineAccount'),
4
    Address = require('./Address'),
5
    Identifiers = require('./Identifiers'),
6
    TextValue = require('./TextValue'),
7
    utils = require('./utils');
8
9
/**
10
 * Someone or something that curates genealogical data, such as a genealogical 
11
 * researcher, user of software, or organization.
12
 * 
13
 * @constructor
14
 * @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...
15
 */
16
var Agent = function(json){
17
  
18
  // Protect against forgetting the new keyword when calling the constructor
19
  if(!(this instanceof Agent)){
20
    return new Agent(json);
21
  }
22
  
23
  // If the given object is already an instance then just return it. DON'T copy it.
24
  if(Agent.isInstance(json)){
25
    return json;
26
  }
27
  
28
  ExtensibleData.call(this, json);
29
  
30
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
31
    this.setIdentifiers(json.identifiers);
32
    this.setNames(json.names);
33
    this.setHomepage(json.homepage);
34
    this.setOpenid(json.openid);
35
    this.setAccounts(json.accounts);
36
    this.setEmails(json.emails);
37
    this.setPhones(json.phones);
38
    this.setAddresses(json.addresses);
39
    this.setPerson(json.person);
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...
40
  }
41
};
42
43
Agent.prototype = Object.create(ExtensibleData.prototype);
44
45
Agent._gedxClass = Agent.prototype._gedxClass = 'GedcomX.Agent';
46
47
/**
48
 * Check whether the given object is an instance of this class.
49
 * 
50
 * @param {Object} obj
51
 * @returns {Boolean}
52
 */
53
Agent.isInstance = function(obj){
54
  return utils.isInstance(obj, this._gedxClass);
55
};
56
57
/**
58
 * Get the identifiers
59
 * 
60
 * @returns {Identifiers}
61
 */
62
Agent.prototype.getIdentifiers = function(){
63
  return this.identifiers;
64
};
65
66
/**
67
 * Set the identifiers
68
 * 
69
 * @param {Identifiers} identifiers
70
 * @returns {Agent}
71
 */
72
Agent.prototype.setIdentifiers = function(identifiers){
73
  if(identifiers){
74
    this.identifiers = Identifiers(identifiers);
75
  }
76
  return this;
77
};
78
79
/**
80
 * Get the names
81
 * 
82
 * @returns {TextValue[]}
83
 */
84
Agent.prototype.getNames = function(){
85
  return this.names || [];
86
};
87
88
/**
89
 * Set the names
90
 * 
91
 * @param {TextValue[]|Object[]} names
92
 * @returns {Agent}
93
 */
94
Agent.prototype.setNames = function(names){
95
  if(Array.isArray(names)){
96
    var agent = this;
97
    this.names = [];
98
    names.forEach(function(name){
99
      agent.addName(name);
100
    });
101
  }
102
  return this;
103
};
104
105
/**
106
 * Add a name
107
 * 
108
 * @param {TextValue|Object} name
109
 * @return {Agent}
110
 */
111
Agent.prototype.addName = function(name){
112
  if(name){
113
    if(!Array.isArray(this.names)){
114
      this.names = [];
115
    }
116
    this.names.push(TextValue(name));
117
  }
118
  return this;
119
};
120
121
/**
122
 * Get the home page
123
 * 
124
 * @returns {ResourceReference}
125
 */
126
Agent.prototype.getHomepage = function(){
127
  return this.homepage;
128
};
129
130
/**
131
 * Set the home page
132
 * 
133
 * @param {ResourceReference|Object} homepage
134
 * @returns {Agent}
135
 */
136
Agent.prototype.setHomepage = function(homepage){
137
  if(homepage){
138
    this.homepage = ResourceReference(homepage);
139
  }
140
  return this;
141
};
142
143
/**
144
 * Get the openid
145
 * 
146
 * @returns {ResourceReference}
147
 */
148
Agent.prototype.getOpenid = function(){
149
  return this.openid;
150
};
151
152
/**
153
 * Set the openid
154
 * 
155
 * @params {ResourceReference} openid
156
 * @returns {Agent}
157
 */
158
Agent.prototype.setOpenid = function(openid){
159
  if(openid){
160
    this.openid = ResourceReference(openid);
161
  }
162
  return this;
163
};
164
165
/**
166
 * Get the accounts
167
 * 
168
 * @returns {OnlineAccount[]}
169
 */
170
Agent.prototype.getAccounts = function(){
171
  return this.accounts || [];
172
};
173
174
/**
175
 * Set the accounts
176
 * 
177
 * @param {OnlineAccount[]|Object[]} accounts
178
 * @returns {Agent}
179
 */
180
Agent.prototype.setAccounts = function(accounts){
181
  if(Array.isArray(accounts)){
182
    var agent = this;
183
    agent.accounts = [];
184
    accounts.forEach(function(account){
185
      agent.addAccount(account);
186
    });
187
  }
188
  return this;
189
};
190
191
/**
192
 * Add an account
193
 * 
194
 * @param {OnlineAccount|Object} account
195
 * @returns {Agent}
196
 */
197
Agent.prototype.addAccount = function(account){
198
  if(account){
199
    if(!Array.isArray(this.accounts)){
200
      this.accounts = [];
201
    }
202
    this.accounts.push(OnlineAccount(account));
203
  }
204
  return this;
205
};
206
207
/**
208
 * Get the emails
209
 * 
210
 * @returns {ResourceReference[]}
211
 */
212
Agent.prototype.getEmails= function(){
213
  return this.emails || [];
214
};
215
216
/**
217
 * Set the emails
218
 * 
219
 * @param {ResourceReference[]|Object[]} emails
220
 * @returns {Agent}
221
 */
222
Agent.prototype.setEmails = function(emails){
223
  if(Array.isArray(emails)){
224
    var agent = this;
225
    agent.emails = [];
226
    emails.forEach(function(email){
227
      agent.addEmail(email);
228
    });
229
  }
230
  return this;
231
};
232
233
/**
234
 * Add an email
235
 * 
236
 * @param {ResourceReference|Object} email
237
 * @returns {Agent}
238
 */
239
Agent.prototype.addEmail = function(email){
240
  if(email){
241
    if(!Array.isArray(this.emails)){
242
      this.emails = [];
243
    }
244
    this.emails.push(ResourceReference(email));
245
  }
246
  return this;
247
};
248
249
/**
250
 * Get the phones
251
 * 
252
 * @returns {ResourceReference[]}
253
 */
254
Agent.prototype.getPhones = function(){
255
  return this.phones || [];
256
};
257
258
/**
259
 * Set the phones
260
 * 
261
 * @param {ResourceReference[]|Object[]} phones
262
 * @returns {Agent}
263
 */
264
Agent.prototype.setPhones = function(phones){
265
  if(Array.isArray(phones)){
266
    var agent = this;
267
    agent.phones = [];
268
    phones.forEach(function(phone){
269
      agent.addPhone(phone);
270
    });
271
  }
272
  return this;
273
};
274
275
/**
276
 * Add a phone
277
 * 
278
 * @param {ResourceReference|Object} phone
279
 * @returns {Agent}
280
 */
281
Agent.prototype.addPhone = function(phone){
282
  if(phone){
283
    if(!Array.isArray(this.phones)){
284
      this.phones = [];
285
    }
286
    this.phones.push(ResourceReference(phone));
287
  }
288
  return this;
289
};
290
291
/**
292
 * Get the addresses
293
 * 
294
 * @returns {Address[]}
295
 */
296
Agent.prototype.getAddresses = function(){
297
  return this.addresses || [];
298
};
299
300
/**
301
 * Set the addresses
302
 * 
303
 * @param {Address[]|Object[]} addresses
304
 * @returns {Agent}
305
 */
306
Agent.prototype.setAddresses = function(addresses){
307
  if(Array.isArray(addresses)){
308
    var agent = this;
309
    agent.addresses = [];
310
    addresses.forEach(function(address){
311
      agent.addAddress(address);
312
    });
313
  }
314
  return this;
315
};
316
317
/**
318
 * Add an address
319
 * 
320
 * @param {Address|Object} address
321
 * @returns {Agent}
322
 */
323
Agent.prototype.addAddress = function(address){
324
  if(address){
325
    if(!Array.isArray(this.addresses)){
326
      this.addresses = [];
327
    }
328
    this.addresses.push(Address(address));
329
  }
330
  return this;
331
};
332
333
/**
334
 * Get the person reference
335
 * 
336
 * @returns {ResourceReference}
337
 */
338
Agent.prototype.getPerson = function(){
339
  return this.person;
340
};
341
342
/**
343
 * Set the person reference
344
 * 
345
 * @param {ResourceReference} person
346
 * @returns {Agent}
347
 */
348
Agent.prototype.setPerson = function(person){
349
  if(person){
350
    this.person = ResourceReference(person);
351
  }
352
  return this;
353
};
354
355
/**
356
 * Export the object as JSON
357
 * 
358
 * @return {Object} JSON object
359
 */
360
Agent.prototype.toJSON = function(){
361
  var json = ExtensibleData.prototype.toJSON.call(this);
362
  
363
  if(this.identifiers){
364
    json.identifiers = this.identifiers.toJSON();
365
  }
366
  
367
  if(this.names){
368
    json.names = this.names.map(function(n){
369
      return n.toJSON();
370
    });
371
  }
372
  
373
  if(this.homepage){
374
    json.homepage = this.homepage.toJSON();
375
  }
376
  
377
  if(this.openid){
378
    json.openid = this.openid.toJSON();
379
  }
380
  
381
  if(this.accounts){
382
    json.accounts = this.accounts.map(function(a){
383
      return a.toJSON();
384
    });
385
  }
386
  
387
  if(this.emails){
388
    json.emails = this.emails.map(function(e){
389
      return e.toJSON();
390
    });
391
  }
392
  
393
  if(this.phones){
394
    json.phones = this.phones.map(function(p){
395
      return p.toJSON();
396
    });
397
  }
398
  
399
  if(this.addresses){
400
    json.addresses = this.addresses.map(function(a){
401
      return a.toJSON();
402
    });
403
  }
404
  
405
  if(this.person){
406
    json.person = this.person.toJSON();
407
  }
408
  
409
  return json;
410
};
411
412
module.exports = Agent;