move utility methods to @/utils

This commit is contained in:
geoffrey45
2022-08-04 18:43:12 +03:00
parent aae7b20c99
commit b9f0368f5b
30 changed files with 244 additions and 158 deletions
-53
View File
@@ -1,53 +0,0 @@
import { subPath } from "@/interfaces";
/**
* Breaks a path into sub-paths.
* @param {string} newpath the new path to break into sub-paths.
* @param {string} oldpath the old path to compare with the new path.
*/
export default function createSubPaths(
newpath: string,
oldpath: string
): [string, subPath[]] {
if (oldpath === undefined) oldpath = "";
const newlist = newpath.split("/");
if (newlist[0] == "$home") {
newlist.shift();
}
if (oldpath.includes(newpath)) {
const oldlist = oldpath.split("/");
const current = newlist.slice(-1)[0];
return [oldpath, createSubs(oldlist, current)];
} else {
const current = newlist.slice(-1)[0];
return [newpath, createSubs(newlist, current)];
}
function createSubs(list: string[], current: string) {
const paths = list.map((path, index) => {
return {
active: false,
name: path,
path: list.slice(0, index + 1).join("/"),
};
});
paths.reverse();
for (let i = 0; i < paths.length; i++) {
if (paths[i].name === current) {
paths[i].active = true;
break;
}
}
return paths.reverse();
}
}
// function that takes 2 strings as parameters and returns a string
// that is the concatenation of the two strings
+1 -74
View File
@@ -1,32 +1,6 @@
import { RouteLocationNormalized } from "vue-router";
const putCommas = (artists: string[]) => {
let result = [];
artists.forEach((i, index, artists) => {
if (index !== artists.length - 1) {
result.push(i + ", ");
} else {
result.push(i);
}
});
return result;
};
function focusElem(className: string, delay?: number, pos?: any) {
const dom = document.getElementsByClassName(className)[0];
setTimeout(() => {
if (dom) {
dom.scrollIntoView({
behavior: "smooth",
block: `${pos ?? "start"}` as any,
inline: "center",
});
}
}, delay | 300);
}
function getElem(id: string, type: string) {
switch (type) {
@@ -49,51 +23,4 @@ function isSameRoute(to: r, from: r) {
return false;
}
/**
* Converts seconds into minutes and hours.
* @param seconds The seconds to convert
* @param long Whether to provide the time in the long format
*/
function formatSeconds(seconds: number, long?: boolean) {
if (seconds == undefined) {
return "00:00";
}
const date = new Date(seconds * 1000);
const hh = date.getUTCHours();
const mm = date.getUTCMinutes();
const ss = date.getUTCSeconds();
let _hh = hh < 10 ? `0${hh}` : hh;
let _mm = mm < 10 ? `0${mm}` : mm;
let _ss = ss < 10 ? `0${ss}` : ss;
if (long == true) {
if (hh === 1) {
_hh = hh + " Hour";
} else {
_hh = `${hh} Hours`;
}
if (mm === 1) {
_mm = mm + " Minute";
} else {
_mm = `${mm} Minutes`;
}
if (hh > 0) {
return `${_hh}, ${_mm}`;
} else {
return `${_mm}`;
}
}
if (hh > 0) {
return `${_hh}:${_mm}:${_ss}`;
} else {
return `${_mm}:${_ss}`;
}
}
export { putCommas, focusElem, formatSeconds, getElem, isSameRoute };
export { getElem, isSameRoute };
-49
View File
@@ -1,49 +0,0 @@
import { customRef, ref } from "vue";
/**
* Debounces a function
*
* @param {*} fn The function to debounce
* @param {*} delay The delay in milliseconds
* @param {*} immediate whether to debounce immediately
* @returns {Function} The debounced function
*/
const debounce = (fn, delay = 0, immediate = false) => {
let timeout;
return (...args) => {
if (immediate && !timeout) fn(...args);
clearTimeout(timeout);
timeout = setTimeout(() => {
fn(...args);
}, delay);
};
};
/**
* Emits the ref updated value after the given delay.
*
* @param {*} initialValue The default value of the ref
* @param {*} delay The delay in milliseconds
* @param {*} immediate Whether to call the function immediately
* @returns {Object} The ref and a function to call to update the ref
*/
const useDebouncedRef = (initialValue, delay, immediate = false) => {
const state = ref(initialValue);
return customRef((track, trigger) => ({
get() {
track();
return state.value;
},
set: debounce(
(value) => {
state.value = value;
trigger();
},
delay,
immediate
),
}));
};
export default useDebouncedRef;
-3
View File
@@ -1,3 +0,0 @@
export function readLocalStorage(key: string) {
return JSON.parse(localStorage.getItem(key));
}
-21
View File
@@ -1,21 +0,0 @@
import { ref } from "@vue/reactivity";
import { useIntersectionObserver } from "@vueuse/core";
import { Ref, watch } from "vue";
export default function useVisibility(
elem: Ref<HTMLElement>,
callback: (state: boolean) => void
) {
const visible = ref(false);
useIntersectionObserver(elem, ([{ isIntersecting }], observerElement) => {
visible.value = isIntersecting;
});
watch(
() => visible.value,
(newVal) => {
callback(newVal);
}
);
}