Skip to content

Commit a7be3a0

Browse files
authored
add eslint (thuva4#883)
Co-authored-by: Thuvarakan Tharmarajasingam <thuva4.dev@gmail.com>
1 parent 9fa2625 commit a7be3a0

File tree

52 files changed

+1947
-891
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1947
-891
lines changed

algorithms/JavaScript/.eslintrc.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"commonjs": true,
5+
"es2021": true
6+
},
7+
"extends": [
8+
"google"
9+
],
10+
"parserOptions": {
11+
"ecmaVersion": 12
12+
},
13+
"rules": {
14+
}
15+
}

algorithms/JavaScript/BinarySearch/__test__/index.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
const binarySearch = require('../index');
22

3-
describe("BinarySearch", () => {
3+
describe('BinarySearch', () => {
44
let array = null;
55

66
beforeEach(() => {
7-
array = [1,2,3,4,5,6,7,8,9,10]
7+
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
88
});
99

10-
it("should return -1 if element is not in the array", () => {
11-
expect(binarySearch(array, 11)).toEqual(-1)
10+
it('should return -1 if element is not in the array', () => {
11+
expect(binarySearch(array, 11)).toEqual(-1);
1212
});
1313

14-
it("should return index of the element if element is in the array", () => {
14+
it('should return index of the element if element is in the array', () => {
1515
expect(binarySearch(array, 6)).toEqual(5);
1616
expect(binarySearch(array, 9)).toEqual(8);
1717
expect(binarySearch(array, 2)).toEqual(1);
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
/**
2-
* Search a value into a sorted array by repeatedly dividing the search interval in half.
2+
* Search a value into a sorted array by repeatedly
3+
* dividing the search interval in half.
34
* @param {Array} arr Array to search into
45
* @param {Number} k Value to search
56
* @return {Number} index of found item or -1 for not found
67
*/
78
const binarySearch = (arr, k) => {
8-
let min = 0
9-
let max = arr.length - 1
10-
9+
let min = 0;
10+
let max = arr.length - 1;
11+
1112
while (min <= max) {
12-
const cur = Math.floor((min + max) / 2)
13-
if (arr[cur] === k) { return cur }
14-
(arr[cur] > k)
15-
? max = cur - 1
16-
: min = cur + 1
13+
const cur = Math.floor((min + max) / 2);
14+
if (arr[cur] === k) {
15+
return cur;
16+
}
17+
(arr[cur] > k) ?
18+
max = cur - 1 :
19+
min = cur + 1;
1720
}
18-
return -1
19-
}
21+
return -1;
22+
};
2023

21-
module.exports = binarySearch;
24+
module.exports = binarySearch;
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
1-
const {GraphFactory, BFS} = require('../index');
1+
const {GraphFactory, bfs} = require('../index');
22

3-
describe("BreadthFirstSearch", () => {
3+
describe('BreadthFirstSearch', () => {
44
let graph = null;
55

66
beforeEach(() => {
77
graph = GraphFactory.getGraph();
88
});
99

10-
it("should throw error on bad graph", () => {
10+
it('should throw error on bad graph', () => {
1111
expect(() => {
12-
BFS({});
13-
}).toThrow("Graph should implement a getNeighbors function");
12+
bfs({});
13+
}).toThrow('Graph should implement a getNeighbors function');
1414
});
1515

16-
it("should throw error on no source vertex", () => {
16+
it('should throw error on no source vertex', () => {
1717
expect(() => {
18-
BFS(graph);
19-
}).toThrow("source should be a number");
18+
bfs(graph);
19+
}).toThrow('source should be a number');
2020
});
2121

22-
it("simple bi-directional graph where target is reachable", () => {
22+
it('simple bi-directional graph where target is reachable', () => {
2323
graph.addEdge(0, 1);
2424
graph.addEdge(0, 3);
2525
graph.addEdge(1, 2);
26-
expect(BFS(graph, 0, 3)).toEqual({
26+
expect(bfs(graph, 0, 3)).toEqual({
2727
order: [0, 1, 3, 2],
28-
found: true
28+
found: true,
2929
});
3030
});
3131

32-
it("complex bi-directional graph where target is reachable", () => {
32+
it('complex bi-directional graph where target is reachable', () => {
3333
graph.addEdge(0, 1);
3434
graph.addEdge(0, 2);
3535
graph.addEdge(1, 3);
3636
graph.addEdge(3, 4);
3737
graph.addEdge(4, 2);
38-
expect(BFS(graph, 0, 3)).toEqual({
38+
expect(bfs(graph, 0, 3)).toEqual({
3939
order: [0, 1, 2, 3, 4],
40-
found: true
40+
found: true,
4141
});
4242
});
43-
44-
it("complex uni-directional graph where target is reachable", () => {
43+
44+
it('complex uni-directional graph where target is reachable', () => {
4545
graph.addEdge(0, 1, false);
4646
graph.addEdge(0, 2, false);
4747
graph.addEdge(1, 3, false);
4848
graph.addEdge(3, 4, false);
4949
graph.addEdge(3, 5, false);
5050
graph.addEdge(4, 2, false);
51-
expect(BFS(graph, 0, 3)).toEqual({
51+
expect(bfs(graph, 0, 3)).toEqual({
5252
order: [0, 1, 2, 3, 4, 5],
53-
found: true
53+
found: true,
5454
});
5555
});
5656

57-
it("simple bi-directional graph where target is not present", () => {
57+
it('simple bi-directional graph where target is not present', () => {
5858
graph.addEdge(0, 1);
5959
graph.addEdge(0, 3);
6060
graph.addEdge(1, 2);
61-
expect(BFS(graph, 0, 5)).toEqual({
61+
expect(bfs(graph, 0, 5)).toEqual({
6262
order: [0, 1, 3, 2],
63-
found: false
63+
found: false,
6464
});
6565
});
6666
});

algorithms/JavaScript/BreadthFirstSearch/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const BFS = (graph, source, target = -1) => {
1+
const bfs = (graph, source, target = -1) => {
22
// Some error handling
3-
if (typeof graph.getNeighbors !== "function") {
4-
throw "Graph should implement a getNeighbors function";
3+
if (typeof graph.getNeighbors !== 'function') {
4+
throw new Error('Graph should implement a getNeighbors function');
55
}
6-
if (typeof source !== "number") {
7-
throw "source should be a number";
6+
if (typeof source !== 'number') {
7+
throw new Error('source should be a number');
88
}
9-
10-
const Q = [], // The queue that will be used
11-
order = [], // Array to hold the order of visit. Mainly for unit testing
12-
visited = {}; // Keep track of visited vertices
13-
9+
10+
const Q = []; // The queue that will be used
11+
const order = []; // Array to hold the order of visit. Mainly for unit testing
12+
const visited = {}; // Keep track of visited vertices
13+
1414
let found = false;
1515
Q.push(source);
1616
visited[source] = true;
@@ -28,7 +28,7 @@ const BFS = (graph, source, target = -1) => {
2828
}
2929
}
3030
}
31-
return { order, found };
31+
return {order, found};
3232
};
3333

3434
const GraphFactory = (() => {
@@ -51,16 +51,16 @@ const GraphFactory = (() => {
5151
},
5252
printGraph() {
5353
console.log(JSON.stringify(this._graph, null, 2));
54-
}
54+
},
5555
};
56-
56+
5757
return {
5858
getGraph() {
5959
const Graph = Object.assign({}, GraphTemplate);
6060
Graph.init();
6161
return Graph;
62-
}
62+
},
6363
};
6464
})();
6565

66-
module.exports = { GraphFactory, BFS };
66+
module.exports = {GraphFactory, bfs};
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
const bubbleSort = require('../index');
22

3-
describe("BubbleSort", () => {
3+
describe('BubbleSort', () => {
44
let array = null;
55

6-
it("sort given distinct element array", () => {
7-
array = [10,9,8,7,6,5,4,3,2,1]
8-
expect(bubbleSort(array)).toEqual([1,2,3,4,5,6,7,8,9,10]);
6+
it('sort given distinct element array', () => {
7+
array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
8+
expect(bubbleSort(array)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
99
});
1010

11-
it("sort array repeated elements", () => {
12-
array = [10,9,8,7,7,5,5,3,2,1]
13-
expect(bubbleSort(array)).toEqual([1,2,3,5,5,7,7,8,9,10]);
11+
it('sort array repeated elements', () => {
12+
array = [10, 9, 8, 7, 7, 5, 5, 3, 2, 1];
13+
expect(bubbleSort(array)).toEqual([1, 2, 3, 5, 5, 7, 7, 8, 9, 10]);
1414
});
1515
});

algorithms/JavaScript/BubbleSort/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
/* eslint-disable require-jsdoc */
12
/**
23
* Sort array in O(n^2)
3-
* Bubble Sort will be faster for small number of elements
4+
* Bubble Sort will be faster for small number of elements
45
* In-place sort without extra space
5-
* @param {Array} arr Array to search into
6+
* @param {Array} array Array to search into
67
* @return {Array} Sorted array
78
*/
89
function bubbleSort(array) {
@@ -23,4 +24,4 @@ function swap(array, firstIndex, secondIndex) {
2324
}
2425

2526

26-
module.exports = bubbleSort;
27+
module.exports = bubbleSort;
Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
function counting_sort(array) {
2-
let high = highest_element(array);
3-
let aux_array = new Array(high-1);
4-
let final_array = new Array(array.length);
5-
for(let i = 0; i < aux_array.length; i++) {
6-
aux_array[i] = 0;
7-
}
8-
for(let j = 0; j < aux_array.length; j++) {
9-
aux_array[array[j]] = aux_array[array[j]-1] + 1;
10-
}
11-
for(let i = 1; i<high; i++) {
12-
aux_array[i] = aux_array[i] + aux_array[i-1];
13-
}
14-
for(let j = array.length; j>0; j-- ) {
15-
final_array[aux_array[array[j]-1]-1] = array[j];
16-
aux_array[array[j]]--;
17-
}
1+
/* eslint-disable require-jsdoc */
2+
function countingSort(array) {
3+
const high = highestElement(array);
4+
const auxArray = new Array(high-1);
5+
const finalArray = new Array(array.length);
6+
for (let i = 0; i < auxArray.length; i++) {
7+
auxArray[i] = 0;
8+
}
9+
for (let j = 0; j < auxArray.length; j++) {
10+
auxArray[array[j]] = auxArray[array[j]-1] + 1;
11+
}
12+
for (let i = 1; i<high; i++) {
13+
auxArray[i] = auxArray[i] + auxArray[i-1];
14+
}
15+
for (let j = array.length; j>0; j-- ) {
16+
finalArray[auxArray[array[j]-1]-1] = array[j];
17+
auxArray[array[j]]--;
18+
}
1819
}
1920

20-
function highest_element(array) {
21-
let high = 0;
22-
for(let i in array) {
23-
if(array[i] > high)
24-
high = array[i];
25-
}
26-
return high;
27-
}
21+
function highestElement(array) {
22+
let high = 0;
23+
for (const i in array) {
24+
if (array[i] > high) {
25+
high = array[i];
26+
}
27+
}
28+
return high;
29+
}
30+
31+
module.exports = {countingSort};

0 commit comments

Comments
 (0)