diff options
author | Phil Hughes <me@iamphill.com> | 2019-02-05 10:33:47 +0000 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2019-02-05 10:33:47 +0000 |
commit | 97041bb6c480e94593b1b7ee3102149ad30804dc (patch) | |
tree | 5a076061868f692f26cfb59c01ec6485997f8395 /app/assets/javascripts/lib | |
parent | 068b41275f6f142acfddca273ace68d03a6c0730 (diff) | |
parent | 9cfb253ed4ebf08c254db22c2da15a0b0d92c61e (diff) | |
download | gitlab-ce-97041bb6c480e94593b1b7ee3102149ad30804dc.tar.gz |
Merge branch 'fe-grammar-util' into 'master'
FE grammar util
See merge request gitlab-org/gitlab-ce!24893
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r-- | app/assets/javascripts/lib/utils/grammar.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/utils/grammar.js b/app/assets/javascripts/lib/utils/grammar.js new file mode 100644 index 00000000000..18f9e2ed846 --- /dev/null +++ b/app/assets/javascripts/lib/utils/grammar.js @@ -0,0 +1,40 @@ +import { sprintf, s__ } from '~/locale'; + +/** + * Combines each given item into a noun series sentence fragment. It does this + * in a way that supports i18n by giving context and punctuation to the locale + * functions. + * + * **Examples:** + * + * - `["A", "B"] => "A and B"` + * - `["A", "B", "C"] => "A, B, and C"` + * + * **Why only nouns?** + * + * Some languages need a bit more context to translate other series. + * + * @param {String[]} items + */ +export const toNounSeriesText = items => { + if (items.length === 0) { + return ''; + } else if (items.length === 1) { + return items[0]; + } else if (items.length === 2) { + return sprintf(s__('nounSeries|%{firstItem} and %{lastItem}'), { + firstItem: items[0], + lastItem: items[1], + }); + } + + return items.reduce((item, nextItem, idx) => + idx === items.length - 1 + ? sprintf(s__('nounSeries|%{item}, and %{lastItem}'), { item, lastItem: nextItem }) + : sprintf(s__('nounSeries|%{item}, %{nextItem}'), { item, nextItem }), + ); +}; + +export default { + toNounSeriesText, +}; |