Best JavaScript code snippet using wpt
search.ts
Source:search.ts
1import dict from '$lib/dict.json'2//import dict from '$lib/dictsm.json'3export function splitWord(word: string) {4 const alphas = word.split("")5 const out = []6 alphas.forEach((a) => {7 if (a.match(/[à¸-ฮ]/) || a.match(/[à¹à¹à¹à¹à¹à¸²à¸³à¸°à¹à¸¯à¸¤à¸²]/) || a.match(/[\.\*\/]/)) {8 out.push(a)9 } else {10 out[out.length - 1] += a11 }12 })13 return out14}15function isUpperOrLowerCharacter(char: string): boolean {16 return !char.match(/[à¸-ฮ]/) && !char.match(/[à¹à¹à¹à¹à¹à¸²à¸³à¸°à¹à¸¯à¸¤à¸²]/)17}18function removeSymbols(word: string) {19 return word.replace(/[\*\.\/\&\|\^\[\]]/g, "")20}21export function search(query: string) {22 // TODO: also check weird input (like several /, or / with multiple *)23 if(!query)24 return {valid: false, count:0, results: []}25 let andMode = query.includes('&')26 let queries = query.split(/[\&\|]/).map((q)=>q.trim())27 let excluded = []28 let excludedQuery = queries.filter((q)=>q.includes("^"))29 excludedQuery.forEach((eq)=> 30 excluded = excluded.concat(splitWord(removeSymbols(eq)))31 )32 queries = queries.filter((q)=>!q.includes("^"))33 // special case: if the user only inputs exclusion string, we search for all strings!34 if(excluded.length > 0 && queries.length == 0)35 queries = ['*']36 37 let results = []38 // check each word against all queries39 dict.forEach((w)=>{40 let matchedQuery = 041 queries.forEach((q)=>{42 var result = matchQuery(w,q,excluded)43 result = q.includes("!")? !result: result44 matchedQuery += result?1:045 })46 if(47 (andMode && matchedQuery === queries.length)48 || (!andMode && matchedQuery > 0)49 )50 results = [...results, w]51 })52 return {53 valid: true,54 count: results.length, 55 results: results56 }57}58function matchQuery(w: string, q: string, e:string[]):boolean{59 // return if the word has any excluded character60 if(e.some((ec)=>w.includes(ec))) return false61 // ignore negation (!) and only check after returned62 q = q.replace(/[\!]/g, "")63 const wordSplitted = splitWord(w)64 let querySplitted = splitWord(q)65 // length66 let minLength = 067 let maxLength = 10068 if(q.includes(":")) {69 let lengthStr = q.slice(0, q.indexOf(":"))70 q = q.slice(q.indexOf(":")+1)71 // a single number - exact match72 if(!lengthStr.includes(":"))73 lengthStr += ":" + lengthStr74 const minStr = lengthStr.slice(0, lengthStr.indexOf("-"))75 const maxStr = lengthStr.slice(lengthStr.indexOf("-")+1)76 if(minStr.length > 0)77 minLength = parseInt(minStr)78 if(maxStr.length > 0)79 maxLength = parseInt(maxStr)80 }81 82 let mode = {wild: false, anagram: false, filler: false}83 const numWilds = querySplitted.reduce((prev, letter) => prev + (letter === '.'? 1:0), 0)84 const numFillers = querySplitted.reduce((prev, letter) => prev + (letter === '*'? 1:0), 0)85 if(querySplitted[0] === '/') mode.anagram = true86 // Type 1: Anagram87 if(mode.anagram) {88 querySplitted = splitWord(removeSymbols(q))89 if(wordSplitted.length < minLength || wordSplitted.length > maxLength) return false90 // first, check if their lenghts match91 if(numFillers == 0 && wordSplitted.length != querySplitted.length + numWilds) return false92 if(numFillers > 0 && wordSplitted.length < querySplitted.length + numWilds) return false93 let numMatches = 094 for(const qIndex in querySplitted)95 for(const wIndex in wordSplitted)96 if(wordSplitted[wIndex] && wordSplitted[wIndex].startsWith(querySplitted[qIndex])) {97 wordSplitted[wIndex] = null98 numMatches ++99 break100 }101 102 // return if not all query letters match103 if(numMatches < querySplitted.length) return false104 }105 // Type 2: No anagram106 if(!mode.anagram) {107 if(wordSplitted.length < minLength || wordSplitted.length > maxLength) return false108 let qIndex = 0, wIndex = 0109 while(qIndex < querySplitted.length && wIndex < wordSplitted.length){110 if(querySplitted[qIndex] === "*"){111 qIndex ++112 // if * was the last character, it's done!113 if(qIndex == querySplitted.length)114 wIndex = wordSplitted.length115 // otherwise, find the next matching character116 if(qIndex != querySplitted.length && querySplitted[qIndex] !== "*"){117 let found = false118 while(!found && wIndex < wordSplitted.length) {119 if(wordSplitted[wIndex].startsWith(querySplitted[qIndex]))120 found = true121 // when found, let the next iteration matches the characters122 else123 wIndex ++124 }125 if(wIndex >= wordSplitted.length) return false126 }127 }128 // letter: must match129 // wild: increment130 else if(querySplitted[qIndex] === "." || wordSplitted[wIndex].startsWith(querySplitted[qIndex])) {131 qIndex ++132 wIndex ++133 }134 else135 return false136 }137 //special case: * is at the end of query string138 // w = ABC139 // q = ABC*140 // here, w finishes earlier, but it should be matched to q141 if(wIndex == wordSplitted.length && qIndex == querySplitted.length-1 && querySplitted[qIndex] === "*")142 return true143 if(qIndex < querySplitted.length || wIndex < wordSplitted.length) 144 return false145 }146 return true...
CodeReview.js
Source:CodeReview.js
1function isStringEmpty(str) {2 return (!str || /^\s*$/.test(str)) || (!str || 0 === str.length);3}4//Create Code Review5function openCodeReviewCreate() {6 $('#createCodeReview').modal('show');7}8function updateCodeReview(x) {9 var title = document.getElementById("CodeReviewTitle-" + x).innerText;10 document.getElementById("EditCodeReviewTitle").value = title;11 document.getElementById("EditCodeLocation").value = document.getElementById("CodeLocation-" + x).innerText;12 document.getElementById("EditCodeReviewNotes").innerText = document.getElementById("CodeReviewNotes-" + x).innerText;13 document.getElementById("fileNamesTwo").innerText = document.getElementById("CodeReviewFiles-" + x).innerText;14 var Id = document.getElementById("CodeReviewId-" + x).innerHTML;15 document.getElementById("EditCodeReviewId").innerText = Id;16 //document.getElementById("#EditCodeReviewTitle").innerText = ;17 $('#EditCodeReview').modal('show');18}19function alphabetical(a, b) {20 return a.localeCompare(b);21}22function cleanFileNames() {23 //get the list of filenames (one per row)24 var splitted = $('#fileNames').val().split("\n");25 //trim all the filenames down to at most 2 directories26 var len = splitted.length;27 for (i = 0; i < len; i++) {28 if (splitted[i].indexOf('@@') != -1) {29 splitted[i] = splitted[i].substring(0, splitted[i].indexOf('@@'));30 }31 splitted[i] = splitted[i].split('\\');32 if (splitted[i].length >= 3) {33 splitted[i] = splitted[i].slice(splitted[i].length - 3).join("\\");34 }35 else {36 splitted[i] = splitted[i].join("\\");37 }38 }39 //sort them40 splitted.sort(alphabetical);41 //get rid of doubles42 splitted = splitted.join('\n'); 43 var uniqueList = splitted.split('\n').filter(function (item, i, allItems) {44 return i == allItems.indexOf(item);45 }).join('\n');46 //console.log(uniqueList); 47 $("#fileNames").val(uniqueList.trim());48}49function cleanFileNames(textBox) {50 //get the list of filenames (one per row)51 var splitted = textBox.val().split("\n");52 //trim all the filenames down to at most 2 directories53 var len = splitted.length;54 for (i = 0; i < len; i++) {55 if (splitted[i].indexOf('@@') != -1) {56 splitted[i] = splitted[i].substring(0, splitted[i].indexOf('@@'));57 }58 splitted[i] = splitted[i].split('\\');59 if (splitted[i].length >= 3) {60 splitted[i] = splitted[i].slice(splitted[i].length - 3).join("\\");61 }62 else {63 splitted[i] = splitted[i].join("\\");64 }65 splitted[i] = splitted[i].trim();66 }67 //sort them68 splitted.sort(alphabetical);69 //get rid of doubles70 splitted = splitted.join('\n');71 var uniqueList = splitted.split('\n').filter(function (item, i, allItems) {72 return i == allItems.indexOf(item);73 }).join('\n');74 //console.log(uniqueList);75 textBox.val(uniqueList.trim());76}77function validateComment(textBox)78{79 var isValid = false;80 if (isStringEmpty(textBox.val()) == false)81 {82 isValid = true;83 }84 else85 {86 alert('The comment field cannot be blank.');87 }88 return isValid;...
timeConversion.js
Source:timeConversion.js
1function timeConversion(s) {2 let splitted = s.split('');3 let gethour = splitted[0] + splitted[1];4 let hour = Number(gethour);5 if (hour == 12 && splitted[8] == 'P') {6 return `${hour}:${splitted[3] + splitted[4]}:${splitted[6] + splitted[7]}`;7 }8 if (splitted[8] == 'P') {9 hour += 12;10 return `${hour}:${splitted[3] + splitted[4]}:${splitted[6] + splitted[7]}`;11 }12 if (hour == 12 && splitted[8] == 'A') {13 hour -= 12;14 return `0${hour}:${splitted[3] + splitted[4]}:${splitted[6] + splitted[7]}`;15 }16 if (splitted[8] == 'A') {17 return `${splitted[0] + splitted[1]}:${splitted[3] + splitted[4]}:${18 splitted[6] + splitted[7]19 }`;20 }21}22const result = timeConversion('12:05:40PM');23const result1 = timeConversion('11:05:40PM');24const result2 = timeConversion('12:05:40AM');25const result3 = timeConversion('09:05:40AM');26console.log(result);27console.log(result1);28console.log(result2);...
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3}, function(err, data) {4 if (err) return console.error(err);5 console.log(data);6 api.getTestResults(data.data.testId, function(err, data) {7 if (err) return console.error(err);8 console.log(data);9 });10});11{ data: { testId: '160813_1A_1C' },12 statusText: 'Ok' }13{ data: 14 { testId: '160813_1A_1C',15 statusText: 'Ok' }16 var request = require('request');17 request(url, function(error, response, body) {18 if (!error && response.statusCode == 200) {19 var data = JSON.parse(body);20 console.log(data);21 }22 });
Using AI Code Generation
1var wptools = require('wptools');2wptools.page('Barack Obama').then(function(page) {3 return page.splitted();4}).then(function(page) {5 console.log(page.data);6}).catch(function(err) {7 console.log(err);8});9### wptools.page('Barack Obama').then(function(page) { return page.splitted(); }).then(function(page) { console.log(page.data); }).catch(function(err) { console.log(err); });10var wptools = require('wptools');11wptools.page('Barack Obama').then(function(page) {12 return page.splitted();13}).then(function(page) {14 console.log(page.data);15}).catch(function(err) {16 console.log(err);17});18### wptools.page('Barack Obama').then(function(page) { return page.splitted(); }).then(function(page) { console.log(page.data); }).catch(function(err) { console.log(err); });19var wptools = require('wptools');20wptools.page('Barack Obama').then(function(page) {21 return page.splitted();22}).then(function(page) {23 console.log(page.data);24}).catch(function(err) {25 console.log(err);26});27### wptools.page('Barack Obama').then(function(page) { return page.splitted(); }).then(function(page) { console.log(page.data); }).catch(function(err) { console.log(err); });28var wptools = require('wptools');29wptools.page('Barack Obama').then(function(page) {30 return page.splitted();31}).then(function(page) {32 console.log(page.data);33}).catch(function(err) {34 console.log(err);35});36### wptools.page('Barack Obama').then(function(page) { return page.splitted(); }).then(function(page) { console.log(page.data); }).catch(function(err) { console.log(err); });37var wptools = require('wptools');
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3api.runTest(url, {location: 'Dulles:Chrome'}, function(err, data) {4 if (err) return console.error(err);5 api.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data.data.runs[1].firstView.TTFB);8 });9});10var wpt = require('webpagetest');11var api = new wpt('www.webpagetest.org');12api.runTest(url, {location: 'Dulles:Chrome'}, function(err, data) {13 if (err) return console.error(err);14 api.getTestResults(data.data.testId, function(err, data) {15 if (err) return console.error(err);16 console.log(data.data.runs[1].firstView.TTFB);17 });18});19api.getTestResults(data.data.testId, function(err, data) {20at Request._callback (/home/ashish/Downloads/wpt/wpt.js:4:24)21at self.callback (/home/ashish/Downloads/wpt/node_modules/request/request.js:186:22)22at Request.EventEmitter.emit (events.js:98:17)23at Request.<anonymous> (/home/ashish/Downloads/wpt/node_modules/request/request.js:1161:10)24at Request.EventEmitter.emit (events.js:117:20)25at IncomingMessage.<anonymous> (/home/ashish/Downloads/wpt/node_modules/request/request.js:1083:12)26at IncomingMessage.EventEmitter.emit (events.js:117:20)
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var file = fs.readFileSync('data.txt').toString().split("\n");4for(i in file) {5 console.log(file[i]);6 wptools.page(file[i]).get(function(err,page){7 page.summary(function(err,summary){8 console.log(summary);9 });10 });11}
Using AI Code Generation
1const wptools = require('wptools');2const wiki = wptools.page('Bill Gates');3wiki.get(function(err, resp) {4 if (err) {5 console.log(err);6 } else {7 console.log(resp);8 }9});10const wptools = require('wptools');11const wiki = wptools.page('Bill Gates');12wiki.get(function(err, resp) {13 if (err) {14 console.log(err);15 } else {16 console.log(resp);17 }18});19const wptools = require('wptools');20const wiki = wptools.page('Bill Gates');21wiki.get(function(err, resp) {22 if (err) {23 console.log(err);24 } else {25 console.log(resp);26 }27});28const wptools = require('wptools');29const wiki = wptools.page('Bill Gates');30wiki.get(function(err, resp) {31 if (err) {32 console.log(err);33 } else {34 console.log(resp);35 }36});37const wptools = require('wptools');38const wiki = wptools.page('Bill Gates');39wiki.get(function(err, resp) {40 if (err) {41 console.log(err);42 } else {43 console.log(resp);44 }45});46const wptools = require('wptools');47const wiki = wptools.page('Bill Gates');48wiki.get(function(err, resp) {49 if (err) {50 console.log(err);51 } else {52 console.log(resp);53 }54});55const wptools = require('wptools');56const wiki = wptools.page('Bill Gates');57wiki.get(function(err, resp) {58 if (err) {59 console.log(err);60 } else {61 console.log(resp);62 }63});64const wptools = require('
Using AI Code Generation
1var wpt = require('webpagetest-promise');2var wpt = new wpt('www.webpagetest.org');3var wpt = require('webpagetest-promise');4var wpt = new wpt('www.webpagetest.org');5var wpt = require('webpagetest-promise');6var wpt = new wpt('www.webpagetest.org');7var wpt = require('webpagetest-promise');8var wpt = new wpt('www.webpagetest.org');9var wpt = require('webpagetest-promise');10var wpt = new wpt('www.webpagetest.org');11var wpt = require('webpagetest-promise');12var wpt = new wpt('www.webpagetest.org');13var wpt = require('webpagetest-promise');14var wpt = new wpt('www.webpagetest.org');15var wpt = require('webpagetest-promise');16var wpt = new wpt('www.webpagetest.org');17var wpt = require('webpagetest-promise');18var wpt = new wpt('www.webpagetest.org');19var wpt = require('webpagetest-promise');20var wpt = new wpt('www.webpagetest.org');21var wpt = require('webpagetest-promise');22var wpt = new wpt('www.webpagetest.org');23var wpt = require('webpagetest-promise');24var wpt = new wpt('www.webpagetest.org');25var wpt = require('webpagetest-promise');
Using AI Code Generation
1var page = require('webpage').create();2var system = require('system');3var fs = require('fs');4page.viewportSize = { width: 1024, height: 768 };5page.clipRect = { top: 0, left: 0, width: 1024, height: 768 };6page.open(system.args[1], function() {7 page.render('screenshot1.png');8 page.evaluate(function() {9 window.callPhantom('split');10 });11});12page.onCallback = function(data) {13 if (data === 'split') {14 page.clipRect = { top: 0, left: 1024, width: 1024, height: 768 };15 page.render('screenshot2.png');16 phantom.exit();17 }18};19page.viewportSize = { width: 2048, height: 768 };20page.clipRect = { top: 0, left: 0, width: 2048, height: 768 };21page.render('screenshot.png');22var page = require('webpage').create();23page.viewportSize = { width: 2048, height: 768
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!!