Skip to content

Commit 976ef33

Browse files
committed
up: eslint fixes, prettier integration and LF EOL
1 parent 08f7893 commit 976ef33

File tree

27 files changed

+202
-153
lines changed

27 files changed

+202
-153
lines changed

β€Ž.editorconfig

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ root = true
33
[*]
44
indent_style = space
55
indent_size = 2
6-
76
end_of_line = lf
87
charset = utf-8
98
trim_trailing_whitespace = true

β€Ž.eslintrc.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
{
2+
"env": {
3+
"node": true,
4+
"jest": true
5+
},
26
"extends": ["airbnb", "prettier"],
37
"plugins": ["prettier"],
48
"rules": {
5-
"prettier/prettier": ["error"]
9+
"prettier/prettier": ["error"],
10+
"no-underscore-dangle": "off",
11+
"no-param-reassign": "off",
12+
"no-console": "warn",
13+
"consistent-return": "warn",
14+
"max-classes-per-file": "off",
15+
"no-bitwise": "warn",
16+
"no-restricted-syntax": "warn",
17+
"no-continue": "warn"
618
}
719
}

β€Ž.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text eol=crlf

β€Ž.prettierrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ module.exports = {
55
printWidth: 120,
66
tabWidth: 2,
77
arrowParens: 'always',
8+
endOfLine: 'lf',
89
};

β€Žsrc/_Algorithms_/lru-cache/LRUCache.test.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ describe('Algorithms: LRU Cache', () => {
2828
lruCache.set('key1', 'value1');
2929
lruCache.set('key2', 'value2');
3030

31-
expect(lruCache.list.head.next.data['key']).toEqual('key2');
32-
expect(lruCache.list.head.next.data['value']).toEqual('value2');
31+
expect(lruCache.list.head.next.data.key).toEqual('key2');
32+
expect(lruCache.list.head.next.data.value).toEqual('value2');
3333

3434
// The least recently used key is moved at the beginning of the list
3535
lruCache.get('key1');
36-
expect(lruCache.list.head.next.data['key']).toEqual('key1');
37-
expect(lruCache.list.head.next.data['value']).toEqual('value1');
36+
expect(lruCache.list.head.next.data.key).toEqual('key1');
37+
expect(lruCache.list.head.next.data.value).toEqual('value1');
3838
});
3939
});
4040

4141
describe('set(key, value)', () => {
4242
it('Should append each <key:value> pair to the beginning of list', () => {
4343
lruCache.set('foo', 'bar');
44-
expect(lruCache.list.head.next.data['key']).toEqual('foo');
45-
expect(lruCache.list.head.next.data['value']).toEqual('bar');
44+
expect(lruCache.list.head.next.data.key).toEqual('foo');
45+
expect(lruCache.list.head.next.data.value).toEqual('bar');
4646
});
4747

4848
it('Should update value if key already exists', () => {
@@ -59,14 +59,14 @@ describe('Algorithms: LRU Cache', () => {
5959
lruCache.set('key1', 'value1');
6060

6161
expect(lruCache.list.length()).toEqual(lruCache.size);
62-
expect(lruCache.list.head.next.data['key']).toEqual('key1');
63-
expect(lruCache.list.head.next.data['value']).toEqual('value1');
64-
expect(lruCache.list.head.next.next.data['key']).toEqual('key2');
65-
expect(lruCache.list.head.next.next.data['value']).toEqual('value2');
66-
expect(lruCache.list.head.next.next.next.data['key']).toEqual('key3');
67-
expect(lruCache.list.head.next.next.next.data['value']).toEqual('value3');
68-
expect(lruCache.list.head.next.next.next.next.data['key']).toEqual('key4');
69-
expect(lruCache.list.head.next.next.next.next.data['value']).toEqual('value4');
62+
expect(lruCache.list.head.next.data.key).toEqual('key1');
63+
expect(lruCache.list.head.next.data.value).toEqual('value1');
64+
expect(lruCache.list.head.next.next.data.key).toEqual('key2');
65+
expect(lruCache.list.head.next.next.data.value).toEqual('value2');
66+
expect(lruCache.list.head.next.next.next.data.key).toEqual('key3');
67+
expect(lruCache.list.head.next.next.next.data.value).toEqual('value3');
68+
expect(lruCache.list.head.next.next.next.next.data.key).toEqual('key4');
69+
expect(lruCache.list.head.next.next.next.next.data.value).toEqual('value4');
7070
});
7171
});
7272
});

