Return an empty array if there are no languages in territory

Only JavaScript is modified here. The corresponding PHP function
is unchanged to preserve consistency with other PHP functions.

Corresponding pull request in jquery.uls:
https://github.com/wikimedia/jquery.uls/pull/370

Downstream bug:
https://phabricator.wikimedia.org/T265158
This commit is contained in:
Amir Aharoni
2020-12-23 10:00:23 +02:00
committed by Niklas Laxström
parent 7093fe88cf
commit 4a61360950
2 changed files with 6 additions and 1 deletions

View File

@@ -281,7 +281,7 @@ function getDir( language ) {
* @return {string[]} list of language codes * @return {string[]} list of language codes
*/ */
function getLanguagesInTerritory( territory ) { function getLanguagesInTerritory( territory ) {
return languageData.territories[ territory ]; return languageData.territories[ territory ] || [];
} }
/** /**

View File

@@ -142,6 +142,11 @@ describe( 'languagedata', function () {
], 'Correct regions of the Laz language were selected' ); ], 'Correct regions of the Laz language were selected' );
assert.strictEqual( languageData.getRegions( 'no-such-language' ), 'UNKNOWN', 'The region of an invalid language is "UNKNOWN"' ); assert.strictEqual( languageData.getRegions( 'no-such-language' ), 'UNKNOWN', 'The region of an invalid language is "UNKNOWN"' );
assert.ok( languageData.getLanguagesInTerritory( 'RU' ).includes( 'sah' ), 'Sakha language is spoken in Russia' ); assert.ok( languageData.getLanguagesInTerritory( 'RU' ).includes( 'sah' ), 'Sakha language is spoken in Russia' );
assert.deepEqual(
languageData.getLanguagesInTerritory( 'no-such-country' ),
[],
'An invalid country has no languages and returns an empty array'
);
languagesAM = [ 'atj', 'chr', 'chy', 'cr', 'en', 'es', 'fr', 'gn', 'haw', 'ike-cans', 'ik', 'kl', 'nl', 'pt', 'qu', 'srn', 'yi' ]; languagesAM = [ 'atj', 'chr', 'chy', 'cr', 'en', 'es', 'fr', 'gn', 'haw', 'ike-cans', 'ik', 'kl', 'nl', 'pt', 'qu', 'srn', 'yi' ];
assert.deepEqual( assert.deepEqual(