Skip to content

Commit 0e075b0

Browse files
authored
Merge pull request #1 from happo/dynamic-drift
Apply drift for lcs algorithm dynamically (based on image height)
2 parents 1dd17ef + cafcbc1 commit 0e075b0

File tree

9 files changed

+75
-3
lines changed

9 files changed

+75
-3
lines changed

.circleci/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: 2
2+
jobs:
3+
build:
4+
docker:
5+
- image: circleci/node:10
6+
working_directory: ~/repo
7+
steps:
8+
- checkout
9+
- run: yarn install
10+
- run: yarn test

snapshots/logo/after.png

1.73 KB
Loading

snapshots/logo/before.png

2.56 KB
Loading

snapshots/logo/diff.png

2.16 KB
Loading

snapshots/long-example/after.png

519 KB
Loading

snapshots/long-example/before.png

519 KB
Loading

snapshots/long-example/diff.png

626 KB
Loading

src/__tests__/snapshots-test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const crypto = require('crypto');
2+
const childProcess = require('child_process');
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const Jimp = require('jimp');
7+
8+
const imageDiff = require('../');
9+
10+
function hashFunction(data) {
11+
return crypto
12+
.createHash('md5')
13+
.update(data)
14+
.digest('hex');
15+
}
16+
17+
const snapshots = childProcess
18+
.execSync('ls snapshots', { encoding: 'utf-8' })
19+
.split(/\n/)
20+
.filter(Boolean);
21+
22+
jest.setTimeout(60000);
23+
24+
describe('snapshot tests', () => {
25+
snapshots.forEach(snapshot => {
26+
it(snapshot, async () => {
27+
console.log('Starting', snapshot);
28+
const [image1, image2] = await Promise.all([
29+
Jimp.read(path.resolve('snapshots', snapshot, 'before.png')),
30+
Jimp.read(path.resolve('snapshots', snapshot, 'after.png')),
31+
]);
32+
console.log('Images ready', snapshot);
33+
const diffImage = imageDiff(image1.bitmap, image2.bitmap, {
34+
hashFunction,
35+
});
36+
37+
console.log('Created diff image', snapshot);
38+
const pathToDiff = path.resolve('snapshots', snapshot, 'diff.png');
39+
40+
if (!fs.existsSync(pathToDiff)) {
41+
console.log(
42+
`No previous diff image for ${snapshot} found -- saving diff.png.`,
43+
);
44+
const newDiffImage = await new Jimp(diffImage);
45+
await newDiffImage.write(pathToDiff);
46+
}
47+
const expectedDiffImage = (await Jimp.read(pathToDiff)).bitmap;
48+
const diffHash = hashFunction(diffImage.data);
49+
const expectedHash = hashFunction(expectedDiffImage.data);
50+
if (diffHash !== expectedHash) {
51+
console.log(
52+
`Diff image did not match existing diff image. Remove this image and run again to re-generate:\n${pathToDiff}`,
53+
);
54+
}
55+
expect(diffHash).toEqual(expectedHash);
56+
});
57+
});
58+
});

src/alignArrays.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const MOVEMENT = {
66
};
77

88
const PLACEHOLDER = '+';
9-
const DRIFT_RANGE = 200;
9+
const MIN_DRIFT_RANGE = 200;
1010

1111
/**
1212
* Creates a 2d matrix of a certain size.
@@ -38,11 +38,15 @@ function longestCommonSubsequence(a, b) {
3838
const memo = initMatrix(aLength + 1, bLength + 1);
3939
const solution = initMatrix(aLength + 1, bLength + 1);
4040

41+
const usedDriftRange = Math.abs(
42+
Math.max(Math.max(aLength, bLength) / 10, MIN_DRIFT_RANGE),
43+
);
44+
4145
// Loop and find the solution
4246
for (let i = 1; i <= aLength; i += 1) {
4347
for (
44-
let j = Math.max(1, i - (DRIFT_RANGE / 2));
45-
j <= Math.min(bLength, i + (DRIFT_RANGE / 2));
48+
let j = Math.max(1, i - usedDriftRange / 2);
49+
j <= Math.min(bLength, i + usedDriftRange / 2);
4650
j += 1
4751
) {
4852
if (a[i - 1] === b[j - 1]) {

0 commit comments

Comments
 (0)