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

Use ES2015-style default parameters #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "timsort",
"version": "0.3.0",
"version": "0.3.1",
"author": {
"name": "Marco Ziccardi",
"url": "http://mziccard.me/"
Expand Down
86 changes: 41 additions & 45 deletions build/timsort.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,63 +753,59 @@
return TimSort;
})();

function sort(array, compare, lo, hi) {
if (!Array.isArray(array)) {
throw new TypeError('Can only sort arrays');
}
function sort(array) {
var compare = arguments.length <= 1 || arguments[1] === undefined ? alphabeticalCompare : arguments[1];
var lo = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
var hi = arguments.length <= 3 || arguments[3] === undefined ? array.length : arguments[3];
return (function () {
if (!Array.isArray(array)) {
throw new TypeError('Can only sort arrays');
}

if (!compare) {
compare = alphabeticalCompare;
} else if (typeof compare !== 'function') {
hi = lo;
lo = compare;
compare = alphabeticalCompare;
}
if (typeof compare !== 'function') {
hi = lo;
lo = compare;
compare = alphabeticalCompare;
}

if (!lo) {
lo = 0;
}
if (!hi) {
hi = array.length;
}
var remaining = hi - lo;

var remaining = hi - lo;
if (remaining < 2) {
return;
}

if (remaining < 2) {
return;
}
var runLength = 0;

var runLength = 0;
if (remaining < DEFAULT_MIN_MERGE) {
runLength = makeAscendingRun(array, lo, hi, compare);
binaryInsertionSort(array, lo, hi, lo + runLength, compare);
return;
}

if (remaining < DEFAULT_MIN_MERGE) {
runLength = makeAscendingRun(array, lo, hi, compare);
binaryInsertionSort(array, lo, hi, lo + runLength, compare);
return;
}
var ts = new TimSort(array, compare);

var ts = new TimSort(array, compare);
var minRun = minRunLength(remaining);

var minRun = minRunLength(remaining);
do {
runLength = makeAscendingRun(array, lo, hi, compare);
if (runLength < minRun) {
var force = remaining;
if (force > minRun) {
force = minRun;
}

do {
runLength = makeAscendingRun(array, lo, hi, compare);
if (runLength < minRun) {
var force = remaining;
if (force > minRun) {
force = minRun;
binaryInsertionSort(array, lo, lo + force, lo + runLength, compare);
runLength = force;
}

binaryInsertionSort(array, lo, lo + force, lo + runLength, compare);
runLength = force;
}

ts.pushRun(lo, runLength);
ts.mergeRuns();
ts.pushRun(lo, runLength);
ts.mergeRuns();

remaining -= runLength;
lo += runLength;
} while (remaining !== 0);
remaining -= runLength;
lo += runLength;
} while (remaining !== 0);

ts.forceMergeRuns();
ts.forceMergeRuns();
})();
}
});
2 changes: 1 addition & 1 deletion build/timsort.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "timsort",
"version": "0.3.0",
"version": "0.3.1",
"author": {
"name": "Marco Ziccardi",
"url": "http://mziccard.me/"
Expand Down
14 changes: 2 additions & 12 deletions src/timsort.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ class TimSort {
* @param {number} hi - Last element in the range.
* comparator.
*/
export function sort(array, compare, lo, hi) {
export function sort(array, compare = alphabeticalCompare, lo = 0, hi = array.length) {
if (!Array.isArray(array)) {
throw new TypeError('Can only sort arrays');
}
Expand All @@ -916,22 +916,12 @@ export function sort(array, compare, lo, hi) {
* Handle the case where a comparison function is not provided. We do
* lexicographic sorting
*/
if (!compare) {
compare = alphabeticalCompare;

} else if (typeof compare !== 'function') {
if (typeof compare !== 'function') {
hi = lo;
lo = compare;
compare = alphabeticalCompare;
}

if (!lo) {
lo = 0;
}
if (!hi) {
hi = array.length;
}

let remaining = hi - lo;

// The array is already sorted
Expand Down