Skip to content

Commit

Permalink
replaced 2x array.splice() with loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Acker committed Jan 9, 2025
1 parent b9d8a4d commit 8703295
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions html/js/statsbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class StatsBox {
class SortedStatsBox extends StatsBox {
inc(key) {
if (this.has(key)) {
const idxOld = this._stats[key];
let idxOld = this._stats[key];
const stat = this._rows[idxOld];

stat.value++;
Expand All @@ -94,8 +94,14 @@ class SortedStatsBox extends StatsBox {

if (idxNew != idxOld) {
this._rows[idxNew].tr.before(stat.tr); // move table row
this._rows.splice(idxOld, 1); // cut
this._rows.splice(idxNew, 0, stat); // paste

// shift array back by one
while (idxOld > idxNew) {
this._rows[idxOld] = this._rows[idxOld-1];
idxOld--;
}

this._rows[idxNew] = stat;

//console.log(`${key}: ${this._stats[key]} => ${idxNew}`);
this._stats[key] = idxNew;
Expand Down
2 changes: 1 addition & 1 deletion html/js/statsbox.min.js

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

0 comments on commit 8703295

Please sign in to comment.