Merge pull request #4 from Flet/update-deps-and-convert-to-esm

update deps and convert to ESM
This commit is contained in:
Víctor Martínez 2024-04-04 10:01:07 +02:00 committed by GitHub
commit f75019868e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 1006 additions and 6831 deletions

7146
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -12,18 +12,19 @@
], ],
"author": "Victor Martinez <jaster.victor@gmail.com>", "author": "Victor Martinez <jaster.victor@gmail.com>",
"license": "MIT", "license": "MIT",
"type": "module",
"scripts": { "scripts": {
"test": "jest" "test": "mocha"
}, },
"dependencies": { "dependencies": {
"@canvas/image": "^1.0.1", "@canvas/image": "^2.0.0",
"axios": "^0.26.1", "axios": "^1.6.8",
"blockhash-core": "^0.1.0" "blockhash-core": "^0.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^27.4.1", "@types/node": "^20.12.3",
"@types/node": "^17.0.21", "expect": "^29.7.0",
"jest": "^27.5.1" "mocha": "^10.4.0"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View file

@ -1,9 +1,9 @@
const { imageFromBuffer, getImageData } = require("@canvas/image"); import { imageFromBuffer, getImageData } from "@canvas/image";
const PHash = require("./PHash"); import { PHash } from "./PHash.js";
const { download, hexToBin } = require("./utils"); import { download, hexToBin } from "./utils/index.js";
const blockhash = require("blockhash-core"); import blockhash from "blockhash-core";
class HashImage { export class HashImage {
constructor(buffer) { constructor(buffer) {
if (!(buffer instanceof Uint8Array)) { if (!(buffer instanceof Uint8Array)) {
throw new Error( throw new Error(
@ -40,5 +40,3 @@ class HashImage {
return hash1.compare(hash2); return hash1.compare(hash2);
} }
} }
module.exports = HashImage;

View file

@ -1,4 +1,4 @@
class PHash { export class PHash {
constructor(hash) { constructor(hash) {
if (typeof hash !== "string") { if (typeof hash !== "string") {
throw new Error( throw new Error(
@ -32,5 +32,3 @@ class PHash {
return similarity / maxLength; return similarity / maxLength;
} }
} }
module.exports = PHash;

View file

@ -1,4 +1,4 @@
const HashImage = require("./HashImage"); import HashImage from "./HashImage.js";
const PHash = require("./PHash"); import PHash from "./PHash.js";
module.exports = { HashImage, PHash }; export default { HashImage, PHash };

View file

@ -1,7 +1,7 @@
const { default: axios } = require("axios"); import axios from "axios";
// Utils // Utils
async function download(url) { export async function download(url) {
const response = await axios.get(url, { responseType: "stream" }); const response = await axios.get(url, { responseType: "stream" });
const stream = response.data; const stream = response.data;
const chunks = []; const chunks = [];
@ -12,7 +12,7 @@ async function download(url) {
return buffer; return buffer;
} }
function calculateHashSimilarity(hash1, hash2) { export function calculateHashSimilarity(hash1, hash2) {
let similarity = 0; let similarity = 0;
const hash1Array = hash1.split(""); const hash1Array = hash1.split("");
hash1Array.forEach((bit, index) => { hash1Array.forEach((bit, index) => {
@ -21,7 +21,7 @@ function calculateHashSimilarity(hash1, hash2) {
return similarity / hash1.length; return similarity / hash1.length;
} }
function hexToBin(hexString) { export function hexToBin(hexString) {
const hexBinLookup = { const hexBinLookup = {
0: "0000", 0: "0000",
1: "0001", 1: "0001",
@ -52,9 +52,3 @@ function hexToBin(hexString) {
} }
return result; return result;
} }
module.exports = {
download,
hexToBin,
calculateHashSimilarity,
};

View file

@ -1,5 +1,7 @@
const HashImage = require("../src/HashImage"); import { HashImage } from "../src/HashImage.js";
const PHash = require("../src/PHash"); import { PHash } from "../src/PHash.js";
import {expect} from 'expect';
const url1 = const url1 =
"https://res.cloudinary.com/demo/image/upload/f_auto,q_auto/w_400/koala1.jpg"; "https://res.cloudinary.com/demo/image/upload/f_auto,q_auto/w_400/koala1.jpg";