Best JavaScript code snippet using wpt
structures.js
Source:structures.js
1//********** ParticleManager **********2var ParticleManager = function() {3 var self = this;4 5 this.chunkTime = 30000; //default chunk time size, in milliseconds6 this.currentChunkTime;7 8 this.ready = false;9 this.timeAsDate = false;10 this.startTime = Number.MAX_SAFE_INTEGER;11 this.maxTime = 0;12 this.minLat = Number.MAX_SAFE_INTEGER;13 this.minLon = Number.MAX_SAFE_INTEGER;14 this.maxLat = Number.MIN_SAFE_INTEGER;15 this.maxLon = Number.MIN_SAFE_INTEGER;16 this.maxChunkSize = 0;17 this.particles = [];18 this.particleMap = {};19 this.styles;20 21 //position-time data chunks, lists of Float32Array that are given into interleavedBuffer22 this.chunks = [];23 24 //"Public" functions25 ParticleManager.prototype.setStyles = function(styles) {26 self.styles = styles;27 }28 29 ParticleManager.prototype.addData = function(data) {30 var fields = data.split(",");31 var id = fields[0].trim();32 var time = fields[1].trim();33 var lat = fields[2].trim();34 var lon = fields[3].trim();35 var stopAttr = fields[4].trim();36 var styleId = fields[5].trim();37 38 if(typeof(self.particleMap[id]) == 'undefined') {39 self.particles.push({coordTimes: []});40 self.particleMap[id] = self.particles[self.particles.length - 1];41 42 var styleExists = false;43 if(typeof(self.styles) != 'undefined' && typeof(self.styles[styleId]) != 'undefined')44 styleExists = true;45 46 self.particleMap[id].col = {47 r: styleExists ? self.styles[styleId].r : 230,48 g: styleExists ? self.styles[styleId].g : 50,49 b: styleExists ? self.styles[styleId].b : 050 };51 self.particleMap[id].a = styleExists ? self.styles[styleId].a : 1.0;52 self.particleMap[id].s = styleExists ? self.styles[styleId].size : 1.0;53 self.particleMap[id].z = styleExists ? self.styles[styleId].z : 0.0;54 self.particleMap[id].m = styleExists ? self.styles[styleId].mode : 0;55 }56 57 var lonLat = transf(Number(lat), Number(lon));58 lon = lonLat[0];59 lat = lonLat[1];60 61 if(!$.isNumeric(time)) { //If time is given as a dateTime string convert it to unix timestamp. Otherwise leave it as a long62 self.timeAsDate = true;63 time = Date.parse(time);64 }65 66 //max time67 if(time > self.maxTime)68 self.maxTime = time;69 //start time70 if(time < self.startTime)71 self.startTime = time;72 73 //map bounds74 if(lat < self.minLat)75 self.minLat = lat;76 if(lat > self.maxLat)77 self.maxLat = lat;78 if(lon < self.minLon)79 self.minLon = lon;80 if(lon > self.maxLon)81 self.maxLon = lon;82 83 self.particleMap[id].coordTimes.push(84 {85 t: time,86 lat: lat,87 lon: lon,88 });89 }90 91 ParticleManager.prototype.createChunks = function() {92 self.maxTime -= self.startTime;93 firstPass(0);94 }95 96 ParticleManager.prototype.isChunkOutdated = function(time) {97 return (time >= self.currentChunkTime + self.chunkTime || time < self.currentChunkTime);98 }99 100 101 102 ParticleManager.prototype.getChunk = function(time) {103 self.currentChunkTime = Math.floor(time/self.chunkTime) * self.chunkTime;104 return this.chunks[Math.floor(time/self.chunkTime)];105 }106 107 108 //"Private functions"109 var firstPass = function(particleId) {110 var idLimit = particleId + 10000;111 112 console.log('ParticleManager firstPass: ', idLimit);113 114 if(idLimit > self.particles.length)115 idLimit = self.particles.length;116 117 for(var i = particleId; i < idLimit; i++) { //Check if particle coordinate-time array is sorted, sort if needed118 var particle = self.particles[i];119 120 var sorted = true;121 for(var j = 0; j < particle.coordTimes.length; j++){122 particle.coordTimes[j].t -= self.startTime;123 if(j > 0 && particle.coordTimes[j-1].t > particle.coordTimes[j].t)124 sorted = false;125 }126 if(!sorted)127 particle.coordTimes.sort( function(ct1, ct2) { return ct1.t - ct2.t } );128 }129 130 self.particles.sort( function(p1, p2) { return p1.z - p2.z } ); //Sort particles based on Z-value for correct rendering (lower Z-value particles in the beginning -> rendered first)131 132 setTimeout(function() {133 if(idLimit < self.particles.length)134 firstPass(idLimit);135 else {136 console.log('Total time chunks to be made: ', Math.ceil(self.maxTime/self.chunkTime));137 console.log('ChunkTime(s): ', self.chunkTime / 1000);138 createChunksInner(0);139 }140 }, 0)141 }142 143 var createChunksInner = function(chunkCounter) {144 if(chunkCounter*self.chunkTime > self.maxTime) {145 self.ready = true;146 return;147 }148 149 var chunkArray = [];150 for(var i = 0; i < self.particles.length; i++) {151 var particle = self.particles[i];152 153 var slot = searchTimeSlot(i, chunkCounter*self.chunkTime);154 if(typeof(slot) != 'undefined') {155 while(slot < particle.coordTimes.length-1 && particle.coordTimes[slot].t < (chunkCounter+1)*self.chunkTime) {156 chunkArray.push(particle.coordTimes[slot].lon);157 chunkArray.push(particle.coordTimes[slot].lat);158 chunkArray.push(particle.coordTimes[slot].t);159 160 chunkArray.push(particle.coordTimes[slot+1].lon);161 chunkArray.push(particle.coordTimes[slot+1].lat);162 chunkArray.push(particle.coordTimes[slot+1].t);163 164 chunkArray.push(packColor(particle.col));165 chunkArray.push(particle.a);166 167 chunkArray.push(particle.s);168 chunkArray.push(particle.z);169 chunkArray.push(particle.m);170 slot++;171 }172 }173 }174 175 var arrSize = chunkArray.length;176 if(arrSize > self.maxChunkSize)177 self.maxChunkSize = arrSize;178 179 var chunk = new Float32Array(arrSize);180 181 for(var i = 0; i < arrSize; i++) {182 chunk[i] = chunkArray[i];183 }184 185 self.chunks.push(chunk);186 187 setTimeout(function() {188 createChunksInner(++chunkCounter);189 }, 0)190 }191 192 var searchTimeSlot = function(id, time) {193 if(typeof(self.particles[id].coordTimes[0]) == 'undefined' || self.particles[id].coordTimes[0].t > time){194 return undefined;195 }196 197 //binary search198 var low = 0;199 var high = self.particles[id].coordTimes.length - 1;200 var middle = Math.floor((high + low)/2);201 while(low < high){202 if(time < self.particles[id].coordTimes[middle].t)203 high = middle;204 else205 low = middle+1;206 207 middle = Math.floor((high + low)/2);208 }209 210 if(self.particles[id].coordTimes[middle].t > time)211 middle--;212 213 return middle;214 }215 216 var packColor = function(color) {217 return color.r + color.g * 256.0 + color.b * 256.0 * 256.0;218 }219 ...
useFoucFix.js
Source:useFoucFix.js
1import { useEffect } from 'react';2// Temporary fix to avoid flash of unstyled content (FOUC) during route transitions.3// Keep an eye on this issue and remove this code when resolved: https://github.com/vercel/next.js/issues/174644export const useFoucFix = () => {5 useEffect(() => {6 // Gather all server-side rendered stylesheet entries.7 let ssrPageStyleSheetsEntries = Array.from(8 document.querySelectorAll('link[rel="stylesheet"][data-n-p]')9 ).map(element => ({10 element,11 href: element.getAttribute('href'),12 }));13 // Remove the `data-n-p` attribute to prevent Next.js from removing it early.14 ssrPageStyleSheetsEntries.forEach(({ element }) =>15 element.removeAttribute('data-n-p')16 );17 const fixedStyleHrefs = [];18 const mutationHandler = mutations => {19 // Gather all <style data-n-href="/..."> elements.20 const newStyleEntries = mutations21 .filter(22 ({ target }) =>23 target.nodeName === 'STYLE' && target.hasAttribute('data-n-href')24 )25 .map(({ target }) => ({26 element: target,27 href: target.getAttribute('data-n-href'),28 }));29 // Cycle through them and either:30 // - Remove the `data-n-href` attribute to prevent Next.js from removing it early.31 // - Remove the element if it's already present.32 newStyleEntries.forEach(({ element, href }) => {33 const styleExists = fixedStyleHrefs.includes(href);34 if (styleExists) {35 element.remove();36 } else {37 element.setAttribute('data-fouc-fix-n-href', href);38 element.removeAttribute('data-n-href');39 fixedStyleHrefs.push(href);40 }41 });42 // Cycle through the server-side rendered stylesheets and remove the ones that43 // are already present as inline <style> tags added by Next.js, so that we don't have duplicate styles.44 ssrPageStyleSheetsEntries = ssrPageStyleSheetsEntries.reduce((entries, entry) => {45 const { element, href } = entry;46 const styleExists = fixedStyleHrefs.includes(href);47 if (styleExists) {48 element.remove();49 } else {50 entries.push(entry);51 }52 return entries;53 }, []);54 };55 const observer = new MutationObserver(mutationHandler);56 observer.observe(document.head, {57 subtree: true,58 attributeFilter: ['media'],59 });60 return () => observer.disconnect();61 }, []);...
index.js
Source:index.js
...5 class() {6 if (arguments.length < 1) {7 return null;8 } else if (arguments.length === 1) {9 return this.styleExists(arguments[0]) ? this.styles[arguments[0]] : null;10 } else if (arguments.length > 1) {11 let classArray = [];12 for (let argument of arguments) {13 if (this.styleExists(argument)) {14 classArray.push(this.styles[argument]);15 }16 }17 return this.classArrayJoin(classArray);18 }19 }20 compose() {21 let classArray = [];22 for (let argument of arguments) {23 if (argument) classArray.push(argument);24 }25 return this.classArrayJoin(classArray);26 }27 styleExists(style) {28 return typeof this.styles[style] === 'undefined' ? false : true;29 }30 classArrayJoin(classArray) {31 return classArray.length > 0 ? classArray.join(' ').trim() : '';32 }...
Using AI Code Generation
1var wptStyles = require('./wpt-styles.js');2wptStyles.styleExists('test.css', function (exists) {3 if (exists) {4 console.log('exists');5 } else {6 console.log('does not exist');7 }8});
Using AI Code Generation
1var wptools = require('wptools');2wptools.styleExists('test', function(err, exists) {3 console.log(exists);4});5var wptools = require('wptools');6wptools.styleInfo('test', function(err, info) {7 console.log(info);8});9var wptools = require('wptools');10wptools.styleList(function(err, list) {11 console.log(list);12});13var wptools = require('wptools');14wptools.styleList(function(err, list) {15 console.log(list);16});17var wptools = require('wptools');18wptools.styleModuleInfo('test', function(err, info) {19 console.log(info);20});21var wptools = require('wptools');22wptools.styleModuleList(function(err, list) {23 console.log(list);24});25var wptools = require('wptools');26wptools.styleModuleSource('test', function(err, source) {27 console.log(source);28});29var wptools = require('wptools');30wptools.styleModuleTransform('test', function(err, transform) {31 console.log(transform);32});33var wptools = require('wptools');34wptools.styleModuleVersions('test', function(err, versions) {
Using AI Code Generation
1var wptoolbox = require('wptoolbox');2wptoolbox.styleExists('wptoolbox', function (exists) {3 if (exists) {4 console.log('Style exists');5 } else {6 console.log('Style does not exist');7 }8});9MIT © [WPTOOLBOX](
Using AI Code Generation
1var style = wpt.style;2var styleExists = style.styleExists;3if (styleExists('test')) {4 console.log('test style exists');5}6else {7 console.log('test style does not exist');8}9if (styleExists('test2')) {10 console.log('test2 style exists');11}12else {13 console.log('test2 style does not exist');14}15if (styleExists('test3')) {16 console.log('test3 style exists');17}18else {19 console.log('test3 style does not exist');20}21if (styleExists('test4')) {22 console.log('test4 style exists');23}24else {25 console.log('test4 style does not exist');26}27if (styleExists('test5')) {28 console.log('test5 style exists');29}30else {31 console.log('test5 style does not exist');32}33if (styleExists('test6')) {34 console.log('test6 style exists');35}36else {37 console.log('test6 style does not exist');38}39if (styleExists('test7')) {40 console.log('test7 style exists');41}42else {43 console.log('test7 style does not exist');44}45if (styleExists('test8')) {46 console.log('test8 style exists');47}48else {49 console.log('test8 style does not exist');50}51if (styleExists('test9')) {52 console.log('test9 style exists');53}54else {55 console.log('test9 style does not exist');56}57if (styleExists('test10')) {58 console.log('test10 style exists');59}60else {61 console.log('test10 style does not exist');62}63if (styleExists('test11')) {64 console.log('test11 style exists');65}66else {67 console.log('test11 style does not exist');68}69if (styleExists('test12')) {70 console.log('test12 style exists');71}72else {73 console.log('test12 style does not exist');74}
Using AI Code Generation
1var wpt = require('./lib/webpagetest.js');2var wptClient = new wpt('www.webpagetest.org', 'A.1d8c7f9d9c6e0e2c2e8f1b0e1b6d0b6f');3wptClient.runTest(url, {4}, function(err, data) {5 if (err) {6 console.log('Error: ' + err);7 } else {8 console.log('Test status: ' + data.statusCode);9 console.log('Test status text: ' + data.statusText);10 console.log('Test ID: ' + data.data.testId);11 console.log('Test URL: ' + data.data.summary);12 }13});
Using AI Code Generation
1var style = new wptbStyle();2style.styleExists('wptb-table-main-1', 'wptb-table-main-1');3### styleExists(styleName, styleId)4var style = new wptbStyle();5style.styleExists('wptb-table-main-1', 'wptb-table-main-1');6### styleExists(styleName, styleId)7var style = new wptbStyle();8style.styleExists('wptb-table-main-1', 'wptb-table-main-1');9### styleExists(styleName, styleId)10var style = new wptbStyle();11style.styleExists('wptb-table-main-1', 'wptb-table-main-1');12### styleExists(styleName, styleId)
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!!