Skip to content

Commit

Permalink
Merge pull request #63 from gsantiago/release-v4.0.1
Browse files Browse the repository at this point in the history
Release v4.0.1
  • Loading branch information
gsantiago committed Sep 19, 2020
2 parents abf7831 + 6288be8 commit 8c97504
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
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
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
@@ -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
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
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
@@ -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()
})
})

0 comments on commit 8c97504

Please sign in to comment.