Skip to content

Commit 4fb4545

Browse files
authored
Add Typescript support (#56)
* WIP - support for typescript * WIP - moved stack to ts * WIP - moved queue from js to ts * WIP - fixed bst * Fixed ts changes
1 parent 449e4f8 commit 4fb4545

32 files changed

+2999
-1291
lines changed

. eslintignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
lib/
3+
demo/
4+
typings/

.babelrc

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
"node": "current"
88
}
99
}
10-
]
10+
],
11+
"@babel/preset-typescript"
1112
],
12-
"plugins": [
13-
"babel-plugin-add-module-exports"
14-
]
15-
}
13+
"plugins": ["babel-plugin-add-module-exports"]
14+
}

.eslintrc

+11-33
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
11
{
2-
"ecmaFeatures": {
3-
"globalReturn": true,
4-
"jsx": true,
5-
"modules": true
6-
},
7-
"env": {
8-
"browser": true,
9-
"es6": true,
10-
"node": true
11-
},
12-
"globals": {
13-
"document": false,
14-
"escape": false,
15-
"navigator": false,
16-
"unescape": false,
17-
"window": false,
18-
"describe": true,
19-
"before": true,
20-
"it": true,
21-
"expect": true,
22-
"sinon": true
23-
},
24-
"parser": "babel-eslint",
25-
"plugins": [],
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": ["@typescript-eslint"],
5+
"extends": [
6+
"eslint:recommended",
7+
"plugin:@typescript-eslint/eslint-recommended",
8+
"plugin:@typescript-eslint/recommended"
9+
],
2610
"rules": {
27-
// ... lots of lots of rules here
11+
"@typescript-eslint/explicit-module-boundary-types": "off"
2812
},
29-
"parserOptions": {
30-
"ecmaVersion": 6,
31-
"sourceType": "module",
32-
"ecmaFeatures": {
33-
"modules": true
34-
}
35-
}
36-
}
13+
"env": { "es6": true }
14+
}

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
22
lib/
33
coverage/
4-
npm-debug.log
4+
npm-debug.log
5+
typings/

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

package.json

