src/records/Collection.js   A
last analyzed

Complexity

Total Complexity 20
Complexity/F 1.25

Size

Lines of Code 193
Function Count 16

Duplication

Duplicated Lines 190
Ratio 98.45 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 2
dl 190
loc 193
rs 10
wmc 20
mnd 1
bc 20
fnc 16
bpm 1.25
cpm 1.25
noi 3

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