Skip to content

Commit d0e8a96

Browse files
author
Bobby Sudekum
authored
Remove getBestMatchingLanguage() and parseLanguageIntoCodes() (#212)
* Remove getBestMatchingLanguage * Remove getBestMatchingLanguage() and parseLanguageIntoCodes() * add comment * rm * readme
1 parent 0aaf51d commit d0e8a96

File tree

5 files changed

+4
-178
lines changed

5 files changed

+4
-178
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ OSRM Text Instructions is a Node.js library that transforms route data generated
1717
var version = 'v5';
1818
var osrmTextInstructions = require('osrm-text-instructions')(version);
1919

20-
// If you’re unsure if the user’s locale is supported, use `getBestMatchingLanguage` method to find an appropriate language.
21-
var language = osrmTextInstructions.getBestMatchingLanguage('en-US');
22-
2320
response.legs.forEach(function(leg) {
2421
leg.steps.forEach(function(step) {
25-
instruction = osrmTextInstructions.compile(language, step, options)
22+
instruction = osrmTextInstructions.compile('en', step, options)
2623
});
2724
});
2825
```
2926

27+
If you are unsure if the user's locale is supported by osrm-text-inustrctions, use [@mapbox/locale-utils](https://github.com/mapbox/locale-utils) for finding the best fitting language.
28+
3029
#### Parameters `require('osrm-text-instructions')(version)`
3130

3231
parameter | required? | values | description

index.js

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -272,47 +272,6 @@ module.exports = function(version) {
272272
}
273273

274274
return output;
275-
},
276-
getBestMatchingLanguage: function(language) {
277-
if (languages.instructions[language]) return language;
278-
279-
var codes = languages.parseLanguageIntoCodes(language);
280-
var languageCode = codes.language;
281-
var scriptCode = codes.script;
282-
var regionCode = codes.region;
283-
284-
// Same language code and script code (lng-Scpt)
285-
if (languages.instructions[languageCode + '-' + scriptCode]) {
286-
return languageCode + '-' + scriptCode;
287-
}
288-
289-
// Same language code and region code (lng-CC)
290-
if (languages.instructions[languageCode + '-' + regionCode]) {
291-
return languageCode + '-' + regionCode;
292-
}
293-
294-
// Same language code (lng)
295-
if (languages.instructions[languageCode]) {
296-
return languageCode;
297-
}
298-
299-
// Same language code and any script code (lng-Scpx) and the found language contains a script
300-
var anyScript = languages.parsedSupportedCodes.find(function (language) {
301-
return language.language === languageCode && language.script;
302-
});
303-
if (anyScript) {
304-
return anyScript.locale;
305-
}
306-
307-
// Same language code and any region code (lng-CX)
308-
var anyCountry = languages.parsedSupportedCodes.find(function (language) {
309-
return language.language === languageCode && language.region;
310-
});
311-
if (anyCountry) {
312-
return anyCountry.locale;
313-
}
314-
315-
return 'en';
316275
}
317276
};
318277
};

languages.js

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,38 +92,9 @@ var abbreviations = {
9292
'uk': abbreviationsUk,
9393
'vi': abbreviationsVi
9494
};
95-
96-
function parseLanguageIntoCodes (language) {
97-
var match = language.match(/(\w\w)(?:-(\w\w\w\w))?(?:-(\w\w))?/i);
98-
var locale = [];
99-
if (match[1]) {
100-
match[1] = match[1].toLowerCase();
101-
locale.push(match[1]);
102-
}
103-
if (match[2]) {
104-
match[2] = match[2][0].toUpperCase() + match[2].substring(1).toLowerCase();
105-
locale.push(match[2]);
106-
}
107-
if (match[3]) {
108-
match[3] = match[3].toUpperCase();
109-
locale.push(match[3]);
110-
}
111-
112-
return {
113-
locale: locale.join('-'),
114-
language: match[1],
115-
script: match[2],
116-
region: match[3]
117-
};
118-
}
119-
12095
module.exports = {
12196
supportedCodes: Object.keys(instructions),
122-
parsedSupportedCodes: Object.keys(instructions).map(function(language) {
123-
return parseLanguageIntoCodes(language);
124-
}),
12597
instructions: instructions,
12698
grammars: grammars,
127-
abbreviations: abbreviations,
128-
parseLanguageIntoCodes: parseLanguageIntoCodes
99+
abbreviations: abbreviations
129100
};

test/index_test.js

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -249,93 +249,6 @@ tape.test('v5 compile', function(t) {
249249

250250
assert.end();
251251
});
252-
253-
t.test('en-US fallback to en', function(assert) {
254-
var language = v5Compiler.getBestMatchingLanguage('en-us');
255-
256-
assert.equal(v5Compiler.compile(language, {
257-
maneuver: {
258-
type: 'turn',
259-
modifier: 'left'
260-
},
261-
name: 'Way Name'
262-
}), 'Turn left onto Way Name');
263-
264-
assert.end();
265-
});
266-
267-
t.test('zh-CN fallback to zh-Hans', function(assert) {
268-
var language = v5Compiler.getBestMatchingLanguage('zh-CN');
269-
270-
assert.equal(v5Compiler.compile(language, {
271-
maneuver: {
272-
type: 'turn',
273-
modifier: 'left'
274-
},
275-
name: 'Way Name'
276-
}), '左转,上Way Name');
277-
278-
assert.end();
279-
});
280-
281-
t.test('zh-Hant fallback to zh-Hanz', function(assert) {
282-
var language = v5Compiler.getBestMatchingLanguage('zh-Hant');
283-
284-
assert.equal(v5Compiler.compile(language, {
285-
maneuver: {
286-
type: 'turn',
287-
modifier: 'left'
288-
},
289-
name: 'Way Name'
290-
}), '左转,上Way Name');
291-
292-
assert.end();
293-
});
294-
295-
t.test('zh-Hant-TW fallback to zh-Hant', function(assert) {
296-
var language = v5Compiler.getBestMatchingLanguage('zh-Hant-TW');
297-
298-
assert.equal(v5Compiler.compile(language, {
299-
maneuver: {
300-
type: 'turn',
301-
modifier: 'left'
302-
},
303-
name: 'Way Name'
304-
}), '左转,上Way Name');
305-
306-
assert.end();
307-
});
308-
309-
t.test('es-MX fallback to es', function(assert) {
310-
var language = v5Compiler.getBestMatchingLanguage('es-MX');
311-
312-
assert.equal(v5Compiler.compile(language, {
313-
maneuver: {
314-
type: 'turn',
315-
modifier: 'straight'
316-
},
317-
name: 'Way Name'
318-
}), 'Ve recto en Way Name');
319-
320-
assert.end();
321-
});
322-
323-
t.test('getBestMatchingLanguage', function(t) {
324-
t.equal(v5Compiler.getBestMatchingLanguage('foo'), 'en');
325-
t.equal(v5Compiler.getBestMatchingLanguage('en-US'), 'en');
326-
t.equal(v5Compiler.getBestMatchingLanguage('zh-CN'), 'zh-Hans');
327-
t.equal(v5Compiler.getBestMatchingLanguage('zh-Hant'), 'zh-Hans');
328-
t.equal(v5Compiler.getBestMatchingLanguage('zh-Hant-TW'), 'zh-Hans');
329-
t.equal(v5Compiler.getBestMatchingLanguage('zh'), 'zh-Hans');
330-
t.equal(v5Compiler.getBestMatchingLanguage('es-MX'), 'es');
331-
t.equal(v5Compiler.getBestMatchingLanguage('es-ES'), 'es-ES');
332-
t.equal(v5Compiler.getBestMatchingLanguage('pt'), 'pt-BR');
333-
t.equal(v5Compiler.getBestMatchingLanguage('pt-AO'), 'pt-BR');
334-
t.equal(v5Compiler.getBestMatchingLanguage('pt-PT'), 'pt-PT');
335-
t.equal(v5Compiler.getBestMatchingLanguage('pt-pt'), 'pt-PT');
336-
t.end();
337-
});
338-
339252
t.test('fixtures match generated instructions', function(assert) {
340253
// pre-load instructions
341254
var version = 'v5';

test/languages_test.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,3 @@ tape.test('verify language files structure', function(assert) {
6868

6969
assert.end();
7070
});
71-
72-
/* eslint-disable */
73-
tape.test('parseLanguageIntoCodes', function(t) {
74-
t.deepEqual(languages.parseLanguageIntoCodes('foo'), { region: undefined, language: 'fo', locale: 'fo', script: undefined });
75-
t.deepEqual(languages.parseLanguageIntoCodes('en-US'), { region: 'US', language: 'en', locale: 'en-US', script: undefined });
76-
t.deepEqual(languages.parseLanguageIntoCodes('zh-CN'), { region: 'CN', language: 'zh', locale: 'zh-CN', script: undefined });
77-
t.deepEqual(languages.parseLanguageIntoCodes('zh-Hant'), { region: undefined, language: 'zh', locale: 'zh-Hant', script: 'Hant' });
78-
t.deepEqual(languages.parseLanguageIntoCodes('zh-Hant-TW'), { region: 'TW', language: 'zh', locale: 'zh-Hant-TW', script: 'Hant' });
79-
t.deepEqual(languages.parseLanguageIntoCodes('zh'), { region: undefined, language: 'zh', locale: 'zh', script: undefined });
80-
t.deepEqual(languages.parseLanguageIntoCodes('es-MX'), { region: 'MX', language: 'es', locale: 'es-MX', script: undefined });
81-
t.deepEqual(languages.parseLanguageIntoCodes('es-ES'), { region: 'ES', language: 'es', locale: 'es-ES', script: undefined });
82-
t.deepEqual(languages.parseLanguageIntoCodes('pt-PT'), { region: 'PT', language: 'pt', locale: 'pt-PT', script: undefined });
83-
t.deepEqual(languages.parseLanguageIntoCodes('pt'), { region: undefined, language: 'pt', locale: 'pt', script: undefined });
84-
t.end();
85-
});
86-
/* eslint-enable */

0 commit comments

Comments
 (0)