Skip to content

Commit

Permalink
Add hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 28, 2024
1 parent e8d7aba commit 7f76b83
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 20 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
26 changes: 26 additions & 0 deletions src/stat/find.test.ts
Original file line number Diff line number Diff line change
@@ -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,
);
});
11 changes: 11 additions & 0 deletions src/stat/hash.test.ts
Original file line number Diff line number Diff line change
@@ -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",
);
});
20 changes: 20 additions & 0 deletions src/stat/hash.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
const hash = crypto.createHash(algorithm);
const fileData = await readFile(filePath);
hash.update(fileData);
return hash.digest("hex");
}
43 changes: 23 additions & 20 deletions src/stat/is.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions src/stat/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,4 @@ export * from "./is.ts";
export * from "./exists.ts";
export * from "./size.ts";
export * from "./find.ts";
export * from "./hash.ts";

0 comments on commit 7f76b83

Please sign in to comment.