From 2efc9eace2c1b72e0e8301799d9b0a33a97fbfb9 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 12 Jul 2024 01:00:31 -0300 Subject: [PATCH] Apply eslint --- src/index.ts | 76 ++++++++++++++++++++++-------------------- src/postman/item.ts | 59 +++++++++++++++++--------------- src/postman/request.ts | 46 ++++++++++++------------- 3 files changed, 94 insertions(+), 87 deletions(-) diff --git a/src/index.ts b/src/index.ts index 20160ef..9402baa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,72 +1,74 @@ -import type { Item, FontItem } from './postman/item'; -import item from './postman/item'; -import request from './postman/request'; +import type { Item, FontItem } from "./postman/item"; +import item from "./postman/item"; +import request from "./postman/request"; -type Postman = { deliver: () => Promise }; -type Args = Array +interface Postman { + deliver: () => Promise; +} +type Args = Array number)>; type Items = Array; -type Package = { - fonts: FontFace[] - images: HTMLElement[] - css: HTMLElement[] - scripts: HTMLElement[] - json: HTMLElement[] -}; +interface Package { + fonts: FontFace[]; + images: HTMLElement[]; + css: HTMLElement[]; + scripts: HTMLElement[]; + json: HTMLElement[]; +} -function parseArgs(args: Args): [Items, Function] { +function parseArgs(args: Args): [Items, (n: number) => number] { const items: Items = []; - let callback: Function = (n: number) => n - args.forEach((item) => { - if (typeof item === 'function') { - callback = item; + let progresscb = (n: number): number => n; + args.forEach(item => { + if (typeof item === "function") { + progresscb = item; } else { items.push(item); } }); - return [items, callback]; + return [items, progresscb]; } export { item }; -export default function (...args: Args) { +export default function (...args: Args): Postman { const self: Postman = Object.create(null); const result: Package = { fonts: [], images: [], css: [], scripts: [], json: [] }; - const [items, callback] = parseArgs(args); - items.sort((i1, i2) => i1.priority >= i2.priority ? 1 : -1); + const [items, progresscb] = parseArgs(args); + items.sort((i1, i2) => (i1.priority >= i2.priority ? 1 : -1)); let index = 0; - const onProgress = (el: T) => { + const onProgress = (el: T): T => { index++; if (index <= items.length) { - callback(100 * (index / items.length)); + progresscb(100 * (index / items.length)); } return el; }; - const spawnRequests = () => { - const reqs = items.map((item: Item | FontItem) => { - if ('fontFamily' in item) { + const spawnRequests = (): Array> => { + const reqs = items.map(async (item: Item | FontItem) => { + if ("fontFamily" in item) { const req = request.font; - return req(item) - .then((el) => onProgress(el)) - .then((font) => result.fonts.push(font)) + return await req(item) + .then(el => onProgress(el)) + .then(font => result.fonts.push(font)) .then(() => result); - } else if(item.requestId !== 'font' && item.group !== 'fonts') { + } else if (item.requestId !== "font" && item.group !== "fonts") { const req = request[item.requestId]; const ary = result[item.group]; - return req(item) - .then((el) => onProgress(el)) - .then((el) => ary.push(el)) + return await req(item) + .then(el => onProgress(el)) + .then(el => ary.push(el)) .then(() => result); + } else { + return null; } - /* unreachable */ - return null; }); - return reqs as Array>; + return reqs; }; self.deliver = async () => { - await Promise.all(spawnRequests()); + await Promise.all(spawnRequests()); return result; }; diff --git a/src/postman/item.ts b/src/postman/item.ts index a798872..f7d9dee 100644 --- a/src/postman/item.ts +++ b/src/postman/item.ts @@ -1,65 +1,70 @@ -type Group = 'fonts' | 'images' | 'css' | 'scripts' | 'json'; -type RequestID = 'font' | 'image' | 'css' | 'script' | 'json'; +type Group = "fonts" | "images" | "css" | "scripts" | "json"; +type RequestID = "font" | "image" | "css" | "script" | "json"; -export type Item = { - priority: number - group: Group - requestId: RequestID - href: string - props?: Partial -}; +export interface Item { + priority: number; + group: Group; + requestId: RequestID; + href: string; + props?: Partial; +} export type FontItem = { - fontFamily: string + fontFamily: string; } & Item; export default { font(fontFamily: string, href: string): FontItem { return { priority: 1, - group: 'fonts', - requestId: 'font', - href, fontFamily, + group: "fonts", + requestId: "font", + href, + fontFamily, }; }, image(href: string, props?: Partial): Item { return { priority: 2, - group: 'images', - requestId: 'image', - href, props + group: "images", + requestId: "image", + href, + props, }; }, css(href: string, props?: Partial): Item { return { priority: 3, - group: 'css', - requestId: 'css', - href, props + group: "css", + requestId: "css", + href, + props, }; }, script(href: string, props?: Partial): Item { return { priority: 4, - group: 'scripts', - requestId: 'script', - href, props + group: "scripts", + requestId: "script", + href, + props, }; }, json(href: string, props?: Partial): Item { return { priority: 5, - group: 'json', - requestId: 'json', - href, props - } + group: "json", + requestId: "json", + href, + props, + }; }, progress(fn: (percent: number) => void): (percent: number) => void { return fn; - } + }, }; diff --git a/src/postman/request.ts b/src/postman/request.ts index 7937ad3..c1be36b 100644 --- a/src/postman/request.ts +++ b/src/postman/request.ts @@ -1,43 +1,43 @@ -import type { Item, FontItem } from './item'; +import type { Item, FontItem } from "./item"; export default { - font(item: FontItem): Promise { + async font(item: FontItem): Promise { const { fontFamily, href } = item; - return new FontFace(fontFamily, href).load(); + return await new FontFace(fontFamily, href).load(); }, - script(item: Item, options: RequestInit = {}): Promise { + async script(item: Item, options: RequestInit = {}): Promise { const { href } = item; - return fetch(href, options) - .then((res) => res.text()) - .then((text) => ({ type: 'application/javascript', text })) - .then((props) => Object.assign(document.createElement('script'), props)); + return await fetch(href, options) + .then(async res => await res.text()) + .then(text => ({ type: "application/javascript", text })) + .then(props => Object.assign(document.createElement("script"), props)); }, - css(item: Item, options: RequestInit = {}): Promise { + async css(item: Item, options: RequestInit = {}): Promise { const { href } = item; - return fetch(href, options) - .then((res) => res.text()) - .then((text) => ({ innerText: text })) - .then((props) => Object.assign(document.createElement('style'), props)); + return await fetch(href, options) + .then(async res => await res.text()) + .then(text => ({ innerText: text })) + .then(props => Object.assign(document.createElement("style"), props)); }, - image(item: Item): Promise { + async image(item: Item): Promise { const { href } = item; - return new Promise((resolve, reject) => { - const el = document.createElement('img'); + return await new Promise((resolve, reject) => { + const el = document.createElement("img"); el.onload = () => resolve(el); el.onerror = reject; el.src = href; }); }, - json(item: Item, options: RequestInit = {}): Promise { + async json(item: Item, options: RequestInit = {}): Promise { const { href } = item; - return fetch(href, options) - .then((res) => res.text()) - .then((text) => ({type: 'application/json', text})) - .then((props) => Object.assign(props, item.props || {})) - .then((props) => Object.assign(document.createElement('script'), props)); - } + return await fetch(href, options) + .then(async res => await res.text()) + .then(text => ({ type: "application/json", text })) + .then(props => Object.assign(props, item.props != null || {})) + .then(props => Object.assign(document.createElement("script"), props)); + }, };