rootsdev /
gedcomx-js
| 1 | var GedcomX = require('../'), |
||
| 2 | utils = require('../utils'); |
||
| 3 | |||
| 4 | /** |
||
| 5 | * An address. |
||
| 6 | * |
||
| 7 | * @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#address|GEDCOM X JSON Spec} |
||
| 8 | * |
||
| 9 | * @class |
||
| 10 | * @extends ExtensibleData |
||
| 11 | * @param {Object} [json] |
||
| 12 | */ |
||
| 13 | var Address = function(json){ |
||
| 14 | |||
| 15 | // Protect against forgetting the new keyword when calling the constructor |
||
| 16 | if(!(this instanceof Address)){ |
||
| 17 | return new Address(json); |
||
| 18 | } |
||
| 19 | |||
| 20 | // If the given object is already an instance then just return it. DON'T copy it. |
||
| 21 | if(Address.isInstance(json)){ |
||
| 22 | return json; |
||
| 23 | } |
||
| 24 | |||
| 25 | this.init(json); |
||
|
0 ignored issues
–
show
Best Practice
introduced
by
Loading history...
|
|||
| 26 | }; |
||
| 27 | |||
| 28 | Address.prototype = Object.create(GedcomX.ExtensibleData.prototype); |
||
| 29 | |||
| 30 | Address._gedxClass = Address.prototype._gedxClass = 'GedcomX.Address'; |
||
| 31 | |||
| 32 | Address.jsonProps = [ |
||
| 33 | 'value', |
||
| 34 | 'city', |
||
| 35 | 'country', |
||
| 36 | 'postalCode', |
||
| 37 | 'stateOrProvince', |
||
| 38 | 'street', |
||
| 39 | 'street2', |
||
| 40 | 'street3', |
||
| 41 | 'street4', |
||
| 42 | 'street5', |
||
| 43 | 'street6' |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Check whether the given object is an instance of this class. |
||
| 48 | * |
||
| 49 | * @param {Object} obj |
||
| 50 | * @returns {Boolean} |
||
| 51 | */ |
||
| 52 | Address.isInstance = function(obj){ |
||
| 53 | return utils.isInstance(obj, this._gedxClass); |
||
| 54 | }; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Initialize from JSON |
||
| 58 | * |
||
| 59 | * @param {Object} |
||
|
0 ignored issues
–
show
|
|||
| 60 | * @return {Address} this |
||
| 61 | */ |
||
| 62 | Address.prototype.init = function(json){ |
||
| 63 | |||
| 64 | GedcomX.ExtensibleData.prototype.init.call(this, json); |
||
| 65 | |||
| 66 | if(json){ |
||
| 67 | this.setValue(json.value); |
||
| 68 | this.setCity(json.city); |
||
| 69 | this.setCountry(json.country); |
||
| 70 | this.setPostalCode(json.postalCode); |
||
| 71 | this.setStateOrProvince(json.stateOrProvince); |
||
| 72 | this.setStreet(json.street); |
||
| 73 | this.setStreet2(json.street2); |
||
| 74 | this.setStreet3(json.street3); |
||
| 75 | this.setStreet4(json.street4); |
||
| 76 | this.setStreet5(json.street5); |
||
| 77 | this.setStreet6(json.street6); |
||
| 78 | } |
||
| 79 | return this; |
||
| 80 | }; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the complete address value |
||
| 84 | * |
||
| 85 | * @returns {String} |
||
| 86 | */ |
||
| 87 | Address.prototype.getValue = function(){ |
||
| 88 | return this.value; |
||
| 89 | }; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Set the complete address value |
||
| 93 | * |
||
| 94 | * @param {String} value |
||
| 95 | * @return {Address} |
||
| 96 | */ |
||
| 97 | Address.prototype.setValue = function(value){ |
||
| 98 | this.value = value; |
||
| 99 | return this; |
||
| 100 | }; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the city |
||
| 104 | * |
||
| 105 | * @returns {String} |
||
| 106 | */ |
||
| 107 | Address.prototype.getCity = function(){ |
||
| 108 | return this.city; |
||
| 109 | }; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set the city |
||
| 113 | * |
||
| 114 | * @param {String} city |
||
| 115 | * @returns {Address} |
||
| 116 | */ |
||
| 117 | Address.prototype.setCity = function(city){ |
||
| 118 | this.city = city; |
||
| 119 | return this; |
||
| 120 | }; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get the country |
||
| 124 | * |
||
| 125 | * @returns {String} |
||
| 126 | */ |
||
| 127 | Address.prototype.getCountry = function(){ |
||
| 128 | return this.country; |
||
| 129 | }; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Set the country |
||
| 133 | * |
||
| 134 | * @param {String} country |
||
| 135 | * @returns {Address} |
||
| 136 | */ |
||
| 137 | Address.prototype.setCountry = function(country){ |
||
| 138 | this.country = country; |
||
| 139 | return this; |
||
| 140 | }; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get the postal code |
||
| 144 | * |
||
| 145 | * @returns {String} |
||
| 146 | */ |
||
| 147 | Address.prototype.getPostalCode = function(){ |
||
| 148 | return this.postalCode; |
||
| 149 | }; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Set the postal code |
||
| 153 | * |
||
| 154 | * @param {String} postalCode |
||
| 155 | * @returns {Address} |
||
| 156 | */ |
||
| 157 | Address.prototype.setPostalCode = function(postalCode){ |
||
| 158 | this.postalCode = postalCode; |
||
| 159 | return this; |
||
| 160 | }; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get the state or province |
||
| 164 | * |
||
| 165 | * @returns {String} |
||
| 166 | */ |
||
| 167 | Address.prototype.getStateOrProvince = function(){ |
||
| 168 | return this.stateOrProvince; |
||
| 169 | }; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set the state or province |
||
| 173 | * |
||
| 174 | * @param {String} stateOrProvince |
||
| 175 | * @returns {Address} |
||
| 176 | */ |
||
| 177 | Address.prototype.setStateOrProvince = function(stateOrProvince){ |
||
| 178 | this.stateOrProvince = stateOrProvince; |
||
| 179 | return this; |
||
| 180 | }; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the street |
||
| 184 | * |
||
| 185 | * @returns {String} |
||
| 186 | */ |
||
| 187 | Address.prototype.getStreet = function(){ |
||
| 188 | return this.street; |
||
| 189 | }; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Set the street |
||
| 193 | * |
||
| 194 | * @param {String} street |
||
| 195 | * @returns {Address} |
||
| 196 | */ |
||
| 197 | Address.prototype.setStreet = function(street){ |
||
| 198 | this.street = street; |
||
| 199 | return this; |
||
| 200 | }; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the street2 |
||
| 204 | * |
||
| 205 | * @returns {String} |
||
| 206 | */ |
||
| 207 | Address.prototype.getStreet2 = function(){ |
||
| 208 | return this.street2; |
||
| 209 | }; |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set the street2 |
||
| 213 | * |
||
| 214 | * @param {String} street2 |
||
| 215 | * @returns {Address} |
||
| 216 | */ |
||
| 217 | Address.prototype.setStreet2 = function(street2){ |
||
| 218 | this.street2 = street2; |
||
| 219 | return this; |
||
| 220 | }; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get the street3 |
||
| 224 | * |
||
| 225 | * @returns {String} |
||
| 226 | */ |
||
| 227 | Address.prototype.getStreet3 = function(){ |
||
| 228 | return this.street3; |
||
| 229 | }; |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set the street3 |
||
| 233 | * |
||
| 234 | * @param {String} street3 |
||
| 235 | * @returns {Address} |
||
| 236 | */ |
||
| 237 | Address.prototype.setStreet3 = function(street3){ |
||
| 238 | this.street3 = street3; |
||
| 239 | return this; |
||
| 240 | }; |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get the street4 |
||
| 244 | * |
||
| 245 | * @returns {String} |
||
| 246 | */ |
||
| 247 | Address.prototype.getStreet4 = function(){ |
||
| 248 | return this.street4; |
||
| 249 | }; |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set the street4 |
||
| 253 | * |
||
| 254 | * @param {String} street4 |
||
| 255 | * @returns {Address} |
||
| 256 | */ |
||
| 257 | Address.prototype.setStreet4 = function(street4){ |
||
| 258 | this.street4 = street4; |
||
| 259 | return this; |
||
| 260 | }; |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the street5 |
||
| 264 | * |
||
| 265 | * @returns {String} |
||
| 266 | */ |
||
| 267 | Address.prototype.getStreet5 = function(){ |
||
| 268 | return this.street5; |
||
| 269 | }; |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Set the street5 |
||
| 273 | * |
||
| 274 | * @param {String} street5 |
||
| 275 | * @returns {Address} |
||
| 276 | */ |
||
| 277 | Address.prototype.setStreet5 = function(street5){ |
||
| 278 | this.street5 = street5; |
||
| 279 | return this; |
||
| 280 | }; |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the street6 |
||
| 284 | * |
||
| 285 | * @returns {String} |
||
| 286 | */ |
||
| 287 | Address.prototype.getStreet6 = function(){ |
||
| 288 | return this.street6; |
||
| 289 | }; |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Set the street6 |
||
| 293 | * |
||
| 294 | * @param {String} street6 |
||
| 295 | * @returns {Address} |
||
| 296 | */ |
||
| 297 | Address.prototype.setStreet6 = function(street6){ |
||
| 298 | this.street6 = street6; |
||
| 299 | return this; |
||
| 300 | }; |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Export the object as JSON |
||
| 304 | * |
||
| 305 | * @return {Object} JSON object |
||
| 306 | */ |
||
| 307 | Address.prototype.toJSON = function(){ |
||
| 308 | return this._toJSON(GedcomX.ExtensibleData, Address.jsonProps); |
||
| 309 | }; |
||
| 310 | |||
| 311 | module.exports = Address; |