33 createTag ,
44 createTagFromOrthography ,
55 defaultRegionForLangTag ,
6+ formatDialectCode ,
67 getMaximalLangtag ,
78 getShortestSufficientLangtag ,
89 isManuallyEnteredTagLanguage ,
@@ -371,6 +372,48 @@ describe("createTagFromOrthography", () => {
371372 } )
372373 ) . toEqual ( "en-x-foobar" ) ;
373374 } ) ;
375+ it ( "should modify dialog name if necessary" , ( ) => {
376+ expect (
377+ createTagFromOrthography ( {
378+ language : {
379+ languageSubtag : "en" ,
380+ exonym : "English" ,
381+ scripts : [ ] ,
382+ iso639_3_code : "eng" ,
383+ regionNamesForDisplay : "" ,
384+ regionNamesForSearch : [ ] ,
385+ names : [ ] ,
386+ alternativeTags : [ ] ,
387+ languageType : LanguageType . Living ,
388+ isMacrolanguage : false ,
389+ } as ILanguage ,
390+ script : { code : "Latn" , name : "Latin" } as IScript ,
391+ customDetails : {
392+ dialect : "Special English!" ,
393+ } as ICustomizableLanguageDetails ,
394+ } )
395+ ) . toEqual ( "en-x-SpecialE" ) ;
396+ expect (
397+ createTagFromOrthography ( {
398+ language : {
399+ languageSubtag : "en" ,
400+ exonym : "English" ,
401+ scripts : [ ] ,
402+ iso639_3_code : "eng" ,
403+ regionNamesForDisplay : "" ,
404+ regionNamesForSearch : [ ] ,
405+ names : [ ] ,
406+ alternativeTags : [ ] ,
407+ languageType : LanguageType . Living ,
408+ isMacrolanguage : false ,
409+ } as ILanguage ,
410+ script : { code : "Latn" , name : "Latin" } as IScript ,
411+ customDetails : {
412+ dialect : "ai-newFancySmartAi" ,
413+ } as ICustomizableLanguageDetails ,
414+ } )
415+ ) . toEqual ( "en-x-ai-newFancy" ) ;
416+ } ) ;
374417} ) ;
375418
376419describe ( "isValidBcp47Tag checking is sane" , ( ) => {
@@ -381,6 +424,7 @@ describe("isValidBcp47Tag checking is sane", () => {
381424 expect ( isValidBcp47Tag ( "en-Latn-US-x-foobar" ) ) . toBeTruthy ( ) ;
382425 expect ( isValidBcp47Tag ( "en-x-foobar" ) ) . toBeTruthy ( ) ;
383426 expect ( isValidBcp47Tag ( "en-US" ) ) . toBeTruthy ( ) ;
427+ expect ( isValidBcp47Tag ( "en-x-ai-google" ) ) . toBeTruthy ( ) ;
384428 } ) ;
385429
386430 it ( "should return true for macrolang-indiv lang formatted tags, including sign language tags" , ( ) => {
@@ -426,3 +470,32 @@ describe("sanity checks for isUnlistedLanguage and isManuallyEnteredTagLanguage"
426470 ) . toEqual ( true ) ;
427471 } ) ;
428472} ) ;
473+
474+ describe ( "formatting dialect codes" , ( ) => {
475+ it ( "should return empty string for undefined or empty input" , ( ) => {
476+ expect ( formatDialectCode ( "" ) ) . toEqual ( "" ) ;
477+ expect ( formatDialectCode ( undefined ) ) . toEqual ( "" ) ;
478+ } ) ;
479+ it ( "should trim whitespace" , ( ) => {
480+ expect ( formatDialectCode ( " foo bar " ) ) . toEqual ( "foobar" ) ;
481+ } ) ;
482+ it ( "should remove illegal characters" , ( ) => {
483+ expect ( formatDialectCode ( "foo!@#$%^&*()bar" ) ) . toEqual ( "foobar" ) ;
484+ } ) ;
485+ it ( "should retain dashes" , ( ) => {
486+ expect ( formatDialectCode ( "ai-google" ) ) . toEqual ( "ai-google" ) ;
487+ } ) ;
488+ it ( "should truncate sections to 8 characters" , ( ) => {
489+ expect (
490+ formatDialectCode ( "123456789-123456789-123456789-123456789" )
491+ ) . toEqual ( "12345678-12345678-12345678-12345678" ) ;
492+ expect ( formatDialectCode ( "ai-newFancySmartAi" ) ) . toEqual ( "ai-newFancy" ) ;
493+ } ) ;
494+ it ( "various combinations" , ( ) => {
495+ expect (
496+ formatDialectCode (
497+ " 1234 5678 9-1234 5678 9-!@#$%^&*()1234 5678 9-foobar"
498+ )
499+ ) . toEqual ( "12345678-12345678-12345678-foobar" ) ;
500+ } ) ;
501+ } ) ;
0 commit comments