42 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-07-01 11:35:21 +00:00
export function* findAll(className: string): IterableIterator<HTMLElement> {
2017-06-26 10:32:07 +00:00
let elements = document.getElementsByClassName(className)
2018-03-07 02:46:44 +00:00
2017-06-20 10:41:26 +00:00
for(let i = 0; i < elements.length; ++i) {
yield elements[i] as HTMLElement
}
2017-06-26 01:57:29 +00:00
}
export function delay<T>(millis: number, value?: T): Promise<T> {
return new Promise(resolve => setTimeout(() => resolve(value), millis))
2017-07-01 11:35:21 +00:00
}
export function plural(count: number, singular: string): string {
2017-07-08 13:40:13 +00:00
return (count === 1 || count === -1) ? (count + " " + singular) : (count + " " + singular + "s")
}
export function canUseWebP(): boolean {
2018-03-29 09:36:54 +00:00
return document.createElement("canvas").toDataURL("image/webp").indexOf("data:image/webp") === 0
2017-10-04 11:39:59 +00:00
}
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)
}
}
2017-06-20 10:41:26 +00:00
}