/**
* Copyright 2014 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var Transform = require('stream').Transform;
var util = require('util');
var clone = require('clone');
/**
* Object-Mode stream that pulls result objects from the results array
*
* Also copies the top-level result_index to the individual results as .index
*
* @constructor
* @param {Object} options
*/
function ResultStream(options) {
options = options || {};
options.objectMode = true;
Transform.call(this, options);
}
util.inherits(ResultStream, Transform);
ResultStream.prototype._transform = function(data, encoding, next) {
// when speaker_labels is enabled, some messages won't have a results array
if (Array.isArray(data.results)) {
// usually there is exactly 1 result, but there can be 0 in some circumstances, and potentially more in future iterations
data.results.forEach(
function(result) {
var cloned = clone(result);
cloned.index = data.result_index;
this.push(cloned);
},
this
);
} else {
this.push(data);
}
next();
};
ResultStream.prototype.promise = require('./to-promise');
module.exports = ResultStream;