formatter.ts 1011 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. /**
  3. * Creates an oxford-comma separated list of items.
  4. * @param args - items to list out
  5. * @param conjunction - in: x, y, and z "and" is the conjunction to use
  6. * @returns
  7. */
  8. export function commaSeparatedList(
  9. items: string[],
  10. conjunction = 'or',
  11. ): string {
  12. return items.reduce((oxford, item, index) => {
  13. let oxfordList = oxford + item
  14. if (index <= items.length - 2 && items.length > 2) {
  15. oxfordList += ', '
  16. }
  17. if (index === items.length - 2) {
  18. oxfordList += `${items.length === 2 ? ' ' : ''}${conjunction} `
  19. }
  20. return oxfordList
  21. }, '')
  22. }
  23. /**
  24. * Orders two variables smallest to largest.
  25. * @param first - first argument
  26. * @param second - Second argument
  27. * @returns
  28. */
  29. export function order(
  30. first: string | number,
  31. second: string | number,
  32. ): [smaller: number | string, larger: number | string] {
  33. return Number(first) >= Number(second) ? [second, first] : [first, second]
  34. }