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