@blazediff/matcher 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +3 -3
- package/dist/index.mjs +3 -3
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var fs=require('fs'),path=require('path'),pngjsTransformer=require('@blazediff/pngjs-transformer'),bin=require('@blazediff/bin'),core=require('@blazediff/core'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim'),s=require('picocolors');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var s__default=/*#__PURE__*/_interopDefault(s);function c(t){return typeof t=="string"}function
|
|
2
|
-
`)}function
|
|
3
|
-
`)}function
|
|
1
|
+
'use strict';var fs=require('fs'),path=require('path'),pngjsTransformer=require('@blazediff/pngjs-transformer'),bin=require('@blazediff/bin'),core=require('@blazediff/core'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim'),s=require('picocolors');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var s__default=/*#__PURE__*/_interopDefault(s);function c(t){return typeof t=="string"}function _(t){return typeof t=="object"&&t!==null&&"data"in t&&"width"in t&&"height"in t}async function w(t){if(!fs.existsSync(t))throw new Error(`Image file not found: ${t}`);let e=await pngjsTransformer.pngjsTransformer.read(t);return {data:new Uint8Array(e.data),width:e.width,height:e.height}}async function l(t,e,n,r){let o=path.dirname(t);fs.existsSync(o)||fs.mkdirSync(o,{recursive:true}),await pngjsTransformer.pngjsTransformer.write({data:e,width:n,height:r},t);}async function C(t){return c(t)?w(t):{data:new Uint8Array(t.data),width:t.width,height:t.height}}function D(t){return fs.existsSync(t)}function N(t){fs.existsSync(t)||fs.mkdirSync(t,{recursive:true});}async function T(t,e,n,r){if(!c(t))throw new Error("Method 'bin' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");if(!c(e))throw new Error("Method 'bin' only supports file paths for baseline, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");let o=await bin.compare(t,e,n,{threshold:r.threshold,antialiasing:r.antialiasing});if(o.match)return {diffCount:0,diffPercentage:0};if(o.reason==="layout-diff")return {diffCount:Number.MAX_SAFE_INTEGER,diffPercentage:100};if(o.reason==="pixel-diff")return {diffCount:o.diffCount,diffPercentage:o.diffPercentage};if(o.reason==="file-not-exists")throw new Error(`Image file not found: ${o.file}`);return {diffCount:0,diffPercentage:0}}function E(t,e,n,r){let{width:o,height:i}=t,a=o*i;if(t.width!==e.width||t.height!==e.height)return {diffCount:a,diffPercentage:100};let m=n?new Uint8Array(a*4):void 0,u=core.diff(t.data,e.data,m,o,i,{threshold:r.threshold??.1,includeAA:r.includeAA??false});return {diffCount:u,diffPercentage:u/a*100,diffOutput:m}}function A(t,e,n,r){let{width:o,height:i}=t,a=o*i;if(t.width!==e.width||t.height!==e.height)return {score:1};let m=n?new Uint8Array(a*4):void 0;return {score:gmsd.gmsd(t.data,e.data,m,o,i,{downsample:r.downsample}),diffOutput:m}}function R(t,e,n,r,o){let{width:i,height:a}=t,m=i*a;if(t.width!==e.width||t.height!==e.height)return {score:0};let u=r?new Uint8Array(m*4):void 0,d={windowSize:o.windowSize,k1:o.k1,k2:o.k2},g;switch(n){case "ssim":g=ssim.ssim(t.data,e.data,u,i,a,d);break;case "msssim":g=msssim.msssim(t.data,e.data,u,i,a,d);break;case "hitchhikers-ssim":g=hitchhikersSsim.hitchhikersSSIM(t.data,e.data,u,i,a,d);break;default:throw new Error(`Unknown SSIM method: ${n}`)}return {score:g,diffOutput:u}}function I(t){return t==="ssim"||t==="msssim"||t==="hitchhikers-ssim"}function U(t,e){if(t==="bin"&&!c(e))throw new Error("Method 'bin' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.")}async function S(t,e,n,r,o){if(U(n,t),U(n,e),n==="bin"){let d=await T(t,e,o,r);return {diffCount:d.diffCount,diffPercentage:d.diffPercentage}}let i=await C(t),a=await C(e),m=o!==void 0;if(I(n)){let d=R(i,a,n,m,r);return {score:d.score,diffOutput:d.diffOutput}}if(n==="gmsd"){let d=A(i,a,m,r);return {score:d.score,diffOutput:d.diffOutput}}let u=E(i,a,m,r);return {diffCount:u.diffCount,diffPercentage:u.diffPercentage,diffOutput:u.diffOutput}}var y={success:s__default.default.isColorSupported?"\u2714":"\u221A",error:s__default.default.isColorSupported?"\u2716":"\xD7",info:s__default.default.isColorSupported?"\u2139":"i",arrow:s__default.default.isColorSupported?"\u2514\u2500":"'-"},q=new Set(["ssim","msssim","hitchhikers-ssim"]);function P(t){return t.snapshotCreated?J(t.baselinePath):t.pass?K():Q(t)}function J(t){return [`${s__default.default.green(y.success)} ${s__default.default.green("New snapshot created")}`,` ${s__default.default.dim(y.arrow)} ${s__default.default.dim(t)}`].join(`
|
|
2
|
+
`)}function K(){return `${s__default.default.green(y.success)} ${s__default.default.green("Image matches snapshot")}`}function Q(t){let{method:e,baselinePath:n,receivedPath:r,diffPath:o,diffCount:i=0,diffPercentage:a,score:m=0,threshold:u,thresholdType:d,updateCommand:g}=t,f=[`${s__default.default.red(y.error)} ${s__default.default.red(s__default.default.bold("Image snapshot mismatch"))}`,""],p=12;if(f.push(` ${s__default.default.dim("Method".padEnd(p))}${e}`),f.push(` ${s__default.default.dim("Baseline".padEnd(p))}${s__default.default.dim(n)}`),f.push(` ${s__default.default.dim("Received".padEnd(p))}${s__default.default.dim(r)}`),f.push(` ${s__default.default.dim("Diff".padEnd(p))}${s__default.default.dim(o)}`),f.push(""),q.has(e)){let h=((1-m)*100).toFixed(2);f.push(` ${s__default.default.dim("SSIM Score".padEnd(p))}${s__default.default.yellow(m.toFixed(4))} ${s__default.default.dim("(1.0 = identical)")}`),f.push(` ${s__default.default.dim("Difference".padEnd(p))}${s__default.default.yellow(`${h}%`)}`);}else if(e==="gmsd")f.push(` ${s__default.default.dim("GMSD Score".padEnd(p))}${s__default.default.yellow(m.toFixed(4))} ${s__default.default.dim("(0.0 = identical)")}`);else {let h=a?.toFixed(2)??"0.00";f.push(` ${s__default.default.dim("Difference".padEnd(p))}${s__default.default.yellow(i.toLocaleString())} pixels ${s__default.default.dim(`(${h}%)`)}`);}let k=d==="percent"?"%":"pixels";return f.push(` ${s__default.default.dim("Threshold".padEnd(p))}${u} ${k}`),f.push(""),f.push(` ${s__default.default.cyan(y.info)} ${s__default.default.cyan(`Run with ${g??"--update"} to update the snapshot`)}`),f.join(`
|
|
3
|
+
`)}function v(t){return t.testName.replace(/[^a-zA-Z0-9-_\s]/g,"").replace(/\s+/g,"-").toLowerCase()||"snapshot"}function tt(t,e){let n=path.dirname(t.testPath),r=e.snapshotsDir??"__snapshots__",o=path.isAbsolute(r)?r:path.join(n,r),i=e.snapshotIdentifier??v(t);return {snapshotDir:o,baselinePath:path.join(o,`${i}.png`),receivedPath:path.join(o,`${i}.received.png`),diffPath:path.join(o,`${i}.diff.png`)}}function B(t,e,n){let r=n.failureThreshold??0,o=n.failureThresholdType??"pixel";if(I(t)){let i=e.score??0;return o==="percent"?(1-i)*100<=r:i>=1-r/100}if(t==="gmsd"){let i=e.score??0;return o==="percent"?i*100<=r:i<=r/100}return o==="percent"?(e.diffPercentage??0)<=r:(e.diffCount??0)<=r}async function et(t,e,n){let r=tt(n,e),{snapshotDir:o,baselinePath:i,receivedPath:a,diffPath:m}=r;N(o);let u=D(i),d=e.updateSnapshots===true?"all":e.updateSnapshots===false||e.updateSnapshots===void 0?"new":e.updateSnapshots;if(d!=="none"&&(u&&d==="all"||!u&&(d==="new"||d==="all"))){if(c(t)){let O=await w(t);await l(i,O.data,O.width,O.height);}else await l(i,t.data,t.width,t.height);return fs.existsSync(a)&&fs.unlinkSync(a),fs.existsSync(m)&&fs.unlinkSync(m),{pass:true,message:P({pass:true,method:e.method,snapshotCreated:true,baselinePath:i,receivedPath:a,diffPath:m,diffCount:0,diffPercentage:0,score:0,threshold:e.failureThreshold??0,thresholdType:e.failureThresholdType??"pixel",updateCommand:e.updateCommand}),baselinePath:i,snapshotStatus:u?"updated":"added"}}let f=await S(t,i,e.method,e,m),p=B(e.method,f,e);if(p)return fs.existsSync(a)&&fs.unlinkSync(a),fs.existsSync(m)&&fs.unlinkSync(m),{pass:true,message:P({pass:p,method:e.method,snapshotCreated:false,baselinePath:i,receivedPath:a,diffPath:m,diffCount:f.diffCount,diffPercentage:f.diffPercentage,score:f.score,threshold:e.failureThreshold??0,thresholdType:e.failureThresholdType??"pixel",updateCommand:e.updateCommand}),diffCount:f.diffCount,diffPercentage:f.diffPercentage,score:f.score,baselinePath:i,snapshotStatus:"matched"};if(c(t)){let h=await w(t);await l(a,h.data,h.width,h.height);}else await l(a,t.data,t.width,t.height);if(f.diffOutput){let h=await C(t);await l(m,f.diffOutput,h.width,h.height);}return {pass:false,message:P({pass:p,method:e.method,snapshotCreated:false,baselinePath:i,receivedPath:a,diffPath:m,diffCount:f.diffCount,diffPercentage:f.diffPercentage,score:f.score,threshold:e.failureThreshold??0,thresholdType:e.failureThresholdType??"pixel",updateCommand:e.updateCommand}),diffCount:f.diffCount,diffPercentage:f.diffPercentage,score:f.score,baselinePath:i,receivedPath:a,diffPath:m,snapshotStatus:"failed"}}async function rt(t,e,n){let r=await S(t,e,n.method,n),o=B(n.method,r,n);return {pass:o,message:o?"Images match.":`Images differ: ${r.diffCount??r.score} ${r.diffCount!==void 0?"pixels":"score"}`,diffCount:r.diffCount,diffPercentage:r.diffPercentage,score:r.score}}exports.compareImages=rt;exports.fileExists=D;exports.formatReport=P;exports.getOrCreateSnapshot=et;exports.isFilePath=c;exports.isImageBuffer=_;exports.loadPNG=w;exports.normalizeImageInput=C;exports.runComparison=S;exports.savePNG=l;exports.validateMethodSupportsInput=U;
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {existsSync,mkdirSync,unlinkSync}from'fs';import {dirname,isAbsolute,join}from'path';import {pngjsTransformer}from'@blazediff/pngjs-transformer';import {compare}from'@blazediff/bin';import {diff}from'@blazediff/core';import {gmsd}from'@blazediff/gmsd';import {ssim}from'@blazediff/ssim';import {hitchhikersSSIM}from'@blazediff/ssim/hitchhikers-ssim';import {msssim}from'@blazediff/ssim/msssim';import s from'picocolors';function c(t){return typeof t=="string"}function
|
|
2
|
-
`)}function
|
|
3
|
-
`)}function
|
|
1
|
+
import {existsSync,mkdirSync,unlinkSync}from'fs';import {dirname,isAbsolute,join}from'path';import {pngjsTransformer}from'@blazediff/pngjs-transformer';import {compare}from'@blazediff/bin';import {diff}from'@blazediff/core';import {gmsd}from'@blazediff/gmsd';import {ssim}from'@blazediff/ssim';import {hitchhikersSSIM}from'@blazediff/ssim/hitchhikers-ssim';import {msssim}from'@blazediff/ssim/msssim';import s from'picocolors';function c(t){return typeof t=="string"}function _(t){return typeof t=="object"&&t!==null&&"data"in t&&"width"in t&&"height"in t}async function w(t){if(!existsSync(t))throw new Error(`Image file not found: ${t}`);let e=await pngjsTransformer.read(t);return {data:new Uint8Array(e.data),width:e.width,height:e.height}}async function l(t,e,n,r){let o=dirname(t);existsSync(o)||mkdirSync(o,{recursive:true}),await pngjsTransformer.write({data:e,width:n,height:r},t);}async function C(t){return c(t)?w(t):{data:new Uint8Array(t.data),width:t.width,height:t.height}}function D(t){return existsSync(t)}function N(t){existsSync(t)||mkdirSync(t,{recursive:true});}async function T(t,e,n,r){if(!c(t))throw new Error("Method 'bin' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");if(!c(e))throw new Error("Method 'bin' only supports file paths for baseline, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");let o=await compare(t,e,n,{threshold:r.threshold,antialiasing:r.antialiasing});if(o.match)return {diffCount:0,diffPercentage:0};if(o.reason==="layout-diff")return {diffCount:Number.MAX_SAFE_INTEGER,diffPercentage:100};if(o.reason==="pixel-diff")return {diffCount:o.diffCount,diffPercentage:o.diffPercentage};if(o.reason==="file-not-exists")throw new Error(`Image file not found: ${o.file}`);return {diffCount:0,diffPercentage:0}}function E(t,e,n,r){let{width:o,height:i}=t,a=o*i;if(t.width!==e.width||t.height!==e.height)return {diffCount:a,diffPercentage:100};let m=n?new Uint8Array(a*4):void 0,u=diff(t.data,e.data,m,o,i,{threshold:r.threshold??.1,includeAA:r.includeAA??false});return {diffCount:u,diffPercentage:u/a*100,diffOutput:m}}function A(t,e,n,r){let{width:o,height:i}=t,a=o*i;if(t.width!==e.width||t.height!==e.height)return {score:1};let m=n?new Uint8Array(a*4):void 0;return {score:gmsd(t.data,e.data,m,o,i,{downsample:r.downsample}),diffOutput:m}}function R(t,e,n,r,o){let{width:i,height:a}=t,m=i*a;if(t.width!==e.width||t.height!==e.height)return {score:0};let u=r?new Uint8Array(m*4):void 0,d={windowSize:o.windowSize,k1:o.k1,k2:o.k2},g;switch(n){case "ssim":g=ssim(t.data,e.data,u,i,a,d);break;case "msssim":g=msssim(t.data,e.data,u,i,a,d);break;case "hitchhikers-ssim":g=hitchhikersSSIM(t.data,e.data,u,i,a,d);break;default:throw new Error(`Unknown SSIM method: ${n}`)}return {score:g,diffOutput:u}}function I(t){return t==="ssim"||t==="msssim"||t==="hitchhikers-ssim"}function U(t,e){if(t==="bin"&&!c(e))throw new Error("Method 'bin' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.")}async function S(t,e,n,r,o){if(U(n,t),U(n,e),n==="bin"){let d=await T(t,e,o,r);return {diffCount:d.diffCount,diffPercentage:d.diffPercentage}}let i=await C(t),a=await C(e),m=o!==void 0;if(I(n)){let d=R(i,a,n,m,r);return {score:d.score,diffOutput:d.diffOutput}}if(n==="gmsd"){let d=A(i,a,m,r);return {score:d.score,diffOutput:d.diffOutput}}let u=E(i,a,m,r);return {diffCount:u.diffCount,diffPercentage:u.diffPercentage,diffOutput:u.diffOutput}}var y={success:s.isColorSupported?"\u2714":"\u221A",error:s.isColorSupported?"\u2716":"\xD7",info:s.isColorSupported?"\u2139":"i",arrow:s.isColorSupported?"\u2514\u2500":"'-"},q=new Set(["ssim","msssim","hitchhikers-ssim"]);function P(t){return t.snapshotCreated?J(t.baselinePath):t.pass?K():Q(t)}function J(t){return [`${s.green(y.success)} ${s.green("New snapshot created")}`,` ${s.dim(y.arrow)} ${s.dim(t)}`].join(`
|
|
2
|
+
`)}function K(){return `${s.green(y.success)} ${s.green("Image matches snapshot")}`}function Q(t){let{method:e,baselinePath:n,receivedPath:r,diffPath:o,diffCount:i=0,diffPercentage:a,score:m=0,threshold:u,thresholdType:d,updateCommand:g}=t,f=[`${s.red(y.error)} ${s.red(s.bold("Image snapshot mismatch"))}`,""],p=12;if(f.push(` ${s.dim("Method".padEnd(p))}${e}`),f.push(` ${s.dim("Baseline".padEnd(p))}${s.dim(n)}`),f.push(` ${s.dim("Received".padEnd(p))}${s.dim(r)}`),f.push(` ${s.dim("Diff".padEnd(p))}${s.dim(o)}`),f.push(""),q.has(e)){let h=((1-m)*100).toFixed(2);f.push(` ${s.dim("SSIM Score".padEnd(p))}${s.yellow(m.toFixed(4))} ${s.dim("(1.0 = identical)")}`),f.push(` ${s.dim("Difference".padEnd(p))}${s.yellow(`${h}%`)}`);}else if(e==="gmsd")f.push(` ${s.dim("GMSD Score".padEnd(p))}${s.yellow(m.toFixed(4))} ${s.dim("(0.0 = identical)")}`);else {let h=a?.toFixed(2)??"0.00";f.push(` ${s.dim("Difference".padEnd(p))}${s.yellow(i.toLocaleString())} pixels ${s.dim(`(${h}%)`)}`);}let k=d==="percent"?"%":"pixels";return f.push(` ${s.dim("Threshold".padEnd(p))}${u} ${k}`),f.push(""),f.push(` ${s.cyan(y.info)} ${s.cyan(`Run with ${g??"--update"} to update the snapshot`)}`),f.join(`
|
|
3
|
+
`)}function v(t){return t.testName.replace(/[^a-zA-Z0-9-_\s]/g,"").replace(/\s+/g,"-").toLowerCase()||"snapshot"}function tt(t,e){let n=dirname(t.testPath),r=e.snapshotsDir??"__snapshots__",o=isAbsolute(r)?r:join(n,r),i=e.snapshotIdentifier??v(t);return {snapshotDir:o,baselinePath:join(o,`${i}.png`),receivedPath:join(o,`${i}.received.png`),diffPath:join(o,`${i}.diff.png`)}}function B(t,e,n){let r=n.failureThreshold??0,o=n.failureThresholdType??"pixel";if(I(t)){let i=e.score??0;return o==="percent"?(1-i)*100<=r:i>=1-r/100}if(t==="gmsd"){let i=e.score??0;return o==="percent"?i*100<=r:i<=r/100}return o==="percent"?(e.diffPercentage??0)<=r:(e.diffCount??0)<=r}async function et(t,e,n){let r=tt(n,e),{snapshotDir:o,baselinePath:i,receivedPath:a,diffPath:m}=r;N(o);let u=D(i),d=e.updateSnapshots===true?"all":e.updateSnapshots===false||e.updateSnapshots===void 0?"new":e.updateSnapshots;if(d!=="none"&&(u&&d==="all"||!u&&(d==="new"||d==="all"))){if(c(t)){let O=await w(t);await l(i,O.data,O.width,O.height);}else await l(i,t.data,t.width,t.height);return existsSync(a)&&unlinkSync(a),existsSync(m)&&unlinkSync(m),{pass:true,message:P({pass:true,method:e.method,snapshotCreated:true,baselinePath:i,receivedPath:a,diffPath:m,diffCount:0,diffPercentage:0,score:0,threshold:e.failureThreshold??0,thresholdType:e.failureThresholdType??"pixel",updateCommand:e.updateCommand}),baselinePath:i,snapshotStatus:u?"updated":"added"}}let f=await S(t,i,e.method,e,m),p=B(e.method,f,e);if(p)return existsSync(a)&&unlinkSync(a),existsSync(m)&&unlinkSync(m),{pass:true,message:P({pass:p,method:e.method,snapshotCreated:false,baselinePath:i,receivedPath:a,diffPath:m,diffCount:f.diffCount,diffPercentage:f.diffPercentage,score:f.score,threshold:e.failureThreshold??0,thresholdType:e.failureThresholdType??"pixel",updateCommand:e.updateCommand}),diffCount:f.diffCount,diffPercentage:f.diffPercentage,score:f.score,baselinePath:i,snapshotStatus:"matched"};if(c(t)){let h=await w(t);await l(a,h.data,h.width,h.height);}else await l(a,t.data,t.width,t.height);if(f.diffOutput){let h=await C(t);await l(m,f.diffOutput,h.width,h.height);}return {pass:false,message:P({pass:p,method:e.method,snapshotCreated:false,baselinePath:i,receivedPath:a,diffPath:m,diffCount:f.diffCount,diffPercentage:f.diffPercentage,score:f.score,threshold:e.failureThreshold??0,thresholdType:e.failureThresholdType??"pixel",updateCommand:e.updateCommand}),diffCount:f.diffCount,diffPercentage:f.diffPercentage,score:f.score,baselinePath:i,receivedPath:a,diffPath:m,snapshotStatus:"failed"}}async function rt(t,e,n){let r=await S(t,e,n.method,n),o=B(n.method,r,n);return {pass:o,message:o?"Images match.":`Images differ: ${r.diffCount??r.score} ${r.diffCount!==void 0?"pixels":"score"}`,diffCount:r.diffCount,diffPercentage:r.diffPercentage,score:r.score}}export{rt as compareImages,D as fileExists,P as formatReport,et as getOrCreateSnapshot,c as isFilePath,_ as isImageBuffer,w as loadPNG,C as normalizeImageInput,S as runComparison,l as savePNG,U as validateMethodSupportsInput};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blazediff/matcher",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Core matcher logic for visual regression testing with blazediff",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"@blazediff/bin": "3.1.0",
|
|
37
37
|
"@blazediff/core": "1.9.0",
|
|
38
38
|
"@blazediff/gmsd": "1.7.0",
|
|
39
|
-
"@blazediff/
|
|
40
|
-
"@blazediff/
|
|
39
|
+
"@blazediff/ssim": "1.7.0",
|
|
40
|
+
"@blazediff/pngjs-transformer": "2.1.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "^24.3.0",
|