diff --git a/framework/core/js/src/common/helpers/punctuateSeries.js b/framework/core/js/src/common/helpers/punctuateSeries.js deleted file mode 100644 index ad8c70592d..0000000000 --- a/framework/core/js/src/common/helpers/punctuateSeries.js +++ /dev/null @@ -1,37 +0,0 @@ -import app from '../../common/app'; - -/** - * The `punctuateSeries` helper formats a list of strings (e.g. names) to read - * fluently in the application's locale. - * - * ```js - * punctuateSeries(['Toby', 'Franz', 'Dominion']) // Toby, Franz, and Dominion - * ``` - * - * @param {import('mithril').Children[]} items - * @return {import('mithril').Children}')} - */ -export default function punctuateSeries(items) { - if (items.length === 2) { - return app.translator.trans('core.lib.series.two_text', { - first: items[0], - second: items[1], - }); - } else if (items.length >= 3) { - // If there are three or more items, we will join all but the first and - // last items with the equivalent of a comma, and then we will feed that - // into the translator along with the first and last item. - const second = items - .slice(1, items.length - 1) - .reduce((list, item) => list.concat([item, app.translator.trans('core.lib.series.glue_text')]), []) - .slice(0, -1); - - return app.translator.trans('core.lib.series.three_text', { - first: items[0], - second, - third: items[items.length - 1], - }); - } - - return items; -} diff --git a/framework/core/js/src/common/helpers/punctuateSeries.ts b/framework/core/js/src/common/helpers/punctuateSeries.ts new file mode 100644 index 0000000000..328d22e1ed --- /dev/null +++ b/framework/core/js/src/common/helpers/punctuateSeries.ts @@ -0,0 +1,34 @@ +import type { Children } from 'mithril'; +import app from '../../common/app'; + +/** + * Formats a list of strings/nodes to read fluently in the locale. + */ +export default function punctuateSeries(items: Children[] = []): Children { + if (items.length === 2) { + return app.translator.trans('core.lib.series.two_text', { + first: items[0], + second: items[1], + }); + } + + if (items.length >= 3) { + const glue = app.translator.trans('core.lib.series.glue_text') as Children; + + // Build middle items safely: [item, glue, item, glue, ...] + const second: Children[] = []; + for (let i = 1; i < items.length - 1; i++) { + second.push(items[i]); + if (i < items.length - 2) second.push(glue); + } + + return app.translator.trans('core.lib.series.three_text', { + first: items[0], + second, + third: items[items.length - 1], + }); + } + + // 0 or 1 item: return as-is (Mithril can render arrays too) + return items; +}