src/records/FieldDescriptor.js   A
last analyzed

Complexity

Total Complexity 16
Complexity/F 1.23

Size

Lines of Code 157
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 4
dl 0
loc 157
rs 10
wmc 16
mnd 1
bc 16
fnc 13
bpm 1.2306
cpm 1.2306
noi 2
1
module.exports = function(GedcomX){
2
  
3
  var utils = require('../utils');
4
  
5
  /**
6
   * Metadata about the structure and layout of a field, as well as the expected
7
   * data to be extracted from a field.
8
   * 
9
   * @see {@link https://github.com/FamilySearch/gedcomx-record/blob/master/specifications/record-specification.md#field-descriptor|GEDCOM X Records Spec}
10
   * 
11
   * @class FieldDescriptor
12
   * @extends ExtensibleData
13
   * @param {Object} [json]
14
   */
15
  var FieldDescriptor = function(json){
16
    
17
    // Protect against forgetting the new keyword when calling the constructor
18
    if(!(this instanceof FieldDescriptor)){
19
      return new FieldDescriptor(json);
20
    }
21
    
22
    // If the given object is already an instance then just return it. DON'T copy it.
23
    if(FieldDescriptor.isInstance(json)){
24
      return json;
25
    }
26
    
27
    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...
28
  };
29
  
30
  FieldDescriptor.prototype = Object.create(GedcomX.ExtensibleData.prototype);
31
  
32
  FieldDescriptor._gedxClass = FieldDescriptor.prototype._gedxClass = 'GedcomX.FieldDescriptor';
33
  
34
  FieldDescriptor.jsonProps = [
35
    'originalLabel',
36
    'descriptions',
37
    'values'
38
  ];
39
  
40
  /**
41
   * Check whether the given object is an instance of this class.
42
   * 
43
   * @param {Object} obj
44
   * @returns {Boolean}
45
   */
46
  FieldDescriptor.isInstance = function(obj){
47
    return utils.isInstance(obj, this._gedxClass);
48
  };
49
50
  /**
51
   * Initialize from JSON
52
   * 
53
   * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
54
   * @return {FieldDescriptor} this
55
   */
56
  FieldDescriptor.prototype.init = function(json){
57
    
58
    GedcomX.ExtensibleData.prototype.init.call(this, json);
59
    
60
    if(json){
61
      this.setOriginalLabel(json.originalLabel);
62
      this.setDescriptions(json.descriptions);
63
      this.setValues(json.values);
64
    }
65
    return this;
66
  };
67
  
68
  /**
69
   * Set the original label
70
   * 
71
   * @param {String} originalLabel
72
   * @return {FieldDescriptor} this
73
   */
74
  FieldDescriptor.prototype.setOriginalLabel = function(originalLabel){
75
    this.originalLabel = originalLabel;
76
    return this;
77
  };
78
  
79
  /**
80
   * Get the original label
81
   * 
82
   * @return {String}
83
   */
84
  FieldDescriptor.prototype.getOriginalLabel = function(){
85
    return this.originalLabel;
86
  };
87
   
88
  /**
89
   * Set the descriptions
90
   * 
91
   * @param {TextValue[]} descriptions
92
   * @return {FieldDescriptor} this
93
   */
94
  FieldDescriptor.prototype.setDescriptions = function(descriptions){
95
    return this._setArray(descriptions, 'descriptions', 'addDescription');
96
  };
97
  
98
  /**
99
   * Add a description
100
   * 
101
   * @param {TextValue} description
102
   * @return {FieldDescriptor} this
103
   */
104
  FieldDescriptor.prototype.addDescription = function(description){
105
    return this._arrayPush(description, 'descriptions', GedcomX.TextValue);
106
  };
107
  
108
  /**
109
   * Get the descriptions
110
   * 
111
   * @return {TextValue[]} descriptions
112
   */
113
  FieldDescriptor.prototype.getDescriptions = function(){
114
    return this.descriptions || [];
115
  };
116
  
117
  /**
118
   * Set the values
119
   * 
120
   * @param {FieldValueDescriptor[]} values
121
   * @return {FieldDescriptor} this
122
   */
123
  FieldDescriptor.prototype.setValues = function(values){
124
    return this._setArray(values, 'values', 'addValue');
125
  };
126
  
127
  /**
128
   * Add a value
129
   * 
130
   * @param {FieldValueDescriptor} value
131
   * @return {FieldDescriptor} this
132
   */
133
  FieldDescriptor.prototype.addValue = function(value){
134
    return this._arrayPush(value, 'values', GedcomX.FieldValueDescriptor);
135
  };
136
  
137
  /**
138
   * Get the values
139
   * 
140
   * @return {FieldValueDescriptor[]} values
141
   */
142
  FieldDescriptor.prototype.getValues = function(){
143
    return this.values || [];
144
  };
145
   
146
  /**
147
   * Export the object as JSON
148
   * 
149
   * @return {Object} JSON object
150
   */
151
  FieldDescriptor.prototype.toJSON = function(){
152
    return this._toJSON(GedcomX.ExtensibleData, FieldDescriptor.jsonProps);
153
  };
154
  
155
  GedcomX.FieldDescriptor = FieldDescriptor;
156
  
157
};