mirror of
https://codeberg.org/JasterV/imgphash.git
synced 2026-04-26 18:10:01 +00:00
update code to support latest node
This commit is contained in:
parent
c9739ecc46
commit
985706b05d
4 changed files with 11 additions and 11 deletions
|
|
@ -7,7 +7,7 @@ export class HashImage {
|
|||
constructor(buffer) {
|
||||
if (!(buffer instanceof Uint8Array)) {
|
||||
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;
|
||||
|
|
@ -19,14 +19,14 @@ export class HashImage {
|
|||
return new HashImage(buffer);
|
||||
} catch (err) {
|
||||
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() {
|
||||
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);
|
||||
return new PHash(hash);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ export class PHash {
|
|||
constructor(hash) {
|
||||
if (typeof hash !== "string") {
|
||||
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
|
||||
|
|
@ -11,7 +11,7 @@ export class PHash {
|
|||
.some((chr) => chr !== "0" && chr !== "1");
|
||||
if (isNonBinary) {
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import HashImage from "./HashImage.js";
|
||||
import PHash from "./PHash.js";
|
||||
import { HashImage } from "./HashImage.js";
|
||||
import { PHash } from "./PHash.js";
|
||||
|
||||
export default { HashImage, PHash };
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ describe("Test HashImage", () => {
|
|||
it("Can't create an instance from an invalid url", async () => {
|
||||
expect.assertions(1);
|
||||
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);
|
||||
const image = new HashImage(testBuffer);
|
||||
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);
|
||||
} catch (err) {
|
||||
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");
|
||||
} catch (err) {
|
||||
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",
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue