helpers.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import moment from 'moment'
  2. function date2s (date) {
  3. return moment(date).format('DD.MM.')
  4. }
  5. function time2s (date) {
  6. return moment(date).format('HH:mm')
  7. }
  8. function datetime2s (date) {
  9. return moment(date).format('DD.MM. HH:mm')
  10. }
  11. function normalize (item, type) {
  12. return item ? String(item).replace(/\s+/g, ' ').trim() : null
  13. }
  14. function fileSize (int) {
  15. let value
  16. if (int > Math.pow(2, 10)) {
  17. value = `${int / Math.pow(2, 10)}kB`
  18. }
  19. if (int > Math.pow(2, 20)) {
  20. value = `${int / Math.pow(2, 20)}MB`
  21. }
  22. if (int > Math.pow(2, 30)) {
  23. value = `${int / Math.pow(2, 30)}GB`
  24. }
  25. return value
  26. }
  27. function normalizePhone (item) {
  28. let phone = String(item).replace(/\s|\+|\/|,|-|'/g, '').replace(/\(0\)/, '').replace(/^0+/, '')
  29. if (phone.match(/[^\d*]/)) {
  30. return `FEHLER (nicht-numerische Zeichen): ${phone}`
  31. }
  32. if (phone.length === 0) {
  33. return null
  34. } else if (phone.length === 9) {
  35. // Assume swiss number
  36. phone = `+41${phone}`
  37. } else if (phone.length === 11) {
  38. phone = `+${phone}`
  39. } else {
  40. return `FEHLER (falsche Länge): ${phone}`
  41. }
  42. return phone
  43. }
  44. function filterFuzzy (item, filter) {
  45. const lowerCaseItem = item.toLowerCase()
  46. const substrings = filter.toLowerCase().split(/\s+/)
  47. let lastIndex = 0
  48. const indices = substrings.map(substring => {
  49. const index = lowerCaseItem.subString(lastIndex).indexOf(substring)
  50. if (index === -1) {
  51. return null
  52. } else {
  53. const startIndex = lastIndex + index
  54. const endIndex = startIndex + substring.length
  55. lastIndex = endIndex
  56. return {startIndex, endIndex}
  57. }
  58. })
  59. return indices
  60. }
  61. function filterExact (item, filter) {
  62. return item === filter
  63. }
  64. export { date2s, time2s, datetime2s, normalize, normalizePhone, fileSize, filterFuzzy, filterExact }