+13-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
"coveralls": "jest --coverage && coveralls < coverage/lcov.info",
2323
"prepare": "npm run build & npm run dev",
2424
"publish:npm": "npm publish --access public",
25-
"test:jest": "jest"
25+
"test:jest": "jest",
26+
"lint": "eslint src/ --ext .ts"
2627
},
2728
"jest": {
2829
"moduleFileExtensions": [
2930
"js",
30-
"jsx"
31+
"jsx",
32+
"ts",
33+
"tsx"
3134
],
3235
"moduleDirectories": [
3336
"node_modules",
@@ -52,20 +55,27 @@
5255
"@babel/cli": "^7.10.0",
5356
"@babel/core": "^7.10.0",
5457
"@babel/preset-env": "^7.11.0",
58+
"@babel/preset-typescript": "^7.10.4",
59+
"@types/jest": "^26.0.9",
60+
"@typescript-eslint/eslint-plugin": "^3.8.0",
61+
"@typescript-eslint/parser": "^3.8.0",
5562
"@webpack-cli/migrate": "^0.1.0",
5663
"babel-eslint": "^8.2.6",
5764
"babel-jest": "^26.2.2",
5865
"babel-loader": "^7.1.5",
5966
"babel-plugin-add-module-exports": "^0.2.1",
6067
"babel-polyfill": "^6.26.0",
6168
"coveralls": "^3.0.2",
62-
"eslint": "^4.18.2",
69+
"eslint": "^7.6.0",
6370
"eslint-loader": "^2.1.0",
6471
"eslint-plugin-import": "^2.13.0",
6572
"istanbul": "^0.4.5",
6673
"jest": "^26.2.2",
74+
"npm": "^6.14.7",
6775
"phantomjs-prebuilt": "^2.1.15",
76+
"ts-loader": "^8.0.2",
6877
"tslint": "^5.11.0",
78+
"typescript": "^3.9.7",
6979
"uglifyjs-webpack-plugin": "^0.4.6",
7080
"webpack-cli": "^3.1.0"
7181
},
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
import { BST } from './binary-search-tree';
1+
import { BST } from "./binary-search-tree";
22

3-
describe('Binary Search Tree', () => {
4-
let bst;
3+
describe("Binary Search Tree", () => {
4+
let bst: BST<number>;
55

66
beforeEach(() => {
7-
bst = new BST();
7+
bst = new BST<number>();
88
});
99

10-
it('should create bst object', () => {
10+
it("should create bst object", () => {
1111
expect(bst).toBeDefined();
1212
});
1313

14-
it('should create BST with null root', () => {
14+
it("should create BST with null root", () => {
1515
expect(bst.root).toBe(null);
1616
expect(bst.len).toBe(0);
1717
});
1818

19-
describe('Height of the node', () => {
19+
describe("Height of the node", () => {
2020
beforeEach(() => {
2121
bst.insert(5);
2222
bst.insert(6);
2323
bst.insert(4);
2424
bst.insert(2);
2525
});
2626

27-
it('should have height of node 4 as 1', () => {
27+
it("should have height of node 4 as 1", () => {
2828
expect(bst.height(bst.lookup(4).currentNode)).toBe(1);
2929
});
3030

31-
it('should have height of tree = 2', () => {
31+
it("should have height of tree = 2", () => {
3232
expect(bst.height()).toBe(2);
3333
});
3434

35-
it('should have height 0 of leaf nodes', () => {
35+
it("should have height 0 of leaf nodes", () => {
3636
expect(bst.height(bst.lookup(6).currentNode)).toBe(0);
3737
});
3838

39-
it('should have height 0 of leaf nodes', () => {
39+
it("should have height 0 of leaf nodes", () => {
4040
expect(bst.height(bst.lookup(2).currentNode)).toBe(0);
4141
});
4242
});
4343

44-
describe('Insertion operation', () => {
45-
it('should insert values in the BST', () => {
44+
describe("Insertion operation", () => {
45+
it("should insert values in the BST", () => {
4646
bst.insert(5);
4747
expect(bst.root.key).toBe(5);
4848
expect(bst.len).toBe(1);
@@ -57,37 +57,37 @@ describe('Binary Search Tree', () => {
5757
});
5858
});
5959

60-
describe('Lookup operation', () => {
60+
describe("Lookup operation", () => {
6161
beforeEach(() => {
6262
bst.insert(5);
6363
bst.insert(6);
6464
bst.insert(4);
6565
bst.insert(2);
6666
});
6767

68-
it('should lookup for value 6 in the bst and return the node with key 6 and its parent node', () => {
68+
it("should lookup for value 6 in the bst and return the node with key 6 and its parent node", () => {
6969
const findNode = bst.lookup(6);
7070
expect(findNode.hasVal).toBeTruthy();
7171
expect(findNode.currentNode.key).toBe(6);
7272
expect(findNode.parentNode.key).toBe(5);
7373
});
7474

75-
it('should lookup for value 100 in the bst and should return the currentNode and parentNode as null', () => {
75+
it("should lookup for value 100 in the bst and should return the currentNode and parentNode as null", () => {
7676
const findNode = bst.lookup(100);
7777
expect(findNode.hasVal).toBeFalsy();
7878
expect(findNode.currentNode).toBe(null);
7979
expect(findNode.parentNode).toBe(null);
8080
});
8181

82-
it('should lookup for value 5 and should return currentNode as 5 and parentNode as null', () => {
82+
it("should lookup for value 5 and should return currentNode as 5 and parentNode as null", () => {
8383
const findNode = bst.lookup(5);
8484
expect(findNode.hasVal).toBeTruthy();
8585
expect(findNode.currentNode.key).toBe(5);
8686
expect(findNode.parentNode).toBe(null);
8787
});
8888
});
8989

90-
describe('Delete operation', () => {
90+
describe("Delete operation", () => {
9191
beforeEach(() => {
9292
bst.insert(11);
9393
bst.insert(6);
@@ -102,11 +102,11 @@ describe('Binary Search Tree', () => {
102102
bst.insert(49);
103103
});
104104

105-
it('should have 11 nodes in the bst', () => {
105+
it("should have 11 nodes in the bst", () => {
106106
expect(bst.len).toBe(11);
107107
});
108108

109-
it('should delete leaf with value 5', () => {
109+
it("should delete leaf with value 5", () => {
110110
expect(bst.len).toBe(11);
111111

112112
const lookUpFor5 = bst.lookup(5);
@@ -119,7 +119,7 @@ describe('Binary Search Tree', () => {
119119
expect(lookUpFor5.parentNode.right).toBe(null);
120120
});
121121

122-
it('should delete node 8, with one child', () => {
122+
it("should delete node 8, with one child", () => {
123123
expect(bst.len).toBe(11);
124124

125125
const lookUpFor8 = bst.lookup(8);
@@ -133,7 +133,7 @@ describe('Binary Search Tree', () => {
133133
expect(lookUpFor8.parentNode.right.key).toBe(10);
134134
});
135135

136-
it('should delete node 19, with two children', () => {
136+
it("should delete node 19, with two children", () => {
137137
expect(bst.len).toBe(11);
138138

139139
const lookupFor19 = bst.lookup(19);
@@ -146,7 +146,10 @@ describe('Binary Search Tree', () => {
146146
const successor = bst.findMin(lookupFor19.currentNode.right);
147147
expect(successor.subtree.key).toBe(31);
148148

149-
const dir = lookupFor19.currentNode.key > lookupFor19.parentNode.key ? 'right' : 'left';
149+
const dir =
150+
lookupFor19.currentNode.key > lookupFor19.parentNode.key
151+
? "right"
152+
: "left";
150153

151154
bst.delete(19);
152155
expect(bst.len).toBe(10);
@@ -155,7 +158,7 @@ describe('Binary Search Tree', () => {
155158
});
156159
});
157160

158-
describe('Traversal Operation', () => {
161+
describe("Traversal Operation", () => {
159162
beforeEach(() => {
160163
bst.insert(11);
161164
bst.insert(6);
@@ -169,20 +172,68 @@ describe('Binary Search Tree', () => {
169172
bst.insert(31);
170173
bst.insert(49);
171174
});
172-
it('should return inorder list', () => {
173-
expect(bst.traverse('inOrder')).toEqual([4, 5, 6, 8, 10, 11, 17, 19, 31, 43, 49]);
174-
});
175-
176-
it('should return preorder list', () => {
177-
expect(bst.traverse('preOrder')).toEqual([11, 6, 4, 5, 8, 10, 19, 17, 43, 31, 49]);
178-
});
179-
180-
it('should return postorder list', () => {
181-
expect(bst.traverse('postOrder')).toEqual([5, 4, 10, 8, 6, 17, 31, 49, 43, 19, 11]);
182-
});
183-
184-
it('should return bfs list', () => {
185-
expect(bst.traverse('levelOrder')).toEqual([11, 6, 19, 4, 8, 17, 43, 5, 10, 31, 49]);
175+
it("should return inorder list", () => {
176+
expect(bst.traverse("inOrder")).toEqual([
177+
4,
178+
5,
179+
6,
180+
8,
181+
10,
182+
11,
183+
17,
184+
19,
185+
31,
186+
43,
187+
49
188+
]);
189+
});
190+
191+
it("should return preorder list", () => {
192+
expect(bst.traverse("preOrder")).toEqual([
193+
11,
194+
6,
195+
4,
196+
5,
197+
8,
198+
10,
199+
19,
200+
17,
201+
43,
202+
31,
203+
49
204+
]);
205+
});
206+
207+
it("should return postorder list", () => {
208+
expect(bst.traverse("postOrder")).toEqual([
209+
5,
210+
4,
211+
10,
212+
8,
213+
6,
214+
17,
215+
31,
216+
49,
217+
43,
218+
19,
219+
11
220+
]);
221+
});
222+
223+
it("should return bfs list", () => {
224+
expect(bst.traverse("levelOrder")).toEqual([
225+
11,
226+
6,
227+
19,
228+
4,
229+
8,
230+
17,
231+
43,
232+
5,
233+
10,
234+
31,
235+
49
236+
]);
186237
});
187238
});
188-
});
239+
});

0 commit comments

Comments
 (0)