diff --git a/README.md b/README.md index e4c2c7c..95b131f 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,10 @@ console.log(await exists("my/file")); // Search for package.json recursively, starting from parent folder console.log(await find("../", (path) => path.endsWith("package.json"))); // ["/home/.../package.json","/home/.../.../package.json"] + +// Get the sha256 hash of a file +console.log(await hash("LICENSE.md", "sha256")); +// f8c9819eb0c322eef28a0d0948877df068276f487b81326af842d3a20e7c9bbc ``` Methods: @@ -46,6 +50,7 @@ Methods: | size | X | X | X | custom | | find | X | X | X | custom | | diskusage | X | X | X | custom | +| hash | X | X | X | custom | ### Io diff --git a/src/stat/find.test.ts b/src/stat/find.test.ts index e69de29..459b616 100644 --- a/src/stat/find.test.ts +++ b/src/stat/find.test.ts @@ -0,0 +1,26 @@ +import { assertEquals } from "@std/assert"; +import { test } from "@cross/test"; +import { find } from "./find.ts"; + +test("find locates find.test.ts file", async () => { + const results = await find(".", (path) => path.endsWith("find.test.ts")); + assertEquals(results.length > 0, true); + assertEquals( + results.find((path) => path.includes("/src/stat/find.test.ts"))?.includes( + "/src/stat/find.test.ts", + ), + true, + ); +}); + +test("find doest not locate findnonexistant.test.ts file", async () => { + const results = await find( + ".", + (path) => path.endsWith("findnonexistant.test.ts"), + ); + assertEquals(results.length === 0, true); + assertEquals( + results.find((path) => path.includes("/src/stat/findnonexistant.test.ts")), + undefined, + ); +}); diff --git a/src/stat/hash.test.ts b/src/stat/hash.test.ts new file mode 100644 index 0000000..f828897 --- /dev/null +++ b/src/stat/hash.test.ts @@ -0,0 +1,11 @@ +import { assertEquals } from "@std/assert"; +import { test } from "@cross/test"; +import { hash } from "./hash.ts"; + +test("hash is calculated correctly", async () => { + const sha = await hash("./LICENSE"); + assertEquals( + sha, + "f8c9819eb0c322eef28a0d0948877df068276f487b81326af842d3a20e7c9bbc", + ); +}); diff --git a/src/stat/hash.ts b/src/stat/hash.ts new file mode 100644 index 0000000..19b2656 --- /dev/null +++ b/src/stat/hash.ts @@ -0,0 +1,20 @@ +import crypto from "node:crypto"; +import { readFile } from "../io/mod.ts"; + +/** + * Calculates the MD5 hash of a file. + * - uses node:crypto for widest compatibility + * + * @param filePath - The path to the file. + * @param algorithm - The algorithm to use + * @returns The hash as a hexadecimal string. + */ +export async function hash( + filePath: string, + algorithm: string = "sha256", +): Promise { + const hash = crypto.createHash(algorithm); + const fileData = await readFile(filePath); + hash.update(fileData); + return hash.digest("hex"); +} diff --git a/src/stat/is.test.ts b/src/stat/is.test.ts index 459b616..1751b6c 100644 --- a/src/stat/is.test.ts +++ b/src/stat/is.test.ts @@ -1,26 +1,29 @@ import { assertEquals } from "@std/assert"; import { test } from "@cross/test"; -import { find } from "./find.ts"; -test("find locates find.test.ts file", async () => { - const results = await find(".", (path) => path.endsWith("find.test.ts")); - assertEquals(results.length > 0, true); - assertEquals( - results.find((path) => path.includes("/src/stat/find.test.ts"))?.includes( - "/src/stat/find.test.ts", - ), - true, - ); +import { isDir, isFile } from "./is.ts"; + +test("isFile returns true on existing file", async () => { + const result = await isFile("./mod.ts"); + assertEquals(result, true); +}); + +test("isFile returns false on non-existiantfile", async () => { + const result = await isFile("./mod-nonexistant.ts"); + assertEquals(result, false); +}); + +test("isDir returns false on existing file", async () => { + const result = await isDir("./mod.ts"); + assertEquals(result, false); +}); + +test("isFile returns false on existing dir", async () => { + const result = await isFile("./src"); + assertEquals(result, false); }); -test("find doest not locate findnonexistant.test.ts file", async () => { - const results = await find( - ".", - (path) => path.endsWith("findnonexistant.test.ts"), - ); - assertEquals(results.length === 0, true); - assertEquals( - results.find((path) => path.includes("/src/stat/findnonexistant.test.ts")), - undefined, - ); +test("isDir returns true on existing dir", async () => { + const result = await isDir("./src"); + assertEquals(result, true); }); diff --git a/src/stat/mod.ts b/src/stat/mod.ts index 8221cde..fae8f47 100644 --- a/src/stat/mod.ts +++ b/src/stat/mod.ts @@ -176,3 +176,4 @@ export * from "./is.ts"; export * from "./exists.ts"; export * from "./size.ts"; export * from "./find.ts"; +export * from "./hash.ts";