-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
198 additions
and
10 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,66 @@ | ||
import * as React from 'react'; | ||
import {IntlConfig, Formatters, IntlFormatters} from '../types'; | ||
import {filterProps, createError} from '../utils'; | ||
import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat'; | ||
|
||
const LIST_FORMAT_OPTIONS: Array<keyof IntlListFormatOptions> = [ | ||
'localeMatcher', | ||
'type', | ||
'style', | ||
]; | ||
|
||
const now = Date.now(); | ||
|
||
function generateToken(i: number) { | ||
return `${now}_${i}_${now}`; | ||
} | ||
|
||
export function formatList( | ||
{locale, onError}: Pick<IntlConfig, 'locale' | 'onError'>, | ||
getListFormat: Formatters['getListFormat'], | ||
values: Parameters<IntlFormatters['formatList']>[0], | ||
options: Parameters<IntlFormatters['formatList']>[1] = {} | ||
) { | ||
const ListFormat: typeof IntlListFormat = (Intl as any).ListFormat; | ||
if (!ListFormat) { | ||
onError( | ||
createError(`Intl.ListFormat is not available in this environment. | ||
Try polyfilling it using "@formatjs/intl-listformat" | ||
`) | ||
); | ||
} | ||
let filteredOptions = filterProps(options, LIST_FORMAT_OPTIONS); | ||
|
||
try { | ||
const richValues: Record<string, React.ReactNode> = {}; | ||
const serializedValues = values.map((v, i) => { | ||
if (typeof v === 'object') { | ||
const id = generateToken(i); | ||
richValues[id] = v; | ||
return id; | ||
} | ||
return String(v); | ||
}); | ||
if (!Object.keys(richValues).length) { | ||
return getListFormat(locale, filteredOptions).format(serializedValues); | ||
} | ||
const parts = getListFormat(locale, filteredOptions).formatToParts( | ||
serializedValues | ||
); | ||
return parts.reduce((all: Array<string | React.ReactNode>, el) => { | ||
const val = el.value; | ||
if (richValues[val]) { | ||
all.push(richValues[val]); | ||
} else if (typeof all[all.length - 1] === 'string') { | ||
all[all.length - 1] += val; | ||
} else { | ||
all.push(val); | ||
} | ||
return all; | ||
}, []); | ||
} catch (e) { | ||
onError(createError('Error formatting list.', e)); | ||
} | ||
|
||
return values; | ||
} |
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
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
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
Oops, something went wrong.