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 692 additions and 6517 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>",
"license": "MIT",
"type": "module",
"scripts": {
"test": "jest"
"test": "mocha"
},
"dependencies": {
"@canvas/image": "^1.0.1",
"axios": "^0.26.1",
"@canvas/image": "^2.0.0",
"axios": "^1.6.8",
"blockhash-core": "^0.1.0"
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/node": "^17.0.21",
"jest": "^27.5.1"
"@types/node": "^20.12.3",
"expect": "^29.7.0",
"mocha": "^10.4.0"
},
"repository": {
"type": "git",

View file

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

View file

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

View file

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

View file

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