2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* The `punctuate` helper formats a list of strings (e.g. names) to read
|
|
|
|
* fluently in the application's locale.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* punctuate(['Toby', 'Franz', 'Dominion'])
|
|
|
|
* // Toby, Franz, and Dominion
|
|
|
|
*
|
|
|
|
* @param {Array} items
|
|
|
|
* @return {Array}
|
|
|
|
*/
|
2015-05-14 21:01:24 +08:00
|
|
|
export default function punctuate(items) {
|
2015-07-15 12:30:11 +08:00
|
|
|
const punctuated = [];
|
2015-05-14 21:01:24 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
// FIXME: update to use translation
|
2015-05-14 21:01:24 +08:00
|
|
|
items.forEach((item, i) => {
|
2015-07-15 12:30:11 +08:00
|
|
|
punctuated.push(item);
|
2015-05-14 21:01:24 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
// If this item is not the last one, then we will follow it with some
|
|
|
|
// punctuation. If the list is more than 2 items long, we'll add a comma.
|
|
|
|
// And if this is the second-to-last item, we'll add 'and'.
|
|
|
|
if (i < items.length - 1) {
|
|
|
|
punctuated.push((items.length > 2 ? ', ' : '') + (i === items.length - 2 ? ' and ' : ''));
|
2015-05-14 21:01:24 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
return punctuated;
|
2015-05-14 21:01:24 +08:00
|
|
|
};
|