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 #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.

(cherry picked from commit 8453bd1)

Conflicts:
	tests/SimpleQuery.js
  • Loading branch information
maier49 authored and Kenneth G. Franqueiro committed Jun 30, 2015
1 parent 8a7712f commit 81fdc58
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
19 changes: 11 additions & 8 deletions SimpleQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,8 @@ define([
return object[properties];
});
};
}
},

/* jshint ignore:start */
,
_createSortQuerier: function (sorted) {
var queryAccessors = this.queryAccessors;
return function (data) {
Expand All @@ -183,10 +181,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 @@ -198,6 +202,5 @@ define([
return data;
};
}
/* jshint ignore:end */
});
});
59 changes: 59 additions & 0 deletions tests/SimpleQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,65 @@ define([
{ id: 3, name: 'three', odd: true },
{ id: 2, name: 'two', odd: false }
]);
},

'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 }
]);
}
});
});

0 comments on commit 81fdc58

Please sign in to comment.