|
1
|
|
|
var utils = require('./utils'), |
|
2
|
|
|
Base = require('./Base'); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Manage the set of identifers for an object. |
|
6
|
|
|
* |
|
7
|
|
|
* @constructor |
|
8
|
|
|
* @param {Object} [json] |
|
|
|
|
|
|
9
|
|
|
*/ |
|
10
|
|
|
var Identifiers = function(json){ |
|
11
|
|
|
|
|
12
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
|
13
|
|
|
if(!(this instanceof Identifiers)){ |
|
14
|
|
|
return new Identifiers(json); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
|
18
|
|
|
if(Identifiers.isInstance(json)){ |
|
19
|
|
|
return json; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
Base.call(this, json); |
|
23
|
|
|
|
|
24
|
|
|
this.identifiers = {}; |
|
25
|
|
|
|
|
26
|
|
|
if(json){ |
|
|
|
|
|
|
27
|
|
|
this.identifiers = json; |
|
28
|
|
|
|
|
29
|
|
|
// The spec allows for types with single values to "forgo the array and use |
|
30
|
|
|
// a single string" but that's a pain to keep track of so we're going to |
|
31
|
|
|
// convert all single strings into arrays |
|
32
|
|
|
for(var a in this.identifiers){ |
|
33
|
|
|
if(this.identifiers.hasOwnProperty(a) && !Array.isArray(this.identifiers[a])){ |
|
34
|
|
|
this.identifiers[a] = [ this.identifiers[a] ]; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
}; |
|
39
|
|
|
|
|
40
|
|
|
Base.prototype = Object.create(Base.prototype); |
|
41
|
|
|
|
|
42
|
|
|
Identifiers._gedxClass = Identifiers.prototype._gedxClass = 'GedcomX.Identifiers'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Check whether the given object is an instance of this class. |
|
46
|
|
|
* |
|
47
|
|
|
* @param {Object} obj |
|
48
|
|
|
* @returns {Boolean} |
|
49
|
|
|
*/ |
|
50
|
|
|
Identifiers.isInstance = function(obj){ |
|
51
|
|
|
return utils.isInstance(obj, this._gedxClass); |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the values for a given type |
|
56
|
|
|
* |
|
57
|
|
|
* @param {String=} type If not specified then values with no associated type |
|
58
|
|
|
* are returned. |
|
59
|
|
|
* @return {String[]} |
|
60
|
|
|
*/ |
|
61
|
|
|
Identifiers.prototype.getValues = function(type){ |
|
62
|
|
|
if(typeof type === 'undefined'){ |
|
63
|
|
|
type = '$'; |
|
64
|
|
|
} |
|
65
|
|
|
return this.identifiers[type] || []; |
|
66
|
|
|
}; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set the values for a given type |
|
70
|
|
|
* |
|
71
|
|
|
* @param {String[]} values |
|
72
|
|
|
* @param {String=} type |
|
73
|
|
|
*/ |
|
74
|
|
|
Identifiers.prototype.setValues = function(values, type){ |
|
75
|
|
|
if(typeof type === 'undefined'){ |
|
76
|
|
|
type = '$'; |
|
77
|
|
|
} |
|
78
|
|
|
if(Array.isArray(values)){ |
|
79
|
|
|
this.identifiers[type] = values; |
|
80
|
|
|
} |
|
81
|
|
|
return this; |
|
82
|
|
|
}; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add a value for a given type |
|
86
|
|
|
* |
|
87
|
|
|
* @param {String} value |
|
88
|
|
|
* @param {String=} type |
|
89
|
|
|
*/ |
|
90
|
|
|
Identifiers.prototype.addValue = function(value, type){ |
|
91
|
|
|
if(typeof type === 'undefined'){ |
|
92
|
|
|
type = '$'; |
|
93
|
|
|
} |
|
94
|
|
|
if(!Array.isArray(this.identifiers[type])){ |
|
95
|
|
|
this.identifiers[type] = []; |
|
96
|
|
|
} |
|
97
|
|
|
this.identifiers[type].push(value); |
|
98
|
|
|
return this; |
|
99
|
|
|
}; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Export the object as JSON |
|
103
|
|
|
* |
|
104
|
|
|
* @return {Object} JSON object |
|
105
|
|
|
*/ |
|
106
|
|
|
Identifiers.prototype.toJSON = function(){ |
|
107
|
|
|
return this.identifiers; |
|
108
|
|
|
}; |
|
109
|
|
|
|
|
110
|
|
|
module.exports = Identifiers; |