1
0

utils.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. export function formatDuration(duration, text = false) {
  2. duration = Math.floor(duration);
  3. var seconds = duration % 60;
  4. const minutes = Math.floor(duration / 60);
  5. if (seconds < 10) {
  6. seconds = '0' + seconds;
  7. }
  8. return `${minutes}${text?'m':''}:${seconds}${text?'s':''}`;
  9. }
  10. export function getQueryString(params) {
  11. var esc = encodeURIComponent;
  12. return Object.keys(params)
  13. .map(k => esc(k) + '=' + esc(params[k]))
  14. .join('&');
  15. }
  16. const SUBST = {
  17. ':':'-colon-',
  18. '/':'-slash-',
  19. '\\':'rslash',
  20. '?':'-qmark-',
  21. '#':'-hash-',
  22. '[':'-obr-',
  23. ']':'-cbr-',
  24. '@':'-at-',
  25. '~':'-tilda-',
  26. ' ':'_',
  27. '_':'-und-',
  28. '‐':'-hyphen-',
  29. '-':'-dash-'
  30. };
  31. const UNSUBST = Object.assign({}, ...Object.entries(SUBST).map(([a,b]) => ({[b]: a})))
  32. const FORW = new RegExp(/[:/\\?#[\]@~ _‐-]/, 'g');
  33. const BACK = new RegExp(`(${Object.values(SUBST).join('|')})`,'g');
  34. //export function slugify(str) { return str && encodeURI(str.replace(FORW, e=>SUBST[e])) }
  35. //export function unslugify(str) { return str && decodeURI(str).replace(BACK, e=>UNSUBST[e]) }
  36. export function slugify(str) { return str && str.replace(FORW, e=>SUBST[e]) }
  37. export function unslugify(str) { return str && str.replace(BACK, e=>UNSUBST[e]) }