Completed
Push — master ( 4f2dd2...dfa083 )
by Justin
01:40
created

NameForm.getFullText   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 9.4285
1
var GedcomX = require('../'),
2
    utils = require('../utils');
3
4
/**
5
 * A form of a name.
6
 * 
7
 * @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#name-form|GEDCOM X JSON Spec}
8
 * 
9
 * @class
10
 * @extends ExtensibleData
11
 * @param {Object} [json]
12
 */
13
var NameForm = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof NameForm)){
17
    return new NameForm(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(NameForm.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
NameForm.prototype = Object.create(GedcomX.ExtensibleData.prototype);
29
30
NameForm._gedxClass = NameForm.prototype._gedxClass = 'GedcomX.NameForm';
31
32
NameForm.jsonProps = [
33
  'lang',
34
  'fullText',
35
  'parts'
36
];
37
38
/**
39
 * Check whether the given object is an instance of this class.
40
 * 
41
 * @param {Object} obj
42
 * @returns {Boolean}
43
 */
44
NameForm.isInstance = function(obj){
45
  return utils.isInstance(obj, this._gedxClass);
46
};
47
48
/**
49
 * Initialize from JSON
50
 * 
51
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
52
 * @return {NameForm} this
53
 */
54
NameForm.prototype.init = function(json){
55
  
56
  GedcomX.ExtensibleData.prototype.init.call(this, json);
57
  
58
  if(json){
59
    this.setLang(json.lang);
60
    this.setFullText(json.fullText);
61
    this.setParts(json.parts);
62
  }
63
  
64
  return this;
65
};
66
67
/**
68
 * Get the lang tag
69
 * 
70
 * @returns {String} lang
71
 */
72
NameForm.prototype.getLang = function(){
73
  return this.lang;
74
};
75
76
/**
77
 * Set the lang tag
78
 * 
79
 * @param {String} lang
80
 * @returns {NameForm} This instance
81
 */
82
NameForm.prototype.setLang = function(lang){
83
  this.lang = lang;
84
  return this;
85
};
86
87
/**
88
 * Get the full text
89
 * 
90
 * @param {Boolean=} calculateIfMissing Calculate the full text if it's not set.
91
 * @returns {String} fullText
92
 */
93
NameForm.prototype.getFullText = function(calculateIfMissing){
94
  if(this.fullText){
95
    return this.fullText;
96
  } else if(calculateIfMissing) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if calculateIfMissing 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...
97
    var parts = [], nameForm = this;
98
    ['Prefix', 'Given', 'Surname', 'Suffix'].forEach(function(type){
99
      parts = parts.concat(nameForm.getParts('http://gedcomx.org/' + type).map(function(p){
100
        return p.value;
101
      }));
102
      /*
103
      var part = nameForm.getPart('http://gedcomx.org/' + type);
104
      if(part){
105
        parts.push(part.value);
106
      }
107
      */
108
    });
109
    return parts.join(' ');
110
  }
111
};
112
113
/**
114
 * Set the full text
115
 * 
116
 * @param {String} fullText
117
 * @returns {NameForm} This instance
118
 */
119
NameForm.prototype.setFullText = function(fullText){
120
  this.fullText = fullText;
121
  return this;
122
};
123
124
/**
125
 * Get the name parts
126
 * 
127
 * @param {String=} type
128
 * @returns {NamePart[]}
129
 */
130
NameForm.prototype.getParts = function(type){
131
  if(!this.parts){
132
    return [];
133
  }
134
  else if(type){
135
    var filtered = [];
136
    for(var i = 0; i < this.parts.length; i++){
137
      if(this.parts[i].getType() === type){
138
        filtered.push(this.parts[i]);
139
      }
140
    }
141
    return filtered;
142
  } 
143
  else {
144
    return this.parts;
145
  }
146
};
147
148
/**
149
 * Set the name parts
150
 * 
151
 * @param {NamePart[]|Object[]} parts
152
 * @returns {NameForm} This instance
153
 */
154
NameForm.prototype.setParts = function(parts){
155
  return this._setArray(parts, 'parts', 'addPart');
156
};
157
158
/**
159
 * Add a name part
160
 * 
161
 * @param {NamePart|Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
162
 * @returns {NameForm}
163
 */
164
NameForm.prototype.addPart = function(part){
165
  return this._arrayPush(part, 'parts', GedcomX.NamePart);
166
};
167
168
/**
169
 * Export the object as JSON
170
 * 
171
 * @return {Object} JSON object
172
 */
173
NameForm.prototype.toJSON = function(){
174
  return this._toJSON(GedcomX.ExtensibleData, NameForm.jsonProps);
175
};
176
177
module.exports = NameForm;