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

Release v4.0.1 #63

Merged
merged 3 commits into from
Sep 19, 2020
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [4.0.1] - 2020-09-19
- Update README
- Add tests for parser errors

## [4.0.0] - 2020-09-19
- Fixes #6 by introducing the stream interface (`parse`, `stringify` and `resync` are now stream-based functions)
- Add `parseSync` and `stringifySync` as synchronous version of `parse` and `stringify`
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ inputStream
.pipe(outputStream)
```

Besides the stream functions, this module also provides synchronous functions like `parseSync` and `stringifySync`. However, you should avoid them and rather use the stream-based functions for better performance:
Besides the stream functions, this module also provides synchronous functions like `parseSync` and `stringifySync`. However, you should avoid them and use the stream-based functions for better performance:

```ts
import { parseSync, stringifySync } from 'subtitle'
Expand Down Expand Up @@ -116,13 +116,13 @@ inputStream
.on('finish', () => console.log('parser has finished'))
```

Check out the [Examples](#examples) section for more examples.
Check out the [Examples](#examples) section for more use cases.

### parseSync

- `parseSync(input: string): Node[]`

> **NOTE**: For better perfomance, consider to use the stream-based `parse` function
> **NOTE**: For better perfomance, consider using the stream-based `parse` function

It receives a string containing a SRT or VTT content and returns
an array of nodes:
Expand Down Expand Up @@ -172,13 +172,13 @@ inputStream
.pipe(stringify({ format: 'WebVTT' }))
```

Check out the [Examples](#examples) section for more examples.
Check out the [Examples](#examples) section for more use cases.

### stringifySync

- `stringify(nodes: Node[], options: { format: 'SRT' | 'vtt }): string`

> **NOTE**: For better perfomance, consider to use the stream-based `stringify` function
> **NOTE**: For better perfomance, consider using the stream-based `stringify` function

It receives an array of captions and returns a string in SRT (default), but it also supports VTT format through the options.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "subtitle",
"version": "4.0.0",
"version": "4.0.1",
"description": "Stream-based library for parsing and manipulating subtitles",
"repository": {
"type": "git",
Expand Down
3 changes: 1 addition & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ export const parse = () => {
try {
parser.parseLine(chunk.toString())
} catch (err) {
next(err)
return
return next(err)
}

next()
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const padLeft = (value: number, length = 2): string =>
export const createDuplex = (options: DuplexOptions) =>
new Duplex({
objectMode: true,
autoDestroy: false,
read() {},
...options
})
33 changes: 26 additions & 7 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import { fixtures, getFixtureStream, getFixture, pipeline } from '../test-utils'
import * as utils from '../test-utils'
import { parse } from '../src'

test.each(fixtures)('parse SRT fixture: %s', async fixture => {
const buffer = await pipeline(getFixtureStream(fixture, 'srt').pipe(parse()))
const expected = JSON.parse(await getFixture(fixture, 'srt.json'))
test.each(utils.fixtures)('parse SRT fixture: %s', async fixture => {
const buffer = await utils.pipeline(
utils.getFixtureStream(fixture, 'srt').pipe(parse())
)
const expected = JSON.parse(await utils.getFixture(fixture, 'srt.json'))

expect(buffer).toEqual(expected)
})

test.each(fixtures)('parse VTT fixture: %s', async fixture => {
const buffer = await pipeline(getFixtureStream(fixture, 'vtt').pipe(parse()))
const expected = JSON.parse(await getFixture(fixture, 'vtt.json'))
test.each(utils.fixtures)('parse VTT fixture: %s', async fixture => {
const buffer = await utils.pipeline(
utils.getFixtureStream(fixture, 'vtt').pipe(parse())
)
const expected = JSON.parse(await utils.getFixture(fixture, 'vtt.json'))

expect(buffer).toEqual(expected)
})

test('error handling', done => {
const stream = utils.createStreamFromString(`
1
Foo Bar
{{ THIS IS A INVALID TIMESTAMP }}
`)

stream.pipe(parse()).on('error', err => {
expect(err).toEqual(
new Error(`expected timestamp at row 2, but received: "Foo Bar"`)
)
done()
})
})