Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class History { | ||
maxLength = Infinity; | ||
history = []; // A circular history array if a maximum length is given | ||
start = 0; // The start position for the history array | ||
index = -1; // Current index of the history array | ||
|
||
constructor(maxLength) { | ||
maxLength = Number(maxLength) || 0; | ||
if (maxLength > 0) { | ||
this.maxLength = maxLength; | ||
} | ||
} | ||
current() { | ||
if (this.history.length === 0) { | ||
return undefined; | ||
} | ||
|
||
const index = (this.start + this.index) % this.history.length; | ||
return this.history[index]; | ||
} | ||
forward() { | ||
if ((this.history.length === 0) || (this.index + 1 >= this.history.length)) { | ||
return undefined; | ||
} | ||
|
||
++this.index; | ||
const index = (this.start + this.index) % this.history.length; | ||
return this.history[index]; | ||
} | ||
back() { | ||
if ((this.history.length === 0) || (this.index - 1 < 0)) { | ||
return undefined; | ||
} | ||
|
||
--this.index; | ||
const index = (this.start + this.index) % this.history.length; | ||
return this.history[index]; | ||
} | ||
go(n) { | ||
if (this.history.length === 0) { | ||
return undefined; | ||
} | ||
n = Number(n) || 0; | ||
this.index = Math.min(Math.max(0, this.index + n), this.history.length - 1); | ||
const index = (this.start + this.index) % this.history.length; | ||
return this.history[index]; | ||
} | ||
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 | ||
// S x x x x x x x x x >> x S x x x x x x x x | ||
push(data) { | ||
if (this.history.length < this.maxLength) { | ||
if (this.index + 1 >= this.history.length) { | ||
++this.index; | ||
} | ||
this.history.push(data); | ||
} else { | ||
this.history[this.start] = data; | ||
this.start = (this.start + 1) % this.history.length; | ||
if (this.index > 0) { | ||
--this.index; | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
} | ||
|
||
export default History; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters