Skip to content

Commit ca2adf8

Browse files
committed
Translate: Detect if array was used as input
1 parent d0fe4db commit ca2adf8

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

packages/translate/src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ util.inherits(Translate, common.Service);
151151
* });
152152
*/
153153
Translate.prototype.detect = function(input, callback) {
154+
var inputIsArray = Array.isArray(input);
154155
input = arrify(input);
155156

156157
this.request({
@@ -176,7 +177,7 @@ Translate.prototype.detect = function(input, callback) {
176177
return result;
177178
});
178179

179-
if (input.length === 1) {
180+
if (input.length === 1 && !inputIsArray) {
180181
results = results[0];
181182
}
182183

@@ -368,6 +369,7 @@ Translate.prototype.getLanguages = function(target, callback) {
368369
* });
369370
*/
370371
Translate.prototype.translate = function(input, options, callback) {
372+
var inputIsArray = Array.isArray(input);
371373
input = arrify(input);
372374

373375
var body = {
@@ -407,7 +409,7 @@ Translate.prototype.translate = function(input, options, callback) {
407409

408410
var translations = resp.data.translations.map(prop('translatedText'));
409411

410-
if (body.q.length === 1) {
412+
if (body.q.length === 1 && !inputIsArray) {
411413
translations = translations[0];
412414
}
413415

packages/translate/test/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ describe('Translate', function() {
236236
done();
237237
});
238238
});
239+
240+
it('should return an array if input was an array', function(done) {
241+
translate.detect([INPUT], function(err, results, apiResponse_) {
242+
assert.ifError(err);
243+
assert.deepEqual(results, [expectedResults]);
244+
assert.strictEqual(apiResponse_, apiResponse);
245+
assert.deepEqual(apiResponse_, originalApiResponse);
246+
done();
247+
});
248+
});
239249
});
240250
});
241251

@@ -494,6 +504,14 @@ describe('Translate', function() {
494504
done();
495505
});
496506
});
507+
508+
it('should return an array if input was an array', function(done) {
509+
translate.translate([INPUT], OPTIONS, function(err, translations) {
510+
assert.ifError(err);
511+
assert.deepEqual(translations, [expectedResults]);
512+
done();
513+
});
514+
});
497515
});
498516
});
499517

0 commit comments

Comments
 (0)