Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit 7eecb31

Browse files
author
Luke Murray
committed
Attempts to sort an already sorted array now return the original array.
1 parent dfe18aa commit 7eecb31

File tree

2 files changed

+69
-36
lines changed

2 files changed

+69
-36
lines changed

src/seamless-immutable.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ function immutableInit(config) {
578578
}
579579

580580
function sortBy(array, sorter) {
581-
var result = [], i, length;
581+
var result = [], i, length, rearranged;
582582

583583
if (!(array instanceof Array)) {
584584
throw new TypeError("The first argument to Immutable#sortBy must be an array.");
@@ -589,7 +589,28 @@ function immutableInit(config) {
589589
}
590590
result.sort(sorter);
591591

592-
return makeImmutableArray(result);
592+
// Test if elements of the array have been rearranged
593+
rearranged = false;
594+
for (i = 0, length = array.length; i < length; i++) {
595+
if(result[i] !== array[i]) {
596+
// Check if both are NaN. Type checks catch cases where isNaN returns true for strings.
597+
if(!(typeof result[i] === "number" && isNaN(result[i]) && typeof array[i] === "number" && isNaN(array[i]))) {
598+
rearranged = true;
599+
}
600+
}
601+
}
602+
603+
// If the elements haven't been rearranged, return the original immutable
604+
if(rearranged) {
605+
return makeImmutableArray(result);
606+
} else {
607+
// Ensure that the returned array is immutable
608+
if(isImmutable(array)) {
609+
return array;
610+
} else {
611+
return makeImmutableArray(array);
612+
}
613+
}
593614
}
594615

595616
// Creates plain object to be used for cloning

test/ImmutableArray/test-sortBy.js

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,58 @@
1-
var JSC = require("jscheck");
1+
var JSC = require("jscheck");
22
var getTestUtils = require("../TestUtils.js");
3+
var assert = require("chai").assert;
34

45
module.exports = function(config) {
5-
var Immutable = config.implementation;
6-
var TestUtils = getTestUtils(Immutable);
7-
var check = TestUtils.check;
8-
9-
function dummySorter(a, b) {
10-
if(b > a) {
11-
return 1;
12-
} else if(b < a) {
13-
return -1;
14-
} else {
15-
return 0;
6+
var Immutable = config.implementation;
7+
var TestUtils = getTestUtils(Immutable);
8+
var check = TestUtils.check;
9+
10+
function dummySorter(a, b) {
11+
if(b > a) {
12+
return 1;
13+
} else if(b < a) {
14+
return -1;
15+
} else {
16+
return 0;
17+
}
1618
}
17-
}
1819

19-
describe("#sortBy", function() {
20-
it("produces a sorted immutable array", function() {
21-
check(100, [ JSC.array(JSC.integer(4), JSC.any()) ], function(array) {
22-
var immutable = Immutable(array);
23-
var mutable = array.slice();
20+
describe("#sortBy", function() {
21+
it("produces a sorted immutable array", function() {
22+
check(100, [JSC.array(JSC.integer(4), JSC.any())], function(array) {
23+
var immutable = Immutable(array);
24+
var mutable = array.slice();
2425

25-
var resultImmutable = Immutable.sortBy(immutable);
26-
var resultMutable = mutable.slice();
27-
resultMutable.sort();
26+
var resultImmutable = Immutable.sortBy(immutable);
27+
var resultMutable = mutable.slice();
28+
resultMutable.sort();
2829

29-
TestUtils.assertJsonEqual(resultImmutable, resultMutable);
30-
});
31-
});
30+
TestUtils.assertJsonEqual(resultImmutable, resultMutable);
31+
});
32+
});
33+
34+
it("produces a sorted immutable array when provided with a sorter function", function() {
35+
check(100, [JSC.array(JSC.integer(4), JSC.any())], function(array) {
36+
var immutable = Immutable(array);
37+
var mutable = array.slice();
38+
39+
var resultImmutable = Immutable.sortBy(immutable, dummySorter);
40+
var resultMutable = mutable.slice();
41+
resultMutable.sort(dummySorter);
42+
43+
TestUtils.assertJsonEqual(resultImmutable, resultMutable);
44+
});
45+
});
3246

33-
it("produces a sorted immutable array when provided with a sorter function", function() {
34-
check(100, [ JSC.array(JSC.integer(4), JSC.any()) ], function(array) {
35-
var immutable = Immutable(array);
36-
var mutable = array.slice();
47+
it("returns the same array if it is already sorted", function() {
48+
check(100, [JSC.array(JSC.integer(4), JSC.any())], function(array) {
49+
var mutable = array.slice().sort();
50+
var immutable = Immutable(mutable);
3751

38-
var resultImmutable = Immutable.sortBy(immutable, dummySorter);
39-
var resultMutable = mutable.slice();
40-
resultMutable.sort(dummySorter);
52+
var resultImmutable = Immutable.sortBy(immutable);
4153

42-
TestUtils.assertJsonEqual(resultImmutable, resultMutable);
43-
});
54+
assert.strictEqual(resultImmutable, immutable);
55+
});
56+
});
4457
});
45-
});
4658
};

0 commit comments

Comments
 (0)