Skip to content

Instantly share code, notes, and snippets.

@Rolilink
Created October 1, 2015 20:21
Show Gist options
  • Select an option

  • Save Rolilink/cc392033739da9ffb8ff to your computer and use it in GitHub Desktop.

Select an option

Save Rolilink/cc392033739da9ffb8ff to your computer and use it in GitHub Desktop.
voyage class
'use strict';
var expect = require('chai').expect;
var Voyage = require('../../lib/resources/voyage.js');
var _ = require("underscore");
describe('Voyage Class', function(){
it('should have a method called getVoyages', function(){
expect(Voyage.getVoyages).to.be.a('function');
});
it('#getVoyages hould return an array of voyages from a an array of Data Segments', function(){
var data = [
["ST"],
["V1"],
["K1"],
["R4"],
["V9"],
["SE"],
["ST"],
["V1"],
["R4"],
["V9"],
["SE"]
];
var voyages = Voyage.getVoyages(data);
expect(voyages.length).to.be.equals(2);
expect(voyages[1].objectType).to.be.equals("voyage");
});
it("#getVoyages should group ports and events as pairs an save them in a array called loops", function(){
var data = [
["ST"],
["V1"],
["K1"],
["R4"],
["V9"],
["R4"],
["V9"],
["SE"]
];
var voyages = Voyage.getVoyages(data);
expect(voyages[0].loops.length).to.be.equals(2);
expect(voyages[0].loops[0]).to.have.a.property("port");
expect(voyages[0].loops[0]).to.have.a.property("event");
});
it("#getVoyages should populate vessel into voyages", function(){
var data = [
["ST"],
["V1"],
["K1"],
["R4"],
["V9"],
["R4"],
["V9"],
["SE"]
];
var voyages = Voyage.getVoyages(data);
expect(voyages[0].vessel).to.exist;
});
describe("Voyage", function(){
var voyage = null;
beforeEach(function(){
var voyageData = {
"loops":[
{
"port":{
/* removed data for privacy purposes */
},
"event":{
/* removed data for privacy purposes */
}
},
{
"port":{
/* removed data for privacy purposes */
},
"event":{
/* removed data for privacy purposes */
}
},
{
"port":{
/* removed data for privacy purposes */
},
"event":{
/* removed data for privacy purposes */
}
},
{
"port":{
/* removed data for privacy purposes */
},
"event":{
/* removed data for privacy purposes */
}
}
],
"vessel":{
/* removed data for privacy purposes */
}
}
voyage = new Voyage(voyageData);
});
describe("#removeRepeatedLoops", function(){
it("should have a removeRepeatedLoops function", function(){
expect(voyage.removeRepeatedLoops).to.be.a('function');
});
it("should remove repeated port loops", function(){
voyage.removeRepeatedLoops();
var array = _.filter(voyage.loops,function(loop){
return loop.port.locationIdentifier === "/* removed data for privacy purposes */";
});
expect(array.length).to.be.equals(1);
});
it("should return this", function(){
expect(voyage.removeRepeatedLoops()).to.be.equal(voyage);
});
});
describe("#calculatePermutations",function(){
it("should have a function to calculate permutations between all the ports", function(){
expect(voyage.calculatePermutations).to.be.a("function");
});
it("should calculate permutation between each loop",function(){
var permutations = voyage.calculatePermutations();
expect(permutations.length).to.be.equal(6);
});
});
describe("#calculatePermutation",function(){
it("should have a function to calculate a single permutation between two ports", function(){
expect(voyage.calculatePermutation).to.be.a("function");
});
it("should return a permutation between two loops", function(){
var examplePermutation = {
/* removed data for privacy purposes */
}
var permutation = voyage.calculatePermutation(0,1);
expect(permutation).to.be.deep.equal(examplePermutation);
});
})
});
});
'use strict';
var _ = require("underscore"),
Port = require("./port.js"),
Event = require("./event.js"),
SailingTable = require("./sailingtable.js"),
Vessel = require("./vessel.js");
/* Class */
var Voyage = function Voyage(opts){
_.extend(this,opts);
this.objectType = "voyage";
}
Voyage.getVoyages = function(ediArr){
var voyages = [],
voyage = null,
lastSegment = [],
loop = {};
ediArr.forEach(function(dataSegment){
switch (dataSegment[0]) {
case "ST":
voyage = {
loops: [],
vessel: null
};
break;
case "SE":
voyages.push(new Voyage(voyage));
break;
case "V1":
if(voyage){
voyage.vessel = Vessel.getVessel(dataSegment);
}
break;
case "R4":
loop = {};
loop.port = Port.getPort(dataSegment);
break;
case "V9":
if(lastSegment[0] === "R4"){
loop.event = Event.getEvent(dataSegment);
voyage.loops.push(loop);
}
break;
default:
}
lastSegment = dataSegment;
});
return voyages;
}
Voyage.prototype.calculatePermutations = function () {
var permutations = [],
self = this,
range = _.range(0,self.loops.length),
rest = range;
_.each(range,function(indexFrom){
rest = _.rest(rest,1);
_.each(rest,function(indexTo){
if(indexFrom < range.length - 1){
permutations.push(self.calculatePermutation(indexFrom,indexTo));
}
});
});
return permutations;
};
Voyage.prototype.calculatePermutation = function (indexFrom,indexTo) {
var loopFrom = this.loops[indexFrom],
loopTo = this.loops[indexTo],
vessel = this.vessel;
return {
carrierCode: "/* removed data for privacy purposes */",
vesselName: vessel.vesselName,
vesselImo : vessel.vesselCode,
fromPort: loopFrom.port.locationIdentifier,
toPort: loopTo.port.locationIdentifier,
portEtd: SailingTable.transformDateToSql(loopFrom.event.eventDate),
portEta: SailingTable.transformDateToSql(loopTo.event.eventDate),
voyageId: vessel.voyageNumber.trim()
}
};
Voyage.prototype.removeRepeatedLoops = function () {
var memory = [];
this.loops = _.filter(this.loops,function(loop){
var isRepeated = false;
if( _.contains(memory,loop.port.locationIdentifier) ){
isRepeated = true;
}else{
memory.push(loop.port.locationIdentifier);
}
return !isRepeated;
});
return this;
};
module.exports = Voyage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment