Skip to content

Commit 826d443

Browse files
authored
Don't sort array when key is empty (#386)
1 parent e24017e commit 826d443

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

addon/helpers/sort-by.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { get } from '@ember/object';
2+
import { isEmpty } from '@ember/utils';
23
import { helper } from '@ember/component/helper';
34

45
function normalizeToBoolean(val) {
@@ -18,6 +19,10 @@ function normalizeToBoolean(val) {
1819
}
1920

2021
function sortDesc(key, a, b) {
22+
if (isEmpty(key)) {
23+
return 0;
24+
}
25+
2126
const aValue = get(a, key);
2227
const bValue = get(b, key);
2328

@@ -44,6 +49,10 @@ function sortDesc(key, a, b) {
4449
}
4550

4651
function sortAsc(key, a, b) {
52+
if (isEmpty(key)) {
53+
return 0;
54+
}
55+
4756
const aValue = get(a, key);
4857
const bValue = get(b, key);
4958

tests/integration/helpers/sort-by-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,22 @@ module('Integration | Helper | {{sort-by}}', function(hooks) {
164164
assert.equal(find('*').textContent.trim(), 'cbaa', 'caba is sorted to cbaa');
165165
});
166166

167+
test('It does not sort the array when the key is an empty string', async function(assert) {
168+
this.set('array', emberArray([
169+
{ name: 'c' },
170+
{ name: 'a' },
171+
{ name: 'b' }
172+
]));
173+
174+
await render(hbs`
175+
{{~#each (sort-by "" array) as |user|~}}
176+
{{~user.name~}}
177+
{{~/each~}}
178+
`);
179+
180+
assert.equal(find('*').textContent.trim(), 'cab', 'cab is unsorted');
181+
});
182+
167183
test('It watches for changes', async function(assert) {
168184
let array = emberArray([
169185
{ name: 'b' },

0 commit comments

Comments
 (0)