Skip to content

Commit

Permalink
Handling empty collections and collections smaller than pageSize
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Esplin committed Dec 20, 2016
1 parent bdc7c76 commit 01be73d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 15 deletions.
25 changes: 16 additions & 9 deletions firebase-paginator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var isWindow = typeof window === 'object';
var FirebasePaginator = function (ref, defaults) {

function FirebasePaginator(ref, defaults) {
var paginator = this;
var defaults = defaults || {};
var pageSize = defaults.pageSize ? parseInt(defaults.pageSize, 10) : 10;
Expand Down Expand Up @@ -112,10 +113,12 @@ var FirebasePaginator = function (ref, defaults) {
} else {
delete collection[keys[0]];
}
} else if (isLastPage && keys.length < pageSize + 1) {
console.log('tiny page', keys.length, pageSize);
} else if (isForward) {
return setPage(); // force a reset if forward pagination overruns the last result
} else {
return setPage(undefined, true, true);
} else {
return setPage(undefined, true, true); // Handle overruns
}

this.snap = snap;
Expand Down Expand Up @@ -189,17 +192,21 @@ var FirebasePaginator = function (ref, defaults) {
var axios = require('axios');
return axios.get(queryPath)
.then(function (res) {
return Object.keys(res.data);
return Object.keys(res.data || {});
});
}
};

this.goToPage = function (pageNumber) {
pageNumber = Math.min(this.pageCount, Math.max(1, parseInt(pageNumber)));
paginator.page = this.pages[pageNumber];
paginator.pageNumber = pageNumber;
paginator.isLastPage = pageNumber === Object.keys(paginator.pages).length;
paginator.ref = ref.orderByKey().limitToLast(pageSize).endAt(paginator.page.endKey);
if (Object.keys(this.pages).length) { // Null check for empty collections
paginator.page = this.pages[pageNumber];
paginator.pageNumber = pageNumber;
paginator.isLastPage = pageNumber === Object.keys(paginator.pages).length;
paginator.ref = ref.orderByKey().limitToLast(pageSize).endAt(paginator.page.endKey);
} else {
paginator.ref = ref.orderByKey().limitToLast(pageSize);
}

return this.ref.once('value')
.then(function (snap) {
Expand All @@ -212,7 +219,7 @@ var FirebasePaginator = function (ref, defaults) {

paginator.snap = snap;
paginator.keys = keys;
paginator.collection = collection;
paginator.collection = collection || {};

fire('value', snap);
if (paginator.isLastPage) {
Expand Down
66 changes: 60 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
var test = require('tape');
var firebase = require('firebase');
var path = 'firebasePaginator/collection';
var smallCollectionPath = 'firebasePaginator/small-collection';
var emptyCollectionPath = 'firebasePaginator/empty-collection';
var firebaseConfig = require('./env.json').firebaseConfig;

firebase.initializeApp(firebaseConfig);

var FirebasePaginator = require('./firebase-paginator');
var ref = firebase.database().ref(path);
var populateCollection = function () {
var smallCollectionRef = firebase.database().ref(smallCollectionPath);
var emptyCollectionRef = firebase.database().ref(emptyCollectionPath);

function populateCollection(count, ref) {
return new Promise(function (resolve, reject) {
var promises = [];
var i = 100;
var i = count;

while (i--) {
promises.push(ref.push(100 - i));
promises.push(ref.push(count - i));
}

Promise.all(promises).then(resolve, reject);
});
};

var testPage = function (paginator, length, start, end, testName) {
function testPage(paginator, length, start, end, testName) {
return new Promise(function (resolve, reject) {
test(testName || `should return records ${start} to ${end}`, function (t) {
paginator.once('value', function (snap) {
Expand All @@ -37,14 +42,63 @@ var testPage = function (paginator, length, start, end, testName) {
});
};

ref.once('value')
return ref.once('value')
.then(function (snap) {
if (snap.numChildren() === 100) {
return true;
} else {
return populateCollection();
return populateCollection(100, ref);
}
})
.then(function() {
return smallCollectionRef.once('value');
})
.then(function(snap) {
if (snap.numChildren() == 3) {
return true;
} else {
return populateCollection(3, smallCollectionRef);
}
})
.then(function() {
return emptyCollectionRef.remove();
})
.then(function() { // Test empty collection inifite pagination
return new FirebasePaginator(emptyCollectionRef);
})
.then(function(paginator) {
return testPage(paginator, 0, undefined, undefined);
})
.then(function() { // Test empty collection finite pagination
return new FirebasePaginator(emptyCollectionRef, {
finite: true,
auth: firebaseConfig.secret
});
})
.then(function(paginator) {
return testPage(paginator, 0, undefined, undefined);
})
.then(function() { // Test small collection infinite pagination
return new FirebasePaginator(smallCollectionRef);
})
.then(function(paginator) {
return testPage(paginator, 3, 1, 3);
})
.then(function() {
return new FirebasePaginator(smallCollectionRef, {pageSize: 3});
})
.then(function(paginator) {
return testPage(paginator, 3, 1, 3);
})
.then(function() { // Test small collection finite pagination
return new FirebasePaginator(smallCollectionRef, {
finite: true,
auth: firebaseConfig.secret
});
})
.then(function(paginator) {
return testPage(paginator, 3, 1, 3);
})
.then(function () {
var paginator = new FirebasePaginator(ref);

Expand Down

0 comments on commit 01be73d

Please sign in to comment.