formatter.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2012-2024 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 const 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 const 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. }
  35. export const camelize = (str: string) => {
  36. return str.replace(/[_.-](\w|$)/g, (_, x) => x.toUpperCase())
  37. }
  38. export const capitalize = (str: string) => {
  39. return str[0].toUpperCase() + str.slice(1)
  40. }
  41. export const toClassName = (str: string) => {
  42. return str.replace(
  43. /([a-z])([A-Z])/g,
  44. (_, lowerCase, upperCase) => `${lowerCase}::${upperCase}`,
  45. )
  46. }
  47. // app/assets/javascripts/app/lib/app_post/utils.coffee:230
  48. export const phoneify = (phone: string) => {
  49. return phone.replace(/[^0-9,+,#,*]+/g, '').replace(/(.)\+/, '$1')
  50. }
  51. /**
  52. * Returns user's initials based on their first name, last name and email, if any present.
  53. * @param firstname - user's first name
  54. * @param lastname - user's last name
  55. * @param email - user's email address
  56. * @param phone - user's phone number
  57. * @param mobile - user's mobile number
  58. */
  59. export const getInitials = (
  60. firstname?: Maybe<string>,
  61. lastname?: Maybe<string>,
  62. email?: Maybe<string>,
  63. phone?: Maybe<string>,
  64. mobile?: Maybe<string>,
  65. ) => {
  66. if (firstname && lastname) {
  67. return firstname[0] + lastname[0]
  68. }
  69. if (firstname || lastname || email) {
  70. return (firstname || lastname || email)?.substring(0, 2).toUpperCase()
  71. }
  72. if (phone || mobile) {
  73. return (phone || mobile)?.slice(-2).toUpperCase()
  74. }
  75. return '??'
  76. }
  77. /**
  78. * Replaces code inside `#{obj.key}` with the value of the corresponding object.
  79. * @param template - string to replace
  80. * @param objects - reference object
  81. * @param encodeLink - should result be encoded
  82. */
  83. export const replaceTags = (
  84. template: string,
  85. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  86. objects: any,
  87. encodeLink = false,
  88. ): string => {
  89. return template.replace(/#\{\s{0,2}(.+?)\s{0,2}\}/g, (index, key) => {
  90. const levels = key.replace(/<.+?>/g, '').split(/\./)
  91. let dataRef = objects
  92. for (const level of levels) {
  93. if (typeof dataRef === 'object' && level in dataRef) {
  94. dataRef = dataRef[level]
  95. } else {
  96. dataRef = ''
  97. break
  98. }
  99. }
  100. let value
  101. // if value is a function, execute function
  102. if (typeof dataRef === 'function') {
  103. value = dataRef()
  104. }
  105. // if value has content
  106. else if (dataRef != null && dataRef.toString) {
  107. // in case if we have a references object, check what datatype the attribute has
  108. // and e. g. convert timestamps/dates to browser locale
  109. // if dataRefLast?.constructor?.className
  110. // localClassRef = App[dataRefLast.constructor.className]
  111. // if localClassRef?.attributesGet
  112. // attributes = localClassRef.attributesGet()
  113. // if attributes?[level]
  114. // if attributes[level]['tag'] is 'datetime'
  115. // value = App.i18n.translateTimestamp(dataRef)
  116. // else if attributes[level]['tag'] is 'date'
  117. // value = App.i18n.translateDate(dataRef)
  118. // as fallback use value of toString()
  119. if (!value) value = dataRef.toString()
  120. } else {
  121. value = ''
  122. }
  123. if (value === '') value = '-'
  124. if (encodeLink) value = encodeURIComponent(value)
  125. return value
  126. })
  127. }