2019-05-05 20:00:32 +02:00
|
|
|
import removeDiacritics from "./js/StackOverflow/removeDiacritics";
|
|
|
|
|
2019-05-03 20:29:53 +02:00
|
|
|
export function cloneObject(object) {
|
|
|
|
return JSON.parse(JSON.stringify(object));
|
|
|
|
}
|
|
|
|
|
2019-05-10 23:39:00 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 23:34:03 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:51:41 +02:00
|
|
|
export function removeTags(html) {
|
2019-05-04 00:10:41 +02:00
|
|
|
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;
|
2019-05-03 20:29:53 +02:00
|
|
|
}
|
2019-05-05 20:00:32 +02:00
|
|
|
|
|
|
|
export function slugify(string) {
|
2019-05-10 23:39:00 +02:00
|
|
|
return removeDiacritics(string).replace(/[^a-zA-Z0-9-_]/g, '-');
|
2019-05-05 20:00:32 +02:00
|
|
|
}
|