src/core/SourceDescription.js   C
last analyzed

Complexity

Total Complexity 56
Complexity/F 1.19

Size

Lines of Code 539
Function Count 47

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 128
dl 0
loc 539
rs 6
wmc 56
mnd 1
bc 56
fnc 47
bpm 1.1914
cpm 1.1914
noi 6

47 Functions

Rating   Name   Duplication   Size   Complexity  
A SourceDescription.addSource 0 3 1
A SourceDescription.addTitle 0 3 1
A SourceDescription.setComponentOf 0 6 2
A SourceDescription.getDescriptions 0 3 1
A SourceDescription.getSources 0 3 1
B SourceDescription.init 0 26 2
A SourceDescription.getCoverage 0 3 1
A SourceDescription.setNotes 0 3 1
A SourceDescription.js ➔ SourceDescription 0 14 3
A SourceDescription.setIdentifiers 0 6 2
A SourceDescription.getCitations 0 3 1
A SourceDescription.getNotes 0 3 1
A SourceDescription.getIdentifiers 0 3 1
A SourceDescription.addNote 0 3 1
A SourceDescription.getMediator 0 3 1
A SourceDescription.setCreated 0 4 1
A SourceDescription.setCoverage 0 3 1
A SourceDescription.getTitles 0 3 1
A SourceDescription.toJSON 0 3 1
A SourceDescription.addCoverage 0 3 1
A SourceDescription.addRight 0 3 1
A SourceDescription.setModified 0 4 1
A SourceDescription.getCreated 0 3 1
A SourceDescription.getAttribution 0 3 1
A SourceDescription.setSources 0 3 1
A SourceDescription.getMediaType 0 3 1
A SourceDescription.setRights 0 3 1
A SourceDescription.setMediaType 0 4 1
A SourceDescription.isInstance 0 3 1
A SourceDescription.setAbout 0 4 1
A SourceDescription.setCitations 0 3 1
A SourceDescription.getRepository 0 3 1
A SourceDescription.getResourceType 0 3 1
A SourceDescription.getRights 0 3 1
A SourceDescription.setResourceType 0 4 1
A SourceDescription.setAnalysis 0 6 2
A SourceDescription.getModified 0 3 1
A SourceDescription.setRepository 0 6 2
A SourceDescription.setTitles 0 3 1
A SourceDescription.addDescription 0 3 1
A SourceDescription.getComponentOf 0 3 1
A SourceDescription.setDescriptions 0 3 1
A SourceDescription.getAnalysis 0 3 1
A SourceDescription.setMediator 0 6 2
A SourceDescription.getAbout 0 3 1
A SourceDescription.addCitation 0 3 1
A SourceDescription.setAttribution 0 6 2

How to fix   Complexity   

Complexity

Complex classes like src/core/SourceDescription.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
var GedcomX = require('../'),
2
    utils = require('../utils');
3
4
/**
5
 * A description of a source.
6
 * 
7
 * @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#source-description|GEDCOM X JSON Spec}
8
 * 
9
 * @class
10
 * @extends ExtensibleData
11
 * @apram {Object} [json]
12
 */
