Skip to content

Commit

Permalink
Merge pull request #3 from eHanlin/master
Browse files Browse the repository at this point in the history
Fixed an error about unshift stack
  • Loading branch information
mattjmattj authored Dec 21, 2017
2 parents 10ad09a + 735755e commit 0b16bd4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-undo",
"version": "1.0.1",
"version": "1.0.2",
"authors": [
"Matthias Jouan <[email protected]>"
],
Expand Down
2 changes: 1 addition & 1 deletion lib/simple-undo.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ SimpleUndo.prototype.clear = function() {

SimpleUndo.prototype.save = function() {
this.provider(function(current) {
truncate(this.stack, this.maxLength);
if (this.position >= this.maxLength) truncate(this.stack, this.maxLength);
this.position = Math.min(this.position,this.stack.length - 1);

this.stack = this.stack.slice(0, this.position + 1);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-undo",
"version": "1.0.1",
"version": "1.0.2",
"description": "a very basic javascript undo/redo stack for managing histories of basically anything",
"main": "./lib/simple-undo.js",
"scripts": {
Expand Down
29 changes: 28 additions & 1 deletion tests/simple-undo.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,31 @@ describe('SimpleUndo', function() {
});

});
})

it('should reserve initial when undo position to be 0 and save', function() {
var count = 0;
var provider = function(done) {
done(count++);
}

var history = new SimpleUndo({
provider: provider,
maxLength: 3
});

history.initialize('initial');
history.save();
history.save();
history.save();
history.undo();
history.undo();
history.undo();

history.canUndo().should.be.false;
history.count().should.equal(3);
history.save();
history.stack.length.should.equal(2);
history.stack[0].should.equal('initial');
history.stack[1].should.equal(3);
});
})

0 comments on commit 0b16bd4

Please sign in to comment.