Lexiconga/src/helpers.js

75 lines
2.0 KiB
JavaScript
Raw Normal View History

import removeDiacritics from "./js/StackOverflow/removeDiacritics";
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);
}
}
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) {
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, '&lt;');
}
return html;
}
export function slugify(string) {
2019-05-10 23:39:00 +02:00
return removeDiacritics(string).replace(/[^a-zA-Z0-9-_]/g, '-');
}