13
var SourceDescription = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof SourceDescription)){
17
    return new SourceDescription(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(SourceDescription.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
SourceDescription.prototype = Object.create(GedcomX.ExtensibleData.prototype);
29
30
SourceDescription._gedxClass = SourceDescription.prototype._gedxClass = 'GedcomX.SourceDescription';
31
32
SourceDescription.jsonProps = [
33
  'resourceType',
34
  'citations',
35
  'mediaType',
36
  'about',
37
  'mediator',
38
  'sources',
39
  'analysis',
40
  'componentOf',
41
  'titles',
42
  'notes',
43
  'attribution',
44
  'rights',
45
  'coverage',
46
  'descriptions',
47
  'identifiers',
48
  'created',
49
  'modified',
50
  'repository'
51
];
52
53
/**
54
 * Check whether the given object is an instance of this class.
55
 * 
56
 * @param {Object} obj
57
 * @returns {Boolean}
58
 */
59
SourceDescription.isInstance = function(obj){
60
  return utils.isInstance(obj, this._gedxClass);
61
};
62
63
/**
64
 * Initialize from JSON
65
 * 
66
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
67
 * @return {SourceDescription} this
68
 */
69
SourceDescription.prototype.init = function(json){
70
  
71
  GedcomX.ExtensibleData.prototype.init.call(this, json);
72
  
73
  if(json){
74
    this.setResourceType(json.resourceType);
75
    this.setCitations(json.citations);
76
    this.setMediaType(json.mediaType);
77
    this.setAbout(json.about);
78
    this.setMediator(json.mediator);
79
    this.setSources(json.sources);
80
    this.setAnalysis(json.analysis);
81
    this.setComponentOf(json.componentOf);
82
    this.setTitles(json.titles);
83
    this.setNotes(json.notes);
84
    this.setAttribution(json.attribution);
85
    this.setRights(json.rights);
86
    this.setCoverage(json.coverage);
87
    this.setDescriptions(json.descriptions);
88
    this.setIdentifiers(json.identifiers);
89
    this.setCreated(json.created);
90
    this.setModified(json.modified);
91
    this.setRepository(json.repository);
92
  }
93
  return this;
94
};
95
96
/**
97
 * Get the resource type
98
 * 
99
 * @returns {String}
100
 */
101
SourceDescription.prototype.getResourceType = function(){
102
  return this.resourceType;
103
};
104
105
/**
106
 * Set the resource type
107
 * 
108
 * @param {String} resourceType
109
 * @returns {SourceDescription}
110
 */
111
SourceDescription.prototype.setResourceType = function(resourceType){
112
  this.resourceType = resourceType;
113
  return this;
114
};
115
116
/**
117
 * Get the citations
118
 * 
119
 * @returns {SourceCitation[]}
120
 */
121
SourceDescription.prototype.getCitations = function(){
122
  return this.citations || [];
123
};
124
125
/**
126
 * Set the citations
127
 * 
128
 * @param {SourceCitation[]|Object[]} citations
129
 * @returns {SourceDescription}
130
 */
131
SourceDescription.prototype.setCitations = function(citations){
132
  return this._setArray(citations, 'citations', 'addCitation');
133
};
134
135
/**
136
 * Add a citation
137
 * 
138
 * @param {SourceCitation|Object} citation
139
 * @returns {SourceDescription}
140
 */
141
SourceDescription.prototype.addCitation = function(citation){
142
  return this._arrayPush(citation, 'citations', GedcomX.SourceCitation);
143
};
144
145
/**
146
 * Get the media type
147
 * 
148
 * @return {String}
149
 */
150
SourceDescription.prototype.getMediaType = function(){
151
  return this.mediaType;
152
};
153
154
/**
155
 * Set the media type
156
 * 
157
 * @param {String} mediaType
158
 * @returns {SourceDescription}
159
 */
160
SourceDescription.prototype.setMediaType = function(mediaType){
161
  this.mediaType = mediaType;
162
  return this;
163
};
164
165
/**
166
 * Get the about property
167
 * 
168
 * @returns {String}
169
 */
170
SourceDescription.prototype.getAbout = function(){
171
  return this.about;
172
};
173
174
/**
175
 * Set the about property
176
 * 
177
 * @param {String} about
178
 * @returns {SourceDescription}
179
 */
180
SourceDescription.prototype.setAbout = function(about){
181
  this.about = about;
182
  return this;
183
};
184
185
/**
186
 * Get the mediator
187
 * 
188
 * @returns {ResourceReference}
189
 */
190
SourceDescription.prototype.getMediator = function(){
191
  return this.mediator;
192
};
193
194
/**
195
 * Set the mediator
196
 * 
197
 * @param {ResourceReference} mediator
198
 * @returns {SourceDescription}
199
 */
200
SourceDescription.prototype.setMediator = function(mediator){
201
  if(mediator){
202
    this.mediator = GedcomX.ResourceReference(mediator);
203
  }
204
  return this;
205
};
206
207
/**
208
 * Get sources
209
 * 
210
 * @returns {SourceReference[]}
211
 */
212
SourceDescription.prototype.getSources = function(){
213
  return this.sources || [];
214
};
215
216
/**
217
 * Set the sources
218
 * 
219
 * @param {SourceReference[]|Object[]} sources
220
 * @returns {SourceDescription}
221
 */
222
SourceDescription.prototype.setSources = function(sources){
223
  return this._setArray(sources, 'sources', 'addSource');
224
};
225
226
/**
227
 * Add a source
228
 * 
229
 * @param {SourceReference|Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
230
 * @returns {SourceDescription}
231
 */
232
SourceDescription.prototype.addSource = function(source){
233
  return this._arrayPush(source, 'sources', GedcomX.SourceReference);
234
};
235
236
/**
237
 * Get the analysis
238
 * 
239
 * @return {ResourceReference}
240
 */
241
SourceDescription.prototype.getAnalysis = function(){
242
  return this.analysis;
243
};
244
245
/**
246
 * Set the analysis
247
 * 
248
 * @param {ResourceReference|Object} analysis
249
 * @return {SourceDescription}
250
 */
251
SourceDescription.prototype.setAnalysis = function(analysis){
252
  if(analysis){
253
    this.analysis = GedcomX.ResourceReference(analysis);
254
  }
255
  return this;
256
};
257
258
/**
259
 * Get the componentOf property
260
 * 
261
 * @return {SourceReference}
262
 */
263
SourceDescription.prototype.getComponentOf = function(){
264
  return this.componentOf;
265
};
266
267
/**
268
 * Set the componentOf property
269
 * 
270
 * @param {SourceReference} componentOf
271
 */
272
SourceDescription.prototype.setComponentOf = function(componentOf){
273
  if(componentOf){
274
    this.componentOf = GedcomX.SourceReference(componentOf);
275
  }
276
  return this;
277
};
278
279
/**
280
 * Get titles
281
 * 
282
 * @returns {TextValue[]}
283
 */
284
SourceDescription.prototype.getTitles = function(){
285
  return this.titles || [];
286
};
287
288
/**
289
 * Set the titles
290
 * 
291
 * @param {TextValue[]|Object[]} titles
292
 * @returns {SourceDescription}
293
 */
294
SourceDescription.prototype.setTitles = function(titles){
295
  return this._setArray(titles, 'titles', 'addTitle');
296
};
297
298
/**
299
 * Add a title
300
 * 
301
 * @param {TextValue|Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
302
 * @returns {SourceDescription}
303
 */
304
SourceDescription.prototype.addTitle = function(title){
305
  return this._arrayPush(title, 'titles', GedcomX.TextValue);
306
};
307
308
/**
309
 * Get notes
310
 * 
311
 * @returns {Note[]}
312
 */
313
SourceDescription.prototype.getNotes = function(){
314
  return this.notes || [];
315
};
316
317
/**
318
 * Set the notes
319
 * 
320
 * @param {Note[]|Object[]} notes
321
 * @returns {SourceDescription}
322
 */
323
SourceDescription.prototype.setNotes = function(notes){
324
  return this._setArray(notes, 'notes', 'addNote');
325
};
326
327
/**
328
 * Add a source
329
 * 
330
 * @param {Note|Object} note
331
 * @returns {SourceDescription}
332
 */
333
SourceDescription.prototype.addNote = function(note){
334
  return this._arrayPush(note, 'notes', GedcomX.Note);
335
};
336
337
/**
338
 * Get the attribution
339
 * 
340
 * @returns {Attribution}
341
 */
342
SourceDescription.prototype.getAttribution = function(){
343
  return this.attribution;
344
};
345
346
/**
347
 * Set the attribution
348
 * 
349
 * @param {Attribution|Object} attribution
350
 * @returns {SourceDescription}
351
 */
352
SourceDescription.prototype.setAttribution = function(attribution){
353
  if(attribution){
354
    this.attribution = GedcomX.Attribution(attribution);
355
  }
356
  return this;
357
};
358
359
/**
360
 * Get the rights
361
 * 
362
 * @returns {ResourceReference[]}
363
 */
364
SourceDescription.prototype.getRights = function(){
365
  return this.rights || [];
366
};
367
368
/**
369
 * Set the rights
370
 * 
371
 * @param {ResourceReference[]|Object[]} rights
372
 * @returns {SourceDescription}
373
 */
374
SourceDescription.prototype.setRights = function(rights){
375
  return this._setArray(rights, 'rights', 'addRight');
376
};
377
378
/**
379
 * Add a source
380
 * 
381
 * @param {ResourceReference|Object} right
382
 * @returns {SourceDescription}
383
 */
384
SourceDescription.prototype.addRight = function(right){
385
  return this._arrayPush(right, 'rights', GedcomX.ResourceReference);
386
};
387
388
/**
389
 * Get the coverage
390
 * 
391
 * @returns {Coverage}
392
 */
393
SourceDescription.prototype.getCoverage = function(){
394
  return this.coverage || [];
395
};
396
397
/**
398
 * Set the coverage
399
 * 
400
 * @param {Coverage[]|Object[]} coverage
401
 * @returns {SourceDescription}
402
 */
403
SourceDescription.prototype.setCoverage = function(coverage){
404
  return this._setArray(coverage, 'coverage', 'addCoverage');
405
};
406
407
/**
408
 * Add coverage
409
 * 
410
 * @param {Coverage}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
411
 * @returns {SourceDescription}
412
 */
413
SourceDescription.prototype.addCoverage = function(coverage){
414
  return this._arrayPush(coverage, 'coverage', GedcomX.Coverage);
415
};
416
417
/**
418
 * Get the descriptions
419
 * 
420
 * @returns {TextValue[]}
421
 */
422
SourceDescription.prototype.getDescriptions = function(){
423
  return this.descriptions || [];
424
};
425
426
/**
427
 * Set the descriptions
428
 * 
429
 * @param {TextValue[]|Object[]} descriptions
430
 * @returns {SourceDescription}
431
 */
432
SourceDescription.prototype.setDescriptions = function(descriptions){
433
  return this._setArray(descriptions, 'descriptions', 'addDescription');
434
};
435
436
/**
437
 * Add a description
438
 * 
439
 * @param {TextValue|Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
440
 * @returns {SourceDescription}
441
 */
442
SourceDescription.prototype.addDescription = function(description){
443
  return this._arrayPush(description, 'descriptions', GedcomX.TextValue);
444
};
445
446
/**
447
 * Get the identifiers
448
 * 
449
 * @returns {Identifiers}
450
 */
451
SourceDescription.prototype.getIdentifiers = function(){
452
  return this.identifiers;
453
};
454
455
/**
456
 * Set the identifiers
457
 * 
458
 * @param {Identifiers} identifiers
459
 * @returns {SourceDescription}
460
 */
461
SourceDescription.prototype.setIdentifiers = function(identifiers){
462
  if(identifiers){
463
    this.identifiers = GedcomX.Identifiers(identifiers);
464
  }
465
  return this;
466
};
467
468
/**
469
 * Get the created timestamp
470
 * 
471
 * @returns {Integer}
472
 */
473
SourceDescription.prototype.getCreated = function(){
474
  return this.created;
475
};
476
477
/**
478
 * Set the created timestamp
479
 * 
480
 * @param {Integer} created
481
 * @returns {SourceDescription}
482
 */
483
SourceDescription.prototype.setCreated = function(created){
484
  this.created = created;
485
  return this;
486
};
487
488
/**
489
 * Get the modified timestamp
490
 * 
491
 * @returns {Integer}
492
 */
493
SourceDescription.prototype.getModified = function(){
494
  return this.modified;
495
};
496
497
/**
498
 * Set the modified timestamp
499
 * 
500
 * @param {Integer} modified
501
 * @returns {SourceDescription}
502
 */
503
SourceDescription.prototype.setModified = function(modified){
504
  this.modified = modified;
505
  return this;
506
};
507
508
/**
509
 * Get the repository
510
 * 
511
 * @returns {ResourceReference}
512
 */
513
SourceDescription.prototype.getRepository = function(){
514
  return this.repository;
515
};
516
517
/**
518
 * Set the repository
519
 * 
520
 * @param {ResourceReference} repository
521
 * @returns {SourceDescription}
522
 */
523
SourceDescription.prototype.setRepository = function(repository){
524
  if(repository){
525
    this.repository = GedcomX.ResourceReference(repository);
526
  }
527
  return this;
528
};
529
530
/**
531
 * Export the object as JSON
532
 * 
533
 * @return {Object} JSON object
534
 */
535
SourceDescription.prototype.toJSON = function(){
536
  return this._toJSON(GedcomX.ExtensibleData, SourceDescription.jsonProps);
537
};
538
539
module.exports = SourceDescription;