Skip to content

Support null and undefined values #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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