Skip to content

Commit 9148fa6

Browse files
committed
added type to Stack
1 parent aa73a46 commit 9148fa6

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

lib/ts/data-structures/stack.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export default class Stack {
2-
private items: any[];
1+
export default class Stack<T> {
2+
private items: T[];
33

44
constructor() {
55
this.items = [];
66
}
77

8-
push(element: any) {
8+
push(element: T) {
99
this.items.push(element);
1010
}
1111

test/ts/data-structures/stack.spec.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { expect } from 'chai';
33
import Stack from '../../../lib/ts/data-structures/stack';
44

55
describe('Stack', () => {
6-
let stack: Stack;
6+
let stack: Stack<number>;
77

88
beforeEach(function() {
9-
stack = new Stack();
9+
stack = new Stack<number>();
1010
});
1111

1212
it('starts empty', () => {
@@ -152,11 +152,12 @@ describe('Stack', () => {
152152
stack.clear();
153153
expect(stack.toString()).to.equal('');
154154

155-
stack.push('el1');
156-
expect(stack.toString()).to.equal('el1');
155+
const stackString = new Stack<string>();
156+
stackString.push('el1');
157+
expect(stackString.toString()).to.equal('el1');
157158

158-
stack.push('el2');
159-
expect(stack.toString()).to.equal('el1,el2');
159+
stackString.push('el2');
160+
expect(stackString.toString()).to.equal('el1,el2');
160161
});
161162

162163
it('returns toString objects', () => {
@@ -167,13 +168,13 @@ describe('Stack', () => {
167168
return `${this.el1.toString()}|${this.el2.toString()}`;
168169
}
169170
}
170-
expect(stack.toString()).to.equal('');
171-
172-
stack.push(new MyObj(1, 2));
173-
expect(stack.toString()).to.equal('1|2');
171+
const stackMyObj = new Stack<MyObj>();
172+
expect(stackMyObj.toString()).to.equal('');
174173

175-
stack.push(new MyObj(3, 4));
176-
expect(stack.toString()).to.equal('1|2,3|4');
174+
stackMyObj.push(new MyObj(1, 2));
175+
expect(stackMyObj.toString()).to.equal('1|2');
177176

177+
stackMyObj.push(new MyObj(3, 4));
178+
expect(stackMyObj.toString()).to.equal('1|2,3|4');
178179
});
179180
});

0 commit comments

Comments
 (0)