Skip to content

Commit

Permalink
Merge pull request #7 from jonschlinkert/null-values
Browse files Browse the repository at this point in the history
Support null and undefined values
  • Loading branch information
doowb authored Sep 11, 2017
2 parents c8024b8 + 5e10279 commit 0268fdc
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 11 deletions.
10 changes: 1 addition & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

'use strict';

var defaultCompare = require('default-compare');
var typeOf = require('kind-of');
var get = require('get-value');

Expand Down Expand Up @@ -89,15 +90,6 @@ function compare(prop, a, b) {
return defaultCompare(a, b);
}

/**
* Default compare function used as a fallback
* for sorting.
*/

function defaultCompare(a, b) {
return a < b ? -1 : (a > b ? 1 : 0);
}

/**
* Flatten the given array.
*/
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"test": "mocha"
},
"dependencies": {
"get-value": "^2.0.5",
"kind-of": "^2.0.0"
"default-compare": "^1.0.0",
"get-value": "^2.0.6",
"kind-of": "^5.0.2"
},
"devDependencies": {
"ansi-bold": "^0.1.1",
Expand Down
106 changes: 106 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ describe('arraySort', function() {
]);
});

it('should sort by a property with null values:', function() {
var arr = [{key: null}, {key: 'z'}, {key: 'x'}];
arraySort(arr, 'key').should.eql([
{key: 'x'},
{key: 'z'},
{key: null}
]);
});

it('should sort by a property with undefined values:', function() {
var arr = [{}, {key: 'z'}, {key: 'x'}];
arraySort(arr, 'key').should.eql([
{key: 'x'},
{key: 'z'},
{}
]);
});

it('should sort by a property with null and undefined values:', function() {
var arr = [{key: null}, {key: 'z'}, {}, {key: 'x'}];
arraySort(arr, 'key').should.eql([
{key: 'x'},
{key: 'z'},
{key: null},
{}
]);
});

it('should sort by a nested property:', function() {
var res = arraySort(posts, 'locals.date');
res.should.eql([
Expand Down Expand Up @@ -116,6 +144,84 @@ describe('arraySort', function() {
]);
});

it('should sort by multiple properties with null values:', function() {
var posts = [
{ foo: 'bbb', locals: { date: '2013-05-06' } },
{ foo: 'aaa', locals: { date: '2012-01-02' } },
{ foo: null, locals: { date: '2015-04-12' } },
{ foo: 'ccc', locals: { date: '2014-01-02' } },
{ foo: null, locals: { date: '2015-01-02' } },
{ foo: 'ddd', locals: { date: '2014-01-09' } },
{ foo: 'bbb', locals: { date: null } },
{ foo: 'aaa', locals: { date: '2014-02-02' } },
];

var actual = arraySort(posts, ['foo', 'locals.date']);

actual.should.eql([
{ foo: 'aaa', locals: { date: '2012-01-02' } },
{ foo: 'aaa', locals: { date: '2014-02-02' } },
{ foo: 'bbb', locals: { date: '2013-05-06' } },
{ foo: 'bbb', locals: { date: null } },
{ foo: 'ccc', locals: { date: '2014-01-02' } },
{ foo: 'ddd', locals: { date: '2014-01-09' } },
{ foo: null, locals: { date: '2015-01-02' } },
{ foo: null, locals: { date: '2015-04-12' } }
]);
});

it('should sort by multiple properties with undefined values:', function() {
var posts = [
{ foo: 'bbb', locals: { date: '2013-05-06' } },
{ foo: 'aaa', locals: { date: '2012-01-02' } },
{ locals: { date: '2015-04-12' } },
{ foo: 'ccc', locals: { date: '2014-01-02' } },
{ locals: { date: '2015-01-02' } },
{ foo: 'ddd', locals: { date: '2014-01-09' } },
{ foo: 'bbb', locals: {} },
{ foo: 'aaa', locals: { date: '2014-02-02' } },
];

var actual = arraySort(posts, ['foo', 'locals.date']);

actual.should.eql([
{ foo: 'aaa', locals: { date: '2012-01-02' } },
{ foo: 'aaa', locals: { date: '2014-02-02' } },
{ foo: 'bbb', locals: { date: '2013-05-06' } },
{ foo: 'bbb', locals: {} },
{ foo: 'ccc', locals: { date: '2014-01-02' } },
{ foo: 'ddd', locals: { date: '2014-01-09' } },
{ locals: { date: '2015-01-02' } },
{ locals: { date: '2015-04-12' } }
]);
});

it('should sort by multiple properties with null and undefined values:', function() {
var posts = [
{ foo: 'bbb', locals: { date: '2013-05-06' } },
{ foo: 'aaa', locals: { date: null } },
{ locals: { date: '2015-04-12' } },
{ foo: 'ccc', locals: { date: '2014-01-02' } },
{ locals: { date: '2015-01-02' } },
{ foo: 'ddd', locals: { date: '2014-01-09' } },
{ foo: null, locals: {} },
{ foo: 'aaa', locals: { date: '2014-02-02' } },
];

var actual = arraySort(posts, ['foo', 'locals.date']);

actual.should.eql([
{ foo: 'aaa', locals: { date: '2014-02-02' } },
{ foo: 'aaa', locals: { date: null } },
{ foo: 'bbb', locals: { date: '2013-05-06' } },
{ foo: 'ccc', locals: { date: '2014-01-02' } },
{ foo: 'ddd', locals: { date: '2014-01-09' } },
{ foo: null, locals: {} },
{ locals: { date: '2015-01-02' } },
{ locals: { date: '2015-04-12' } }
]);
});

it('should sort with a function:', function() {
var arr = [{key: 'y'}, {key: 'z'}, {key: 'x'}];

Expand Down

0 comments on commit 0268fdc

Please sign in to comment.