File tree Expand file tree Collapse file tree 3 files changed +34
-14
lines changed Expand file tree Collapse file tree 3 files changed +34
-14
lines changed Original file line number Diff line number Diff line change
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' ;
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ var Duplex = require('stream').Duplex;
4
4
var util = require ( 'util' ) ;
5
5
var clone = require ( 'clone' ) ;
6
6
var defaults = require ( 'defaults' ) ;
7
+ var noTimestamps = require ( './no-timestamps' ) ;
7
8
8
9
/**
9
10
* Slows results down to no faster than real time.
@@ -234,17 +235,6 @@ TimingStream.prototype.checkForEnd = function() {
234
235
}
235
236
} ;
236
237
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
- }
248
238
249
239
/**
250
240
* Creates a new result with all transcriptions formatted
@@ -253,8 +243,9 @@ function noTimestamps(data) {
253
243
*/
254
244
TimingStream . prototype . handleResult = function handleResult ( data ) {
255
245
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 ) ;
258
249
}
259
250
260
251
// http://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#SpeechRecognitionEvent
Original file line number Diff line number Diff line change 3
3
var assert = require ( 'assert' ) ;
4
4
var sinon = require ( 'sinon' ) ;
5
5
var PassThrough = require ( 'stream' ) . PassThrough ;
6
-
6
+ var clone = require ( 'clone' ) ;
7
7
var TimingStream = require ( '../speech-to-text/timing-stream.js' ) ;
8
8
9
9
var results = require ( './resources/results.json' ) ;
@@ -273,4 +273,20 @@ describe('TimingStream', function() {
273
273
} ) ;
274
274
} ) ;
275
275
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
+
276
292
} ) ;
You can’t perform that action at this time.
0 commit comments