formatter.ts 3.9 KB

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