From 32adc18c0fabad9552ed8f99d867129d923099d7 Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Fri, 3 Jun 2022 16:56:47 -0500 Subject: [PATCH] Enable random and suffix options --- helpers/categories.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/helpers/categories.js b/helpers/categories.js index 6279415..8f5ca75 100644 --- a/helpers/categories.js +++ b/helpers/categories.js @@ -1,4 +1,6 @@ // Universal JS imports only +import shuffle from 'just-shuffle' + import { makeSlug } from './slug.js' export function makeCategorySlug ( categoryName ) { @@ -262,9 +264,19 @@ export function findCategoryForTagsSet ( tags ) { return null } -export function makeSummaryOfListings ({ list, length = 25 } = {}) { - return list +const sampleListFormatter = new Intl.ListFormat( 'en', { style: 'long', type: 'unit' }) + +export function makeSummaryOfListings ({ + list, + length = 25, + random = false, + suffix = ', etc...', +} = {}) { + const listToSample = random ? shuffle( list ) : list + + const samples = listToSample .slice(0, length) .map( listing => listing.name ) - .join(', ') + ', etc...' + + return sampleListFormatter.format( samples ) + suffix }