Skip to content

Commit

Permalink
migrate the package to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuspoehls committed Nov 6, 2023
1 parent 29e8f93 commit 4cc9049
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -27,7 +33,6 @@
"superchargejs"
],
"license": "MIT",
"main": "dist/src",
"publishConfig": {
"access": "public"
},
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict'

export * from './queue'
export { Queue } from './queue.js'
3 changes: 1 addition & 2 deletions src/iterator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict'

export class QueueIterator<T> implements IterableIterator<T> {
/**
Expand Down Expand Up @@ -37,7 +36,7 @@ export class QueueIterator<T> implements IterableIterator<T> {
*/
next (): IteratorResult<T> {
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 }
}
}
3 changes: 1 addition & 2 deletions src/queue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

import { QueueIterator } from './iterator'
import { QueueIterator } from './iterator.js'

export class Queue<T> implements Iterable<T> {
/**
Expand Down
142 changes: 142 additions & 0 deletions test/queue.js
Original file line number Diff line number Diff line change
@@ -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()
141 changes: 0 additions & 141 deletions test/queue.ts

This file was deleted.

6 changes: 2 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
"extends": "@supercharge/tsconfig",
"compilerOptions": {
"outDir": "./dist",
"lib": [
"es2017"
]
},
"include": [
"./**/*"
],
"exclude": [
"./node_modules",
"./dist"
"./dist",
"./test",
]
}

0 comments on commit 4cc9049

Please sign in to comment.