Skip to content

Commit

Permalink
Merge PR #2 from 'nodech/ts-lint'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Aug 31, 2023
2 parents 6bd70bc + c34649e commit 3dc6dc6
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 20 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ package-lock.json
test/
webpack.*.js
yarn.lock
coverage/
4 changes: 3 additions & 1 deletion lib/bheep.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = require('./heap');
const {Heap} = require('./heap');

module.exports = Heap;
50 changes: 35 additions & 15 deletions lib/heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>} */
compare;

/** @type {T[]} */
items;

/**
* Create a binary heap.
* @constructor
* @param {Function?} compare
* @param {HeapComparator<T>} [compare]
*/

constructor(compare) {
Expand Down Expand Up @@ -53,7 +68,7 @@ class Heap {

/**
* Set comparator.
* @param {Function} compare
* @param {HeapComparator<T>} compare
*/

set(compare) {
Expand All @@ -64,8 +79,8 @@ class Heap {

/**
* Push item onto heap.
* @param {Object} item
* @returns {Number}
* @param {T} item
* @returns {Number} Heap size.
*/

insert(item) {
Expand All @@ -76,8 +91,7 @@ class Heap {

/**
* Pop next item off of heap.
* @param {Object} item
* @returns {Object}
* @returns {T}
*/

shift() {
Expand All @@ -95,7 +109,7 @@ class Heap {
/**
* Remove item from heap.
* @param {Number} i
* @returns {Object}
* @returns {T}
*/

remove(i) {
Expand Down Expand Up @@ -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)
Expand All @@ -215,13 +228,14 @@ class Heap {

/**
* Instantiate heap from array and comparator.
* @param {Function} compare
* @param {Object[]} items
* @returns {Heap}
* @template T
* @param {HeapComparator<T>} compare
* @param {T[]} items
* @returns {Heap<T>}
*/

static fromArray(compare, items) {
const heap = new Heap();
const heap = new Heap(compare);
heap.set(compare);
heap.items = items;
heap.init();
Expand All @@ -233,6 +247,12 @@ class Heap {
* Helpers
*/

/**
* @param {*} a
* @param {*} b
* @returns {Number}
*/

function comparator(a, b) {
throw new Error('No heap comparator set.');
}
Expand All @@ -241,4 +261,4 @@ function comparator(a, b) {
* Expose
*/

module.exports = Heap;
exports.Heap = Heap;
49 changes: 49 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.3"
},
"engines": {
"node": ">=8.0.0"
Expand Down
31 changes: 31 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"include": [
"lib/**/*.js"
],
"compilerOptions": {
"rootDir": ".",
"target": "ES2020",
"lib": [
"ES2020"
],

"noEmit": true,

"allowJs": true,
"checkJs": true,
"maxNodeModuleJsDepth": 10,

"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,

"stripInternal": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": false,

"typeRoots": [
"node_modules/bts-type-deps/types"
]
}
}

0 comments on commit 3dc6dc6

Please sign in to comment.