TypeScript cleanup
This commit is contained in:
3
scripts/Utils/canUseWebP.ts
Normal file
3
scripts/Utils/canUseWebP.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export function canUseWebP(): boolean {
|
||||
return document.createElement("canvas").toDataURL("image/webp").indexOf("data:image/webp") === 0
|
||||
}
|
3
scripts/Utils/delay.ts
Normal file
3
scripts/Utils/delay.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export function delay<T>(millis: number, value?: T): Promise<T> {
|
||||
return new Promise(resolve => setTimeout(() => resolve(value), millis))
|
||||
}
|
7
scripts/Utils/findAll.ts
Normal file
7
scripts/Utils/findAll.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export function* findAll(className: string): IterableIterator<HTMLElement> {
|
||||
let elements = document.getElementsByClassName(className)
|
||||
|
||||
for(let i = 0; i < elements.length; ++i) {
|
||||
yield elements[i] as HTMLElement
|
||||
}
|
||||
}
|
6
scripts/Utils/index.ts
Normal file
6
scripts/Utils/index.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export * from "./canUseWebP"
|
||||
export * from "./delay"
|
||||
export * from "./findAll"
|
||||
export * from "./plural"
|
||||
export * from "./requestIdleCallback"
|
||||
export * from "./swapElements"
|
3
scripts/Utils/plural.ts
Normal file
3
scripts/Utils/plural.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export function plural(count: number, singular: string): string {
|
||||
return (count === 1 || count === -1) ? (count + " " + singular) : (count + " " + singular + "s")
|
||||
}
|
7
scripts/Utils/requestIdleCallback.ts
Normal file
7
scripts/Utils/requestIdleCallback.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export function requestIdleCallback(func: Function) {
|
||||
if("requestIdleCallback" in window) {
|
||||
window["requestIdleCallback"](func)
|
||||
} else {
|
||||
func()
|
||||
}
|
||||
}
|
22
scripts/Utils/swapElements.ts
Normal file
22
scripts/Utils/swapElements.ts
Normal file
@ -0,0 +1,22 @@
|
||||
export function swapElements(a: Node, b: Node) {
|
||||
let parent = b.parentNode
|
||||
let bNext = b.nextSibling
|
||||
|
||||
// Special case for when a is the next sibling of b
|
||||
if(bNext === a) {
|
||||
// Just put a before b
|
||||
parent.insertBefore(a, b)
|
||||
} else {
|
||||
// Insert b right before a
|
||||
a.parentNode.insertBefore(b, a)
|
||||
|
||||
// Now insert a where b was
|
||||
if(bNext) {
|
||||
// If there was an element after b, then insert a right before that
|
||||
parent.insertBefore(a, bNext)
|
||||
} else {
|
||||
// Otherwise just append it as the last child
|
||||
parent.appendChild(a)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user