formatter.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. export const getFullName = (
  52. firstname?: Maybe<string>,
  53. lastname?: Maybe<string>,
  54. ): string => {
  55. const fullname = [firstname, lastname].filter(Boolean).join(' ')
  56. if (fullname === '-') return ''
  57. return fullname
  58. }
  59. /**
  60. * Returns user's initials based on their first name, last name and email, if any present.
  61. * @param firstname - user's first name
  62. * @param lastname - user's last name
  63. * @param email - user's email address
  64. */
  65. export const getInitials = (
  66. firstname?: Maybe<string>,
  67. lastname?: Maybe<string>,
  68. email?: Maybe<string>,
  69. ) => {
  70. if (firstname && lastname) {
  71. return firstname[0] + lastname[0]
  72. }
  73. return (firstname || lastname || email)?.substring(0, 2).toUpperCase() || '??'
  74. }
  75. /**
  76. * Replaces code inside `#{obj.key}` with the value of the corresponding object.
  77. * @param template - string to replace
  78. * @param objects - reference object
  79. * @param encodeLink - should result be encoded
  80. */
  81. export const replaceTags = (
  82. template: string,
  83. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  84. objects: any,
  85. encodeLink = false,
  86. ): string => {
  87. return template.replace(/#\{\s{0,2}(.+?)\s{0,2}\}/g, (index, key) => {
  88. const levels = key.replace(/<.+?>/g, '').split(/\./)
  89. let dataRef = objects
  90. for (const level of levels) {
  91. if (typeof dataRef === 'object' && level in dataRef) {
  92. dataRef = dataRef[level]
  93. } else {
  94. dataRef = ''
  95. break
  96. }
  97. }
  98. let value
  99. // if value is a function, execute function
  100. if (typeof dataRef === 'function') {
  101. value = dataRef()
  102. }
  103. // if value has content
  104. else if (dataRef != null && dataRef.toString) {
  105. // in case if we have a references object, check what datatype the attribute has
  106. // and e. g. convert timestamps/dates to browser locale
  107. // if dataRefLast?.constructor?.className
  108. // localClassRef = App[dataRefLast.constructor.className]
  109. // if localClassRef?.attributesGet
  110. // attributes = localClassRef.attributesGet()
  111. // if attributes?[level]
  112. // if attributes[level]['tag'] is 'datetime'
  113. // value = App.i18n.translateTimestamp(dataRef)
  114. // else if attributes[level]['tag'] is 'date'
  115. // value = App.i18n.translateDate(dataRef)
  116. // as fallback use value of toString()
  117. if (!value) value = dataRef.toString()
  118. } else {
  119. value = ''
  120. }
  121. if (value === '') value = '-'
  122. if (encodeLink) value = encodeURIComponent(value)
  123. return value
  124. })
  125. }