Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/services/address/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ AddressHistory.prototype.get = function(callback) {

self.combineTransactionInfo();
totalCount = Number(self.combinedArray.length);
self.sortAndPaginateCombinedArray();

async.eachSeries(
self.combinedArray,
function(txInfo, next) {
self.getDetailedInfo(txInfo, next);
},
async.parallel(
self.combinedArray.map(function (txInfo) {
return function (cb) {
return self.getDetailedInfo(txInfo, cb);
}
}),
function(err) {
if (err) {
return callback(err);
}

self.sortAndPaginateDetailedArray();
callback(null, {
totalCount: totalCount,
items: self.detailedArray
Expand Down Expand Up @@ -163,10 +165,10 @@ AddressHistory.prototype.combineTransactionInfo = function() {
/**
* A helper function to sort and slice/paginate the `combinedArray`
*/
AddressHistory.prototype.sortAndPaginateCombinedArray = function() {
this.combinedArray.sort(AddressHistory.sortByHeight);
AddressHistory.prototype.sortAndPaginateDetailedArray = function() {
this.detailedArray.sort(AddressHistory.sortByHeight);
if (!_.isUndefined(this.options.from) && !_.isUndefined(this.options.to)) {
this.combinedArray = this.combinedArray.slice(this.options.from, this.options.to);
this.detailedArray = this.detailedArray.slice(this.options.from, this.options.to);
}
};

Expand Down
50 changes: 25 additions & 25 deletions test/services/address/history.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Address Service History', function() {
history.combinedArray = [{}];
history.getTransactionInfo = sinon.stub().callsArg(1);
history.combineTransactionInfo = sinon.stub();
history.sortAndPaginateCombinedArray = sinon.stub();
history.sortAndPaginateDetailedArray = sinon.stub();
history.getDetailedInfo = sinon.stub().callsArg(1);
history.sortTransactionsIntoArray = sinon.stub();
history.get(function(err, results) {
Expand All @@ -60,7 +60,7 @@ describe('Address Service History', function() {
history.getTransactionInfo.callCount.should.equal(1);
history.getDetailedInfo.callCount.should.equal(1);
history.combineTransactionInfo.callCount.should.equal(1);
history.sortAndPaginateCombinedArray.callCount.should.equal(1);
history.sortAndPaginateDetailedArray.callCount.should.equal(1);
results.should.deep.equal({
totalCount: 1,
items: expected
Expand Down Expand Up @@ -386,7 +386,7 @@ describe('Address Service History', function() {
});
});

describe('#sortAndPaginateCombinedArray', function() {
describe('#sortAndPaginateDetailedArray', function() {
it('from 0 to 2', function() {
var history = new AddressHistory({
node: {},
Expand All @@ -396,7 +396,7 @@ describe('Address Service History', function() {
},
addresses: []
});
history.combinedArray = [
history.detailedArray = [
{
height: 13
},
Expand All @@ -407,10 +407,10 @@ describe('Address Service History', function() {
height: 12
}
];
history.sortAndPaginateCombinedArray();
history.combinedArray.length.should.equal(2);
history.combinedArray[0].height.should.equal(14);
history.combinedArray[1].height.should.equal(13);
history.sortAndPaginateDetailedArray();
history.detailedArray.length.should.equal(2);
history.detailedArray[0].height.should.equal(14);
history.detailedArray[1].height.should.equal(13);
});
it('from 0 to 4 (exceeds length)', function() {
var history = new AddressHistory({
Expand All @@ -421,7 +421,7 @@ describe('Address Service History', function() {
},
addresses: []
});
history.combinedArray = [
history.detailedArray = [
{
height: 13
},
Expand All @@ -432,11 +432,11 @@ describe('Address Service History', function() {
height: 12
}
];
history.sortAndPaginateCombinedArray();
history.combinedArray.length.should.equal(3);
history.combinedArray[0].height.should.equal(14);
history.combinedArray[1].height.should.equal(13);
history.combinedArray[2].height.should.equal(12);
history.sortAndPaginateDetailedArray();
history.detailedArray.length.should.equal(3);
history.detailedArray[0].height.should.equal(14);
history.detailedArray[1].height.should.equal(13);
history.detailedArray[2].height.should.equal(12);
});
it('from 0 to 1', function() {
var history = new AddressHistory({
Expand All @@ -447,7 +447,7 @@ describe('Address Service History', function() {
},
addresses: []
});
history.combinedArray = [
history.detailedArray = [
{
height: 13
},
Expand All @@ -458,9 +458,9 @@ describe('Address Service History', function() {
height: 12
}
];
history.sortAndPaginateCombinedArray();
history.combinedArray.length.should.equal(1);
history.combinedArray[0].height.should.equal(14);
history.sortAndPaginateDetailedArray();
history.detailedArray.length.should.equal(1);
history.detailedArray[0].height.should.equal(14);
});
it('from 2 to 3', function() {
var history = new AddressHistory({
Expand All @@ -471,7 +471,7 @@ describe('Address Service History', function() {
},
addresses: []
});
history.combinedArray = [
history.detailedArray = [
{
height: 13
},
Expand All @@ -482,9 +482,9 @@ describe('Address Service History', function() {
height: 12
}
];
history.sortAndPaginateCombinedArray();
history.combinedArray.length.should.equal(1);
history.combinedArray[0].height.should.equal(12);
history.sortAndPaginateDetailedArray();
history.detailedArray.length.should.equal(1);
history.detailedArray[0].height.should.equal(12);
});
it('from 10 to 20 (out of range)', function() {
var history = new AddressHistory({
Expand All @@ -495,7 +495,7 @@ describe('Address Service History', function() {
},
addresses: []
});
history.combinedArray = [
history.detailedArray = [
{
height: 13
},
Expand All @@ -506,8 +506,8 @@ describe('Address Service History', function() {
height: 12
}
];
history.sortAndPaginateCombinedArray();
history.combinedArray.length.should.equal(0);
history.sortAndPaginateDetailedArray();
history.detailedArray.length.should.equal(0);
});
});

Expand Down