Issues (148)

src/Base.js (2 issues)

1
var utils = require('./utils');
2
3
/**
4
 * Base prototype that all other classes in this library extend
5
 * 
6
 * @class
7
 * @param {Object} [json]
8
 */
9
var Base = function(json){
10
  
11
  // Protect against forgetting the new keyword when calling the constructor
12
  if(!(this instanceof Base)){
13
    return new Base(json);
14
  }
15
  
16
  // If the given object is already an instance then just return it. DON'T copy it.
17
  if(Base.isInstance(json)){
18
    return json;
19
  }
20
  
21
  this.init(json);
22
};
23
24
Base._gedxClass = Base.prototype._gedxClass = 'GedcomX.Base';
25
26
/**
27
 * Check whether the given object is an instance of this class.
28
 * 
29
 * @param {Object} obj
30
 * @returns {Boolean}
31
 */
32
Base.isInstance = function(obj){
33
  return utils.isInstance(obj, this._gedxClass);
34
};
35
36
/**
37
 * Initialize from JSON
38
 * 
39
 * @param {Object}
0 ignored issues
show
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
40
 * @return {Base} this
41
 */
42
Base.prototype.init = function(json){
43
  // Nothing to do here, yet. It is being called though by classes which extend
44
  // it in case we think of something to do in the future.
45
  return this;
46
};
47
48
/**
49
 * Set the value of a property that is an array. The new array is copied
50
 * by calling the associated addMethod on each value in the array.
51
 * 
52
 * @private
53
 * @param {Array} array New array that will be saved (copied)
54
 * @param {String} prop Property name where the array is found
55
 * @param {String} addMethod Name of the add method for this data
56
 * @return {Function} Generated function to be added to the prototype
57
 */
58
Base.prototype._setArray = function(array, prop, addMethod){
59
  if(Array.isArray(array)){
60
    this[prop] = [];
61
    var self = this;
62
    array.forEach(function(n){
63
      self[addMethod](n);
64
    });
65
  }
66
  return this;
67
};
68
69
/**
70
 * Add an object to an array at the given property. 
71
 * 
72
 * This method centralizes code for ensuring that the property is an array and
73
 * that the given data exists (isn't null or undefined). 
74
 * This method is designed to be used internally.
75
 * 
76
 * @private
77
 * @param {Object} data
78
 * @param {String} arrayName
0 ignored issues
show
The parameter arrayName does not exist. Did you maybe forget to remove this comment?
Loading history...
79
 * @param {Fucntion} constructor
80
 * @return {ExtensibleData} This object
81
 */
82
Base.prototype._arrayPush = function(data, prop, constructor){
83
  if(data){
84
    if(constructor){
85
      data = constructor(data);
86
    }
87
    if(!Array.isArray(this[prop])){
88
      this[prop] = [];
89
    }
90
    this[prop].push(data);
91
  }
92
  return this;
93
};
94
95
/**
96
 * Internal helper method for constructing JSON when toJSON() is called.
97
 * 
98
 * Recursively call toJSON on properties that will be serialized, call toJSON on
99
 * the parent prototype, merge the two resulting objects together, and remove 
100
 * any remaining undefined properties.
101
 * 
102
 * @private
103
 * @param {Function} parent Parent prototype
104
 * @param {String[]} properties List of properties that will be serialized
105
 * @return {Object}
106
 */
107
Base.prototype._toJSON = function(parent, properties){
108
  return utils.removeEmpty(
109
    // toJSON MUST be called on all data before merging otherwise the merge method
110
    // will reaching into the class instances and extract data you might not want
111
    // to be serialized. The data will come out as a plain object and you will lose
112
    // the ability to detect that it hasn't been properly serialized.
113
    utils.merge(
114
      parent.prototype.toJSON.call(this), 
115
      utils.toJSON(utils.pick(this, properties))
116
    )
117
  );
118
};
119
120
/**
121
 * Export the object as JSON
122
 * 
123
 * @return {Object} JSON object
124
 */
125
Base.prototype.toJSON = function(){
126
  return {};
127
};
128
129
module.exports = Base;