Skip to content

Commit 86d333e

Browse files
defaudegithub-actionsappgurueu
authored
feat: Test running overhaul, switch to Prettier & reformat everything (TheAlgorithms#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see TheAlgorithms#1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
1 parent 0ca18c2 commit 86d333e

File tree

392 files changed

+6350
-17123
lines changed

Some content is hidden

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

392 files changed

+6350
-17123
lines changed

.github/workflows/Ci.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@ jobs:
1515

1616
- uses: actions/setup-node@v3
1717
with:
18-
node-version: 16
18+
node-version: 20
1919
cache: npm
2020

2121
- name: 📦 Install dependencies
2222
run: npm ci
2323

2424
- name: 🧪 Run all tests
25-
if: ${{ github.event_name == 'push' }}
2625
run: npm run test
2726

28-
- name: 🧪 Run tests for changed files only
29-
if: ${{ github.event_name == 'pull_request' }}
30-
run: npm run test-changed
31-
3227
- name: 💄 Code style
3328
run: npm run style
3429

.github/workflows/UpdateDirectory.mjs renamed to .github/workflows/UpdateDirectory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ globby([
6363
"!**/test/**/*",
6464
'!**/*.test.js',
6565
'!**/*.manual-test.js',
66-
'!babel.config.js'
66+
'!vitest.config.ts'
6767
])
6868
// create markdown content
6969
.then(pathsToMarkdown)

.github/workflows/UpdateDirectory.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v3
14+
1415
- uses: actions/setup-node@v3
1516
with:
16-
node-version: 16
17+
node-version: 20
1718
cache: npm
1819

1920
- name: 📦 Install dependencies
2021
run: npm ci
2122

2223
- name: 🗄️ Create Directory from JS files
23-
run: node .github/workflows/UpdateDirectory.mjs
24+
run: node .github/workflows/UpdateDirectory.js
2425

2526
- name: Configure Github Action
2627
run: |

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
. "$(dirname "$0")/_/husky.sh"
33

44
npm run style
5-
npm run test-changed
5+
npm run test

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.github
2+
DIRECTORY.md

Backtracking/AllCombinationsOfSizeK.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
*/
2323

2424
class Combinations {
25-
constructor (n, k) {
25+
constructor(n, k) {
2626
this.n = n
2727
this.k = k
2828
this.current = [] // will be used for storing current combination
2929
this.combinations = []
3030
}
3131

32-
findCombinations (high = this.n, total = this.k, low = 1) {
32+
findCombinations(high = this.n, total = this.k, low = 1) {
3333
if (total === 0) {
3434
this.combinations.push([...this.current])
3535
return this.combinations

Backtracking/GeneratePermutations.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
*/
1111

1212
const swap = (arr, i, j) => {
13-
const newArray = [...arr];
13+
const newArray = [...arr]
1414

15-
[newArray[i], newArray[j]] = [newArray[j], newArray[i]] // Swapping elements ES6 way
15+
;[newArray[i], newArray[j]] = [newArray[j], newArray[i]] // Swapping elements ES6 way
1616

1717
return newArray
1818
}
1919

20-
const permutations = arr => {
20+
const permutations = (arr) => {
2121
const P = []
2222
const permute = (arr, low, high) => {
2323
if (low === high) {

Backtracking/KnightTour.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Wikipedia: https://en.wikipedia.org/wiki/Knight%27s_tour
22

33
class OpenKnightTour {
4-
constructor (size) {
4+
constructor(size) {
55
this.board = new Array(size).fill(0).map(() => new Array(size).fill(0))
66
this.size = size
77
}
88

9-
getMoves ([i, j]) {
9+
getMoves([i, j]) {
1010
// helper function to get the valid moves of the knight from the current position
1111
const moves = [
1212
[i + 2, j - 1],
@@ -19,15 +19,17 @@ class OpenKnightTour {
1919
[i - 1, j + 2]
2020
]
2121

22-
return moves.filter(([y, x]) => y >= 0 && y < this.size && x >= 0 && x < this.size)
22+
return moves.filter(
23+
([y, x]) => y >= 0 && y < this.size && x >= 0 && x < this.size
24+
)
2325
}
2426

25-
isComplete () {
27+
isComplete() {
2628
// helper function to check if the board is complete
27-
return !this.board.map(row => row.includes(0)).includes(true)
29+
return !this.board.map((row) => row.includes(0)).includes(true)
2830
}
2931

30-
solve () {
32+
solve() {
3133
// function to find the solution for the given board
3234
for (let i = 0; i < this.size; i++) {
3335
for (let j = 0; j < this.size; j++) {
@@ -37,7 +39,7 @@ class OpenKnightTour {
3739
return false
3840
}
3941

40-
solveHelper ([i, j], curr) {
42+
solveHelper([i, j], curr) {
4143
// helper function for the main computation
4244
if (this.isComplete()) return true
4345

@@ -52,7 +54,7 @@ class OpenKnightTour {
5254
return false
5355
}
5456

55-
printBoard (output = value => console.log(value)) {
57+
printBoard(output = (value) => console.log(value)) {
5658
// utility function to display the board
5759
for (const row of this.board) {
5860
let string = ''

Backtracking/NQueens.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class NQueens {
2-
constructor (size) {
2+
constructor(size) {
33
if (size < 0) {
44
throw RangeError('Invalid board size')
55
}
@@ -8,7 +8,7 @@ class NQueens {
88
this.solutionCount = 0
99
}
1010

11-
isValid ([row, col]) {
11+
isValid([row, col]) {
1212
// function to check if the placement of the queen in the given location is valid
1313

1414
// checking the left of the current row
@@ -29,15 +29,15 @@ class NQueens {
2929
return true
3030
}
3131

32-
placeQueen (row, col) {
32+
placeQueen(row, col) {
3333
this.board[row][col] = 'Q'
3434
}
3535

36-
removeQueen (row, col) {
36+
removeQueen(row, col) {
3737
this.board[row][col] = '.'
3838
}
3939

40-
solve (col = 0) {
40+
solve(col = 0) {
4141
if (col >= this.size) {
4242
this.solutionCount++
4343
return true
@@ -54,7 +54,7 @@ class NQueens {
5454
return false
5555
}
5656

57-
printBoard (output = value => console.log(value)) {
57+
printBoard(output = (value) => console.log(value)) {
5858
if (!output._isMockFunction) {
5959
output('\n')
6060
}

Backtracking/RatInAMaze.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@
2121
* @param grid The grid to check.
2222
* @throws TypeError When the given grid is invalid.
2323
*/
24-
function validateGrid (grid) {
25-
if (!Array.isArray(grid) || grid.length === 0) throw new TypeError('Grid must be a non-empty array')
24+
function validateGrid(grid) {
25+
if (!Array.isArray(grid) || grid.length === 0)
26+
throw new TypeError('Grid must be a non-empty array')
2627

27-
const allRowsHaveCorrectLength = grid.every(row => row.length === grid.length)
28+
const allRowsHaveCorrectLength = grid.every(
29+
(row) => row.length === grid.length
30+
)
2831
if (!allRowsHaveCorrectLength) throw new TypeError('Grid must be a square')
2932

30-
const allCellsHaveValidValues = grid.every(row => {
31-
return row.every(cell => cell === 0 || cell === 1)
33+
const allCellsHaveValidValues = grid.every((row) => {
34+
return row.every((cell) => cell === 0 || cell === 1)
3235
})
33-
if (!allCellsHaveValidValues) throw new TypeError('Grid must only contain 0s and 1s')
36+
if (!allCellsHaveValidValues)
37+
throw new TypeError('Grid must only contain 0s and 1s')
3438
}
3539

36-
function isSafe (grid, x, y) {
40+
function isSafe(grid, x, y) {
3741
const n = grid.length
3842
return x >= 0 && x < n && y >= 0 && y < n && grid[y][x] === 1
3943
}
@@ -48,7 +52,7 @@ function isSafe (grid, x, y) {
4852
* @param path The path we took to get from the source cell to the current location.
4953
* @returns {string|boolean} Either the path to the target cell or false.
5054
*/
51-
function getPathPart (grid, x, y, solution, path) {
55+
function getPathPart(grid, x, y, solution, path) {
5256
const n = grid.length
5357

5458
// are we there yet?
@@ -89,7 +93,7 @@ function getPathPart (grid, x, y, solution, path) {
8993
return false
9094
}
9195

92-
function getPath (grid) {
96+
function getPath(grid) {
9397
// grid dimensions
9498
const n = grid.length
9599

@@ -108,7 +112,7 @@ function getPath (grid) {
108112
* Creates an instance of the "rat in a maze" based on a given grid (maze).
109113
*/
110114
export class RatInAMaze {
111-
constructor (grid) {
115+
constructor(grid) {
112116
// first, let's do some error checking on the input
113117
validateGrid(grid)
114118

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy