Skip to content
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

Return a sorted copy from array.sort() #250

Merged
merged 2 commits into from
Feb 26, 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
4 changes: 2 additions & 2 deletions addon/array/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function(array, sortDefinition) {
let computedCallback;

if (sortDefinition === undefined) {
computedCallback = array => array.sort();
computedCallback = array => array.slice().sort();
} else {
computedCallback = function(array, sortDefinition) {
let sortCallback;
Expand Down Expand Up @@ -38,7 +38,7 @@ export default function(array, sortDefinition) {
};
}

return array.sort(sortCallback);
return array.slice().sort(sortCallback);
};
}

Expand Down
52 changes: 49 additions & 3 deletions tests/integration/array/sort-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sort } from 'ember-awesome-macros/array';
import { raw } from 'ember-awesome-macros';
import { A as emberA } from 'ember-array/utils';
import { module, test } from 'qunit';
import sinon from 'sinon';
Expand All @@ -14,7 +15,7 @@ test('it returns undefined if array undefined', function(assert) {
});
});

test('it calls sort on array without a parameter', function(assert) {
test('it returns a sorted array without a parameter', function(assert) {
compute({
assert,
computed: sort('array'),
Expand All @@ -25,7 +26,7 @@ test('it calls sort on array without a parameter', function(assert) {
});
});

test('it calls sort on array with an array parameter', function(assert) {
test('it returns a sorted array with an array parameter', function(assert) {
compute({
assert,
computed: sort('array', 'sortDefinition'),
Expand Down Expand Up @@ -58,7 +59,18 @@ test('it calls sort on array with an array parameter', function(assert) {
});
});

test('it calls sort on array with function parameter', function(assert) {
test('it returns a sorted array with a direct array parameter', function(assert) {
compute({
assert,
computed: sort('array', ['prop:desc']),
properties: {
array: [{prop: 1}, {prop: 3}, {prop: 2}]
},
deepEqual: [{prop: 3}, {prop: 2}, {prop: 1}]
});
});

test('it returns a sorted array with function parameter', function(assert) {
let sortDefinition = sinon.stub().returns(1);

compute({
Expand Down Expand Up @@ -144,3 +156,37 @@ test('the callback is passed the correct args', function(assert) {
}
]]);
});

test('it does not sort the source array for default sorts', function(assert) {
let array = [1, 3, 2];
compute({
assert,
computed: sort('array'),
properties: {
array
},
deepEqual: [1, 2, 3]
});
assert.deepEqual(array, [1, 3, 2]);
});

test('it does not sort the source array for property sorts', function(assert) {
let array = [{prop: 1}, {prop: 3}, {prop: 2}];
compute({
assert,
computed: sort('array', ['prop:desc']),
properties: {
array
},
deepEqual: [{prop: 3}, {prop: 2}, {prop: 1}]
});
assert.deepEqual(array, [{prop: 1}, {prop: 3}, {prop: 2}]);
});

test('composable: it returns a sorted array', function(assert) {
compute({
assert,
computed: sort(raw([1, 3, 2])),
deepEqual: [1, 2, 3]
});
});