Skip to content

Commit

Permalink
refactor(ssr): use Intl.ListFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Aug 20, 2024
1 parent 39c0855 commit dddd10c
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions ssr/page-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ export function getPageDescription(doc: Doc): string {
.filter(([section]) => sections.includes(section))
.map(([, text]) => text);

const syntaxContents = enumerate(...syntaxItems);
const otherContents = enumerate(...otherItems);
const listFormatter = new Intl.ListFormat("en", {
style: "long",
type: "conjunction",
});
const syntaxContents = listFormatter.format(syntaxItems);
const otherContents = listFormatter.format(otherItems);
const contents = [
syntaxContents ? `its ${syntaxContents}` : "",
otherContents,
Expand Down Expand Up @@ -71,11 +75,3 @@ export function getPageDescription(doc: Doc): string {

return doc.summary;
}

function enumerate(...items: string[]) {
items = items.filter(Boolean);

const lastItem = items.pop();

return [items.join(", "), lastItem].filter(Boolean).join(", and ");
}

0 comments on commit dddd10c

Please sign in to comment.