Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

utils.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // transform /path/to/file.ext to file.ext
  2. export function basename(path = '') {
  3. return path ? path.replace(/^.*\//, '') : '';
  4. }
  5. // transform /path/to/file.ext to .ext
  6. export function extname(path = '') {
  7. const [_, ext] = /.+(\.[^.]+)$/.exec(path) || [];
  8. return ext || '';
  9. }
  10. // test whether a variable is an object
  11. export function isObject(obj) {
  12. return Object.prototype.toString.call(obj) === '[object Object]';
  13. }
  14. // returns whether a dark theme is enabled
  15. export function isDarkTheme() {
  16. return document.documentElement.classList.contains('theme-arc-green');
  17. }
  18. // removes duplicate elements in an array
  19. export function uniq(arr) {
  20. return Array.from(new Set(arr));
  21. }
  22. // strip <tags> from a string
  23. export function stripTags(text) {
  24. return text.replace(/<[^>]*>?/gm, '');
  25. }
  26. // searches the inclusive range [minValue, maxValue].
  27. // credits: https://matthiasott.com/notes/write-your-media-queries-in-pixels-not-ems
  28. export function mqBinarySearch(feature, minValue, maxValue, step, unit) {
  29. if (maxValue - minValue < step) {
  30. return minValue;
  31. }
  32. const mid = Math.ceil((minValue + maxValue) / 2 / step) * step;
  33. if (matchMedia(`screen and (min-${feature}:${mid}${unit})`).matches) {
  34. return mqBinarySearch(feature, mid, maxValue, step, unit); // feature is >= mid
  35. }
  36. return mqBinarySearch(feature, minValue, mid - step, step, unit); // feature is < mid
  37. }