From 9561f77b46cb320927fd3a4d5f1f3d46825a8f8f Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Tue, 13 Jun 2023 18:19:09 +0400 Subject: [PATCH] pkg: add lint-types and tsconfig to the project. --- .github/workflows/build.yml | 8 +++++- .npmignore | 1 + lib/bheep.js | 4 ++- lib/heap.js | 50 ++++++++++++++++++++++++++----------- package-lock.json | 49 ++++++++++++++++++++++++++++++++++++ package.json | 8 +++--- tsconfig.json | 31 +++++++++++++++++++++++ 7 files changed, 131 insertions(+), 20 deletions(-) create mode 100644 package-lock.json create mode 100644 tsconfig.json diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bb13805..e13659e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,12 +15,18 @@ jobs: with: node-version: 18.x + - name: Install tools + run: npm install --location=global bslint typescript + - name: Install dependencies - run: npm install --location=global bslint + run: npm install - name: Lint run: npm run lint + - name: Lint types + run: npm run lint-types + test: name: Test runs-on: ubuntu-latest diff --git a/.npmignore b/.npmignore index 67d7010..ce7583b 100644 --- a/.npmignore +++ b/.npmignore @@ -15,3 +15,4 @@ package-lock.json test/ webpack.*.js yarn.lock +coverage/ diff --git a/lib/bheep.js b/lib/bheep.js index d00c663..65cbc58 100644 --- a/lib/bheep.js +++ b/lib/bheep.js @@ -1,3 +1,5 @@ 'use strict'; -module.exports = require('./heap'); +const {Heap} = require('./heap'); + +module.exports = Heap; diff --git a/lib/heap.js b/lib/heap.js index 2de1695..60d2414 100644 --- a/lib/heap.js +++ b/lib/heap.js @@ -8,16 +8,31 @@ const assert = require('bsert'); +/** + * @template T + * @callback HeapComparator + * @param {T} a + * @param {T} b + * @returns {Number} + */ + /** * Binary Heap * @alias module:utils.Heap + * @template T */ class Heap { + /** @type {HeapComparator} */ + compare; + + /** @type {T[]} */ + items; + /** * Create a binary heap. * @constructor - * @param {Function?} compare + * @param {HeapComparator} [compare] */ constructor(compare) { @@ -53,7 +68,7 @@ class Heap { /** * Set comparator. - * @param {Function} compare + * @param {HeapComparator} compare */ set(compare) { @@ -64,8 +79,8 @@ class Heap { /** * Push item onto heap. - * @param {Object} item - * @returns {Number} + * @param {T} item + * @returns {Number} Heap size. */ insert(item) { @@ -76,8 +91,7 @@ class Heap { /** * Pop next item off of heap. - * @param {Object} item - * @returns {Object} + * @returns {T} */ shift() { @@ -95,7 +109,7 @@ class Heap { /** * Remove item from heap. * @param {Number} i - * @returns {Object} + * @returns {T} */ remove(i) { @@ -197,14 +211,13 @@ class Heap { /** * Convert heap to sorted array. - * @returns {Object[]} + * @returns {T[]} */ toArray() { - const heap = new Heap(); + const heap = new Heap(this.compare); const result = []; - heap.compare = this.compare; heap.items = this.items.slice(); while (heap.size() > 0) @@ -215,13 +228,14 @@ class Heap { /** * Instantiate heap from array and comparator. - * @param {Function} compare - * @param {Object[]} items - * @returns {Heap} + * @template T + * @param {HeapComparator} compare + * @param {T[]} items + * @returns {Heap} */ static fromArray(compare, items) { - const heap = new Heap(); + const heap = new Heap(compare); heap.set(compare); heap.items = items; heap.init(); @@ -233,6 +247,12 @@ class Heap { * Helpers */ +/** + * @param {*} a + * @param {*} b + * @returns {Number} + */ + function comparator(a, b) { throw new Error('No heap comparator set.'); } @@ -241,4 +261,4 @@ function comparator(a, b) { * Expose */ -module.exports = Heap; +exports.Heap = Heap; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b6ea44a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,49 @@ +{ + "name": "bheep", + "version": "0.1.5", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "bheep", + "version": "0.1.5", + "license": "MIT", + "dependencies": { + "bsert": "~0.0.12" + }, + "devDependencies": { + "bmocha": "^2.1.0", + "bts-type-deps": "^0.0.2" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/bmocha": { + "version": "2.1.8", + "dev": true, + "license": "MIT", + "bin": { + "_bmocha": "bin/_bmocha", + "bmocha": "bin/bmocha" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/bsert": { + "version": "0.0.12", + "resolved": "https://registry.npmjs.org/bsert/-/bsert-0.0.12.tgz", + "integrity": "sha512-lUB0EMu4KhIf+VQ6RZJ7J3dFdohYSeta+gNgDi00Hi/t3k/W6xZlwm9PSSG0q7hJ2zW9Rsn5yaMPymETxroTRw==", + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/bts-type-deps": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/bts-type-deps/-/bts-type-deps-0.0.2.tgz", + "integrity": "sha512-n3/ETOR6hW0AADX5EeV/93W10QAL8O1suQO2Kw0OHNwdMor+hbhWKYhSOC/AiAL/xiYDytSQSZGGw0O++r8xIA==", + "dev": true + } + } +} diff --git a/package.json b/package.json index 1f7187e..36264be 100644 --- a/package.json +++ b/package.json @@ -16,13 +16,15 @@ "main": "./lib/bheep.js", "scripts": { "lint": "eslint lib/ test/", - "test": "bmocha --reporter spec test/*-test.js" + "test": "bmocha --reporter spec test/*-test.js", + "lint-types": "tsc -p ." }, "dependencies": { - "bsert": "~0.0.10" + "bsert": "~0.0.12" }, "devDependencies": { - "bmocha": "^2.1.0" + "bmocha": "^2.1.0", + "bts-type-deps": "^0.0.2" }, "engines": { "node": ">=8.0.0" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..56c78ac --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,31 @@ +{ + "include": [ + "lib/**/*.js" + ], + "compilerOptions": { + "rootDir": ".", + "target": "ES2016", + "lib": [ + "ES2016" + ], + + "noEmit": true, + + "allowJs": true, + "checkJs": true, + "maxNodeModuleJsDepth": 2, + + "module": "commonjs", + "moduleResolution": "node", + "resolveJsonModule": true, + + "stripInternal": true, + "noImplicitThis": true, + "noUnusedLocals": true, + "noUnusedParameters": false, + + "typeRoots": [ + "node_modules/bts-type-deps/types" + ] + } +}