Skip to content

Commit 0268fdc

Browse files
authored
Merge pull request #7 from jonschlinkert/null-values
Support null and undefined values
2 parents c8024b8 + 5e10279 commit 0268fdc

File tree

3 files changed

+110
-11
lines changed

3 files changed

+110
-11
lines changed

index.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
'use strict';
99

10+
var defaultCompare = require('default-compare');
1011
var typeOf = require('kind-of');
1112
var get = require('get-value');
1213

@@ -89,15 +90,6 @@ function compare(prop, a, b) {
8990
return defaultCompare(a, b);
9091
}
9192

92-
/**
93-
* Default compare function used as a fallback
94-
* for sorting.
95-
*/
96-
97-
function defaultCompare(a, b) {
98-
return a < b ? -1 : (a > b ? 1 : 0);
99-
}
100-
10193
/**
10294
* Flatten the given array.
10395
*/

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
"test": "mocha"
2525
},
2626
"dependencies": {
27-
"get-value": "^2.0.5",
28-
"kind-of": "^2.0.0"
27+
"default-compare": "^1.0.0",
28+
"get-value": "^2.0.6",
29+
"kind-of": "^5.0.2"
2930
},
3031
"devDependencies": {
3132
"ansi-bold": "^0.1.1",

test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@ describe('arraySort', function() {
6060
]);
6161
});
6262

63+
it('should sort by a property with null values:', function() {
64+
var arr = [{key: null}, {key: 'z'}, {key: 'x'}];
65+
arraySort(arr, 'key').should.eql([
66+
{key: 'x'},
67+
{key: 'z'},
68+
{key: null}
69+
]);
70+
});
71+
72+
it('should sort by a property with undefined values:', function() {
73+
var arr = [{}, {key: 'z'}, {key: 'x'}];
74+
arraySort(arr, 'key').should.eql([
75+
{key: 'x'},
76+
{key: 'z'},
77+
{}
78+
]);
79+
});
80+
81+
it('should sort by a property with null and undefined values:', function() {
82+
var arr = [{key: null}, {key: 'z'}, {}, {key: 'x'}];
83+
arraySort(arr, 'key').should.eql([
84+
{key: 'x'},
85+
{key: 'z'},
86+
{key: null},
87+
{}
88+
]);
89+
});
90+
6391
it('should sort by a nested property:', function() {
6492
var res = arraySort(posts, 'locals.date');
6593
res.should.eql([
@@ -116,6 +144,84 @@ describe('arraySort', function() {
116144
]);
117145
});
118146

147+
it('should sort by multiple properties with null values:', function() {
148+
var posts = [
149+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
150+
{ foo: 'aaa', locals: { date: '2012-01-02' } },
151+
{ foo: null, locals: { date: '2015-04-12' } },
152+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
153+
{ foo: null, locals: { date: '2015-01-02' } },
154+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
155+
{ foo: 'bbb', locals: { date: null } },
156+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
157+
];
158+
159+
var actual = arraySort(posts, ['foo', 'locals.date']);
160+
161+
actual.should.eql([
162+
{ foo: 'aaa', locals: { date: '2012-01-02' } },
163+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
164+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
165+
{ foo: 'bbb', locals: { date: null } },
166+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
167+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
168+
{ foo: null, locals: { date: '2015-01-02' } },
169+
{ foo: null, locals: { date: '2015-04-12' } }
170+
]);
171+
});
172+
173+
it('should sort by multiple properties with undefined values:', function() {
174+
var posts = [
175+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
176+
{ foo: 'aaa', locals: { date: '2012-01-02' } },
177+
{ locals: { date: '2015-04-12' } },
178+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
179+
{ locals: { date: '2015-01-02' } },
180+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
181+
{ foo: 'bbb', locals: {} },
182+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
183+
];
184+
185+
var actual = arraySort(posts, ['foo', 'locals.date']);
186+
187+
actual.should.eql([
188+
{ foo: 'aaa', locals: { date: '2012-01-02' } },
189+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
190+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
191+
{ foo: 'bbb', locals: {} },
192+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
193+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
194+
{ locals: { date: '2015-01-02' } },
195+
{ locals: { date: '2015-04-12' } }
196+
]);
197+
});
198+
199+
it('should sort by multiple properties with null and undefined values:', function() {
200+
var posts = [
201+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
202+
{ foo: 'aaa', locals: { date: null } },
203+
{ locals: { date: '2015-04-12' } },
204+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
205+
{ locals: { date: '2015-01-02' } },
206+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
207+
{ foo: null, locals: {} },
208+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
209+
];
210+
211+
var actual = arraySort(posts, ['foo', 'locals.date']);
212+
213+
actual.should.eql([
214+
{ foo: 'aaa', locals: { date: '2014-02-02' } },
215+
{ foo: 'aaa', locals: { date: null } },
216+
{ foo: 'bbb', locals: { date: '2013-05-06' } },
217+
{ foo: 'ccc', locals: { date: '2014-01-02' } },
218+
{ foo: 'ddd', locals: { date: '2014-01-09' } },
219+
{ foo: null, locals: {} },
220+
{ locals: { date: '2015-01-02' } },
221+
{ locals: { date: '2015-04-12' } }
222+
]);
223+
});
224+
119225
it('should sort with a function:', function() {
120226
var arr = [{key: 'y'}, {key: 'z'}, {key: 'x'}];
121227

0 commit comments

Comments
 (0)