From 0770421a96850a5dd9ebbf6ce5f92c3e6acd2213 Mon Sep 17 00:00:00 2001 From: doowb Date: Sun, 10 Sep 2017 18:57:54 -0400 Subject: [PATCH 1/4] add tests for null and undefined values --- test.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) 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'}]; From cb459d937a314390295dcf87adabf45010ad9aca Mon Sep 17 00:00:00 2001 From: doowb Date: Sun, 10 Sep 2017 18:58:12 -0400 Subject: [PATCH 2/4] add implementation for null and undefined values --- index.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 621340f..91e23ca 100644 --- a/index.js +++ b/index.js @@ -91,11 +91,23 @@ function compare(prop, a, b) { /** * Default compare function used as a fallback - * for sorting. + * for sorting. Built-in array sorting pushes + * null and undefined values to the end of the array. */ function defaultCompare(a, b) { - return a < b ? -1 : (a > b ? 1 : 0); + var typeA = typeOf(a); + var typeB = typeOf(b); + + if (typeA === 'null') { + return typeB === 'null' ? 0 : (typeB === 'undefined' ? -1 : 1); + } else if (typeA === 'undefined') { + return typeB === 'null' ? 1 : (typeB === 'undefined' ? 0 : 1); + } else if (typeB === 'null' || typeB === 'undefined') { + return -1; + } else { + return a < b ? -1 : (a > b ? 1 : 0); + } } /** From 503e1508faea6d0301489f6ccf527d3e23122192 Mon Sep 17 00:00:00 2001 From: doowb Date: Mon, 11 Sep 2017 12:50:41 -0400 Subject: [PATCH 3/4] use default-compare lib --- index.js | 22 +--------------------- package.json | 1 + 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 91e23ca..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,27 +90,6 @@ function compare(prop, a, b) { return defaultCompare(a, b); } -/** - * Default compare function used as a fallback - * for sorting. Built-in array sorting pushes - * null and undefined values to the end of the array. - */ - -function defaultCompare(a, b) { - var typeA = typeOf(a); - var typeB = typeOf(b); - - if (typeA === 'null') { - return typeB === 'null' ? 0 : (typeB === 'undefined' ? -1 : 1); - } else if (typeA === 'undefined') { - return typeB === 'null' ? 1 : (typeB === 'undefined' ? 0 : 1); - } else if (typeB === 'null' || typeB === 'undefined') { - return -1; - } else { - return a < b ? -1 : (a > b ? 1 : 0); - } -} - /** * Flatten the given array. */ diff --git a/package.json b/package.json index 3e3cffc..998e6d3 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "test": "mocha" }, "dependencies": { + "default-compare": "^1.0.0", "get-value": "^2.0.5", "kind-of": "^2.0.0" }, From 5e10279d2ebc85fdd04d2942ecea71b4a199d2de Mon Sep 17 00:00:00 2001 From: doowb Date: Mon, 11 Sep 2017 12:53:38 -0400 Subject: [PATCH 4/4] bump deps --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 998e6d3..d6405a6 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,8 @@ }, "dependencies": { "default-compare": "^1.0.0", - "get-value": "^2.0.5", - "kind-of": "^2.0.0" + "get-value": "^2.0.6", + "kind-of": "^5.0.2" }, "devDependencies": { "ansi-bold": "^0.1.1",