Best JavaScript code snippet using wpt
Rect.ts
Source:Rect.ts
1import { Point } from "./Point"2import { Size } from "./Size"3import { roundedNumber } from "../utils/roundedNumber"4/**5 * @public6 */7export interface Rect extends Point, Size {}8/**9 * @public10 */11export namespace Rect {12 /**13 *14 * @param rect15 * @param other16 * @public17 */18 export function equals(rect: Rect | null, other: Rect | null): boolean {19 if (rect === other) return true20 if (!rect || !other) return false21 return rect.x === other.x && rect.y === other.y && rect.width === other.width && rect.height === other.height22 }23 /** @alpha */24 export const atOrigin = (size: Size) => {25 return { ...size, x: 0, y: 0 }26 }27 /** @alpha */28 export const fromTwoPoints = (a: Point, b: Point): Rect => {29 return {30 x: Math.min(a.x, b.x),31 y: Math.min(a.y, b.y),32 width: Math.abs(a.x - b.x),33 height: Math.abs(a.y - b.y),34 }35 }36 /** @alpha */37 export const fromRect = (rect: ClientRect): Rect => {38 return {39 x: rect.left,40 y: rect.top,41 width: rect.right - rect.left,42 height: rect.bottom - rect.top,43 }44 }45 /** @alpha */46 export const multiply = (rect: Rect, n: number): Rect => {47 return {48 x: rect.x * n,49 y: rect.y * n,50 width: rect.width * n,51 height: rect.height * n,52 }53 }54 /** @alpha */55 export const divide = (rect: Rect, n: number): Rect => {56 return multiply(rect, 1 / n)57 }58 /** @alpha */59 export const offset = (rect: Rect, delta: Partial<Point>): Rect => {60 const xOffset = typeof delta.x === "number" ? delta.x : 061 const yOffset = typeof delta.y === "number" ? delta.y : 062 return {63 ...rect,64 x: rect.x + xOffset,65 y: rect.y + yOffset,66 }67 }68 /** @alpha */69 export function inflate(rect: Rect, value: number) {70 if (value === 0) return rect71 const doubleValue = 2 * value72 return {73 x: rect.x - value,74 y: rect.y - value,75 width: rect.width + doubleValue,76 height: rect.height + doubleValue,77 }78 }79 /** @alpha */80 export const pixelAligned = (rect: Rect): Rect => {81 const x = Math.round(rect.x)82 const y = Math.round(rect.y)83 const rectMaxX = Math.round(rect.x + rect.width)84 const rectMaxY = Math.round(rect.y + rect.height)85 const width = Math.max(rectMaxX - x, 0)86 const height = Math.max(rectMaxY - y, 0)87 return { x, y, width, height }88 }89 /** @alpha */90 export const halfPixelAligned = (rect: Rect): Rect => {91 const x = Math.round(rect.x * 2) / 292 const y = Math.round(rect.y * 2) / 293 const rectMaxX = Math.round((rect.x + rect.width) * 2) / 294 const rectMaxY = Math.round((rect.y + rect.height) * 2) / 295 const width = Math.max(rectMaxX - x, 1)96 const height = Math.max(rectMaxY - y, 1)97 return { x, y, width, height }98 }99 /** @alpha */100 export const round = (rect: Rect, decimals = 0): Rect => {101 const x = roundedNumber(rect.x, decimals)102 const y = roundedNumber(rect.y, decimals)103 const width = roundedNumber(rect.width, decimals)104 const height = roundedNumber(rect.height, decimals)105 return { x, y, width, height }106 }107 /** @alpha */108 export const roundToOutside = (rect: Rect): Rect => {109 const x = Math.floor(rect.x)110 const y = Math.floor(rect.y)111 const rectMaxX = Math.ceil(rect.x + rect.width)112 const rectMaxY = Math.ceil(rect.y + rect.height)113 const width = Math.max(rectMaxX - x, 0)114 const height = Math.max(rectMaxY - y, 0)115 return { x, y, width, height }116 }117 /**118 * @param rect119 * @beta120 */121 export const minX = (rect: Rect) => {122 return rect.x123 }124 /**125 * @param rect126 * @beta127 */128 export const maxX = (rect: Rect) => {129 return rect.x + rect.width130 }131 /**132 * @param rect133 * @beta134 */135 export const minY = (rect: Rect) => {136 return rect.y137 }138 /**139 * @param rect140 * @beta141 */142 export const maxY = (rect: Rect) => {143 return rect.y + rect.height144 }145 /** @internal */146 export const positions = (rect: Rect) => {147 return {148 minX: rect.x,149 midX: rect.x + rect.width / 2,150 maxX: maxX(rect),151 minY: rect.y,152 midY: rect.y + rect.height / 2,153 maxY: maxY(rect),154 }155 }156 /**157 *158 * @param rect159 * @beta160 */161 export const center = (rect: Rect) => {162 return {163 x: rect.x + rect.width / 2,164 y: rect.y + rect.height / 2,165 }166 }167 /** @internal */168 export const fromPoints = (ps: Point[]) => {169 const xValues = ps.map(point => point.x)170 const yValues = ps.map(point => point.y)171 const x = Math.min(...xValues)172 const y = Math.min(...yValues)173 const width = Math.max(...xValues) - x174 const height = Math.max(...yValues) - y175 return { x, y, width, height }176 }177 /**178 * Returns a rect containing all input rects179 * @param rect a list of rectangels180 * @returns A rectangle that fits exactly around the input rects181 * @internal182 */183 export const merge = (...rect: Rect[]): Rect => {184 const min = {185 x: Math.min(...rect.map(minX)),186 y: Math.min(...rect.map(minY)),187 }188 const max = {189 x: Math.max(...rect.map(maxX)),190 y: Math.max(...rect.map(maxY)),191 }192 return fromTwoPoints(min, max)193 }194 /** @alpha */195 export const intersection = (rect1: Rect, rect2: Rect): Rect => {196 const x = Math.max(rect1.x, rect2.x)197 const x2 = Math.min(rect1.x + rect1.width, rect2.x + rect2.width)198 const y = Math.max(rect1.y, rect2.y)199 const y2 = Math.min(rect1.y + rect1.height, rect2.y + rect2.height)200 return { x, y, width: x2 - x, height: y2 - y }201 }202 /**203 * Returns all the corner points for a rect204 * @param rect205 * @internal206 */207 export const points = (rect: Rect): Point[] => {208 return [209 { x: minX(rect), y: minY(rect) },210 { x: minX(rect), y: maxY(rect) },211 { x: maxX(rect), y: minY(rect) },212 { x: maxX(rect), y: maxY(rect) },213 ]214 }215 /**216 * Checks if a rectangle contains a point217 * @param rect The rectangle to check218 * @param point The point to check219 * @returns true if the provided rectangle contains the provided point220 * @beta221 */222 export const containsPoint = (rect: Rect, point: Point) => {223 if (point.x < minX(rect)) {224 return false225 }226 if (point.x > maxX(rect)) {227 return false228 }229 if (point.y < minY(rect)) {230 return false231 }232 if (point.y > maxY(rect)) {233 return false234 }235 if (isNaN(rect.x)) {236 return false237 }238 if (isNaN(rect.y)) {239 return false240 }241 return true242 }243 /**244 * Returns wether a rect contains another rect entirely245 * @param rectA246 * @param rectB247 * @returns true if rectA contains rectB248 */249 export const containsRect = (rectA: Rect, rectB: Rect) => {250 for (const point of points(rectB)) {251 if (!containsPoint(rectA, point)) {252 return false253 }254 }255 return true256 }257 /** @alpha */258 export const toCSS = (rect: Rect) => {259 return {260 display: "block",261 transform: `translate(${rect.x}px, ${rect.y}px)`,262 width: `${rect.width}px`,263 height: `${rect.height}px`,264 }265 }266 /** @alpha */267 export const inset = (rect: Rect, n: number) => {268 return {269 x: rect.x + n,270 y: rect.y + n,271 width: Math.max(0, rect.width - 2 * n),272 height: Math.max(0, rect.height - 2 * n),273 }274 }275 /** @alpha */276 export const intersects = (rectA: Rect, rectB: Rect): boolean => {277 return !(rectB.x >= maxX(rectA) || maxX(rectB) <= rectA.x || rectB.y >= maxY(rectA) || maxY(rectB) <= rectA.y)278 }279 /** @internal */280 export const overlapHorizontally = (rectA: Rect, rectB: Rect): boolean => {281 const aMax = Rect.maxX(rectA)282 const bMax = Rect.maxX(rectB)283 return aMax > rectB.x && bMax > rectA.x284 }285 /** @internal */286 export const overlapVertically = (rectA: Rect, rectB: Rect): boolean => {287 const aMax = Rect.maxY(rectA)288 const bMax = Rect.maxY(rectB)289 return aMax > rectB.y && bMax > rectA.y290 }291 /** @internal */292 export const doesNotIntersect = (rect: Rect, rects: Rect[]): boolean => {293 return (294 rects.find(comparingRect => {295 return Rect.intersects(comparingRect, rect)296 }) === undefined297 )298 }299 /**300 *301 * @param rectA302 * @param rectB303 * @return if the input rectangles are equal in size and position304 * @public305 */306 export const isEqual = (rectA: Rect | null, rectB: Rect | null) => {307 if (rectA && rectB) {308 const { x, y, width, height } = rectA309 return rectB.x === x && rectB.y === y && rectB.width === width && rectB.height === height310 } else {311 return rectA === rectB312 }313 }314 /** @internal */315 export const cornerPoints = (rect: Rect): Point[] => {316 const rectMinX = rect.x317 const rectMaxX = rect.x + rect.width318 const rectMinY = rect.y319 const rectMaxY = rect.y + rect.height320 const corner1 = { x: rectMinX, y: rectMinY }321 const corner2 = { x: rectMaxX, y: rectMinY }322 const corner3 = { x: rectMaxX, y: rectMaxY }323 const corner4 = { x: rectMinX, y: rectMaxY }324 return [corner1, corner2, corner3, corner4]325 }326 /** @internal */327 export const midPoints = (rect: Rect): Point[] => {328 const rectMinX = rect.x329 const rectMidX = rect.x + rect.width / 2330 const rectMaxX = rect.x + rect.width331 const rectMinY = rect.y332 const rectMidY = rect.y + rect.height / 2333 const rectMaxY = rect.y + rect.height334 const corner1 = { x: rectMidX, y: rectMinY }335 const corner2 = { x: rectMaxX, y: rectMidY }336 const corner3 = { x: rectMidX, y: rectMaxY }337 const corner4 = { x: rectMinX, y: rectMidY }338 return [corner1, corner2, corner3, corner4]339 }340 /** @internal */341 export const pointDistance = (rect: Rect, point: Point) => {342 let x = 0343 let y = 0344 if (point.x < rect.x) {345 x = rect.x - point.x346 } else if (point.x > Rect.maxX(rect)) {347 x = point.x - Rect.maxX(rect)348 }349 if (point.y < rect.y) {350 y = rect.y - point.y351 } else if (point.y > Rect.maxY(rect)) {352 y = point.y - Rect.maxY(rect)353 }354 return Point.distance({ x, y }, { x: 0, y: 0 })355 }356 const fromAnyDefaults = {357 x: 0,358 y: 0,359 width: 0,360 height: 0,361 }362 /** @internal */363 export const fromAny = (rect: any, defaults = fromAnyDefaults): Rect => {364 return {365 x: rect.x || defaults.x,366 y: rect.y || defaults.y,367 width: rect.width || defaults.width,368 height: rect.height || defaults.height,369 }370 }...
vertical-scroll.js
Source:vertical-scroll.js
1function rectMaxY(rect) {2 return rect.height + rect.y;3}4function rectMaxX(rect) {5 return rect.width + rect.x;6}7function isEmptyRect(rect) {8 return !rect.width || !rect.height;9}10// Returns true if the given rectangles intersect.11function rects_intersect(rect1, rect2) {12 if (isEmptyRect(rect1) || isEmptyRect(rect2))13 return false;14 return rect1.x < rectMaxX(rect2) &&15 rect2.x < rectMaxX(rect1) &&16 rect1.y < rectMaxY(rect2) &&17 rect2.y < rectMaxY(rect1);18}19function rectToString(rect) {20 return `Location: (${rect.x}, ${rect.y}) Size: (${rect.width}, ${rect.height})`;...
Using AI Code Generation
1var wptools = require('wptools');2var rectMaxY = wptools.rectMaxY;3var rect = {x: 0, y: 0, width: 100, height: 100};4var wptools = require('wptools');5var rectMaxX = wptools.rectMaxX;6var rect = {x: 0, y: 0, width: 100, height: 100};7var wptools = require('wptools');8var rectMinY = wptools.rectMinY;9var rect = {x: 0, y: 0, width: 100, height: 100};10var wptools = require('wptools');11var rectMinX = wptools.rectMinX;12var rect = {x: 0, y: 0, width: 100, height: 100};13var wptools = require('wptools');14var rectMidY = wptools.rectMidY;15var rect = {x: 0, y: 0, width: 100, height: 100};16var wptools = require('wptools');17var rectMidX = wptools.rectMidX;18var rect = {x: 0, y: 0, width: 100, height: 100};19var wptools = require('wptools');20var rectCenter = wptools.rectCenter;21var rect = {x: 0, y: 0, width: 100
Using AI Code Generation
1var rectMaxY = require('wpt.js').rectMaxY;2var rect = {top: 10, bottom: 20, left: 0, right: 0};3var rectMaxY = require('wpt.js').rectMaxY;4var rect = {top: 10, bottom: 20, left: 0, right: 0};5var rectMaxY = require('wpt.js').rectMaxY;6var rect = {top: 10, bottom: 20, left: 0, right: 0};7var rectMaxY = require('wpt.js').rectMaxY;8var rect = {top: 10, bottom: 20, left: 0, right: 0};9var rectMaxY = require('wpt.js').rectMaxY;10var rect = {top: 10, bottom: 20, left: 0, right: 0};11var rectMaxY = require('wpt.js').rectMaxY;12var rect = {top: 10, bottom: 20, left: 0, right: 0};13var rectMaxY = require('wpt.js').rectMaxY;14var rect = {top: 10, bottom: 20, left: 0, right: 0};15var rectMaxY = require('wpt.js').rectMaxY;16var rect = {top: 10, bottom: 20, left: 0, right:
Using AI Code Generation
1var wpt = require('wpt.js');2var rectMaxY = wpt.rectMaxY;3var rect = {top: 0, bottom: 100, left: 0, right: 100};4var maxY = rectMaxY(rect);5console.log("rectMaxY = " + maxY);6exports.rectMaxY = function(rect) {7 return rect.bottom;8};
Using AI Code Generation
1var rectMaxY = require('./wpt.js').rectMaxY;2console.log(rectMaxY({x: 10, y: 10, width: 100, height: 100}));3var rectMaxY = require('./wpt.js').rectMaxY;4console.log(rectMaxY({x: 10, y: 10, width: 100, height: 100}));5module.exports.rectMaxY = function(rect) {6 return rect.y + rect.height;7};8var wpt = require('./wpt.js');9console.log(wpt.rectMaxY({x: 10, y: 10, width: 100, height: 100}));10module.exports.rectMaxY = function(rect) {11 return rect.y + rect.height;12};13var wpt = require('./wpt.js');14console.log(wpt.rectMaxY({x: 10, y: 10, width: 100, height: 100}));15module.exports.rectMaxY = function(rect) {16 return rect.y + rect.height;17};18var wpt = require('./wpt.js');19console.log(wpt.rectMaxY({x: 10, y: 10, width: 100, height: 100}));20module.exports.rectMaxY = function(rect) {21 return rect.y + rect.height;22};23var wpt = require('./wpt.js');24console.log(wpt.rectMaxY({x: 10, y: 10, width: 100, height: 100}));25module.exports.rectMaxY = function(rect) {
Using AI Code Generation
1var rect = new wptRect(10, 10, 100, 100);2var maxY = rect.rectMaxY();3this.rectMaxY = function () {4 return this.rect.rectMaxY();5};6this.rectMaxY = function () {7 return this.rect.rectMaxY();8};9this.rectMaxY = function () {10 return this.rect.rectMaxY();11};12this.rectMaxY = function () {13 return this.rect.rectMaxY();14};15this.rectMaxY = function () {16 return this.rect.rectMaxY();17};18this.rectMaxY = function () {19 return this.rect.rectMaxY();20};21this.rectMaxY = function () {22 return this.rect.rectMaxY();23};
Using AI Code Generation
1var rectMaxY = wpt.rectMaxY;2var rect = new rectMaxY(0,0,0,0);3var rect = new rectMaxY(0,0,0,0);4var rect = new rectMaxY(0,0,0,0);5var rect = new rectMaxY(0,0,0,0);6var rect = new rectMaxY(0,0,0,0);7var rect = new rectMaxY(0,0,0,0);8var rect = new rectMaxY(0,0,0,0);9var rect = new rectMaxY(0,0,0,0);10var rect = new rectMaxY(0,0,0,0);11var rect = new rectMaxY(0,0,0,0);
Using AI Code Generation
1var rectMaxY = wpt.rectMaxY;2var rect = document.getElementById("rect1");3var rectMaxY = rectMaxY(rect);4console.log("rectMaxY = " + rectMaxY);5var rectMinX = wpt.rectMinX;6var rect = document.getElementById("rect1");7var rectMinX = rectMinX(rect);8console.log("rectMinX = " + rectMinX);9var rectMinY = wpt.rectMinY;10var rect = document.getElementById("rect1");11var rectMinY = rectMinY(rect);12console.log("rectMinY = " + rectMinY);13var rectWidth = wpt.rectWidth;14var rect = document.getElementById("rect1");15var rectWidth = rectWidth(rect);16console.log("rectWidth = " + rectWidth);17var rectHeight = wpt.rectHeight;18var rect = document.getElementById("rect1");19var rectHeight = rectHeight(rect);20console.log("rectHeight = " + rectHeight);21var rectCenterX = wpt.rectCenterX;22var rect = document.getElementById("rect1");23var rectCenterX = rectCenterX(rect);24console.log("rectCenterX = " + rectCenterX);25var rectCenterY = wpt.rectCenterY;26var rect = document.getElementById("rect1");27var rectCenterY = rectCenterY(rect);28console.log("rectCenterY = " + rectCenterY);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!