Skip to content

Instantly share code, notes, and snippets.

@tinovyatkin
Created February 23, 2022 14:03
Show Gist options
  • Save tinovyatkin/29482f9316c57e3393e6c7c314b3ba5d to your computer and use it in GitHub Desktop.
Save tinovyatkin/29482f9316c57e3393e6c7c314b3ba5d to your computer and use it in GitHub Desktop.
list format
function namesList(names: string | readonly string[]): string {
if (typeof names === 'string') return names;
if (names instanceof Array) return names.join(', ');
throw new TypeError(
`Invalid parameter, expected a string or array of strings, received: ${names}`,
);
}
const nameList2 = (names: string | Iterable<string>) =>
typeof names === 'string'
? names
: new Intl.ListFormat('en-GB', { type: 'unit' }).format(names);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment