Skip to content

Commit

Permalink
clean up SortableSet
Browse files Browse the repository at this point in the history
- remove unnecessary _isSorted flag
- remove clear-override as an empty set is "sorted"
- early return on empty sets
  • Loading branch information
timse committed Jun 20, 2017
1 parent f7ffed8 commit 5f16dd8
Showing 1 changed file with 1 addition and 13 deletions.
14 changes: 1 addition & 13 deletions lib/util/SortableSet.js
Expand Up @@ -6,7 +6,6 @@ module.exports = class SortableSet extends Set {
super(initialIterable);
this._sortFn = defaultSort;
this._lastActiveSortFn = null;
this._isSorted = false;
}

/**
Expand All @@ -15,26 +14,16 @@ module.exports = class SortableSet extends Set {
*/
add(value) {
this._lastActiveSortFn = null;
this._isSorted = false;
super.add(value);
return this;
}

/**
* @returns {void}
*/
clear() {
this._lastActiveSortFn = null;
this._isSorted = false;
super.clear();
}

/**
* @param {Function} sortFn - function to sort the set
* @returns {void}
*/
sortWith(sortFn) {
if(this._isSorted && sortFn === this._lastActiveSortFn) {
if(this.size === 0 || sortFn === this._lastActiveSortFn) {
// already sorted - nothing to do
return;
}
Expand All @@ -45,7 +34,6 @@ module.exports = class SortableSet extends Set {
this.add(sortedArray[i]);
}
this._lastActiveSortFn = sortFn;
this._isSorted = true;
}

/**
Expand Down

0 comments on commit 5f16dd8

Please sign in to comment.