mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
109 lines
3.0 KiB
JavaScript
109 lines
3.0 KiB
JavaScript
export {
|
|
onlyUnique,
|
|
shuffle,
|
|
download,
|
|
urlContentToDataUri,
|
|
getBase64Async,
|
|
getStringHash,
|
|
debounce,
|
|
delay,
|
|
isSubsetOf,
|
|
incrementString,
|
|
stringFormat,
|
|
};
|
|
|
|
/// UTILS
|
|
function onlyUnique(value, index, array) {
|
|
return array.indexOf(value) === index;
|
|
}
|
|
|
|
function shuffle(array) {
|
|
let currentIndex = array.length,
|
|
randomIndex;
|
|
|
|
while (currentIndex != 0) {
|
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
currentIndex--;
|
|
[array[currentIndex], array[randomIndex]] = [
|
|
array[randomIndex],
|
|
array[currentIndex],
|
|
];
|
|
}
|
|
return array;
|
|
}
|
|
|
|
function download(content, fileName, contentType) {
|
|
const a = document.createElement("a");
|
|
const file = new Blob([content], { type: contentType });
|
|
a.href = URL.createObjectURL(file);
|
|
a.download = fileName;
|
|
a.click();
|
|
}
|
|
|
|
async function urlContentToDataUri(url, params) {
|
|
const response = await fetch(url, params);
|
|
const blob = await response.blob();
|
|
return await new Promise(callback => {
|
|
let reader = new FileReader();
|
|
reader.onload = function () { callback(this.result); };
|
|
reader.readAsDataURL(blob);
|
|
});
|
|
}
|
|
|
|
function getBase64Async(file) {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.readAsDataURL(file);
|
|
reader.onload = function () {
|
|
resolve(reader.result);
|
|
};
|
|
reader.onerror = function (error) {
|
|
reject(error);
|
|
};
|
|
});
|
|
}
|
|
|
|
function getStringHash(str, seed = 0) {
|
|
let h1 = 0xdeadbeef ^ seed,
|
|
h2 = 0x41c6ce57 ^ seed;
|
|
for (let i = 0, ch; i < str.length; i++) {
|
|
ch = str.charCodeAt(i);
|
|
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
}
|
|
|
|
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
|
|
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
};
|
|
|
|
function debounce(func, timeout = 300) {
|
|
let timer;
|
|
return (...args) => {
|
|
clearTimeout(timer);
|
|
timer = setTimeout(() => { func.apply(this, args); }, timeout);
|
|
};
|
|
}
|
|
|
|
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
const isSubsetOf = (a, b) => (Array.isArray(a) && Array.isArray(b)) ? b.every(val => a.includes(val)) : false;
|
|
|
|
function incrementString(str) {
|
|
// Find the trailing number or it will match the empty string
|
|
const count = str.match(/\d*$/);
|
|
|
|
// Take the substring up until where the integer was matched
|
|
// Concatenate it to the matched count incremented by 1
|
|
return str.substr(0, count.index) + (++count[0]);
|
|
};
|
|
|
|
function stringFormat(format) {
|
|
const args = Array.prototype.slice.call(arguments, 1);
|
|
return format.replace(/{(\d+)}/g, function (match, number) {
|
|
return typeof args[number] != 'undefined'
|
|
? args[number]
|
|
: match
|
|
;
|
|
});
|
|
}; |