Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the Package to ESM #29

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Changelog


## [3.0.0](https://github.com/supercharge/queue-datastructure/compare/v2.1.0...v3.0.0) - 2022-xx-xx
## [3.0.0](https://github.com/supercharge/queue-datastructure/compare/v2.1.0...v3.0.0) - 2023-11-xx

### Updated
- bump dependencies

### Breaking Changes
- require Node.js v14, drop support for Node.js v10 and v12
- require Node.js v20, drop support for Node.js any earlier version
- migrate the package to ESM
- use named exports: use `import { Queue } from '@supercharge/queue-datastructure`
```ts
// before
Expand All @@ -16,7 +17,6 @@

// now
import { Queue } from '@supercharge/queue-datastructure'
const { Queue } = require('@supercharge/queue-datastructure')
```


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()
Loading