UNPKG

testplane

Version:

Tests framework based on mocha and wdio

198 lines 6.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromBcrToRect = exports.fromDeviceToCssNumber = exports.fromCssToDeviceNumber = exports.fromCssToDevice = exports.ceilCoords = exports.floorCoords = exports.roundCoords = exports.getCoveringRect = exports.fromViewportToCaptureArea = exports.fromCaptureAreaToViewport = exports.getMinCoord = exports.getMaxCoord = exports.getMaxLength = exports.getRight = exports.getBottom = exports.getWidth = exports.getHeight = exports.getIntersection = exports.intersectXBands = exports.intersectYBands = exports.prettyRect = exports.prettySize = exports.prettyPoint = exports.equalsSize = exports.subtractCoords = exports.addCoords = exports.getSize = void 0; const assign_1 = require("./assign"); const getSize = (rect) => { return { width: rect.width, height: rect.height, }; }; exports.getSize = getSize; const addCoords = (a, b) => { return (a + b); }; exports.addCoords = addCoords; const subtractCoords = (a, b) => { return (a - b); }; exports.subtractCoords = subtractCoords; const equalsSize = (a, b) => { return a.width === b.width && a.height === b.height; }; exports.equalsSize = equalsSize; const prettyPoint = (point) => { return `{ left: ${point.left}, top: ${point.top} }`; }; exports.prettyPoint = prettyPoint; const prettySize = (size) => { return `{ width: ${size.width}, height: ${size.height} }`; }; exports.prettySize = prettySize; const prettyRect = (rect) => { return `{ left: ${rect.left}, top: ${rect.top}, width: ${rect.width}, height: ${rect.height} }`; }; exports.prettyRect = prettyRect; const intersectYBands = (a, b) => { if (!a || !b) { return null; } const top = Math.max(a.top, b.top); const bottom = Math.min(a.top + a.height, b.top + b.height); return bottom <= top ? null : { top, height: bottom - top }; }; exports.intersectYBands = intersectYBands; const intersectXBands = (a, b) => { if (!a || !b) { return null; } const left = Math.max(a.left, b.left); const right = Math.min(a.left + a.width, b.left + b.width); return right <= left ? null : { left, width: right - left }; }; exports.intersectXBands = intersectXBands; const getIntersection = (a, b) => { if (!a || !b) return null; const result = { ...a, ...b }; if ("top" in a && "top" in b) { const y = (0, exports.intersectYBands)(a, b); if (!y) return null; (0, assign_1.assign)(result, y); } if ("left" in a && "left" in b) { const x = (0, exports.intersectXBands)(a, b); if (!x) return null; (0, assign_1.assign)(result, x); } return result; }; exports.getIntersection = getIntersection; /* Note: width and height between a and b is computed inclusive of a, but exclusive of b. Width between 1 and 2 is 1. bottom of rect with top=0 and height=1 is 1. These conventions are very useful when dealing with 0-sized areas. */ const getHeight = (a, b) => { return Math.abs(a - b); }; exports.getHeight = getHeight; const getWidth = (a, b) => { return Math.abs(a - b); }; exports.getWidth = getWidth; const getBottom = (bandOrRect) => { return (bandOrRect.top + bandOrRect.height); }; exports.getBottom = getBottom; const getRight = (bandOrRect) => { return (bandOrRect.left + bandOrRect.width); }; exports.getRight = getRight; const getMaxLength = (...lengths) => { return Math.max(...lengths); }; exports.getMaxLength = getMaxLength; const getMaxCoord = (...coords) => { return Math.max(...coords); }; exports.getMaxCoord = getMaxCoord; const getMinCoord = (...coords) => { return Math.min(...coords); }; exports.getMinCoord = getMinCoord; const fromCaptureAreaToViewport = (coordRelativeToCaptureArea, captureAreaTopRelativeToViewport) => { return (coordRelativeToCaptureArea + captureAreaTopRelativeToViewport); }; exports.fromCaptureAreaToViewport = fromCaptureAreaToViewport; const fromViewportToCaptureArea = (coordRelativeToViewport, captureAreaTopRelativeToViewport) => { return (coordRelativeToViewport - captureAreaTopRelativeToViewport); }; exports.fromViewportToCaptureArea = fromViewportToCaptureArea; const getCoveringRect = (rects) => { if (rects.length === 0) { throw new Error("No rectangles to cover"); } let left = rects[0].left; let top = rects[0].top; let right = (0, exports.getRight)(rects[0]); let bottom = (0, exports.getBottom)(rects[0]); for (let i = 1; i < rects.length; i++) { const r = rects[i]; const rLeft = r.left; const rTop = r.top; left = (0, exports.getMinCoord)(left, rLeft); top = (0, exports.getMinCoord)(top, rTop); right = (0, exports.getMaxCoord)(right, (0, exports.getRight)(r)); bottom = (0, exports.getMaxCoord)(bottom, (0, exports.getBottom)(r)); } return { left, top, width: (0, exports.getWidth)(left, right), height: (0, exports.getHeight)(top, bottom) }; }; exports.getCoveringRect = getCoveringRect; const roundCoords = (value) => { const v = value; const result = {}; if ("top" in v) { const top = v.top; result.top = Math.floor(top); } if ("height" in v) { result.height = "top" in v ? Math.ceil(v.top + v.height) - result.top : Math.ceil(v.height); } if ("left" in v) { const left = v.left; result.left = Math.floor(left); } if ("width" in v) { result.width = "left" in v ? Math.ceil(v.left + v.width) - result.left : Math.ceil(v.width); } return result; }; exports.roundCoords = roundCoords; const floorCoords = (value) => { const v = value; const result = {}; for (const key in v) { result[key] = Math.floor(v[key]); } return result; }; exports.floorCoords = floorCoords; const ceilCoords = (value) => { const v = value; const result = {}; for (const key in v) { result[key] = Math.ceil(v[key]); } return result; }; exports.ceilCoords = ceilCoords; const fromCssToDevice = (value, pixelRatio) => { const v = value; const scaled = {}; for (const key in v) { scaled[key] = v[key] * pixelRatio; } return (pixelRatio % 1 === 0 ? scaled : (0, exports.roundCoords)(scaled)); }; exports.fromCssToDevice = fromCssToDevice; const fromCssToDeviceNumber = (value, pixelRatio) => { return (value * pixelRatio); }; exports.fromCssToDeviceNumber = fromCssToDeviceNumber; const fromDeviceToCssNumber = (value, pixelRatio) => { return (value / pixelRatio); }; exports.fromDeviceToCssNumber = fromDeviceToCssNumber; const fromBcrToRect = (bcr) => { return { left: bcr.left, top: bcr.top, width: bcr.width, height: bcr.height, }; }; exports.fromBcrToRect = fromBcrToRect; //# sourceMappingURL=geometry.js.map