From 8156f239680f625fba41d370f9d0916a725cd8ad Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 14 May 2015 22:31:24 +0930 Subject: [PATCH] Add helper function for punctuating a list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit e.g. [1,2,3] ⇒ [1, ‘, ‘, 2, ‘, and’, 3] --- framework/core/js/lib/helpers/punctuate.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 framework/core/js/lib/helpers/punctuate.js diff --git a/framework/core/js/lib/helpers/punctuate.js b/framework/core/js/lib/helpers/punctuate.js new file mode 100644 index 000000000..a208093e6 --- /dev/null +++ b/framework/core/js/lib/helpers/punctuate.js @@ -0,0 +1,13 @@ +export default function punctuate(items) { + var newItems = []; + + items.forEach((item, i) => { + newItems.push(item); + + if (i <= items.length - 2) { + newItems.push((items.length > 2 ? ', ' : '')+(i === items.length - 2 ? ' and ' : '')); + } + }); + + return newItems; +};