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.

utils.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // join a list of path segments with slashes, ensuring no double slashes
  11. export function joinPaths(...parts) {
  12. let str = '';
  13. for (const part of parts) {
  14. if (!part) continue;
  15. str = !str ? part : `${str.replace(/\/$/, '')}/${part.replace(/^\//, '')}`;
  16. }
  17. return str;
  18. }
  19. // test whether a variable is an object
  20. export function isObject(obj) {
  21. return Object.prototype.toString.call(obj) === '[object Object]';
  22. }
  23. // returns whether a dark theme is enabled
  24. export function isDarkTheme() {
  25. return document.documentElement.classList.contains('theme-arc-green');
  26. }
  27. // removes duplicate elements in an array
  28. export function uniq(arr) {
  29. return Array.from(new Set(arr));
  30. }
  31. // strip <tags> from a string
  32. export function stripTags(text) {
  33. return text.replace(/<[^>]*>?/gm, '');
  34. }
  35. // searches the inclusive range [minValue, maxValue].
  36. // credits: https://matthiasott.com/notes/write-your-media-queries-in-pixels-not-ems
  37. export function mqBinarySearch(feature, minValue, maxValue, step, unit) {
  38. if (maxValue - minValue < step) {
  39. return minValue;
  40. }
  41. const mid = Math.ceil((minValue + maxValue) / 2 / step) * step;
  42. if (matchMedia(`screen and (min-${feature}:${mid}${unit})`).matches) {
  43. return mqBinarySearch(feature, mid, maxValue, step, unit); // feature is >= mid
  44. }
  45. return mqBinarySearch(feature, minValue, mid - step, step, unit); // feature is < mid
  46. }