| Conditions | 1 |
| Paths | 2 |
| Total Lines | 122 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | module.exports = function(GedcomX){ |
||
| 2 | |||
| 3 | var utils = require('../utils'); |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Information about the fields of a record from which genealogical data is extracted. |
||
| 7 | * |
||
| 8 | * @constructor |
||
| 9 | * @param {Object} [json] |
||
|
|
|||
| 10 | */ |
||
| 11 | var Field = function(json){ |
||
| 12 | |||
| 13 | // Protect against forgetting the new keyword when calling the constructor |
||
| 14 | if(!(this instanceof Field)){ |
||
| 15 | return new Field(json); |
||
| 16 | } |
||
| 17 | |||
| 18 | // If the given object is already an instance then just return it. DON'T copy it. |
||
| 19 | if(Field.isInstance(json)){ |
||
| 20 | return json; |
||
| 21 | } |
||
| 22 | |||
| 23 | this.init(json); |
||
| 24 | }; |
||
| 25 | |||
| 26 | Field.prototype = Object.create(GedcomX.ExtensibleData.prototype); |
||
| 27 | |||
| 28 | Field._gedxClass = Field.prototype._gedxClass = 'GedcomX.Field'; |
||
| 29 | |||
| 30 | Field.jsonProps = [ |
||
| 31 | 'type', |
||
| 32 | 'values' |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Check whether the given object is an instance of this class. |
||
| 37 | * |
||
| 38 | * @param {Object} obj |
||
| 39 | * @returns {Boolean} |
||
| 40 | */ |
||
| 41 | Field.isInstance = function(obj){ |
||
| 42 | return utils.isInstance(obj, this._gedxClass); |
||
| 43 | }; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Initialize from JSON |
||
| 47 | * |
||
| 48 | * @param {Object} |
||
| 49 | * @return {Field} this |
||
| 50 | */ |
||
| 51 | Field.prototype.init = function(json){ |
||
| 52 | |||
| 53 | GedcomX.ExtensibleData.prototype.init.call(this, json); |
||
| 54 | |||
| 55 | if(json){ |
||
| 56 | this.setType(json.type); |
||
| 57 | this.setValues(json.values); |
||
| 58 | } |
||
| 59 | return this; |
||
| 60 | }; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Set the type |
||
| 64 | * |
||
| 65 | * @param {String} type |
||
| 66 | * @return {Field} this |
||
| 67 | */ |
||
| 68 | Field.prototype.setType = function(type){ |
||
| 69 | this.type = type; |
||
| 70 | return this; |
||
| 71 | }; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the type |
||
| 75 | * |
||
| 76 | * @return {String} type |
||
| 77 | */ |
||
| 78 | Field.prototype.getType = function(){ |
||
| 79 | return this.type; |
||
| 80 | }; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Set the values |
||
| 84 | * |
||
| 85 | * @param {FieldValue[]} values |
||
| 86 | * @return {Field} this |
||
| 87 | */ |
||
| 88 | Field.prototype.setValues = function(values){ |
||
| 89 | return this._setArray(values, 'values', 'addValue'); |
||
| 90 | }; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Add a value |
||
| 94 | * |
||
| 95 | * @param {FieldValue} value |
||
| 96 | * @return {Field} this |
||
| 97 | */ |
||
| 98 | Field.prototype.addValue = function(value){ |
||
| 99 | return this._arrayPush(value, 'values', GedcomX.FieldValue); |
||
| 100 | }; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the values |
||
| 104 | * |
||
| 105 | * @return {FieldValue[]} values |
||
| 106 | */ |
||
| 107 | Field.prototype.getValues = function(){ |
||
| 108 | return this.values || []; |
||
| 109 | }; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Export the object as JSON |
||
| 113 | * |
||
| 114 | * @return {Object} JSON object |
||
| 115 | */ |
||
| 116 | Field.prototype.toJSON = function(){ |
||
| 117 | return this._toJSON(GedcomX.ExtensibleData, Field.jsonProps); |
||
| 118 | }; |
||
| 119 | |||
| 120 | GedcomX.Field = Field; |
||
| 121 | |||
| 122 | }; |