β€Žsrc/_Algorithms_/path-finder/a-star/index.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function AStar(s, e, row, col, inputGrid) {
1010
const start = s;
1111
const end = e;
1212

13-
function cell() {
13+
function Cell() {
1414
this.cellValue = null;
1515
this.parent_i = -1;
1616
this.parent_j = -1;
@@ -19,7 +19,7 @@ function AStar(s, e, row, col, inputGrid) {
1919
this.f = Number.MAX_SAFE_INTEGER;
2020
}
2121

22-
function pair(i, j, f) {
22+
function Pair(i, j, f) {
2323
this.i = i;
2424
this.j = j;
2525
this.f = f;
@@ -30,15 +30,13 @@ function AStar(s, e, row, col, inputGrid) {
3030
for (let i = 0; i < Row; i += 1) {
3131
grid[i] = [];
3232
for (let j = 0; j < Col; j += 1) {
33-
grid[i][j] = new cell();
33+
grid[i][j] = new Cell();
3434
grid[i][j].cellValue = inputGrid[i][j];
3535
}
3636
}
3737

3838
const isValid = (i, j) => i >= 0 && j >= 0 && i < Row && j < Col;
39-
4039
const isDestination = (i, j) => end.i === i && end.j === j;
41-
4240
const isBlocked = (i, j) => grid[i][j].cellValue === 0;
4341

4442
const euclideanDistance = (i, j) =>
@@ -64,7 +62,7 @@ function AStar(s, e, row, col, inputGrid) {
6462
}
6563
path.push([i, j]);
6664

67-
for (let i = 0; i < path.length; i += 1) {
65+
for (let z = 0; z < path.length; z += 1) {
6866
console.log(path[i]);
6967
}
7068
};
@@ -101,7 +99,7 @@ function AStar(s, e, row, col, inputGrid) {
10199
grid[i][j].h = h;
102100
grid[i][j].f = g + h;
103101

104-
const item = new pair(i, j, f);
102+
const item = new Pair(i, j, f);
105103
// can be improved by using Min-Heap DataStructure
106104
if (!openList.length) {
107105
openList.push(item);
@@ -123,8 +121,8 @@ function AStar(s, e, row, col, inputGrid) {
123121
return false;
124122
}
125123

126-
let i = start.i;
127-
let j = start.j;
124+
let { i } = start;
125+
let { j } = start;
128126
const openList = [];
129127
const openListMap = new Map();
130128
const closedListMap = new Map();
@@ -135,7 +133,7 @@ function AStar(s, e, row, col, inputGrid) {
135133
grid[i][j].g = 0;
136134
grid[i][j].f = 0;
137135

138-
openList.push(new pair(i, j, 0.0));
136+
openList.push(new Pair(i, j, 0.0));
139137

140138
openListMap[[i, j]] = 0;
141139

β€Žsrc/_Classics_/caeser_cipher/index.js

+39-31
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,52 @@ function caesarCipher(str, num) {
1818
const alphabetsMap = new Map();
1919

2020
for (const index in alphabets) {
21-
alphabetsMap[alphabets[index]] = index;
21+
if (index) {
22+
alphabetsMap[alphabets[index]] = index;
23+
}
2224
}
2325

24-
for (let index in lowerCaseString) {
25-
// get the current character
26-
const currentCharacter = lowerCaseString[index];
26+
for (const index in lowerCaseString) {
27+
if (index) {
28+
// get the current character
29+
const currentCharacter = lowerCaseString[index];
2730

28-
// if character is space, add it to the result and continue to next
29-
if (currentCharacter === ' ') {
30-
result += currentCharacter;
31-
continue;
32-
}
31+
// if character is space, add it to the result and continue to next
32+
if (currentCharacter === ' ') {
33+
result += currentCharacter;
34+
continue;
35+
}
3336

34-
// determine the new index
35-
/**
36-
* const currentIndex = alphabets.indexOf(currentCharacter);
37-
*
38-
* With indexOf complexity will be O(n*26)
39-
* With Map complexity will be O(n).
40-
*/
41-
const currentIndex = Number(alphabetsMap[currentCharacter]);
42-
let newIndex = currentIndex + num;
43-
44-
// if the index passes 25, restart from 0
45-
if (newIndex > totalAlphabets - 1) {
46-
newIndex -= totalAlphabets;
47-
}
37+
// determine the new index
38+
/**
39+
* const currentIndex = alphabets.indexOf(currentCharacter);
40+
*
41+
* With indexOf complexity will be O(n*26)
42+
* With Map complexity will be O(n).
43+
*/
44+
const currentIndex = Number(alphabetsMap[currentCharacter]);
45+
let newIndex = currentIndex + num;
4846

49-
if (newIndex < 0) {
50-
newIndex = totalAlphabets + newIndex;
51-
}
47+
// if the index passes 25, restart from 0
48+
if (newIndex > totalAlphabets - 1) {
49+
newIndex -= totalAlphabets;
50+
}
51+
52+
if (newIndex < 0) {
53+
newIndex = totalAlphabets + newIndex;
54+
}
5255

53-
// check if the character in original string was upper case
54-
if (str[index] === str[index].toUpperCase()) {
55-
result += alphabets[newIndex].toUpperCase();
56-
} else {
57-
result += alphabets[newIndex];
56+
// check if the character in original string was upper case
57+
if (str[index] === str[index].toUpperCase()) {
58+
result += alphabets[newIndex].toUpperCase();
59+
} else {
60+
result += alphabets[newIndex];
61+
}
5862
}
5963
}
6064
return result;
6165
}
66+
67+
module.exports = {
68+
caesarCipher,
69+
};

β€Žsrc/_Classics_/fibonacci/index.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
//The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
1+
// The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
22
// the algorithm has time complexity of O(n^2), very bad!
33
function fibonacci(position) {
44
// if position is 1 or 2, the number in fibonacci sequence will be 1
55
if (position === 1 || position === 0) {
66
return position;
7-
} else if (position < 0) {
7+
}
8+
if (position < 0) {
89
throw new Error('Invalid Position');
910
}
1011

@@ -27,15 +28,16 @@ function fibonacciMemoized(index, cache) {
2728

2829
if (cache[index]) {
2930
return cache[index];
31+
}
32+
if (index === 1 || index === 0) {
33+
return index;
34+
}
35+
if (index < 0) {
36+
throw new Error('Invalid Position');
3037
} else {
31-
if (index === 1 || index === 0) {
32-
return index;
33-
} else if (index < 0) {
34-
throw new Error('Invalid Position');
35-
} else {
36-
cache[index] = fibonacciMemoized(index - 1, cache) + fibonacciMemoized(index - 2, cache);
37-
}
38+
cache[index] = fibonacciMemoized(index - 1, cache) + fibonacciMemoized(index - 2, cache);
3839
}
40+
3941
return cache[index];
4042
}
4143

@@ -47,7 +49,8 @@ function fibonacciTabular(n) {
4749
const table = [0, 1];
4850
if (n === 1 || n === 0) {
4951
return n;
50-
} else if (n < 0) {
52+
}
53+
if (n < 0) {
5154
throw new Error('Invalid Position');
5255
}
5356
for (let i = 2; i <= n; i += 1) {
@@ -63,3 +66,9 @@ function fibonacciTabular(n) {
6366
// console.log('--');
6467
// console.log(`Fib memo - ${fibonacciMemoized(number)}`);
6568
// console.log(`Fib table - ${fibonacciTabular(number)}`);
69+
70+
module.exports = {
71+
fibonacci,
72+
fibonacciMemoized,
73+
fibonacciTabular,
74+
};

β€Žsrc/_DataStructures_/LinkedList/reverse-linked-list/reverse-linked-list.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Reverse a LinkedList', () => {
1818
});
1919

2020
it('Should return `5`->`4`->`3`->`2`->`1` for the given list', () => {
21-
let reversedList = reverseLinkedList(list);
21+
const reversedList = reverseLinkedList(list);
2222
expect(reversedList.data).toEqual('5');
2323
expect(reversedList.next.data).toEqual('4');
2424
expect(reversedList.next.next.data).toEqual('3');
@@ -29,7 +29,7 @@ describe('Reverse a LinkedList', () => {
2929
it('Should return `3`->`2`->`1` after deleting 2 last nodes of the list', () => {
3030
list.removeFromEnd();
3131
list.removeFromEnd();
32-
let reversedList2 = reverseLinkedList(list);
32+
const reversedList2 = reverseLinkedList(list);
3333
expect(reversedList2.data).toEqual('3');
3434
expect(reversedList2.next.data).toEqual('2');
3535
expect(reversedList2.next.next.data).toEqual('1');

β€Žsrc/_DataStructures_/Stack/balanced-parenthesis/index.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,35 @@ Output: false
1313
const Stack = require('../index');
1414

1515
function checkBalancedParenthesis(expression) {
16-
let s = new Stack();
17-
for (let i = 0; i < expression.length; i++) {
16+
const s = new Stack();
17+
for (let i = 0; i < expression.length; i += 1) {
1818
const char = expression[i];
1919
if (char === '{' || char === '(' || char === '[') {
20-
//If current character is a starting bracket (β€˜(β€˜ or β€˜{β€˜ or β€˜[β€˜) then push it to stack
20+
// If current character is a starting bracket (β€˜(β€˜ or β€˜{β€˜ or β€˜[β€˜) then push it to stack
2121
s.push(char);
2222
} else {
2323
if (s.isEmpty()) {
24-
//when we have only right parenthesis or brackets in expresion
24+
// when we have only right parenthesis or brackets in expresion
2525
return false;
26-
} else if (
26+
}
27+
if (
2728
(char === '}' && s.peek() !== '{') ||
2829
(char === ')' && s.peek() !== '(') ||
2930
(char === ']' && s.peek() !== '[')
3031
) {
3132
return false;
3233
}
33-
//If the current character is a closing bracket (β€˜)’ or β€˜}’ or β€˜]’) then pop it from stack
34+
// If the current character is a closing bracket (β€˜)’ or β€˜}’ or β€˜]’) then pop it from stack
3435
s.pop();
3536
}
3637
}
3738
if (s.isEmpty()) {
38-
//expression has balanced parenthesis
39+
// expression has balanced parenthesis
3940
return true;
40-
} else {
41-
return false;
4241
}
42+
return false;
4343
}
4444

45-
console.log(checkBalancedParenthesis('{()}[]')); //true
46-
console.log(checkBalancedParenthesis('{()}}')); //false
45+
module.exports = {
46+
checkBalancedParenthesis,
47+
};

β€Žsrc/_DataStructures_/Stack/baseball-game/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,7 @@ The size of the input list will be between 1 and 1000.
100100
Every integer represented in the list will be between -30000 and 30000.
101101
102102
*/
103+
104+
module.exports = {
105+
sumOfPoints,
106+
};

0 commit comments

Comments
Β (0)