Skip to content

Commit

Permalink
NameFormInfo; close #3
Browse files Browse the repository at this point in the history
  • Loading branch information
justincy committed Nov 3, 2016
1 parent 91c4c7c commit 66e75f2
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/NameForm.js
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 || [];
};

};
96 changes: 96 additions & 0 deletions src/NameFormInfo.js
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;

};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ module.exports = function(GedcomX){
require('./Merge')(GedcomX);
require('./MergeAnalysis')(GedcomX);
require('./MergeConflict')(GedcomX);
require('./NameFormInfo')(GedcomX);
require('./SearchInfo')(GedcomX);
require('./SourceReference')(GedcomX);
require('./Tag')(GedcomX);
require('./User')(GedcomX);

// Property extensions
require('./AtomEntry')(GedcomX);
require('./NameForm')(GedcomX);
require('./Person')(GedcomX);
require('./Root')(GedcomX);
require('./SourceDescription')(GedcomX);
Expand Down
38 changes: 38 additions & 0 deletions test/NameForm.js
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);
}

});

});
39 changes: 39 additions & 0 deletions test/NameFormInfo.js
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);
}

0 comments on commit 66e75f2

Please sign in to comment.