Skip to content

Commit

Permalink
add translate tags function
Browse files Browse the repository at this point in the history
  • Loading branch information
williambelle committed May 13, 2019
1 parent 2d94692 commit a1b8ea5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ epflMenuApi.findResto().then(function(restos) {
}).catch(function(err) {
console.log(err);
});

let tags = epflMenuApi.translateTags('Poisson,Viande,Chinois');
console.log(tags) //=> 'Fish,Meat,Chinese'
```

API
Expand Down Expand Up @@ -150,6 +153,18 @@ Type: `number`

Restaurant id.

### .translateTags(str)

Type: `function`

Translate tags from French to English.

##### str

Type: `string`

Tags to translate.

See also
--------

Expand Down
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,20 @@ exports.findResto = (id) => {
}).catch((err) => reject(err));
});
};

exports.translateTags = (strTags) => {
if (!strTags) {
throw new TypeError('Not a valid tags');
}

let listTags = prepareTags(strTags);
let translatedList = [];
for (var i = 0; i < listTags.length; i++) {
if (TAGS[listTags[i]]) {
translatedList.push(TAGS[listTags[i]]);
} else {
translatedList.push(listTags[i]);
}
}
return translatedList.join(',');
};
29 changes: 29 additions & 0 deletions test/test-translateTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* (c) William Belle, 2019.
* See the LICENSE file for more details.
*/

require('chai').should();

const epflMenuApi = require('../src/index.js');

describe('epfl-menu-api translateTags', function () {
this.timeout(10000);

it('should throw an exception with undefined', function () {
(function () {
epflMenuApi.translateTags(undefined);
}).should.throw('Not a valid tags');
});

it('should throw an exception with ""', function () {
(function () {
epflMenuApi.translateTags('');
}).should.throw('Not a valid tags');
});

it('should translate tags "Poisson,Viande,Chinois,Toto"', function () {
let tags = epflMenuApi.translateTags('Poisson,Viande,Chinois,Toto');
tags.should.equal('Fish,Meat,Chinese,Toto');
});
});

0 comments on commit a1b8ea5

Please sign in to comment.