Skip to content

Commit 510a3c9

Browse files
committed
only one test not passing
1 parent b713650 commit 510a3c9

4 files changed

Lines changed: 11 additions & 98 deletions

File tree

lib/compiler.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ function compile (input) {
2929
throw new CompilerError('Input must be valid');
3030
}
3131

32+
if (input && (!input.cues || input.cues.length < 1)) {
33+
return '';
34+
}
35+
3236
let output = '';
3337

3438
let lastTime = null;
@@ -45,7 +49,7 @@ function compile (input) {
4549
output += '\n\n';
4650
});
4751

48-
return output;
52+
return output.slice(0, -1);
4953
}
5054

5155
/**

lib/parser.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function parse (input, options) {
1818
options = {};
1919
}
2020

21-
const { meta = false, strict = true } = options;
21+
const { strict = true } = options;
2222

2323
if (typeof input !== 'string') {
2424
throw new ParserError('Input must be a string');
@@ -29,12 +29,9 @@ function parse (input, options) {
2929
input = input.replace(/\r/g, '\n');
3030

3131
const parts = input.split('\n\n');
32-
const header = parts.shift();
33-
34-
const headerParts = header.split('\n');
3532

3633
// nothing of interests, return early
37-
if (parts.length === 0 && headerParts.length === 1) {
34+
if (parts.length === 0) {
3835
return { valid: true, strict, cues: [], errors: [] };
3936
}
4037

@@ -44,28 +41,11 @@ function parse (input, options) {
4441
throw errors[0];
4542
}
4643

47-
const headerMeta = meta ? parseMeta(headerParts) : null;
48-
4944
const result = { valid: errors.length === 0, strict, cues, errors };
5045

51-
if (meta) {
52-
result.meta = headerMeta;
53-
}
54-
5546
return result;
5647
}
5748

58-
function parseMeta (headerParts) {
59-
const meta = {};
60-
headerParts.slice(1).forEach(header => {
61-
const splitIdx = header.indexOf(':');
62-
const key = header.slice(0, splitIdx).trim();
63-
const value = header.slice(splitIdx + 1).trim();
64-
meta[key] = value;
65-
});
66-
return Object.keys(meta).length > 0 ? meta : null;
67-
}
68-
6949
function parseCues (cues, strict) {
7050
const errors = [];
7151

@@ -129,6 +109,8 @@ function parseCue (cue, i, strict) {
129109
!validTimestamp(times[0]) ||
130110
!validTimestamp(times[1])) {
131111

112+
console.log('times', times);
113+
console.log('lines', lines);
132114
throw new ParserError(`Invalid cue timestamp (cue #${i})`);
133115
}
134116

test/compiler.test.js

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -440,45 +440,6 @@ Hello world
440440
compile(input).should.equal(output);
441441
});
442442

443-
it('should compile metadata', () => {
444-
const input = {
445-
meta: {
446-
Kind: 'captions',
447-
Language: 'en',
448-
'X-TIMESTAMP-MAP=LOCAL': '00:00:00.000,MPEGTS:0'
449-
},
450-
cues: [{
451-
end: 140,
452-
identifier: '1',
453-
start: 135.001,
454-
text: 'Hello world',
455-
styles: ''
456-
}],
457-
valid: true
458-
};
459-
460-
const output = `1
461-
00:02:15.001 --> 00:02:20.000
462-
Hello world
463-
`;
464-
465-
compile(input).should.equal(output);
466-
});
467-
468-
it('should not compile non-object metadata', () => {
469-
(() => {
470-
compile({ meta: [], cues: [], valid: true });
471-
})
472-
.should.throw(compilerError, /Metadata must be an object/);
473-
});
474-
475-
it('should not compile non-string metadata values', () => {
476-
(() => {
477-
compile({ meta: { foo: [] }, cues: [], valid: true });
478-
})
479-
.should.throw(compilerError, /Metadata value for "foo" must be string/);
480-
});
481-
482443
it('should not compile cues in non-chronological order', () => {
483444
const input = {
484445
valid: true,

test/parser.test.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ describe('WebVTT parser', () => {
1717

1818
it('should not parse the empty subtitle', () => {
1919
(() => { parse(''); })
20-
.should.throw(parserError, /WEBVTT/);
20+
.should.throw(parserError, '');
2121
});
2222

2323
it('should not parse non-string subtitles', () => {
2424
(() => { parse(''); })
25-
.should.throw(parserError, /WEBVTT/);
25+
.should.throw(parserError, '');
2626
});
2727

2828
it('should fail parsing cue with standalone identifier', () => {
@@ -189,40 +189,6 @@ Chapter 17`;
189189
parse(input).should.not.have.property('meta');
190190
});
191191

192-
it('should accept an options object', () => {
193-
const input = `1
194-
00:00.000 --> 00:00.001
195-
Options`;
196-
const options = { meta: true };
197-
198-
parse(input, options).cues[0].start.should.equal(0);
199-
parse(input, options).cues[0].end.should.equal(0.001);
200-
});
201-
202-
it('should return meta if meta option is true', () => {
203-
const input = `1
204-
00:00.000 --> 00:00.001`;
205-
const options = { meta: true };
206-
207-
parse(input, options).should.have.property('valid').be.true;
208-
parse(input, options).should.have.property('meta').be.deep.equal(
209-
{
210-
Kind: 'captions',
211-
Language: 'en',
212-
'X-TIMESTAMP-MAP=LOCAL': '00:00:00.000,MPEGTS:0'
213-
}
214-
);
215-
});
216-
217-
it('should return null if meta option is true but no meta', () => {
218-
const input = `1
219-
00:00.000 --> 00:00.001`;
220-
const options = { meta: true };
221-
222-
parse(input, options).should.have.property('valid').be.true;
223-
parse(input, options).should.have.property('meta').be.equal(null);
224-
});
225-
226192
it('should return strict as default true', () => {
227193

228194
const input = `1

0 commit comments

Comments
 (0)