Skip to content

Commit

Permalink
feat(supporting-array-as-root): supporting passing in array as the ro…
Browse files Browse the repository at this point in the history
…ot object 🚀

close issue #5
  • Loading branch information
KhaledMohamedP committed Jul 1, 2017
1 parent 0b47daa commit 85c23a5
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/translate-json-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ function TranslateJSONObject() {
return Promise.reject(serviceType + ' doesn\'t support the language code you specified [' + language + '], please try another language code (ISO-639-1)');
}

var ARRAY_ROOT_TYPE = _.isArray(srcObj);
if (ARRAY_ROOT_TYPE) {
srcObj = {
arrayType: srcObj
};
}

function recurisveTranslateObject(destObj, srcObj) {
// Loop through the entire object collection
_.forEach(srcObj, loopHandler);
Expand Down Expand Up @@ -113,12 +120,16 @@ function TranslateJSONObject() {

// Recursivly loop through an object
recurisveTranslateObject(destObj, {
data: srcObj
ROOT: srcObj
}, language);

return new Promise(function (resolve, reject) {
Promise.all(promises).then(function () {
resolve(destObj.data);
if (ARRAY_ROOT_TYPE) {
resolve(destObj.ROOT.arrayType);
} else {
resolve(destObj.ROOT);
}
}).catch(reject);
});
}
Expand Down

0 comments on commit 85c23a5

Please sign in to comment.