Skip to content

Commit 91eb2f1

Browse files
powerfulyangpowerfulyang
authored andcommitted
chore: update
1 parent 380db52 commit 91eb2f1

6 files changed

Lines changed: 3806 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
dist
44
package-lock.json
55
pnpm-lock.yaml
6+
.turbo

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@
5454
"devDependencies": {
5555
"@powerfulyang/lint": "3.1.3",
5656
"@rollup/plugin-node-resolve": "15.0.2",
57-
"@rollup/plugin-typescript": "10.0.1",
57+
"@rollup/plugin-typescript": "11.1.0",
5858
"@types/sharp": "0.31.1",
59+
"sharp": "0.31.3",
5960
"typescript": "5.0.4"
6061
},
6162
"peerDependencies": {
62-
"sharp": "~0.31.3",
63-
"@powerfulyang/utils": "^1.9.2"
63+
"sharp": "~0.31.3 || ~0.32.0"
64+
},
65+
"dependencies": {
66+
"@powerfulyang/utils": "1.9.2"
6467
}
6568
}

src/__test__/image/phash.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { readFileSync } from 'fs';
22
import { join } from 'path';
3-
import { binaryHammingDistance, pHash } from '@/image';
3+
import { binaryStringHammingDistance, pHash } from '@/image';
44

55
describe('test images phash', () => {
66
it('get phash', async () => {
77
const buf1 = readFileSync(join(__dirname, '2e7d394191b41f8bdedf0a0901dcde883d1b9420.jpg'));
88
const buf2 = readFileSync(join(__dirname, 'd047ad7bd219d954c2f1290e3e57d62b34a2dbeb.jpg'));
99
const phash1 = await pHash(buf1);
1010
const phash2 = await pHash(buf2);
11-
expect(binaryHammingDistance(phash1, phash2)).toBe(11);
11+
expect(binaryStringHammingDistance(phash1, phash2)).toBe(11);
1212
});
1313
});

src/image/image.spec.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
1-
import { binaryHammingDistance, decimalHammingDistance } from '@/image/phash';
1+
import {
2+
binaryHammingDistance,
3+
binaryStringHammingDistance,
4+
decimalHammingDistance,
5+
} from '@/image/phash';
6+
import * as fs from 'fs';
7+
import * as path from 'path';
28

39
describe('images', () => {
4-
const a = '1234';
5-
const b = '4321';
10+
const a = '14870051262695068000';
11+
const b = '6827366053207273000';
612

713
it('decimal hamming distance', () => {
814
const hammingDistance = decimalHammingDistance(a, b);
915
expect(hammingDistance).toBe(6);
1016
});
1117

1218
it('binary hamming distance', () => {
13-
const hammingDistance = binaryHammingDistance(Number(a).toString(2), Number(b).toString(2));
19+
const _a = Number(a).toString(2);
20+
const _b = Number(b).toString(2);
21+
const hammingDistance = binaryStringHammingDistance(_a, _b);
1422
expect(hammingDistance).toBe(6);
23+
const hammingDistance2 = binaryHammingDistance(_a, _b);
24+
expect(hammingDistance2).toBe(6);
25+
});
26+
27+
it('timing', () => {
28+
const data = fs.readFileSync(path.join(__dirname, 'test_public_asset.csv'), 'utf8');
29+
const lines = data.split('\n');
30+
const images = lines.filter((line) => line);
31+
const start = Date.now();
32+
for (let i = 0; i < images.length; i++) {
33+
for (let j = i + 1; j < images.length; j++) {
34+
binaryHammingDistance(images[i], images[j]);
35+
}
36+
}
37+
const end = Date.now();
38+
const start2 = Date.now();
39+
for (let i = 0; i < images.length; i++) {
40+
for (let j = i + 1; j < images.length; j++) {
41+
binaryStringHammingDistance(images[i], images[j]);
42+
}
43+
}
44+
const end2 = Date.now();
45+
expect(end2 - start2).toBeLessThan(end - start);
1546
});
1647
});

src/image/phash.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-bitwise */
12
import sharp from 'sharp';
23

34
const SAMPLE_SIZE = 32;
@@ -95,7 +96,6 @@ export const bitCount = (n: number) => {
9596

9697
while (tmp) {
9798
result += 1;
98-
// eslint-disable-next-line no-bitwise
9999
tmp &= tmp - 1;
100100
}
101101

@@ -108,7 +108,6 @@ export const bitCount = (n: number) => {
108108
* @param b - second string or number e.g. '654321'
109109
*/
110110
export const decimalHammingDistance = (a: string | number, b: string | number) => {
111-
// eslint-disable-next-line no-bitwise
112111
const res = Number(a) ^ Number(b);
113112
return bitCount(res);
114113
};
@@ -133,3 +132,13 @@ export const binaryHammingDistance = (a: string, b: string) => {
133132
}
134133
throw new Error('Invalid binary string');
135134
};
135+
136+
/**
137+
* Hamming distance between two binary strings
138+
* @param a - first string e.g. '101010'
139+
* @param b - second string e.g. '010101'
140+
*/
141+
export const binaryStringHammingDistance = (a: string, b: string) => {
142+
const res = Number.parseInt(a, 2) ^ Number.parseInt(b, 2);
143+
return bitCount(res);
144+
};

0 commit comments

Comments
 (0)