Skip to content

Commit

Permalink
Feat eslint rules (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ariskemper authored Nov 8, 2023
1 parent 2576f29 commit 2b793ba
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 82 deletions.
22 changes: 0 additions & 22 deletions .eslintrc

This file was deleted.

56 changes: 56 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable prettier/prettier */
module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'eslint-plugin-tsdoc', 'simple-import-sort', 'import'],
env: {
node: true,
es6: true
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'prettier',
'plugin:prettier/recommended'
],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
project: ['./tsconfig.json']
},
rules: {
// Type-checking rules
'@typescript-eslint/strict-boolean-expressions': 'error', // Avoid code that may be unsafe when converted to a boolean
'@typescript-eslint/no-floating-promises': 'error', // Disallow floating promises without error handling
'@typescript-eslint/no-misused-promises': 'error', // Disallow no misuses promises without error handling
'@typescript-eslint/restrict-template-expressions': 'warn', // Enforce template string expressions to be of string type
'@typescript-eslint/no-unsafe-return': 'error', // Disallow returning any from a function

// Best practices
'array-callback-return': 'error', // Enforce return statements in callbacks of array’s methods
'consistent-return': 'error', // Require return statements to either always or never specify values
'no-await-in-loop': 'error', // Disallow await inside of loops
'no-console': 'warn', // Warn on console.log statements in production code
'no-template-curly-in-string': 'error', // Disallow template literals placeholder syntax in regular strings
'no-return-await': 'off', // Disable base rule for no return await
'@typescript-eslint/return-await': 'error', // Allow return await
'@typescript-eslint/consistent-type-imports': 'error', // Enforce consistent usage of type imports.
'@typescript-eslint/consistent-type-exports': 'error', // Enforce consistent usage of type exports.

// Style
'camelcase': 'error',
'prettier/prettier': 'warn',
'semi': ['error', 'never'],
'quotes': [2, 'single'],

// ES6+
'prefer-const': 'error', // Suggest using const
'no-var': 'error', // Require let or const instead of var

// Import / exports sort
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',
'import/first': 'error',
'import/no-duplicates': 'error'
}
}
3 changes: 2 additions & 1 deletion src/benchmark/benchmark-sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ group(`Sort Nearly Sorted Array ${size} of integer numbers`, () => {
bench('treeSort', () => treeSort([...nearlySortedArray]))
})

