common.js 700 B

12345678910111213141516171819202122232425262728293031323334353637
  1. export function trim (str) {
  2. if (!str) return str
  3. return str.replace(/^\s+|\s+$/g, '')
  4. }
  5. export function toCommaList (arr, key = 'name') {
  6. if (!arr) return
  7. return arr.map(item => item[key]).join(', ')
  8. }
  9. export function orderByVariant (arr) {
  10. const orderedVariants = [
  11. 'lecture',
  12. 'reflexion',
  13. 'critique',
  14. 'sensible',
  15. 'echo',
  16. 'kit',
  17. 'creation'
  18. ]
  19. return [...arr].sort((a, b) => {
  20. return orderedVariants.indexOf(a.variant) > orderedVariants.indexOf(b.variant)
  21. })
  22. }
  23. export function shuffle (a) {
  24. let j, x, i
  25. for (i = a.length - 1; i > 0; i--) {
  26. j = Math.floor(Math.random() * (i + 1))
  27. x = a[i]
  28. a[i] = a[j]
  29. a[j] = x
  30. }
  31. return a
  32. }