From cae2664bca9a82660560739074a28fdedb57a414 Mon Sep 17 00:00:00 2001 From: Bjorn Stromberg Date: Wed, 9 Aug 2023 09:24:01 +0800 Subject: [PATCH] Use Intl.ListFormat --- source/index.ts | 21 +++------------------ tsconfig.json | 8 +++++++- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/source/index.ts b/source/index.ts index 978bd51..137dd40 100644 --- a/source/index.ts +++ b/source/index.ts @@ -799,28 +799,13 @@ function unique(values: T[]): T[] { return Array.from(new Set(values)); } -function joinWithWord(values: string[], word: 'and' | 'or'): string { - switch (values.length) { - case 0: - case 1: { - return values.join(''); - } - - case 2: { - return values.join(` ${word} `); - } - - default: { - // TODO: Replace with .at after node v14 support is dropped - return `${[...values].slice(0, -1).join(', ')}, ${word} ${values[values.length - 1] ?? ''}`; - } - } -} +const andFormatter = new Intl.ListFormat('en', {style: 'long', type: 'conjunction'}); +const orFormatter = new Intl.ListFormat('en', {style: 'long', type: 'disjunction'}); function typeErrorMessageMultipleValues(expectedType: AssertionTypeDescription | AssertionTypeDescription[], values: unknown[]): string { const uniqueExpectedTypes = unique((isArray(expectedType) ? expectedType : [expectedType]).map(value => `\`${value}\``)); const uniqueValueTypes = unique(values.map(value => `\`${is(value)}\``)); - return `Expected values which are ${joinWithWord(uniqueExpectedTypes, 'or')}. Received values of type${uniqueValueTypes.length > 1 ? 's' : ''} ${joinWithWord(uniqueValueTypes, 'and')}.`; + return `Expected values which are ${orFormatter.format(uniqueExpectedTypes)}. Received values of type${uniqueValueTypes.length > 1 ? 's' : ''} ${andFormatter.format(uniqueValueTypes)}.`; } // Type assertions have to be declared with an explicit type. diff --git a/tsconfig.json b/tsconfig.json index e1c05ee..5275116 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,13 @@ { "extends": "@sindresorhus/tsconfig", "compilerOptions": { - "outDir": "dist" + "lib": [ + "DOM", + "DOM.Iterable", + "ES2021" + ], + "outDir": "dist", + "target": "ES2021" }, "include": [ "source"