From 20b2c4ac636a17d0098d8fe9a89bc5c434046501 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sat, 6 Jan 2024 21:57:05 -0500 Subject: [PATCH] testing husky pre-commit hooks --- .husky/pre-commit | 4 ++ package.json | 4 +- src/js/jagex2/util/JsUtil.ts | 77 ++++++++++++++++++------------------ 3 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 00000000..d24fdfc6 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +npx lint-staged diff --git a/package.json b/package.json index 817de190..dc0146a0 100644 --- a/package.json +++ b/package.json @@ -55,9 +55,9 @@ } }, "lint-staged": { - "*.{js,ts}": [ + "src/**/*.{js,ts}": [ "prettier --write", - "eslint . --ext .ts --fix", + "npm run lint:fix", "git add" ] } diff --git a/src/js/jagex2/util/JsUtil.ts b/src/js/jagex2/util/JsUtil.ts index d955de13..64f3d2e6 100644 --- a/src/js/jagex2/util/JsUtil.ts +++ b/src/js/jagex2/util/JsUtil.ts @@ -1,53 +1,54 @@ import {decompress} from '../../vendor/bz2.js'; export const sleep = async (ms: number): Promise => new Promise(resolve => setTimeout(resolve, ms)); -export const downloadUrl = async (url: string): Promise => new Uint8Array(await (await fetch(url)).arrayBuffer()); +export const downloadUrl = async (url: string): Promise => + new Uint8Array(await (await fetch(url)).arrayBuffer()); export const downloadText = async (url: string): Promise => (await fetch(url)).text(); export const decompressBz2 = (data: Uint8Array, addMagic: boolean = true, prepend: boolean = true): Uint8Array => { - if (addMagic) { - const magic = Uint8Array.from(['B'.charCodeAt(0), 'Z'.charCodeAt(0), 'h'.charCodeAt(0), '1'.charCodeAt(0)]); + if (addMagic) { + const magic = Uint8Array.from(['B'.charCodeAt(0), 'Z'.charCodeAt(0), 'h'.charCodeAt(0), '1'.charCodeAt(0)]); - if (prepend) { - const temp = data; - data = new Uint8Array(magic.length + data.length); - data.set(temp, magic.length); - } - - data.set(magic, 0); + if (prepend) { + const temp = data; + data = new Uint8Array(magic.length + data.length); + data.set(temp, magic.length); } - return decompress(data); + data.set(magic, 0); + } + + return decompress(data); }; export const decodeJpeg = async (data: Uint8Array | null): Promise => { - if (!data) { - throw new Error("Input jpeg data was null!"); - } - if (data[0] !== 0xFF) { - // fix invalid JPEG header - data[0] = 0xFF; - } - - // create img element - const img = document.createElement('img'); - img.src = 'data:image/jpeg;base64,' + btoa(String.fromCharCode(...data)); - - // wait for img to load - await new Promise(resolve => img.onload = resolve); - - // get imagedata from img element - const canvas = document.createElement('canvas'); - canvas.width = img.naturalWidth; - canvas.height = img.naturalHeight; - const ctx = canvas.getContext('2d'); - if (!ctx) { - throw new Error('Canvas 2d not found!!!!!!!!'); - } - ctx.drawImage(img, 0, 0); - return ctx.getImageData(0, 0, canvas.width, canvas.height); + if (!data) { + throw new Error('Input jpeg data was null!'); + } + if (data[0] !== 0xff) { + // fix invalid JPEG header + data[0] = 0xff; + } + + // create img element + const img = document.createElement('img'); + img.src = 'data:image/jpeg;base64,' + btoa(String.fromCharCode(...data)); + + // wait for img to load + await new Promise(resolve => (img.onload = resolve)); + + // get imagedata from img element + const canvas = document.createElement('canvas'); + canvas.width = img.naturalWidth; + canvas.height = img.naturalHeight; + const ctx = canvas.getContext('2d'); + if (!ctx) { + throw new Error('Canvas 2d not found!!!!!!!!'); + } + ctx.drawImage(img, 0, 0); + return ctx.getImageData(0, 0, canvas.width, canvas.height); }; export const arraycopy = (src: Uint32Array, srcPos: number, dst: Uint32Array, dstPos: number, length: number): void => { - while (length--) dst[dstPos++] = src[srcPos++]; -} + while (length--) dst[dstPos++] = src[srcPos++]; +};