Skip to content

Commit

Permalink
fixes #22 address test failing also converts Address Module to define…
Browse files Browse the repository at this point in the history
…d oop instead of prototypical oop.
  • Loading branch information
RIAEvangelist committed Jun 22, 2015
1 parent cc1d23c commit 6f4b8f3
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 171 deletions.
176 changes: 115 additions & 61 deletions src/Address.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,134 @@
var util = require('util');

var Address = function(parameters) {
if (typeof paramaters == 'object' && parameters.PostalCode) {
this.Street = parameters.Street;
this.City = parameters.City;
this.Region = parameters.Region;
this.PostalCode = parameters.PostalCode;
}
Object.defineProperties(
this,
{
parse:{
value:parse,
enumerable:true,
writable:false
},
getAddressLines:{
value:getAddressLines,
enumerable:true,
writable:false
},
Type:{
value:'House',
enumerable:true,
writable:true
},
Street:{
value:'',
enumerable:true,
writable:true
},
City:{
value:'',
enumerable:true,
writable:true
},
Region:{
value:'',
enumerable:true,
writable:true
},
PostalCode:{
value:'',
enumerable:true,
writable:true
},
_init:{
value:init,
enumerable:false,
writable:false
},
_parameters:{
value:parameters,
enumerable:false,
writable:false
}
}
);

if (util.isArray(parameters)) {
this.Street = parameters[0];
this.City = parameters[1];
this.Region = parameters[2];
this.PostalCode = parameters[3];
}
this._init();

if (typeof paramaters == 'number') {
parameters+='';
}
function init(){
if (typeof this._parameters == 'object' && this._parameters.PostalCode) {
this.Street = this._parameters.Street;
this.City = this._parameters.City;
this.Region = this._parameters.Region;
this.PostalCode = this._parameters.PostalCode;
}


if (typeof parameters == 'string') {
this.parse(parameters);
}

if (!parameters.Type) {
this.Type = 'House';
}
};
if (util.isArray(this._parameters)) {
this.Street = this._parameters[0];
this.City = this._parameters[1];
this.Region = this._parameters[2];
this.PostalCode = this._parameters[3];
}

if (typeof this._parameters == 'number') {
this._parameters+='';
}

Address.prototype.parse = function(locationString) {
var splitAddress = locationString.split(',');
if (typeof this._parameters == 'string') {
this.parse(this._parameters);
}

for (var i in splitAddress) {
splitAddress[i] = splitAddress[i].trim();
if (this._parameters.Type) {
this.Type = this._parameters.Type;
}
}

//need better intellegence for auto determining address parts
switch (splitAddress.length) {
case 1:
this.PostalCode = Number(splitAddress[0]);
function parse(locationString) {
var splitAddress = locationString.split(',');

if (!this.PostalCode) {
for (var i in splitAddress) {
splitAddress[i] = splitAddress[i].trim();
}

//need better intellegence for auto determining address parts
switch (splitAddress.length) {
case 1:
this.PostalCode = Number(splitAddress[0])+'';

if (!this.PostalCode) {
this.City = splitAddress[0];
}
break;
case 2:
this.Region = splitAddress[0];
this.PostalCode = splitAddress[1];
break;
case 3:
this.City = splitAddress[0];
}
break;
case 2:
this.Region = splitAddress[0];
this.PostalCode = splitAddress[1];
break;
case 3:
this.City = splitAddress[0];
this.Region = splitAddress[1]
this.PostalCode = splitAddress[2];
break;
case 4:
this.Street = splitAddress[0];
this.City = splitAddress[1];
this.Region = splitAddress[2];
this.PostalCode = splitAddress[3];
}
};
this.Region = splitAddress[1]
this.PostalCode = splitAddress[2];
break;
case 4:
this.Street = splitAddress[0];
this.City = splitAddress[1];
this.Region = splitAddress[2];
this.PostalCode = splitAddress[3];
}
};

Address.prototype.getAddressLines = function() {
var line1 = (this.Street ? this.Street : '');
var line2 = new String(
(this.City ? this.City + ',' : '') +
(this.Region ? this.Region + ',' : '') +
this.PostalCode
);
function getAddressLines() {
var line1 = (this.Street ? this.Street : '');
var line2 = (this.City ? this.City + ',' : '') +
(this.Region ? this.Region + ',' : '') +
this.PostalCode

var lines=[line1, line2];
lines.line1=line1;
lines.line2=line2;
var lines=[line1, line2];
lines.line1=line1;
lines.line2=line2;

return lines;
}
return lines;
}
};

