Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate: Detect if array was used as input #2514

Merged
merged 1 commit into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/translate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ util.inherits(Translate, common.Service);
* });
*/
Translate.prototype.detect = function(input, callback) {
var inputIsArray = Array.isArray(input);
input = arrify(input);

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

if (input.length === 1) {
if (input.length === 1 && !inputIsArray) {
results = results[0];
}

Expand Down Expand Up @@ -368,6 +369,7 @@ Translate.prototype.getLanguages = function(target, callback) {
* });
*/
Translate.prototype.translate = function(input, options, callback) {
var inputIsArray = Array.isArray(input);
input = arrify(input);

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

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

if (body.q.length === 1) {
if (body.q.length === 1 && !inputIsArray) {
translations = translations[0];
}

Expand Down
18 changes: 18 additions & 0 deletions packages/translate/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ describe('Translate', function() {
done();
});
});

it('should return an array if input was an array', function(done) {
translate.detect([INPUT], function(err, results, apiResponse_) {
assert.ifError(err);
assert.deepEqual(results, [expectedResults]);
assert.strictEqual(apiResponse_, apiResponse);
assert.deepEqual(apiResponse_, originalApiResponse);
done();
});
});
});
});

Expand Down Expand Up @@ -494,6 +504,14 @@ describe('Translate', function() {
done();
});
});

it('should return an array if input was an array', function(done) {
translate.translate([INPUT], OPTIONS, function(err, translations) {
assert.ifError(err);
assert.deepEqual(translations, [expectedResults]);
done();
});
});
});
});

Expand Down