Skip to content

Commit

Permalink
Added 'stack' function to api
Browse files Browse the repository at this point in the history
  • Loading branch information
orangemug committed May 19, 2019
1 parent efef382 commit 12832e4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ General methods
- `errors` - list of current errors
- `current` - the current immutable object
- `history` - array of immutable objects
- `stack(idx)` - get a item from the history stack, supports negative index lookups to start from the end of history
- `canUndo()` - is there anything to undo in the history stack
- `undo()` - move backward in the history stack
- `canRedo()` - is there anything to redo in the history stack
Expand Down
8 changes: 8 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ class IcepickStyle extends EventEmitter {

return this;
}

stack(idx) {
if (idx > -1) {
return this.history[idx];
}

return this.history[this.history.length - 1 + idx];
}
}

module.exports = IcepickStyle;
21 changes: 21 additions & 0 deletions test/ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,27 @@ describe('ops', () => {
);
});

it('stack(idx)', () => {
const style = new IcepickStyle({
layers: [],
sources: {},
zoom: 10
});

style.modifyRoot('zoom', 11);
style.modifyRoot('zoom', 12);
style.modifyRoot('zoom', 13);

assert.strictEqual(style.stack(-1).style.zoom, 12);
assert.strictEqual(style.stack(-2).style.zoom, 11);
assert.strictEqual(style.stack(-3).style.zoom, 10);

assert.strictEqual(style.stack(0).style.zoom, 10);
assert.strictEqual(style.stack(1).style.zoom, 11);
assert.strictEqual(style.stack(2).style.zoom, 12);
assert.strictEqual(style.stack(3).style.zoom, 13);
});

it('replace()', () => {
const style = new IcepickStyle({
version: 8,
Expand Down

0 comments on commit 12832e4

Please sign in to comment.