-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add list getSeparator utility (#107)
- Loading branch information
Showing
4 changed files
with
108 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { getLanguage } from './common.js'; | ||
|
||
const nbsp = '\xa0'; | ||
|
||
/** | ||
* Returns a region-specific list separator for the current language. | ||
* @param { Boolean } nonBreaking Whether to replace spaces with non-breaking spaces. | ||
*/ | ||
export function getSeparator({ nonBreaking } = {}) { | ||
const langTag = getLanguage(); | ||
const baseLanguage = langTag.split('-')[0]; | ||
const space = nonBreaking ? nbsp : ' '; | ||
|
||
// In this function, most languages return the separator used by the Intl.ListFormat() API with options: | ||
// { style: 'short', type: 'conjunction' } | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat | ||
// Arabic, due to some inconsistencies, returns the separator used by Google Translate. | ||
|
||
if (baseLanguage === 'ar') return `${space}،${space}`; | ||
if (['ja', 'zh'].includes(baseLanguage)) return '、'; | ||
return `,${space}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { getDocumentLocaleSettings } from '../lib/common.js'; | ||
import { getSeparator } from '../lib/list.js'; | ||
|
||
const expect = chai.expect; | ||
|
||
describe('getSeparator', () => { | ||
|
||
const documentLocaleSettings = getDocumentLocaleSettings(); | ||
|
||
afterEach(() => documentLocaleSettings.reset()); | ||
|
||
[ | ||
{ locale: 'ar', expect: [' ، ', '\xa0،\xa0'] }, | ||
{ locale: 'ar-SA', expect: [' ، ', '\xa0،\xa0'] }, | ||
{ locale: 'cy-GB', expect: [', ', ',\xa0'] }, | ||
{ locale: 'da', expect: [', ', ',\xa0'] }, | ||
{ locale: 'da-DK', expect: [', ', ',\xa0'] }, | ||
{ locale: 'de', expect: [', ', ',\xa0'] }, | ||
{ locale: 'de-DE', expect: [', ', ',\xa0'] }, | ||
{ locale: 'en', expect: [', ', ',\xa0'] }, | ||
{ locale: 'en-CA', expect: [', ', ',\xa0'] }, | ||
{ locale: 'en-GB', expect: [', ', ',\xa0'] }, | ||
{ locale: 'en-US', expect: [', ', ',\xa0'] }, | ||
{ locale: 'es', expect: [', ', ',\xa0'] }, | ||
{ locale: 'es-ES', expect: [', ', ',\xa0'] }, | ||
{ locale: 'es-MX', expect: [', ', ',\xa0'] }, | ||
{ locale: 'fr', expect: [', ', ',\xa0'] }, | ||
{ locale: 'fr-CA', expect: [', ', ',\xa0'] }, | ||
{ locale: 'fr-FR', expect: [', ', ',\xa0'] }, | ||
{ locale: 'fr-ON', expect: [', ', ',\xa0'] }, | ||
{ locale: 'hi', expect: [', ', ',\xa0'] }, | ||
{ locale: 'hi-IN', expect: [', ', ',\xa0'] }, | ||
{ locale: 'ja', expect: ['、', '、'] }, | ||
{ locale: 'ja-JP', expect: ['、', '、'] }, | ||
{ locale: 'ko', expect: [', ', ',\xa0'] }, | ||
{ locale: 'ko-KR', expect: [', ', ',\xa0'] }, | ||
{ locale: 'nl', expect: [', ', ',\xa0'] }, | ||
{ locale: 'nl-NL', expect: [', ', ',\xa0'] }, | ||
{ locale: 'pt', expect: [', ', ',\xa0'] }, | ||
{ locale: 'pt-BR', expect: [', ', ',\xa0'] }, | ||
{ locale: 'sv', expect: [', ', ',\xa0'] }, | ||
{ locale: 'sv-SE', expect: [', ', ',\xa0'] }, | ||
{ locale: 'tr', expect: [', ', ',\xa0'] }, | ||
{ locale: 'tr-TR', expect: [', ', ',\xa0'] }, | ||
{ locale: 'zh', expect: ['、', '、'] }, | ||
{ locale: 'zh-CN', expect: ['、', '、'] }, | ||
{ locale: 'zh-TW', expect: ['、', '、'] }, | ||
].forEach((input) => { | ||
it(`should return the separator for locale "${input.locale}"`, () => { | ||
documentLocaleSettings.language = input.locale; | ||
expect(getSeparator()).to.equal(input.expect[0]); | ||
expect(getSeparator({ nonBreaking: true })).to.equal(input.expect[1]); | ||
}); | ||
}); | ||
|
||
}); |