Skip to content

Commit 0038ab3

Browse files
committed
extract noTimestamps function, give error a name
1 parent 0cb7394 commit 0038ab3

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

speech-to-text/no-timestamps.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Returns true if the result is missing it's timestamps
3+
* @param {Object} data
4+
* @returns {Boolean}
5+
*/
6+
module.exports = function noTimestamps(data) {
7+
return data.results.some(function(result) {
8+
var alt = result.alternatives && result.alternatives[0];
9+
return !!(alt && (alt.transcript.trim() && !alt.timestamps || !alt.timestamps.length));
10+
});
11+
};
12+
13+
module.exports.ERROR_NO_TIMESTAMPS = 'NO_TIMESTAMPS';

speech-to-text/timing-stream.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var Duplex = require('stream').Duplex;
44
var util = require('util');
55
var clone = require('clone');
66
var defaults = require('defaults');
7+
var noTimestamps = require('./no-timestamps');
78

89
/**
910
* Slows results down to no faster than real time.
@@ -234,17 +235,6 @@ TimingStream.prototype.checkForEnd = function() {
234235
}
235236
};
236237

237-
/**
238-
* Returns true if the result is missing it's timestamps
239-
* @param {Object} data
240-
* @returns {Boolean}
241-
*/
242-
function noTimestamps(data) {
243-
return data.results.some(function(result) {
244-
var alt = result.alternatives && result.alternatives[0];
245-
return !!(alt && (alt.transcript.trim() && !alt.timestamps || !alt.timestamps.length));
246-
});
247-
}
248238

249239
/**
250240
* Creates a new result with all transcriptions formatted
@@ -253,8 +243,9 @@ function noTimestamps(data) {
253243
*/
254244
TimingStream.prototype.handleResult = function handleResult(data) {
255245
if (noTimestamps(data)) {
256-
this.emit('error', new Error('TimingStream requires timestamps'));
257-
return;
246+
var err = new Error('TimingStream requires timestamps');
247+
err.name = noTimestamps.ERROR_NO_TIMESTAMPS;
248+
return this.emit('error', err);
258249
}
259250

260251
// http://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#SpeechRecognitionEvent

test/timing-stream-spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var assert = require('assert');
44
var sinon = require('sinon');
55
var PassThrough = require('stream').PassThrough;
6-
6+
var clone = require('clone');
77
var TimingStream = require('../speech-to-text/timing-stream.js');
88

99
var results = require('./resources/results.json');
@@ -273,4 +273,20 @@ describe('TimingStream', function() {
273273
});
274274
});
275275

276+
it('should error if given results with no timestamps', function(done) {
277+
var noTimestamps = require('../speech-to-text/no-timestamps');
278+
assert(noTimestamps.ERROR_NO_TIMESTAMPS, 'noTimestamps.ERROR_NO_TIMESTAMPS should be defined');
279+
var stream = new TimingStream({objectMode: true});
280+
var data = clone(require('./resources/results.json'));
281+
delete data.results[0].alternatives[0].timestamps;
282+
stream.on('data', function(data) {
283+
assert.fail(data, undefined, "data emitted")
284+
});
285+
stream.on('error', function(err) {
286+
assert.equal(err.name, noTimestamps.ERROR_NO_TIMESTAMPS);
287+
done();
288+
});
289+
stream.end(data);
290+
});
291+
276292
});

0 commit comments

Comments
 (0)