feat: updates
This commit is contained in:
@@ -6,7 +6,7 @@ import { assertEquals } from "jsr:@std/assert";
|
||||
import { dirname } from "https://deno.land/std@0.208.0/path/mod.ts";
|
||||
|
||||
export async function sleep(timeoutMillis: number): Promise<void> {
|
||||
await new Promise(resolve => setTimeout(resolve, timeoutMillis))
|
||||
await new Promise((resolve) => setTimeout(resolve, timeoutMillis));
|
||||
}
|
||||
|
||||
export function compareVersion(ver1: string, ver2: string): 0 | 1 | -1 {
|
||||
@@ -236,6 +236,14 @@ class Logger {
|
||||
|
||||
export const log = new Logger();
|
||||
|
||||
export function getHomeDirOrDie(): string {
|
||||
const homeDir = getHomeDir();
|
||||
if (homeDir === null) {
|
||||
throw new Error("Cannot find home dir");
|
||||
}
|
||||
return homeDir;
|
||||
}
|
||||
|
||||
export function getHomeDir(): string | null {
|
||||
if (Deno.build.os === "windows") {
|
||||
const userProfile = Deno.env.get("USERPROFILE");
|
||||
@@ -268,32 +276,62 @@ export async function existsPath(path: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function readFileToString(filename: string): Promise<string | null> {
|
||||
export async function readFileToString(
|
||||
filename: string,
|
||||
): Promise<string | null> {
|
||||
try {
|
||||
return await Deno.readTextFile(resolveFilename(filename));
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.name == 'NotFound') {
|
||||
if (e instanceof Error && e.name == "NotFound") {
|
||||
return null;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeStringToFile(filename: string, data: string | null): Promise<void> {
|
||||
export async function writeStringToFile(
|
||||
filename: string,
|
||||
data: string | null,
|
||||
): Promise<void> {
|
||||
const newFilename = resolveFilename(filename);
|
||||
if (data == null) {
|
||||
if (await existsPath(newFilename)) {
|
||||
await Deno.remove(newFilename)
|
||||
await Deno.remove(newFilename);
|
||||
}
|
||||
} else {
|
||||
const parentDirname = dirname(newFilename);
|
||||
if (!await existsPath(parentDirname)) {
|
||||
await Deno.mkdir(parentDirname, {recursive: true});
|
||||
await Deno.mkdir(parentDirname, { recursive: true });
|
||||
}
|
||||
await Deno.writeTextFile(newFilename, data);
|
||||
}
|
||||
}
|
||||
|
||||
export function uint8ArrayToHexString(uint8: Uint8Array): string {
|
||||
return Array.from(uint8)
|
||||
.map((b) => b.toString(16).padStart(2, "0"))
|
||||
.join("");
|
||||
}
|
||||
|
||||
export function hexStringToUint8Array(hex: string): Uint8Array {
|
||||
hex = hex.trim();
|
||||
if (hex.startsWith("0x") || hex.startsWith("0X")) {
|
||||
hex = hex.slice(2);
|
||||
}
|
||||
if (hex.length % 2 !== 0) {
|
||||
throw new Error("Hex string must have an even number of characters");
|
||||
}
|
||||
if (!/^[0-9a-fA-F]*$/.test(hex)) {
|
||||
throw new Error("Invalid hex string");
|
||||
}
|
||||
const byteLength = hex.length / 2;
|
||||
const uint8 = new Uint8Array(byteLength);
|
||||
for (let i = 0; i < byteLength; i++) {
|
||||
uint8[i] = parseInt(hex.substring(i * 2, (i + 1) * 2), 16);
|
||||
}
|
||||
return uint8;
|
||||
}
|
||||
|
||||
Deno.test("isOn", () => {
|
||||
assertEquals(false, isOn(undefined));
|
||||
assertEquals(false, isOn(""));
|
||||
@@ -354,7 +392,7 @@ Deno.test("formatPercent", () => {
|
||||
|
||||
Deno.test("sleep", async () => {
|
||||
const t1 = new Date().getTime();
|
||||
await sleep(1000)
|
||||
await sleep(1000);
|
||||
const t2 = new Date().getTime();
|
||||
assert(Math.abs(1000 - (t2 - t1)) < 20);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user