Skip to content

Commit ceaa881

Browse files
committed
Stack
1 parent 16cc595 commit ceaa881

File tree

3 files changed

+124
-11
lines changed

3 files changed

+124
-11
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { Stack } from './Stack'
3+
4+
describe('stack', () => {
5+
it('should create empty stack', () => {
6+
const stack = new Stack()
7+
8+
expect(stack.size()).toBe(0)
9+
expect(stack.isEmpty()).toBe(true)
10+
})
11+
12+
it('should push items to stack', () => {
13+
const stack = new Stack()
14+
15+
stack.push(1)
16+
expect(stack.size()).toBe(1)
17+
expect(stack.isEmpty()).toBe(false)
18+
expect(stack.toString()).toBe('1')
19+
20+
stack.push(2)
21+
expect(stack.size()).toBe(2)
22+
expect(stack.isEmpty()).toBe(false)
23+
expect(stack.toString()).toBe('2,1')
24+
})
25+
26+
it('should pop items from stack', () => {
27+
const stack = new Stack()
28+
29+
stack.push(1)
30+
stack.push(2)
31+
32+
expect(stack.pop()).toBe(2)
33+
expect(stack.size()).toBe(1)
34+
expect(stack.pop()).toBe(1)
35+
expect(stack.size()).toBe(0)
36+
expect(stack.pop()).toBe(undefined)
37+
expect(stack.isEmpty()).toBe(true)
38+
})
39+
40+
it('should peek data from stack', () => {
41+
const stack = new Stack()
42+
43+
expect(stack.peek()).toBe(undefined)
44+
45+
stack.push(1)
46+
stack.push(2)
47+
48+
expect(stack.peek()).toBe(2)
49+
expect(stack.size()).toBe(2)
50+
})
51+
52+
it('should check if stack is empty', () => {
53+
const stack = new Stack()
54+
55+
expect(stack.isEmpty()).toBe(true)
56+
57+
stack.push(1)
58+
expect(stack.isEmpty()).toBe(false)
59+
})
60+
61+
it('should clear the stack', () => {
62+
const stack = new Stack()
63+
64+
stack.push(1)
65+
stack.push(2)
66+
67+
stack.clear()
68+
expect(stack.isEmpty()).toBe(true)
69+
expect(stack.size()).toBe(0)
70+
})
71+
72+
it('should be possible to push/pop objects', () => {
73+
const stack = new Stack()
74+
75+
stack.push({ value: 'test1', key: 'key1' })
76+
stack.push({ value: 'test2', key: 'key2' })
77+
78+
const stringifier = (value: any) => `${value.key}:${value.value}`
79+
expect(stack.toString(stringifier)).toBe('key2:test2,key1:test1')
80+
expect(stack.pop().value).toBe('test2')
81+
expect(stack.pop().value).toBe('test1')
82+
})
83+
})

src/data-structures/stack/Stack.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export class Stack {
2+
private items: any[] = []
3+
4+
push(item: any) {
5+
this.items.push(item)
6+
}
7+
8+
pop() {
9+
return this.items.pop()
10+
}
11+
12+
peek() {
13+
return this.items[this.items.length - 1]
14+
}
15+
16+
isEmpty() {
17+
return this.items.length === 0
18+
}
19+
20+
size() {
21+
return this.items.length
22+
}
23+
24+
clear() {
25+
this.items = []
26+
}
27+
28+
toString(callback?: (item: any) => string) {
29+
const list = this.items.toReversed()
30+
if (callback)
31+
return list.map(callback).toString()
32+
return list.toString()
33+
}
34+
}

tsconfig.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2020",
3+
"target": "ES2015",
4+
"lib": ["ES2023", "DOM", "DOM.Iterable"],
45
"useDefineForClassFields": true,
56
"module": "ESNext",
6-
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7-
"skipLibCheck": true,
8-
9-
/* Bundler mode */
107
"moduleResolution": "bundler",
11-
"allowImportingTsExtensions": true,
128
"resolveJsonModule": true,
13-
"isolatedModules": true,
14-
"noEmit": true,
15-
16-
/* Linting */
9+
"allowImportingTsExtensions": true,
1710
"strict": true,
11+
"noFallthroughCasesInSwitch": true,
1812
"noUnusedLocals": true,
1913
"noUnusedParameters": true,
20-
"noFallthroughCasesInSwitch": true
14+
"noEmit": true,
15+
"isolatedModules": true,
16+
"skipLibCheck": true
2117
},
2218
"include": ["src"]
2319
}

0 commit comments

Comments
 (0)