NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const convertTitleCase = type => {
  2. if (typeof type !== 'string') return ''
  3. return type.charAt(0).toUpperCase() + type.slice(1)
  4. }
  5. const dePluralize = type => {
  6. return type[type.length - 1] === 's' ? type.slice(0, -1) : type
  7. }
  8. const sortTypes = {
  9. alpha: 'by-alpha',
  10. recent: 'by-recent',
  11. material: 'by-material',
  12. artist: 'by-artist',
  13. episode: 'by-episode',
  14. upcoming: 'by-upcoming',
  15. current: 'by-current',
  16. currentAndUpcoming: 'by-current-and-upcoming',
  17. past: 'by-past',
  18. }
  19. /**
  20. * A list of custom post types used to dispatch vuex actions
  21. * This makes ALL post type modules available for the
  22. * list and single components to request data
  23. */
  24. const postTypes = [
  25. 'sticky',
  26. 'episode',
  27. 'artist',
  28. 'exhibition',
  29. 'event',
  30. 'post',
  31. 'page',
  32. ]
  33. /**
  34. * Type assigned from Route :type
  35. * In case of failure, tries to derive type
  36. * matching pieces to postTypes array
  37. */
  38. const typeFromRoute = route => {
  39. let type = route.params.type ? route.params.type : route.fullPath.split('/')
  40. if (!route.params.type) {
  41. // Remove blank path sections and match to postTypes array
  42. type = type
  43. .filter(pathSection => pathSection != '')
  44. .filter(pathSection => postTypes.includes(pathSection))
  45. // Only take the first match
  46. type = type[0]
  47. // console.log(`type derived from route.pajth: ${type}`)
  48. }
  49. // console.log('type from route:',type)
  50. return type
  51. }
  52. const ytThumbnail = (url, desiredSize) => {
  53. const remove = [
  54. '',
  55. 'https:',
  56. 'http:',
  57. 'youtu.be',
  58. 'www.youtube.com',
  59. 'youtube.com',
  60. 'embed',
  61. ]
  62. const videoId = url
  63. .split('/')
  64. .filter(urlSection => !remove.includes(urlSection))
  65. // Uncomment to see what the url transformer is finding
  66. // console.log('video:', videoId[0])
  67. let size = 'maxresdefault'
  68. switch (desiredSize) {
  69. case 'medium':
  70. size = 'mqdefault'
  71. break
  72. case 'large':
  73. size = 'hqdefault'
  74. break
  75. case 'standard':
  76. size = 'sddefault'
  77. break
  78. case 'max':
  79. size = 'maxresdefault'
  80. break
  81. }
  82. return `https://img.youtube.com/vi/${videoId[0]}/${size}.jpg`
  83. }
  84. const formatDate = (unix, includeTime) => {
  85. const d = new Date(parseInt(unix) * 1000)
  86. return includeTime ? d.toLocaleString('en-US', { timeZone: 'UTC' }) : d.toLocaleDateString('en-US', { timeZone: 'UTC' })
  87. }
  88. export {
  89. convertTitleCase,
  90. dePluralize,
  91. typeFromRoute,
  92. sortTypes,
  93. postTypes,
  94. ytThumbnail,
  95. formatDate
  96. }