Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bergos committed Jun 11, 2024
0 parents commit de6e2a2
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: daily
- package-ecosystem: npm
directory: /
schedule:
interval: daily
versioning-strategy: increase-if-necessary
21 changes: 21 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test
on:
- pull_request
- push
jobs:
test:
runs-on: ubuntu-24.04
strategy:
matrix:
node:
- '18'
- '20'
- '22'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v4
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
node_modules
package-lock.json
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Thomas Bergwinkl

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# rdf-utils-stream

[![build status](https://img.shields.io/github/actions/workflow/status/rdf-ext/rdf-utils-stream/test.yaml?branch=master)](https://github.com/rdf-ext/rdf-utils-stream/actions/workflows/test.yaml)
[![npm version](https://img.shields.io/npm/v/rdf-utils-stream.svg)](https://www.npmjs.com/package/rdf-utils-stream)

Utils for RDF/JS Streams.
8 changes: 8 additions & 0 deletions eventToPromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
async function eventToPromise (event) {
return new Promise((resolve, reject) => {
event.on('end', () => resolve())
event.on('error', err => reject(err))
})
}

export default eventToPromise
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "rdf-utils-stream",
"version": "0.0.0",
"type": "module",
"description": "RDF/JS Stream utils",
"main": "index.js",
"scripts": {
"test": "stricter-standard && c8 --reporter=lcov --reporter=text-summary mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/rdf-ext/rdf-utils-stream.git"
},
"keywords": [
"rdf",
"rdfjs",
"rdf-ext",
"stream",
"utils"
],
"author": "Thomas Bergwinkl <[email protected]> (https://www.bergnet.org/people/bergi/card#me)",
"license": "MIT",
"bugs": {
"url": "https://github.com/rdf-ext/rdf-utils-stream/issues"
},
"homepage": "https://github.com/rdf-ext/rdf-utils-stream",
"dependencies": {
"events": "^3.3.0"
},
"devDependencies": {
"c8": "^10.0.0",
"mocha": "^10.4.0",
"stricter-standard": "^0.3.1"
}
}
13 changes: 13 additions & 0 deletions promiseToEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { EventEmitter } from 'events'

function promiseToEvent (promise) {
const event = new EventEmitter()

promise
.then(() => event.emit('end'))
.catch(err => event.emit('error', err))

return event
}

export default promiseToEvent
46 changes: 46 additions & 0 deletions test/eventToPromise.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { doesNotReject, rejects, strictEqual } from 'node:assert'
import { EventEmitter } from 'events' // eslint-disable-line import/order
import { describe, it } from 'mocha'
import eventToPromise from '../eventToPromise.js'

describe('eventToPromise', () => {
it('should be a function', () => {
strictEqual(typeof eventToPromise, 'function')
})

it('should resolve on end event', async () => {
const event = new EventEmitter()

await doesNotReject(async () => {
setTimeout(() => {
event.emit('end')
}, 10)

await eventToPromise(event)
})
})

it('should reject on error event', async () => {
const event = new EventEmitter()

await rejects(async () => {
setTimeout(() => {
event.emit('error')
}, 10)

await eventToPromise(event)
})
})

it('should forward the error from the error event', async () => {
const event = new EventEmitter()

await rejects(async () => {
setTimeout(() => {
event.emit('error', new Error('test'))
}, 10)

await eventToPromise(event)
}, { message: 'test' })
})
})
68 changes: 68 additions & 0 deletions test/promiseToEvent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { strictEqual } from 'node:assert'
import { setTimeout } from 'node:timers/promises'
import { describe, it } from 'mocha'
import promiseToEvent from '../promiseToEvent.js'

describe('promiseToEvent', () => {
it('should be a function', () => {
strictEqual(typeof promiseToEvent, 'function')
})

it('should return an EventEmitter object', () => {
const result = promiseToEvent(new Promise(() => {}))

strictEqual(typeof result.emit, 'function')
strictEqual(typeof result.on, 'function')
})

it('should emit an end event on resolve', async () => {
let touched
const result = promiseToEvent(setTimeout(10))

result.on('end', () => {
touched = true
})

await setTimeout(20)

strictEqual(touched, true)
})

it('should emit an error event on reject', async () => {
let touched

const func = async () => {
await setTimeout(10)
throw new Error('test')
}

const result = promiseToEvent(func())

result.on('error', () => {
touched = true
})

await setTimeout(20)

strictEqual(touched, true)
})

it('should forward the emitted error', async () => {
let error

const func = async () => {
await setTimeout(10)
throw new Error('test')
}

const result = promiseToEvent(func())

result.on('error', err => {
error = err
})

await setTimeout(20)

strictEqual(error.message, 'test')
})
})

0 comments on commit de6e2a2

Please sign in to comment.