mirror of
https://github.com/Alamantus/Lexiconga.git
synced 2025-04-01 17:22:07 +02:00
83 lines
2.2 KiB
JavaScript
83 lines
2.2 KiB
JavaScript
import removeDiacritics from "./js/StackOverflow/removeDiacritics";
|
|
|
|
export function cloneObject(object) {
|
|
return JSON.parse(JSON.stringify(object));
|
|
}
|
|
|
|
export function download(data, filename, type) {
|
|
var file = new Blob([data], { type });
|
|
if (window.navigator.msSaveOrOpenBlob) // IE10+
|
|
window.navigator.msSaveOrOpenBlob(file, filename);
|
|
else { // Others
|
|
var a = document.createElement("a"),
|
|
url = URL.createObjectURL(file);
|
|
a.href = url;
|
|
a.download = filename;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
setTimeout(function () {
|
|
document.body.removeChild(a);
|
|
window.URL.revokeObjectURL(url);
|
|
}, 0);
|
|
}
|
|
}
|
|
|
|
export function getIndicesOf(searchStr, findIn, caseSensitive) {
|
|
// https://stackoverflow.com/a/3410557
|
|
const searchStrLen = searchStr.length;
|
|
if (searchStrLen == 0) {
|
|
return [];
|
|
}
|
|
let startIndex = 0, index, indices = [];
|
|
if (!caseSensitive) {
|
|
findIn = findIn.toLowerCase();
|
|
searchStr = searchStr.toLowerCase();
|
|
}
|
|
while ((index = findIn.indexOf(searchStr, startIndex)) > -1) {
|
|
indices.push(index);
|
|
startIndex = index + searchStrLen;
|
|
}
|
|
return indices;
|
|
}
|
|
|
|
export function getTimestampInSeconds() {
|
|
return Math.round(Date.now() / 1000);
|
|
}
|
|
|
|
export function removeTags(html) {
|
|
if (html) {
|
|
var tagBody = '(?:[^"\'>]|"[^"]*"|\'[^\']*\')*';
|
|
var tagOrComment = new RegExp(
|
|
'<(?:'
|
|
// Comment body.
|
|
+ '!--(?:(?:-*[^->])*--+|-?)'
|
|
// Special "raw text" elements whose content should be elided.
|
|
+ '|script\\b' + tagBody + '>[\\s\\S]*?</script\\s*'
|
|
+ '|style\\b' + tagBody + '>[\\s\\S]*?</style\\s*'
|
|
// Regular name
|
|
+ '|/?[a-z]'
|
|
+ tagBody
|
|
+ ')>',
|
|
'gi');
|
|
var oldHtml;
|
|
do {
|
|
oldHtml = html;
|
|
html = html.replace(tagOrComment, '');
|
|
} while (html !== oldHtml);
|
|
return html.replace(/</g, '<');
|
|
}
|
|
return html;
|
|
}
|
|
|
|
export function shuffle(array) {
|
|
// Fisher-Yates shuffle
|
|
for (let i = array.length - 1; i > 0; i--) {
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
[array[i], array[j]] = [array[j], array[i]];
|
|
}
|
|
return array;
|
|
}
|
|
|
|
export function slugify(string) {
|
|
return removeDiacritics(string).replace(/[^a-zA-Z0-9-_]/g, '-');
|
|
}
|