Refactor scripts

This commit is contained in:
2019-11-18 11:04:13 +09:00
parent 7e25ee6faf
commit 1ddcd4d570
33 changed files with 670 additions and 749 deletions

View File

@ -1,4 +1,4 @@
export function bytesHumanReadable(fileSize: number): string {
export default function bytesHumanReadable(fileSize: number): string {
let unit = "bytes"
if(fileSize >= 1024) {

View File

@ -1,3 +1,3 @@
export function delay<T>(millis: number, value?: T): Promise<T> {
export default function delay<T>(millis: number, value?: T): Promise<T> {
return new Promise(resolve => setTimeout(() => resolve(value), millis))
}

View File

@ -0,0 +1 @@
export default "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="

View File

@ -1,15 +1,7 @@
export function* findAll(className: string): IterableIterator<HTMLElement> {
export default function* findAll(className: string): IterableIterator<HTMLElement> {
const elements = document.getElementsByClassName(className)
for(let i = 0; i < elements.length; ++i) {
yield elements[i] as HTMLElement
}
}
export function* findAllInside(className: string, root: HTMLElement): IterableIterator<HTMLElement> {
const elements = root.getElementsByClassName(className)
for(let i = 0; i < elements.length; ++i) {
yield elements[i] as HTMLElement
for(const element of elements) {
yield element as HTMLElement
}
}

View File

@ -0,0 +1,7 @@
export default function* findAllInside(className: string, root: HTMLElement): IterableIterator<HTMLElement> {
const elements = root.getElementsByClassName(className)
for(const element of elements) {
yield element as HTMLElement
}
}

View File

@ -1,4 +1,4 @@
export function hexToHSL(hex: string) {
export default function hexToHSL(hex: string) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
if(!result) {
@ -20,7 +20,7 @@ export function hexToHSL(hex: string) {
let s = 0
const l = (max + min) / 2
if(max == min) {
if(max === min) {
h = s = 0
} else {
const d = max - min

View File

@ -1,9 +0,0 @@
export * from "./supportsWebP"
export * from "./delay"
export * from "./findAll"
export * from "./hexToHSL"
export * from "./plural"
export * from "./requestIdleCallback"
export * from "./swapElements"
export * from "./uploadWithProgress"
export * from "./bytesHumanReadable"

View File

@ -2,7 +2,7 @@ const specialized = {
"new activity": "new activities"
}
export function plural(count: number, singular: string): string {
export default function plural(count: number, singular: string): string {
if(count === 1 || count === -1) {
return count + " " + singular
}

View File

@ -1,4 +1,4 @@
export function requestIdleCallback(func: Function) {
export default function requestIdleCallback(func: Function) {
if("requestIdleCallback" in window) {
(window["requestIdleCallback"] as Function)(func)
} else {

View File

@ -1,4 +1,4 @@
export async function supportsWebP(): Promise<boolean> {
export default async function supportsWebP(): Promise<boolean> {
if(!window.createImageBitmap) {
return false
}

View File

@ -1,5 +1,5 @@
// swapElements assumes that both elements have valid parent nodes.
export function swapElements(a: Node, b: Node) {
export default function swapElements(a: Node, b: Node) {
const bParent = b.parentNode as Node
const bNext = b.nextSibling

View File

@ -1,4 +1,4 @@
export function uploadWithProgress(url, options: RequestInit, onProgress: ((ev: ProgressEvent) => any) | null): Promise<string> {
export default function uploadWithProgress(url, options: RequestInit, onProgress: ((ev: ProgressEvent) => any) | null): Promise<string> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()