You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * Checks if the given Object is a correct last N limit mapping, coverts both keys and values to numbers and sorts
  3. * the keys in ascending order.
  4. *
  5. * @param {Object} lastNLimits - The Object to be verified.
  6. * @returns {undefined|Map<number, number>}
  7. */
  8. export function validateLastNLimits(lastNLimits) {
  9. // Checks if only numbers are used
  10. if (typeof lastNLimits !== 'object'
  11. || !Object.keys(lastNLimits).length
  12. || Object.keys(lastNLimits)
  13. .find(limit => limit === null || isNaN(Number(limit))
  14. || lastNLimits[limit] === null || isNaN(Number(lastNLimits[limit])))) {
  15. return undefined;
  16. }
  17. // Converts to numbers and sorts the keys
  18. const sortedMapping = new Map();
  19. const orderedLimits = Object.keys(lastNLimits)
  20. .map(n => Number(n))
  21. .sort((n1, n2) => n1 - n2);
  22. for (const limit of orderedLimits) {
  23. sortedMapping.set(limit, Number(lastNLimits[limit]));
  24. }
  25. return sortedMapping;
  26. }
  27. /**
  28. * Returns "last N" value which corresponds to a level defined in the {@code lastNLimits} mapping. See
  29. * {@code config.js} for more detailed explanation on how the mapping is defined.
  30. *
  31. * @param {number} participantsCount - The current number of participants in the conference.
  32. * @param {Map<number, number>} [lastNLimits] - The mapping of number of participants to "last N" values. NOTE that
  33. * this function expects a Map that has been preprocessed by {@link validateLastNLimits}, because the keys must be
  34. * sorted in ascending order and both keys and values should be numbers.
  35. * @returns {number|undefined} - A "last N" number if there was a corresponding "last N" value matched with the number
  36. * of participants or {@code undefined} otherwise.
  37. */
  38. export function limitLastN(participantsCount, lastNLimits) {
  39. if (!lastNLimits || !lastNLimits.keys) {
  40. return undefined;
  41. }
  42. let selectedLimit;
  43. for (const participantsN of lastNLimits.keys()) {
  44. if (participantsCount >= participantsN) {
  45. selectedLimit = participantsN;
  46. }
  47. }
  48. return selectedLimit ? lastNLimits.get(selectedLimit) : undefined;
  49. }