UNPKG

looks-same

Version:

Pure node.js library for comparing PNG-images, taking into account human color perception.

34 lines (27 loc) 1.11 kB
'use strict'; const {REQUIRED_IMAGE_FIELDS, REQUIRED_BOUNDING_BOX_FIELDS} = require('./constants'); const validateRequiredFields = (value, fields) => { [].concat(fields).forEach((field) => { if (!value || !Object.hasOwn(value, field)) { throw new TypeError(`Field "${field}" does not exist in ${JSON.stringify(value)}`); } }); }; const validateBoundingBoxCoords = ({boundingBox}) => { if (boundingBox.left > boundingBox.right) { throw new TypeError('"left" coordinate in "boundingBox" field cannot be greater than "right"'); } if (boundingBox.top > boundingBox.bottom) { throw new TypeError('"top" coordinate in "boundingBox" field cannot be greater than "bottom"'); } }; exports.validateImages = (img1, img2) => { [img1, img2].forEach((i) => { if (Buffer.isBuffer(i) || i === null || typeof i !== 'object') { return; } validateRequiredFields(i, REQUIRED_IMAGE_FIELDS); validateRequiredFields(i.boundingBox, REQUIRED_BOUNDING_BOX_FIELDS); validateBoundingBoxCoords(i); }); };