run({
void run({
avg: true, // enable/disable avg column (default: true)
json: false, // enable/disable json output (default: false)
colors: true, // enable/disable colors (default: true)
// eslint-disable-next-line camelcase
min_max: true, // enable/disable min/max column (default: true)
collect: false, // enable/disable collecting returned values into an array during the benchmark (default: false)
percentiles: true // enable/disable percentiles column (default: true)
Expand Down
42 changes: 21 additions & 21 deletions src/benchmark/utils/array-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,64 @@ import {
} from './array-generator'
import { containsSomeValues } from './contain'
import { isInGivenRange } from './range'
describe('generateRandomizedIntegerArray', () => {
it('should generate array of given size', () => {
void describe('generateRandomizedIntegerArray', () => {
void it('should generate array of given size', () => {
const arr = generateRandomizedIntegerArray(10, { min: 1, max: 10 })

assert.equal(arr.length, 10)
})

it('should generate array with values in given range', () => {
void it('should generate array with values in given range', () => {
const min = 1
const max = 10
const arr = generateRandomizedIntegerArray(10, { min, max })

assert.equal(isInGivenRange(arr, min, max), true)
})

it('should throw an error for negative min range', () => {
void it('should throw an error for negative min range', () => {
assert.throws(
() => generateRandomizedIntegerArray(10, { min: 11, max: 10 }),
new Error('Min value cannot be greater than Max value')
)
})
})

describe('generateSortedIntegerArray', () => {
it('should generate a sorted array of a given size', () => {
void describe('generateSortedIntegerArray', () => {
void it('should generate a sorted array of a given size', () => {
const size = 5
const expectedResult = [0, 1, 2, 3, 4]

assert.deepStrictEqual(generateSortedIntegerArray(size), expectedResult)
})

it('should generate a sorted array of a given size with start and step', () => {
void it('should generate a sorted array of a given size with start and step', () => {
const size = 7
const expectedResult = [2, 4, 6, 8, 10, 12, 14]

assert.deepStrictEqual(generateSortedIntegerArray(size, 2, 2), expectedResult)
})

it('should generate an empty array when size is 0', () => {
void it('should generate an empty array when size is 0', () => {
assert.deepStrictEqual(generateSortedIntegerArray(0), [])
})

it('should throw an error for negative size', () => {
void it('should throw an error for negative size', () => {
assert.throws(() => generateSortedIntegerArray(-1), new Error('Size must be a non-negative integer'))
})
})

describe('generateDescSortedArray', () => {
it('should generate a sorted array of a given size in descending order', () => {
void describe('generateDescSortedArray', () => {
void it('should generate a sorted array of a given size in descending order', () => {
const size = 5
const expectedResult = [4, 3, 2, 1, 0]

assert.deepStrictEqual(generateDescSortedIntegerArray(size), expectedResult)
})
})

describe('generateArrayWithUniqueValues', () => {
it('should generate an array containing only unique values', () => {
void describe('generateArrayWithUniqueValues', () => {
void it('should generate an array containing only unique values', () => {
const size = 10
const uniqueValues = [4, 3, 2, 1, 0]

Expand All @@ -76,37 +76,37 @@ describe('generateArrayWithUniqueValues', () => {
assert.equal(containsSomeValues(unique, uniqueValues), true)
})

it('should throw an error for negative size', () => {
void it('should throw an error for negative size', () => {
assert.throws(
() => generateWithUniqueValuesIntegerArray(-1, [1, 2, 3, 4]),
new Error('Size must be a non-negative integer')
)
})

it('should throw an error, if unique value array is empty', () => {
void it('should throw an error, if unique value array is empty', () => {
assert.throws(() => generateWithUniqueValuesIntegerArray(2, []), new Error('Unique values array must not be empty'))
})
})

describe('generateDescSortedArray', () => {
it('should generate a sorted array of a given size in descending order', () => {
void describe('generateDescSortedArray', () => {
void it('should generate a sorted array of a given size in descending order', () => {
const size = 5
const expectedResult = [4, 3, 2, 1, 0]

assert.deepStrictEqual(generateDescSortedIntegerArray(size), expectedResult)
})
})

describe('generateNearlySortedArray', () => {
it('should generate of specified size', () => {
void describe('generateNearlySortedArray', () => {
void it('should generate of specified size', () => {
const size = 10
const swpCnt = 2
const result = generateNearlySortedIntegerArray(size, swpCnt)

assert.equal(result.length, size)
})

it('should perform the specified number of swaps', () => {
void it('should perform the specified number of swaps', () => {
const size = 100
const swpCnt = 20

Expand All @@ -123,7 +123,7 @@ describe('generateNearlySortedArray', () => {
assert.equal(actualSwapCount <= swpCnt, true)
})

it('should throw an error for negative size or invalid swap count', () => {
void it('should throw an error for negative size or invalid swap count', () => {
assert.throws(
() => generateNearlySortedIntegerArray(-1, 10),
new Error('Size and swap count must be non-negative integers')
Expand Down
4 changes: 2 additions & 2 deletions src/benchmark/utils/range.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { describe, it } from 'node:test'

import { isInGivenRange } from './range'

describe('isInGivenRange function', () => {
it('should verify if all elements in the array are within the given range', () => {
void describe('isInGivenRange function', () => {
void it('should verify if all elements in the array are within the given range', () => {
const arr = [2, 4, 6, 8]
const min = 1
const max = 10
Expand Down
2 changes: 1 addition & 1 deletion src/sort/counting/counting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function countingSort(arr: number[]): number[] {

const { min, max } = getMinMaxValue(arr)

const countArr = new Array(max - min + 1).fill(0)
const countArr = new Array<number>(max - min + 1).fill(0)

arr.forEach((value) => {
countArr[value - min]++
Expand Down
4 changes: 2 additions & 2 deletions src/sort/cube/cube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function cubeSort(arr: number[]): number[] {
const range = maxValue - minValue + 1

// Initialize the count array with zeros
const count: number[] = new Array(range).fill(0)
const count = new Array<number>(range).fill(0)

// Count the occurrences of each element
for (let i = 0; i < n; i++) {
Expand All @@ -47,7 +47,7 @@ export function cubeSort(arr: number[]): number[] {
}

// Create the sorted result array
const result: number[] = new Array(n)
const result = new Array(n) as number[]
let resultIndex = 0

for (let i = 0; i < range; i++) {
Expand Down
57 changes: 38 additions & 19 deletions src/sort/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
import { bitonicSort, BitonicSortFn } from './bitonic/bitonic'
import { bogoSort, BogoSortFn } from './bogo/bogo'
import { bubbleSort, BubbleSortFn } from './bubble/bubble'
import { bucketSort, BucketSortFn } from './bucket/bucket'
import { cocktailShakerSort, CocktailShakerSortFn } from './cocktail-shaker/cocktail-shaker'
import { comboSort, ComboSortFn } from './combo/combo'
import { countingSort, CountingSortFn } from './counting/counting'
import { cubeSort, CubeSortFn } from './cube/cube'
import { cycleSort, CycleSortFn } from './cycle/cycle'
import { gnomeSort, GnomeSortFn } from './gnome/gnome'
import { heapSort, HeapSortFn } from './heap/heap'
import { insertionSort, InsertionSortFn } from './insertion/insertion'
import { introSort, IntroSortFn } from './intro/intro'
import { mergeSort, MergeSortFn } from './merge/merge'
import { radixSort, RadixSortFn } from './radix/radix'
import { randomQuickSort, RandomQuickSortFn } from './random-quick/random-quick'
import { selectionSort, SelectionSortFn } from './selection/selection'
import { shellSort, ShellSortFn } from './shell/shell'
import { treeSort, TreeSortFn } from './tree/tree'
import type { BitonicSortFn } from './bitonic/bitonic'
import { bitonicSort } from './bitonic/bitonic'
import type { BogoSortFn } from './bogo/bogo'
import { bogoSort } from './bogo/bogo'
import type { BubbleSortFn } from './bubble/bubble'
import { bubbleSort } from './bubble/bubble'
import type { BucketSortFn } from './bucket/bucket'
import { bucketSort } from './bucket/bucket'
import type { CocktailShakerSortFn } from './cocktail-shaker/cocktail-shaker'
import { cocktailShakerSort } from './cocktail-shaker/cocktail-shaker'
import type { ComboSortFn } from './combo/combo'
import { comboSort } from './combo/combo'
import type { CountingSortFn } from './counting/counting'
import { countingSort } from './counting/counting'
import type { CubeSortFn } from './cube/cube'
import { cubeSort } from './cube/cube'
import type { CycleSortFn } from './cycle/cycle'
import { cycleSort } from './cycle/cycle'
import type { GnomeSortFn } from './gnome/gnome'
import { gnomeSort } from './gnome/gnome'
import type { HeapSortFn } from './heap/heap'
import { heapSort } from './heap/heap'
import type { InsertionSortFn } from './insertion/insertion'
import { insertionSort } from './insertion/insertion'
import type { IntroSortFn } from './intro/intro'
import { introSort } from './intro/intro'
import type { MergeSortFn } from './merge/merge'
import { mergeSort } from './merge/merge'
import type { RadixSortFn } from './radix/radix'
import { radixSort } from './radix/radix'
import type { RandomQuickSortFn } from './random-quick/random-quick'
import { randomQuickSort } from './random-quick/random-quick'
import type { SelectionSortFn } from './selection/selection'
import { selectionSort } from './selection/selection'
import type { ShellSortFn } from './shell/shell'
import { shellSort } from './shell/shell'
import type { TreeSortFn } from './tree/tree'
import { treeSort } from './tree/tree'

export type SortFn =
| BubbleSortFn
Expand Down
4 changes: 2 additions & 2 deletions src/sort/radix/radix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function radixSort(arr: number[]): number[] {
}

function countingSortForRadix(arr: number[], position: number): number[] {
const count: number[] = Array(10).fill(0)
const count: number[] = Array<number>(10).fill(0)

arr.forEach((num) => {
const digit = getDigit(num, position)
Expand All @@ -50,7 +50,7 @@ function countingSortForRadix(arr: number[], position: number): number[] {
count[i] += count[i - 1]
}

const output = new Array(arr.length)
const output = new Array<number>(arr.length)
arr
.slice()
.reverse()
Expand Down
4 changes: 2 additions & 2 deletions src/utils/compare.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { describe, it } from 'node:test'

import { compare } from './compare'

describe('compare', () => {
it('should thrown an error', () => {
void void describe('compare', () => {
void void it('should thrown an error', () => {
assert.throws(() => compare<number | string>(1, 'banana'), new Error('Incompatible types for comparison'))
})
})
Loading

0 comments on commit 2b793ba

Please sign in to comment.