function formatSelectedOptionsText()

in components/src/sharing-setting/sharing-setting.js [14:48]


function formatSelectedOptionsText(
  selectedUsersOrGroups, implicitSelectedUsers = []
) {
  if (!selectedUsersOrGroups.length) {
    return implicitSelectedUsers.length
      ? `${i18n('Owner')} (${makeLabelFromArray(implicitSelectedUsers)})`
      : i18n('Owner');
  }

  const allUsersGroup = (selectedUsersOrGroups || []).
    filter(group => group.allUsersGroup)[0];
  if (allUsersGroup) {
    return allUsersGroup.name;
  }
  return makeLabelFromArray([
    ...hideUsersFromList(selectedUsersOrGroups, implicitSelectedUsers),
    ...implicitSelectedUsers]
  );

  function makeLabelFromArray(usersOrGroups) {
    const names = usersOrGroups.
      filter((s, i, arr) => arr.findIndex(it => it.id === s.id) === i).
      map(s => s.name);

    if (names.length === 1) {
      return names[0];
    }

    const namesToDisplay = 2;
    const othersNum = names.length - namesToDisplay;
    const others = othersNum > 0 ? ` +${othersNum}` : '';

    return `${names[0]}, ${names[1]}${others}`;
  }
}