mirror of
https://github.com/pyscript/pyscript.git
synced 2026-02-13 16:00:37 -05:00
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
|
|
function addClasses(element: HTMLElement, classes: Array<string>){
|
|
for (let entry of classes) {
|
|
element.classList.add(entry);
|
|
}
|
|
}
|
|
|
|
const getLastPath = function (str) {
|
|
return str.split('\\').pop().split('/').pop();
|
|
}
|
|
|
|
function htmlDecode(input) {
|
|
var doc = new DOMParser().parseFromString(input, "text/html");
|
|
return ltrim(doc.documentElement.textContent);
|
|
}
|
|
|
|
function ltrim(code: string): string {
|
|
const lines = code.split("\n")
|
|
if (lines.length == 0)
|
|
return code
|
|
|
|
const lengths = lines
|
|
.filter((line) => line.trim().length != 0)
|
|
.map((line) => {
|
|
const [prefix] = line.match(/^\s*/)
|
|
return prefix.length
|
|
})
|
|
|
|
const k = Math.min(...lengths)
|
|
|
|
if (k != 0)
|
|
return lines.map((line) => line.substring(k)).join("\n")
|
|
else
|
|
return code
|
|
}
|
|
|
|
function guidGenerator(): string {
|
|
var S4 = function(): string {
|
|
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
|
|
};
|
|
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
|
|
}
|
|
|
|
export {addClasses, getLastPath, ltrim, htmlDecode, guidGenerator}
|