update code to support latest node

This commit is contained in:
JasterV 2026-03-01 18:15:40 +01:00
parent c9739ecc46
commit 985706b05d
4 changed files with 11 additions and 11 deletions

View file

@ -7,7 +7,7 @@ export class HashImage {
constructor(buffer) { constructor(buffer) {
if (!(buffer instanceof Uint8Array)) { if (!(buffer instanceof Uint8Array)) {
throw new Error( throw new Error(
"Invalid parameter, please use a buffer or an instance of Uint8Array" "Invalid parameter, please use a buffer or an instance of Uint8Array",
); );
} }
this.buffer = buffer; this.buffer = buffer;
@ -19,14 +19,14 @@ export class HashImage {
return new HashImage(buffer); return new HashImage(buffer);
} catch (err) { } catch (err) {
throw new Error( throw new Error(
"Error on image download, make sure you are passing a valid string url" "Error on image download, make sure you are passing a valid string url",
); );
} }
} }
async phash() { async phash() {
const data = await imageFromBuffer(this.buffer); const data = await imageFromBuffer(this.buffer);
const hexHash = await blockhash.bmvbhash(getImageData(data), 8); const hexHash = blockhash.bmvbhash(getImageData(data), 8);
const hash = hexToBin(hexHash); const hash = hexToBin(hexHash);
return new PHash(hash); return new PHash(hash);
} }

View file

@ -2,7 +2,7 @@ export class PHash {
constructor(hash) { constructor(hash) {
if (typeof hash !== "string") { if (typeof hash !== "string") {
throw new Error( throw new Error(
"Can't construct a PHash instance with a non-string value" "Can't construct a PHash instance with a non-string value",
); );
} }
const isNonBinary = hash const isNonBinary = hash
@ -11,7 +11,7 @@ export class PHash {
.some((chr) => chr !== "0" && chr !== "1"); .some((chr) => chr !== "0" && chr !== "1");
if (isNonBinary) { if (isNonBinary) {
throw new Error( throw new Error(
"Can't construct a PHash instance with a non-binary string value" "Can't construct a PHash instance with a non-binary string value",
); );
} }
this.hash = hash.trim(); this.hash = hash.trim();

View file

@ -1,4 +1,4 @@
import HashImage from "./HashImage.js"; import { HashImage } from "./HashImage.js";
import PHash from "./PHash.js"; import { PHash } from "./PHash.js";
export default { HashImage, PHash }; export default { HashImage, PHash };

View file

@ -28,7 +28,7 @@ describe("Test HashImage", () => {
it("Can't create an instance from an invalid url", async () => { it("Can't create an instance from an invalid url", async () => {
expect.assertions(1); expect.assertions(1);
await expect(HashImage.fromUrl("invalid-url")).rejects.toThrowError( await expect(HashImage.fromUrl("invalid-url")).rejects.toThrowError(
"Error on image download, make sure you are passing a valid string url" "Error on image download, make sure you are passing a valid string url",
); );
}); });
@ -51,7 +51,7 @@ describe("Test HashImage", () => {
expect.assertions(1); expect.assertions(1);
const image = new HashImage(testBuffer); const image = new HashImage(testBuffer);
await expect(image.compare("asdfsdf")).rejects.toThrowError( await expect(image.compare("asdfsdf")).rejects.toThrowError(
"Can't compare with a non HashImage value" "Can't compare with a non HashImage value",
); );
}); });
}); });
@ -69,7 +69,7 @@ describe("Test PHash", () => {
new PHash(2); new PHash(2);
} catch (err) { } catch (err) {
expect(err.message).toBe( expect(err.message).toBe(
"Can't construct a PHash instance with a non-string value" "Can't construct a PHash instance with a non-string value",
); );
} }
}); });
@ -80,7 +80,7 @@ describe("Test PHash", () => {
new PHash("dfgdfg"); new PHash("dfgdfg");
} catch (err) { } catch (err) {
expect(err.message).toBe( expect(err.message).toBe(
"Can't construct a PHash instance with a non-binary string value" "Can't construct a PHash instance with a non-binary string value",
); );
} }
}); });