From 4cc904903e7dc248b71f7ba4a3a9a5b50a17e981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20P=C3=B6hls?= Date: Mon, 6 Nov 2023 05:13:58 +0100 Subject: [PATCH] migrate the package to ESM --- .github/workflows/run-tests.yml | 2 +- package.json | 33 ++++---- src/index.ts | 2 +- src/iterator.ts | 3 +- src/queue.ts | 3 +- test/queue.js | 142 ++++++++++++++++++++++++++++++++ test/queue.ts | 141 ------------------------------- tsconfig.json | 6 +- 8 files changed, 166 insertions(+), 166 deletions(-) create mode 100644 test/queue.js delete mode 100644 test/queue.ts diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index d6d31cc..5857f19 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: - node-version: [14.x, 16.x, 18.x, 19.x] + node-version: [20.x, 21.x] name: Node ${{ matrix.node-version }} diff --git a/package.json b/package.json index 0bbf7db..eff29b7 100644 --- a/package.json +++ b/package.json @@ -7,17 +7,23 @@ "url": "https://github.com/supercharge/queue-datastructure/issues" }, "devDependencies": { - "@supercharge/eslint-config-typescript": "~2.0.0", - "@supercharge/tsconfig": "~3.1.0", - "@types/jest": "~27.5.1", - "eslint": "~8.15.0", - "jest": "~28.1.0", - "ts-jest": "~28.0.2", - "typescript": "~4.6.4" + "@supercharge/eslint-config-typescript": "~4.0.0", + "@supercharge/tsconfig": "~7.0.0", + "c8": "~8.0.1", + "eslint": "~8.53.0", + "expect": "~29.7.0", + "typescript": "~5.2.2", + "uvu": "~0.5.6" }, "files": [ - "dist/src" + "dist" ], + "main": "dist/index.js", + "exports": { + ".": "./dist/index.js" + }, + "type": "module", + "types": "dist", "homepage": "https://github.com/supercharge/queue-datastructure", "keywords": [ "queue", @@ -27,7 +33,6 @@ "superchargejs" ], "license": "MIT", - "main": "dist/src", "publishConfig": { "access": "public" }, @@ -41,10 +46,8 @@ "lint": "eslint src --ext .js,.ts", "lint:fix": "eslint src --ext .js,.ts --fix", "list:tests": "jest --listTests", - "test": "npm run build && npm run lint && npm run test:run", - "test:run": "jest --config=jest.config.js", - "test:single": "lab --transform node_modules/lab-transform-typescript --assert @hapi/code --leaks --lint --id", - "test:types": "tsc" - }, - "types": "dist/src" + "test": "npm run build && npm run lint && npm run test:run ", + "test:run": "c8 --include=dist uvu --ignore helpers", + "posttest": "c8 report --reporter=html" + } } diff --git a/src/index.ts b/src/index.ts index fc5b29a..1c3b3b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ 'use strict' -export * from './queue' +export { Queue } from './queue.js' diff --git a/src/iterator.ts b/src/iterator.ts index 6ba8a67..d61d8e3 100644 --- a/src/iterator.ts +++ b/src/iterator.ts @@ -1,4 +1,3 @@ -'use strict' export class QueueIterator implements IterableIterator { /** @@ -37,7 +36,7 @@ export class QueueIterator implements IterableIterator { */ next (): IteratorResult { return this.pointer < this.items.length - ? { done: false, value: this.items[this.pointer++] } + ? { done: false, value: this.items[this.pointer++] as T } : { done: true, value: undefined } } } diff --git a/src/queue.ts b/src/queue.ts index 30ff595..18efaf7 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -1,6 +1,5 @@ -'use strict' -import { QueueIterator } from './iterator' +import { QueueIterator } from './iterator.js' export class Queue implements Iterable { /** diff --git a/test/queue.js b/test/queue.js new file mode 100644 index 0000000..4846591 --- /dev/null +++ b/test/queue.js @@ -0,0 +1,142 @@ + +import { test } from 'uvu' +import { expect } from 'expect' +import { Queue } from '../dist/index.js' + +test('creates an empty new Queue', () => { + const queue = new Queue() + + expect(queue.size()).toEqual(0) + expect(queue.peek()).toBeUndefined() +}) + +test('creates a new Queue() from an array', () => { + const queue = new Queue([1, 2]) + + expect(queue.size()).toEqual(2) + expect(queue.dequeue()).toEqual(1) +}) + +test('enqueue', () => { + const queue = new Queue(1, 2) + queue.enqueue(3) + expect(queue.size()).toEqual(3) + + queue.enqueue(4, 5) + expect(queue.size()).toEqual(5) + expect(queue.peek()).toEqual(1) + + queue.enqueue(...[6, 7]) + expect(queue.size()).toEqual(7) +}) + +test('dequeue', () => { + const queue = new Queue(1) + expect(queue.dequeue()).toEqual(1) + expect(queue.size()).toEqual(0) + expect(queue.dequeue()).toBeUndefined() +}) + +test('peek', () => { + const queue = new Queue(1, 2, 3) + const item = queue.peek() + expect(item).toEqual(1) + expect(queue.size()).toEqual(3) +}) + +test('size', () => { + expect( + new Queue(1, 2).size() + ).toEqual(2) +}) + +test('items', () => { + expect( + new Queue(1, 2, 3).items() + ).toEqual([1, 2, 3]) + + expect( + new Queue([1, 2, 3]).items() + ).toEqual([1, 2, 3]) + + expect( + new Queue() + .enqueue(1, 2) + .enqueue(3) + .enqueue(...[4, 5, 6]) + .items() + ).toEqual([1, 2, 3, 4, 5, 6]) +}) + +test('isEmpty', () => { + const queue = new Queue(1) + expect(queue.isEmpty()).toBe(false) + + queue.dequeue() + expect(queue.isEmpty()).toBe(true) +}) + +test('isNotEmpty', () => { + const queue = new Queue(1) + expect(queue.isNotEmpty()).toBe(true) + expect(queue.isEmpty()).toBe(false) + + queue.dequeue() + expect(queue.isNotEmpty()).toBe(false) + expect(queue.isEmpty()).toBe(true) +}) + +test('clear', () => { + const queue = new Queue(1) + expect(queue.isEmpty()).toBe(false) + + queue.clear() + expect(queue.isEmpty()).toBe(true) +}) + +test('for..of', () => { + const queue = new Queue(1, 2, 3) + const items = [] + + for (const item of queue) { + items.push(item) + } + + expect(items).toEqual([1, 2, 3]) +}) + +test('Symbol.iterator', () => { + const queue = new Queue(1, 2, 3) + + const iterable = queue[Symbol.iterator]() + expect(iterable.next).toBeInstanceOf(Function) + + const items = [] + + for (const item of iterable) { + items.push(item) + } + + expect(items).toEqual([1, 2, 3]) +}) + +test('Symbol.iterator is iterable', () => { + const queue = new Queue(1, 2, 3) + + const iterable = queue[Symbol.iterator]() + expect(iterable.next).toBeInstanceOf(Function) + + const items = [] + + items.push(iterable.next().value) + expect(items).toEqual([1]) + + // Continue with same iterable + for (const item of iterable) { + items.push(item) + } + + expect(items).toEqual([1, 2, 3]) +}) + +test.run() diff --git a/test/queue.ts b/test/queue.ts deleted file mode 100644 index 50af759..0000000 --- a/test/queue.ts +++ /dev/null @@ -1,141 +0,0 @@ -'use strict' - -import { Queue } from '../src' - -describe('Queue', () => { - it('creates an empty new Queue', () => { - const queue = new Queue() - - expect(queue.size()).toEqual(0) - expect(queue.peek()).toBeUndefined() - }) - - it('creates a new Queue() from an array', () => { - const queue = new Queue([1, 2]) - - expect(queue.size()).toEqual(2) - expect(queue.dequeue()).toEqual(1) - }) - - it('enqueue', () => { - const queue = new Queue(1, 2) - queue.enqueue(3) - expect(queue.size()).toEqual(3) - - queue.enqueue(4, 5) - expect(queue.size()).toEqual(5) - expect(queue.peek()).toEqual(1) - - queue.enqueue(...[6, 7]) - expect(queue.size()).toEqual(7) - }) - - it('dequeue', () => { - const queue = new Queue(1) - expect(queue.dequeue()).toEqual(1) - expect(queue.size()).toEqual(0) - expect(queue.dequeue()).toBeUndefined() - }) - - it('peek', () => { - const queue = new Queue(1, 2, 3) - const item = queue.peek() - expect(item).toEqual(1) - expect(queue.size()).toEqual(3) - }) - - it('size', () => { - expect( - new Queue(1, 2).size() - ).toEqual(2) - }) - - it('items', () => { - expect( - new Queue(1, 2, 3).items() - ).toEqual([1, 2, 3]) - - expect( - new Queue([1, 2, 3]).items() - ).toEqual([1, 2, 3]) - - expect( - new Queue() - .enqueue(1, 2) - .enqueue(3) - .enqueue(...[4, 5, 6]) - .items() - ).toEqual([1, 2, 3, 4, 5, 6]) - }) - - it('isEmpty', () => { - const queue = new Queue(1) - expect(queue.isEmpty()).toBe(false) - - queue.dequeue() - expect(queue.isEmpty()).toBe(true) - }) - - it('isNotEmpty', () => { - const queue = new Queue(1) - expect(queue.isNotEmpty()).toBe(true) - expect(queue.isEmpty()).toBe(false) - - queue.dequeue() - expect(queue.isNotEmpty()).toBe(false) - expect(queue.isEmpty()).toBe(true) - }) - - it('clear', () => { - const queue = new Queue(1) - expect(queue.isEmpty()).toBe(false) - - queue.clear() - expect(queue.isEmpty()).toBe(true) - }) - - test('for..of', () => { - const queue = new Queue(1, 2, 3) - const items = [] - - for (const item of queue) { - items.push(item) - } - - expect(items).toEqual([1, 2, 3]) - }) - - test('Symbol.iterator', () => { - const queue = new Queue(1, 2, 3) - - const iterable = queue[Symbol.iterator]() - expect(iterable.next).toBeInstanceOf(Function) - - const items = [] - - for (const item of iterable) { - items.push(item) - } - - expect(items).toEqual([1, 2, 3]) - }) - - test('Symbol.iterator is iterable', () => { - const queue = new Queue(1, 2, 3) - - const iterable = queue[Symbol.iterator]() - expect(iterable.next).toBeInstanceOf(Function) - - const items = [] - - items.push(iterable.next().value) - expect(items).toEqual([1]) - - // Continue with same iterable - for (const item of iterable) { - items.push(item) - } - - expect(items).toEqual([1, 2, 3]) - }) -}) diff --git a/tsconfig.json b/tsconfig.json index 3f79eb4..e9319d5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,15 +2,13 @@ "extends": "@supercharge/tsconfig", "compilerOptions": { "outDir": "./dist", - "lib": [ - "es2017" - ] }, "include": [ "./**/*" ], "exclude": [ "./node_modules", - "./dist" + "./dist", + "./test", ] }