Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prehook #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Create image differential between two images.
expectedFilename: string;
diffFilename?: string;
options?: {
preprocess?: ([Image, Image]) => void | Promise<[Image, Image]>;
threshold?: number; // default 0.1
includeAA?: boolean; // default false
}
Expand All @@ -51,7 +52,18 @@ Create image differential between two images.
- `actualFilename` - *Required* - Path to actual image file.
- `expectedFilename` - *Required* - Path to expected image file.
- `diffFilename` - *Optional* - Path to differential image file. If omitted, `imgDiff` does not output image file.
- `options` - *Optional* - An object to pass through [pixelmatch](https://github.com/mapbox/pixelmatch#api).
- `options` - *Optional*
- `options.preprocess` - *Optional* - Preprocess function. It's called with decoded images and also can process them.
- `options.threshold`, `options.includeAA` - *Optional* - Parameters using by [pixelmatch](https://github.com/mapbox/pixelmatch#api).

#### `Image`
```ts
{
width: number;
height: number;
data: Uint8Array;
}
```

#### `ImgDiffResult`

Expand Down
14 changes: 13 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,19 @@ function imgDiff(opt) {
decode(opt.actualFilename),
decode(opt.expectedFilename),
]).then(imgs => {
return compare(imgs[0], imgs[1], opt.diffFilename, opt.options);
const options = opt.options || { };
let pp;
if (options.preprocess && typeof options.preprocess === "function") {
const ret = options.preprocess(imgs);
if (typeof ret === "object" && ret.then) {
pp = ret;
} else {
pp = Promise.resolve(imgs);
}
} else {
pp = Promise.resolve(imgs);
}
return pp.then(imgs => compare(imgs[0], imgs[1], opt.diffFilename, options));
})
;
}
Expand Down
29 changes: 29 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,32 @@ test("compare with 2 jpeg files", async t => {
});
t.truthy(fs.statSync(path.resolve(__dirname, "images/diff_generated.jpg.png")));
});

test("call preprocess hook", async t => {
let called = false;
await imgDiff({
actualFilename: path.resolve(__dirname, "images/expected.png"),
expectedFilename: path.resolve(__dirname, "images/expected.png"),
options: {
preprocess: ([img1, img2]) => {
called = !!img1 && !!img2;
},
},
});
t.is(called, true);
});

test("call preprocess hook with promiss", async t => {
let called = false;
await imgDiff({
actualFilename: path.resolve(__dirname, "images/expected.png"),
expectedFilename: path.resolve(__dirname, "images/expected.png"),
options: {
preprocess: ([img1, img2]) => {
called = !!img1 && !!img2;
return Promise.resolve([img2, img1]);
},
},
});
t.is(called, true);
});