Skip to content

Commit c6d5af7

Browse files
committed
fix: add bool to priorityComparator, add tests
1 parent cf2d344 commit c6d5af7

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/utils.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,21 @@ exports.priAndKeyComparator = function priAndKeyComparator(testPri, testKey, val
6262
};
6363

6464
exports.priorityComparator = function priorityComparator(a, b) {
65+
// https://firebase.google.com/docs/database/web/lists-of-data#data-order
6566
if (a !== b) {
6667
if (a === null || b === null) {
6768
return a === null ? -1 : 1;
6869
}
70+
if(typeof a === 'boolean' && typeof b === 'boolean') {
71+
return !a ? -1 : 1;
72+
}
6973
if (typeof a !== typeof b) {
74+
if(typeof a === 'boolean' || typeof b === 'boolean') {
75+
return typeof a === 'boolean' ? -1 : 1;
76+
}
7077
return typeof a === 'number' ? -1 : 1;
71-
} else {
72-
return a > b ? 1 : -1;
7378
}
79+
return a > b ? 1 : -1;
7480
}
7581
return 0;
7682
};

test/unit/utils.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var removeEmptyRtdbProperties = require('../../src/utils').removeEmptyRtdbProper
77
var removeEmptyFirestoreProperties = require('../../src/utils').removeEmptyFirestoreProperties;
88
var updateToRtdbObject = require('../../src/utils').updateToRtdbObject;
99
var updateToFirestoreObject = require('../../src/utils').updateToFirestoreObject;
10+
var priorityComparator = require('../../src/utils').priorityComparator;
1011

1112
describe('utils', function () {
1213
describe('removeEmptyRtdbProperties', function () {
@@ -164,4 +165,43 @@ describe('utils', function () {
164165
});
165166
});
166167
});
168+
169+
describe('priorityComparator', function() {
170+
// https://firebase.google.com/docs/database/web/lists-of-data#data-order
171+
it('should give no priority to equal items', function() {
172+
expect(priorityComparator('a', 'a')).to.eql(0);
173+
});
174+
it('should order null items first', function() {
175+
expect(priorityComparator(null, 0)).to.eql(-1);
176+
expect(priorityComparator(0, null)).to.eql(1);
177+
});
178+
it('should order false before true', function() {
179+
expect(priorityComparator(false, true)).to.eql(-1);
180+
expect(priorityComparator(true, false)).to.eql(1);
181+
});
182+
it('should order booleans before numbers', function() {
183+
expect(priorityComparator(false, 0)).to.eql(-1);
184+
expect(priorityComparator(true, 1)).to.eql(-1);
185+
expect(priorityComparator(0, false)).to.eql(1);
186+
expect(priorityComparator(1, true)).to.eql(1);
187+
});
188+
it('should order numbers before strings', function() {
189+
expect(priorityComparator(5, 'foo')).to.eql(-1);
190+
expect(priorityComparator(3, '3')).to.eql(-1);
191+
expect(priorityComparator('foo', 0)).to.eql(1);
192+
expect(priorityComparator('10', 10)).to.eql(1);
193+
});
194+
it('should order numbers in ascending order', function() {
195+
expect(priorityComparator(4,5)).to.eql(-1);
196+
expect(priorityComparator(5,4)).to.eql(1);
197+
expect(priorityComparator(1,10)).to.eql(-1);
198+
expect(priorityComparator(10,1)).to.eql(1);
199+
});
200+
it('should order strings lexicographically', function() {
201+
expect(priorityComparator('bar', 'foo')).to.eql(-1);
202+
expect(priorityComparator('foo', 'bar')).to.eql(1);
203+
expect(priorityComparator('ant', 'art')).to.eql(-1);
204+
expect(priorityComparator('art', 'ant')).to.eql(1);
205+
});
206+
});
167207
});

0 commit comments

Comments
 (0)