Skip to content

Commit

Permalink
Make default comparison assert that undefined > null > any other value
Browse files Browse the repository at this point in the history
Fixes dojo#131 by making the default comparison function created in
SimpleQuery#_createSortQuerier treat undefined as greater than any
value, and null greater than any value besides undefined.
This conveniently places all null and undefined values at
the end of a sorted list.
  • Loading branch information
maier49 committed Jul 10, 2015
1 parent de8132c commit 2e3d653
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/SimpleQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,8 @@ define([
return object[properties];
});
};
}
},

/* jshint ignore:start */
,
_createSortQuerier: function (sorted) {
var queryAccessors = this.queryAccessors;
return function (data) {
Expand All @@ -184,10 +182,16 @@ define([

aValue != null && (aValue = aValue.valueOf());
bValue != null && (bValue = bValue.valueOf());

comparison = aValue === bValue
? 0
: (!!descending === (aValue === null || aValue > bValue && bValue !== null) ? -1 : 1);
if (aValue === bValue) {
comparison = 0;
}
else {
// Prioritize undefined > null > defined
var isALessThanB = typeof bValue === 'undefined' ||
bValue === null && typeof aValue !== 'undefined' ||
aValue != null && aValue < bValue;
comparison = Boolean(descending) === isALessThanB ? 1 : -1;
}
}

if (comparison !== 0) {
Expand All @@ -199,6 +203,5 @@ define([
return data;
};
}
/* jshint ignore:end */
});
});
59 changes: 59 additions & 0 deletions tests/unit/SimpleQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,65 @@ define([
]);
},

'sort null and undefined with strings': function () {
var sort = simpleQuery._createSortQuerier([ { property: 'name' } ]);
var sortDescending = simpleQuery._createSortQuerier([ { descending: true, property: 'name' } ]);
var data = testData.slice();
data.splice(2 , 0, { id: 6, odd: false });
data.splice(2, 0, { id: 7, name: null, odd: true });

assert.deepEqual(sort(data), [
{ id: 5, name: 'five', odd: true },
{ id: 4, name: 'four', odd: false },
{ id: 1, name: 'one', odd: true },
{ id: 3, name: 'three', odd: true },
{ id: 2, name: 'two', odd: false },
{ id: 7, name: null, odd: true },
{ id: 6, odd: false }
]);

assert.deepEqual(sortDescending(data), [
{ id: 6, odd: false },
{ id: 7, name: null, odd: true },
{ id: 2, name: 'two', odd: false },
{ id: 3, name: 'three', odd: true },
{ id: 1, name: 'one', odd: true },
{ id: 4, name: 'four', odd: false },
{ id: 5, name: 'five', odd: true }
]);
},

'sort null and undefined with numbers': function () {
var sort = simpleQuery._createSortQuerier([ { property: 'id' } ]);
var sortDescending = simpleQuery._createSortQuerier([ { descending: true, property: 'id' } ]);
var data = testData.slice();
data.splice(2 , 0, {});
data.splice(2, 0, { id: null});
data.splice(2, 0, { id: -1});

assert.deepEqual(sort(data), [
{ id: -1 },
{ id: 1, name: 'one', odd: true },
{ id: 2, name: 'two', odd: false },
{ id: 3, name: 'three', odd: true },
{ id: 4, name: 'four', odd: false },
{ id: 5, name: 'five', odd: true },
{ id: null },
{}
]);

assert.deepEqual(sortDescending(data), [
{},
{ id: null },
{ id: 5, name: 'five', odd: true },
{ id: 4, name: 'four', odd: false },
{ id: 3, name: 'three', odd: true },
{ id: 2, name: 'two', odd: false },
{ id: 1, name: 'one', odd: true },
{ id: -1 }
]);
},

'nested queries': function () {
var f = new Filter.default();
var isEven = f.eq('odd', false);
Expand Down

0 comments on commit 2e3d653

Please sign in to comment.