diff --git a/.eslintrc.js b/.eslintrc.js index 8a44df5..3b46646 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,9 +1,14 @@ module.exports = { + "parser": "@typescript-eslint/parser", + "plugins": [ + "import", + "@typescript-eslint" + ], "extends": [ "plugin:@typescript-eslint/recommended", "prettier" ], - ignorePatterns: ["lib/", "dist/", "umd/", "coverage/", "docs/"], + "ignorePatterns": ["lib/", "dist/", "umd/", "coverage/", "docs/"], "rules": { "import/no-anonymous-default-export": "off", "@typescript-eslint/no-unused-vars": "error", @@ -39,9 +44,6 @@ module.exports = { } ] }, - "plugins": [ - "import" - ], "settings": { "import/parsers": { "@typescript-eslint/parser": [ diff --git a/package.json b/package.json index 292e221..f40e5f1 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "build:commonjs": "rm -rf dist && tsc --project tsconfig.prod.json", "build:umd": "webpack", "build:docs": "typedoc --out docs ./src", + "check": "tsc --noEmit", "lint:src": "eslint --fix 'src/**/*.{js,ts}'", "lint:test": "eslint --fix 'test/**/*.{js,ts}'", "lint": "npm run lint:src && npm run lint:test", diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 4f1e942..fef5769 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -13,7 +13,6 @@ import type { BinaryTreeNodeProperty, BinaryTreeOptions } from '../../types'; -import {IBinaryTree} from '../../interfaces'; import { BinaryTreeDeletedResult, BinaryTreeNodePropertyName, @@ -22,6 +21,7 @@ import { LoopType, NodeOrPropertyName } from '../../types'; +import {IBinaryTree} from '../../interfaces'; import {trampoline} from '../../utils'; export class BinaryTreeNode = BinaryTreeNodeNested> { @@ -574,7 +574,7 @@ export class BinaryTree = BinaryTreeNode> /** * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if * no node is specified. - * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree + * generic type representing a node in a binary tree, `BinaryTreeNodeKey` (a type representing the ID of a binary tree * node), or `null`. * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index c6e0afd..4205333 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -5,13 +5,14 @@ * @license MIT License */ -import type {HeapComparator, HeapDFSOrderPattern} from '../../types'; +import type {Comparator} from '../../types'; +import {DFSOrderPattern} from '../../types'; export class Heap { protected nodes: E[] = []; - private readonly comparator: HeapComparator; + private readonly comparator: Comparator; - constructor(comparator: HeapComparator) { + constructor(comparator: Comparator) { this.comparator = comparator; } @@ -158,7 +159,7 @@ export class Heap { * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order). * @returns An array containing elements traversed in the specified order. */ - dfs(order: HeapDFSOrderPattern): E[] { + dfs(order: DFSOrderPattern): E[] { const result: E[] = []; // Auxiliary recursive function, traverses the binary heap according to the traversal order @@ -227,7 +228,7 @@ export class Heap { * @param comparator - Comparison function. * @returns A new Heap instance. */ - static heapify(nodes: E[], comparator: HeapComparator): Heap { + static heapify(nodes: E[], comparator: Comparator): Heap { const binaryHeap = new Heap(comparator); binaryHeap.nodes = [...nodes]; binaryHeap.fix(); // Fix heap properties diff --git a/src/data-structures/heap/max-heap.ts b/src/data-structures/heap/max-heap.ts index 410b166..55ace08 100644 --- a/src/data-structures/heap/max-heap.ts +++ b/src/data-structures/heap/max-heap.ts @@ -7,11 +7,11 @@ */ import {Heap} from './heap'; -import type {HeapComparator} from '../../types'; +import type {Comparator} from '../../types'; export class MaxHeap extends Heap { constructor( - comparator: HeapComparator = (a: E, b: E) => { + comparator: Comparator = (a: E, b: E) => { if (!(typeof a === 'number' && typeof b === 'number')) { throw new Error('The a, b params of compare function must be number'); } else { diff --git a/src/data-structures/heap/min-heap.ts b/src/data-structures/heap/min-heap.ts index 2998199..fcc809b 100644 --- a/src/data-structures/heap/min-heap.ts +++ b/src/data-structures/heap/min-heap.ts @@ -7,11 +7,11 @@ */ import {Heap} from './heap'; -import type {HeapComparator} from '../../types'; +import type {Comparator} from '../../types'; export class MinHeap extends Heap { constructor( - comparator: HeapComparator = (a: E, b: E) => { + comparator: Comparator = (a: E, b: E) => { if (!(typeof a === 'number' && typeof b === 'number')) { throw new Error('The a, b params of compare function must be number'); } else { diff --git a/src/data-structures/priority-queue/max-priority-queue.ts b/src/data-structures/priority-queue/max-priority-queue.ts index 2418a76..49f016e 100644 --- a/src/data-structures/priority-queue/max-priority-queue.ts +++ b/src/data-structures/priority-queue/max-priority-queue.ts @@ -6,11 +6,11 @@ * @license MIT License */ import {PriorityQueue} from './priority-queue'; -import type {HeapComparator} from '../../types'; +import type {Comparator} from '../../types'; export class MaxPriorityQueue extends PriorityQueue { constructor( - compare: HeapComparator = (a: E, b: E) => { + compare: Comparator = (a: E, b: E) => { if (!(typeof a === 'number' && typeof b === 'number')) { throw new Error('The a, b params of compare function must be number'); } else { diff --git a/src/data-structures/priority-queue/min-priority-queue.ts b/src/data-structures/priority-queue/min-priority-queue.ts index 9635a1d..0e1b3e8 100644 --- a/src/data-structures/priority-queue/min-priority-queue.ts +++ b/src/data-structures/priority-queue/min-priority-queue.ts @@ -6,11 +6,11 @@ * @license MIT License */ import {PriorityQueue} from './priority-queue'; -import type {HeapComparator} from '../../types'; +import type {Comparator} from '../../types'; export class MinPriorityQueue extends PriorityQueue { constructor( - compare: HeapComparator = (a: E, b: E) => { + compare: Comparator = (a: E, b: E) => { if (!(typeof a === 'number' && typeof b === 'number')) { throw new Error('The a, b params of compare function must be number'); } else { diff --git a/src/data-structures/priority-queue/priority-queue.ts b/src/data-structures/priority-queue/priority-queue.ts index 9f4585c..2bc2769 100644 --- a/src/data-structures/priority-queue/priority-queue.ts +++ b/src/data-structures/priority-queue/priority-queue.ts @@ -7,10 +7,10 @@ */ import {Heap} from '../heap'; -import {HeapComparator} from '../../types'; +import {Comparator} from '../../types'; export class PriorityQueue extends Heap { - constructor(comparator: HeapComparator) { + constructor(comparator: Comparator) { super(comparator); } } diff --git a/src/types/data-structures/binary-tree.ts b/src/types/data-structures/binary-tree.ts index a5f8f04..a296960 100644 --- a/src/types/data-structures/binary-tree.ts +++ b/src/types/data-structures/binary-tree.ts @@ -26,8 +26,6 @@ export type BinaryTreeNodePropertyName = 'key' | 'val'; export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName; -export type DFSOrderPattern = 'in' | 'pre' | 'post'; - export type BinaryTreeNodeKey = number; export type BinaryTreeNodeProperty> = diff --git a/src/types/data-structures/heap.ts b/src/types/data-structures/heap.ts index 55bf2b6..cb0ff5c 100644 --- a/src/types/data-structures/heap.ts +++ b/src/types/data-structures/heap.ts @@ -1,3 +1 @@ -export type HeapComparator = (a: T, b: T) => number; - -export type HeapDFSOrderPattern = 'pre' | 'in' | 'post'; +export {}; diff --git a/src/types/helpers.ts b/src/types/helpers.ts index cb0ff5c..f1991de 100644 --- a/src/types/helpers.ts +++ b/src/types/helpers.ts @@ -1 +1,4 @@ -export {}; +export type Comparator = (a: T, b: T) => number; + +// export enum DFSOrderPattern {'pre' = 'pre', 'in' = 'in', 'post' = 'post'} +export type DFSOrderPattern = 'pre' | 'in' | 'post'; diff --git a/test/unit/data-structures/heap/max-heap.test.ts b/test/unit/data-structures/heap/max-heap.test.ts index 744406f..b6dd044 100644 --- a/test/unit/data-structures/heap/max-heap.test.ts +++ b/test/unit/data-structures/heap/max-heap.test.ts @@ -1,7 +1,7 @@ -import {HeapComparator, MaxHeap} from '../../../../src'; +import {Comparator, MaxHeap} from '../../../../src'; describe('MaxHeap', () => { - const numberComparator: HeapComparator = (a, b) => b - a; + const numberComparator: Comparator = (a, b) => b - a; let maxHeap: MaxHeap; beforeEach(() => { diff --git a/test/unit/data-structures/heap/min-heap.test.ts b/test/unit/data-structures/heap/min-heap.test.ts index 2425162..be8d548 100644 --- a/test/unit/data-structures/heap/min-heap.test.ts +++ b/test/unit/data-structures/heap/min-heap.test.ts @@ -1,7 +1,7 @@ -import {HeapComparator, MinHeap} from '../../../../src'; +import {Comparator, MinHeap} from '../../../../src'; describe('MinHeap', () => { - const numberComparator: HeapComparator = (a, b) => a - b; + const numberComparator: Comparator = (a, b) => a - b; let minHeap: MinHeap; beforeEach(() => {