module.exports = Address;
2 changes: 0 additions & 2 deletions src/Utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ var findNearbyStores = function(address, pickUpType, callback) {
var address = new Address(address)
var addressLines=address.getAddressLines();

console.log(address,addressLines);

var url = urls.store.find.replace(
'${line1}',
encodeURI(
Expand Down
112 changes: 62 additions & 50 deletions test/address_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,102 +9,114 @@ describe(
function() {
it(
'should create Address object from JSON',
function(done) {
function() {
var jsonObj = {
Street: "123 Easy Street",
City: "St. Louis",
Region: "MO",
PostalCode: "63105",
Type: "House"
Street: '123 Easy Street',
City: 'St. Louis',
Region: 'MO',
PostalCode: '63105',
Type: 'House'
};

var address = new Address(jsonObj);

for(var i in address){
console.log(i,address[i])
}

expect(address).not.to.be.null;
console.log(address, typeof address);
expect(address.Street).to.equal(jsonObj.Street);
expect(address.City).to.equal(jsonObj.City);
expect(address.Region).to.equal(jsonObj.Region);

done();
}
);

it(
'should create Address object from string',
function(done) {
var address = new Address("123 Easy Street, St. Louis, MO, 63105");

expect(address).not.to.be.null;
expect(address.Street).to.equal("123 Easy Street");
expect(address.City).to.equal("St. Louis");
expect(address.Region).to.equal("MO");
expect(address.PostalCode).to.equal("63105");

done();
function() {
var address = new Address('123 Easy Street, St. Louis, MO, 63105');

expect(address).to.be.defined;
expect(address.Street).to.equal('123 Easy Street');
expect(address.City).to.equal('St. Louis');
expect(address.Region).to.equal('MO');
expect(address.PostalCode).to.equal('63105');
}
);

it(
'should return full address to pass to findNearbyStores',
function(done) {
var address = new Address("123 Easy Street, St.Louis, MO, 63105");
'should return address lines',
function() {
var address = new Address('123 Easy Street, St.Louis, MO, 63105');
var addressLines = address.getAddressLines();

expect(addressLines).to.have.length(2);
expect(addressLines[0]).to.equal("123 Easy Street");
expect(addressLines[1].trim()).to.equal(address.City + "," + address.Region + "," + address.PostalCode);

done();
expect(addressLines.length).to.equal(2);

expect(
addressLines[0]
).to.equal(
'123 Easy Street'
);

expect(
addressLines[1]
).to.equal(
address.City + ',' +
address.Region + ',' +
address.PostalCode
);

expect(
addressLines.line1
).to.equal(
'123 Easy Street'
);

expect(
addressLines.line2
).to.equal(
address.City + ',' +
address.Region + ',' +
address.PostalCode
);

}
);

it(
'should return zip code to pass to findNearbyStores',
function(done) {
var address = new Address("63105");
'should return only zip code in line2',
function() {
var address = new Address('63105');
var addressLines = address.getAddressLines();

expect(addressLines).to.have.length(2);
expect(addressLines.length).to.equal(2);
expect(addressLines[0]).to.equal('');
expect(addressLines[1]).to.equal(address.PostalCode);

done();
expect(addressLines.line1).to.equal('');
expect(addressLines.line2).to.equal(address.PostalCode);

}
);

it(
'should return city, state, zip to pass to findNearbyStores',
function(done) {
var address = new Address("St. Louis, MO, 63105");
'should return city, state, zip in address lines',
function() {
var address = new Address('St. Louis, MO, 63105');
var addressLines = address.getAddressLines();

expect(addressLines).to.have.length(2);
expect(addressLines[0]).to.equal('');
expect(addressLines[1]).to.equal(address.City + "," + address.Region + "," + address.PostalCode);

done();

expect(addressLines[1]).to.equal(address.City + ',' + address.Region + ',' + address.PostalCode);
}
);

it(
'should create Address object from array',
function(done) {
var address = new Address(["123 Easy Street", "St. Louis", "MO", "63105"]);
function() {
var address = new Address(['123 Easy Street', 'St. Louis', 'MO', '63105']);
expect(address).not.to.be.null;
expect(address.Street).to.equal("123 Easy Street");
expect(address.City).to.equal("St. Louis");
expect(address.Region).to.equal("MO");
expect(address.PostalCode).to.equal("63105");

done();
expect(address.Street).to.equal('123 Easy Street');
expect(address.City).to.equal('St. Louis');
expect(address.Region).to.equal('MO');
expect(address.PostalCode).to.equal('63105');
}
);
}
Expand Down
Loading

0 comments on commit 6f4b8f3

Please sign in to comment.