diff --git a/index.js b/index.js index 621340f..01880e1 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ 'use strict'; +var defaultCompare = require('default-compare'); var typeOf = require('kind-of'); var get = require('get-value'); @@ -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. */ diff --git a/package.json b/package.json index 3e3cffc..d6405a6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test.js b/test.js index 43c116f..7482f80 100644 --- a/test.js +++ b/test.js @@ -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([ @@ -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'}];