-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Extensions to NameForm | ||
*/ | ||
module.exports = function(GedcomX){ | ||
|
||
// Extend serialization properties | ||
GedcomX.NameForm.jsonProps.push('nameFormInfo'); | ||
|
||
// Override init() | ||
var oldInit = GedcomX.NameForm.prototype.init; | ||
GedcomX.NameForm.prototype.init = function(json){ | ||
oldInit.call(this, json); | ||
if(json){ | ||
this.setNameFormInfo(json.nameFormInfo); | ||
} | ||
}; | ||
|
||
/** | ||
* Set the NameFormInfo | ||
* | ||
* @param {NameFormInfo[]} nameFormInfo | ||
* @return {NameForm} this | ||
*/ | ||
GedcomX.NameForm.prototype.setNameFormInfo = function(nameFormInfo){ | ||
return this._setArray(nameFormInfo, 'nameFormInfo', 'addNameFormInfo'); | ||
}; | ||
|
||
/** | ||
* Add a NameFormInfo | ||
* | ||
* @param {NameFormInfo} nameFormInfo | ||
* @return {NameForm} this | ||
*/ | ||
GedcomX.NameForm.prototype.addNameFormInfo = function(nameFormInfo){ | ||
return this._arrayPush(nameFormInfo, 'nameFormInfo', GedcomX.NameFormInfo); | ||
}; | ||
|
||
/** | ||
* Get the NameFormInfo | ||
* | ||
* @return {NameFormInfo[]} nameFormInfo | ||
*/ | ||
GedcomX.NameForm.prototype.getNameFormInfo = function(){ | ||
return this.nameFormInfo || []; | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
module.exports = function(GedcomX){ | ||
|
||
/** | ||
* Extra information about a name form. | ||
* | ||
* @class NameFormInfo | ||
* @extends Base | ||
* @param {Object} [json] | ||
*/ | ||
var NameFormInfo = function(json){ | ||
|
||
// Protect against forgetting the new keyword when calling the constructor | ||
if(!(this instanceof NameFormInfo)){ | ||
return new NameFormInfo(json); | ||
} | ||
|
||
// If the given object is already an instance then just return it. DON'T copy it. | ||
if(NameFormInfo.isInstance(json)){ | ||
return json; | ||
} | ||
|
||
this.init(json); | ||
}; | ||
|
||
NameFormInfo.prototype = Object.create(GedcomX.Base.prototype); | ||
|
||
NameFormInfo._gedxClass = NameFormInfo.prototype._gedxClass = 'GedcomX.NameFormInfo'; | ||
|
||
NameFormInfo.jsonProps = [ | ||
'order' | ||
]; | ||
|
||
/** | ||
* Check whether the given object is an instance of this class. | ||
* | ||
* @memberof NameFormInfo | ||
* @static | ||
* @param {Object} obj | ||
* @returns {Boolean} | ||
*/ | ||
NameFormInfo.isInstance = function(obj){ | ||
return GedcomX.utils.isInstance(obj, this._gedxClass); | ||
}; | ||
|
||
/** | ||
* Initialize from JSON | ||
* | ||
* @memberof NameFormInfo | ||
* @param {Object} | ||
* @return NameFormInfo this | ||
*/ | ||
NameFormInfo.prototype.init = function(json){ | ||
|
||
GedcomX.Base.prototype.init.call(this, json); | ||
|
||
if(json){ | ||
this.setOrder(json.order); | ||
} | ||
return this; | ||
}; | ||
|
||
/** | ||
* Get the order | ||
* | ||
* @memberof NameFormInfo | ||
* @returns {String} order | ||
*/ | ||
NameFormInfo.prototype.getOrder = function(){ | ||
return this.order; | ||
}; | ||
|
||
/** | ||
* Set the order | ||
* | ||
* @memberof NameFormInfo | ||
* @param {String} order | ||
* @returns {NameFormInfo} this | ||
*/ | ||
NameFormInfo.prototype.setOrder = function(order){ | ||
this.order = order; | ||
return this; | ||
}; | ||
|
||
/** | ||
* Export the object as JSON | ||
* | ||
* @memberof NameFormInfo | ||
* @return {Object} JSON object | ||
*/ | ||
NameFormInfo.prototype.toJSON = function(){ | ||
return this._toJSON(GedcomX.Base, NameFormInfo.jsonProps); | ||
}; | ||
|
||
GedcomX.NameFormInfo = NameFormInfo; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var assert = require('chai').assert, | ||
GedcomX = require('gedcomx-js'); | ||
|
||
describe('NameForm property extensions', function(){ | ||
|
||
describe('nameFormInfo', function(){ | ||
|
||
var json = { | ||
"nameFormInfo" : [ { | ||
"order" : "http://familysearch.org/v1/Eurotypic" | ||
} ] | ||
}; | ||
|
||
it('Create with JSON', function(){ | ||
test(GedcomX.NameForm(json)); | ||
}); | ||
|
||
it('Build', function(){ | ||
test(GedcomX.NameForm() | ||
.addNameFormInfo( | ||
GedcomX.NameFormInfo() | ||
.setOrder(json.nameFormInfo[0].order) | ||
)); | ||
}); | ||
|
||
it('toJSON', function(){ | ||
assert.deepEqual(GedcomX.NameForm(json).toJSON(), json); | ||
}); | ||
|
||
function test(nameForm){ | ||
assert.equal(nameForm.getNameFormInfo().length, 1); | ||
var nameFormInfo = nameForm.getNameFormInfo()[0]; | ||
assert.equal(nameFormInfo.getOrder(), json.nameFormInfo[0].order); | ||
} | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var assert = require('chai').assert, | ||
GedcomX = require('gedcomx-js'); | ||
|
||
var json = { | ||
"order" : "http://familysearch.org/v1/Eurotypic" | ||
}; | ||
|
||
describe('NameFormInfo', function(){ | ||
|
||
it('Create plain', function(){ | ||
assert.instanceOf(new GedcomX.NameFormInfo(), GedcomX.NameFormInfo, 'An instance of NameFormInfo is not returned when calling the constructor with new.'); | ||
assert.instanceOf(GedcomX.NameFormInfo(), GedcomX.NameFormInfo, 'An instance of NameFormInfo is not returned when calling the constructor without new.'); | ||
}); | ||
|
||
it('Create with JSON', function(){ | ||
test(GedcomX.NameFormInfo(json)); | ||
}); | ||
|
||
it('Build', function(){ | ||
test(GedcomX.NameFormInfo() | ||
.setOrder(json.order) | ||
); | ||
}); | ||
|
||
it('toJSON', function(){ | ||
assert.deepEqual(GedcomX.NameFormInfo(json).toJSON(), json); | ||
}); | ||
|
||
it('constructor does not copy instances', function(){ | ||
var obj1 = GedcomX.NameFormInfo(); | ||
var obj2 = GedcomX.NameFormInfo(obj1); | ||
assert.strictEqual(obj1, obj2); | ||
}); | ||
|
||
}); | ||
|
||
function test(nameFormInfo){ | ||
assert.equal(nameFormInfo.getOrder(), json.order); | ||
} |