Icu 10307 create a new helper (#1791)

* chore: 🤖 truncate helper for longer list
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2023-07-25 14:14:19 -07:00
1 parent f16f295290
commit b12427a02a
6 files changed
+137 -1

No files matched your search

@@ -0,0 +1,42 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
/**
* Use this helper to truncate the list if there are more items than the limit specified
*
* @example
*
* {{truncate-list array limit}}
*/
import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
export default class extends Helper {
// =services
@service intl;
// =compute method
/**
* This helper truncates the list if there are more items than the limit specified
* and returns the original list appended with +N remaining items
* @param {array} list List of items
* @return string
*/
compute([translation, list, limit = 3]) {
if (list) {
const numberOfItems = Object.keys(list).length;
const items = list.map(({ value }) => value);
if (numberOfItems > limit) {
const remainingItems = items.splice(limit).length;
return items.join(', ') + this.intl.t(translation, { remainingItems });
} else {
return items.join(', ');
}
}
}
}