Best JavaScript code snippet using wpt
parser.js
Source:parser.js
...50 pushToken(result);51 return result;52 }53 54 function consumeKind(kind)55 {56 let token = nextToken();57 if (token.kind != kind) {58 throw new Error("At " + token.sourceLineNumber + ": expected " + kind + " but got: " + token.string);59 }60 return token;61 }62 63 function consumeToken(string)64 {65 let token = nextToken();66 if (token.string.toLowerCase() != string.toLowerCase())67 throw new Error("At " + token.sourceLineNumber + ": expected " + string + " but got: " + token.string);68 return token;69 }70 71 function parseVariable()72 {73 let name = consumeKind("identifier").string;74 let result = {evaluate: Basic.Variable, name, parameters: []};75 if (peekToken().string == "(") {76 do {77 nextToken();78 result.parameters.push(parseNumericExpression());79 } while (peekToken().string == ",");80 consumeToken(")");81 }82 return result;83 }84 85 function parseNumericExpression()86 {87 function parsePrimary()88 {89 let token = nextToken();90 switch (token.kind) {91 case "identifier": {92 let result = {evaluate: Basic.NumberApply, name: token.string, parameters: []};93 if (peekToken().string == "(") {94 do {95 nextToken();96 result.parameters.push(parseNumericExpression());97 } while (peekToken().string == ",");98 consumeToken(")");99 }100 return result;101 }102 103 case "number":104 return {evaluate: Basic.Const, value: token.value};105 106 case "operator":107 switch (token.string) {108 case "(": {109 let result = parseNumericExpression();110 consumeToken(")");111 return result;112 } }113 break;114 }115 throw new Error("At " + token.sourceLineNumber + ": expected identifier, number, or (, but got: " + token.string);116 }117 118 function parseFactor()119 {120 let primary = parsePrimary();121 122 let ok = true;123 while (ok) {124 switch (peekToken().string) {125 case "^":126 nextToken();127 primary = {evaluate: Basic.NumberPow, left: primary, right: parsePrimary()};128 break;129 default:130 ok = false;131 break;132 }133 }134 135 return primary;136 }137 138 function parseTerm()139 {140 let factor = parseFactor();141 142 let ok = true;143 while (ok) {144 switch (peekToken().string) {145 case "*":146 nextToken();147 factor = {evaluate: Basic.NumberMul, left: factor, right: parseFactor()};148 break;149 case "/":150 nextToken();151 factor = {evaluate: Basic.NumberDiv, left: factor, right: parseFactor()};152 break;153 default:154 ok = false;155 break;156 }157 }158 159 return factor;160 }161 162 // Only the leading term in Basic can have a sign.163 let negate = false;164 switch (peekToken().string) {165 case "+":166 nextToken();167 break;168 case "-":169 negate = true;170 nextToken()171 break;172 }173 174 let term = parseTerm();175 if (negate)176 term = {evaluate: Basic.NumberNeg, term: term};177 178 let ok = true;179 while (ok) {180 switch (peekToken().string) {181 case "+":182 nextToken();183 term = {evaluate: Basic.NumberAdd, left: term, right: parseTerm()};184 break;185 case "-":186 nextToken();187 term = {evaluate: Basic.NumberSub, left: term, right: parseTerm()};188 break;189 default:190 ok = false;191 break;192 }193 }194 195 return term;196 }197 198 function parseConstant()199 {200 switch (peekToken().string) {201 case "+":202 nextToken();203 return consumeKind("number").value;204 case "-":205 nextToken();206 return -consumeKind("number").value;207 default:208 if (isStringExpression())209 return consumeKind("string").value;210 return consumeKind("number").value;211 }212 }213 214 function parseStringExpression()215 {216 let token = nextToken();217 switch (token.kind) {218 case "string":219 return {evaluate: Basic.Const, value: token.value};220 case "identifier":221 consumeToken("$");222 return {evaluate: Basic.StringVar, name: token.string};223 default:224 throw new Error("At " + token.sourceLineNumber + ": expected string expression but got " + token.string);225 }226 }227 228 function isStringExpression()229 {230 // A string expression must start with a string variable or a string constant.231 let token = nextToken();232 if (token.kind == "string") {233 pushToken(token);234 return true;235 }236 if (token.kind == "identifier") {237 let result = peekToken().string == "$";238 pushToken(token);239 return result;240 }241 pushToken(token);242 return false;243 }244 245 function parseRelationalExpression()246 {247 if (isStringExpression()) {248 let left = parseStringExpression();249 let operator = nextToken();250 let evaluate;251 switch (operator.string) {252 case "=":253 evaluate = Basic.Equals;254 break;255 case "<>":256 evaluate = Basic.NotEquals;257 break;258 default:259 throw new Error("At " + operator.sourceLineNumber + ": expected a string comparison operator but got: " + operator.string);260 }261 return {evaluate, left, right: parseStringExpression()};262 }263 264 let left = parseNumericExpression();265 let operator = nextToken();266 let evaluate;267 switch (operator.string) {268 case "=":269 evaluate = Basic.Equals;270 break;271 case "<>":272 evaluate = Basic.NotEquals;273 break;274 case "<":275 evaluate = Basic.LessThan;276 break;277 case ">":278 evaluate = Basic.GreaterThan;279 break;280 case "<=":281 evaluate = Basic.LessEqual;282 break;283 case ">=":284 evaluate = Basic.GreaterEqual;285 break;286 default:287 throw new Error("At " + operator.sourceLineNumber + ": expected a numeric comparison operator but got: " + operator.string);288 }289 return {evaluate, left, right: parseNumericExpression()};290 }291 292 function parseNonNegativeInteger()293 {294 let token = nextToken();295 if (!/^[0-9]+$/.test(token.string))296 throw new Error("At ", token.sourceLineNumber + ": expected a line number but got: " + token.string);297 return token.value;298 }299 300 function parseGoToStatement()301 {302 statement.kind = Basic.GoTo;303 statement.target = parseNonNegativeInteger();304 }305 306 function parseGoSubStatement()307 {308 statement.kind = Basic.GoSub;309 statement.target = parseNonNegativeInteger();310 }311 312 function parseStatement()313 {314 let statement = {};315 statement.lineNumber = consumeKind("userLineNumber").userLineNumber;316 program.statements.set(statement.lineNumber, statement);317 318 let command = nextToken();319 statement.sourceLineNumber = command.sourceLineNumber;320 switch (command.kind) {321 case "keyword":322 switch (command.string.toLowerCase()) {323 case "def":324 statement.process = Basic.Def;325 statement.name = consumeKind("identifier");326 statement.parameters = [];327 if (peekToken().string == "(") {328 do {329 nextToken();330 statement.parameters.push(consumeKind("identifier"));331 } while (peekToken().string == ",");332 }333 statement.expression = parseNumericExpression();334 break;335 case "let":336 statement.process = Basic.Let;337 statement.variable = parseVariable();338 consumeToken("=");339 if (statement.process == Basic.Let)340 statement.expression = parseNumericExpression();341 else342 statement.expression = parseStringExpression();343 break;344 case "go": {345 let next = nextToken();346 if (next.string == "to")347 parseGoToStatement();348 else if (next.string == "sub")349 parseGoSubStatement();350 else351 throw new Error("At " + next.sourceLineNumber + ": expected to or sub but got: " + next.string);352 break;353 }354 case "goto":355 parseGoToStatement();356 break;357 case "gosub":358 parseGoSubStatement();359 break;360 case "if":361 statement.process = Basic.If;362 statement.condition = parseRelationalExpression();363 consumeToken("then");364 statement.target = parseNonNegativeInteger();365 break;366 case "return":367 statement.process = Basic.Return;368 break;369 case "stop":370 statement.process = Basic.Stop;371 break;372 case "on":373 statement.process = Basic.On;374 statement.expression = parseNumericExpression();375 if (peekToken().string == "go") {376 consumeToken("go");377 consumeToken("to");378 } else379 consumeToken("goto");380 statement.targets = [];381 for (;;) {382 statement.targets.push(parseNonNegativeInteger());383 if (peekToken().string != ",")384 break;385 nextToken();386 }387 break;388 case "for":389 statement.process = Basic.For;390 statement.variable = consumeKind("identifier").string;391 consumeToken("=");392 statement.initial = parseNumericExpression();393 consumeToken("to");394 statement.limit = parseNumericExpression();395 if (peekToken().string == "step") {396 nextToken();397 statement.step = parseNumericExpression();398 } else399 statement.step = {evaluate: Basic.Const, value: 1};400 consumeKind("newLine");401 let lastStatement = parseStatements();402 if (lastStatement.process != Basic.Next)403 throw new Error("At " + lastStatement.sourceLineNumber + ": expected next statement");404 if (lastStatement.variable != statement.variable)405 throw new Error("At " + lastStatement.sourceLineNumber + ": expected next for " + statement.variable + " but got " + lastStatement.variable);406 lastStatement.target = statement;407 statement.target = lastStatement;408 return statement;409 case "next":410 statement.process = Basic.Next;411 statement.variable = consumeKind("identifier").string;412 break;413 case "print": {414 statement.process = Basic.Print;415 statement.items = [];416 let ok = true;417 while (ok) {418 switch (peekToken().string) {419 case ",":420 nextToken();421 statement.items.push({kind: "comma"});422 break;423 case ";":424 nextToken();425 break;426 case "tab":427 nextToken();428 consumeToken("(");429 statement.items.push({kind: "tab", value: parseNumericExpression()});430 break;431 case "\n":432 ok = false;433 break;434 default:435 if (isStringExpression()) {436 statement.items.push({kind: "string", value: parseStringExpression()});437 break;438 }439 statement.items.push({kind: "number", value: parseNumericExpression()});440 break;441 }442 }443 break;444 }445 case "input":446 statement.process = Basic.Input;447 statement.items = [];448 for (;;) {449 stament.items.push(parseVariable());450 if (peekToken().string != ",")451 break;452 nextToken();453 }454 break;455 case "read":456 statement.process = Basic.Read;457 statement.items = [];458 for (;;) {459 stament.items.push(parseVariable());460 if (peekToken().string != ",")461 break;462 nextToken();463 }464 break;465 case "restore":466 statement.process = Basic.Restore;467 break;468 case "data":469 for (;;) {470 program.data.push(parseConstant());471 if (peekToken().string != ",")472 break;473 nextToken();474 }475 break;476 case "dim":477 statement.process = Basic.Dim;478 statement.items = [];479 for (;;) {480 let name = consumeKind("identifier").string;481 consumeToken("(");482 let bounds = [];483 bounds.push(parseNonNegativeInteger());484 if (peekToken().string == ",") {485 nextToken();486 bounds.push(parseNonNegativeInteger());487 }488 consumeToken(")");489 statement.items.push({name, bounds});490 491 if (peekToken().string != ",")492 break;493 consumeToken(",");494 }495 break;496 case "option": {497 consumeToken("base");498 let base = parseNonNegativeInteger();499 if (base != 0 && base != 1)500 throw new Error("At " + command.sourceLineNumber + ": unexpected base: " + base);501 program.base = base;502 break;503 }504 case "randomize":505 statement.process = Basic.Randomize;506 break;507 case "end":508 statement.process = Basic.End;509 break;510 default:511 throw new Error("At " + command.sourceLineNumber + ": unexpected command but got: " + command.string);512 }513 break;514 case "remark":515 // Just ignore it.516 break;517 default:518 throw new Error("At " + command.sourceLineNumber + ": expected command but got: " + command.string + " (of kind " + command.kind + ")");519 }520 521 consumeKind("newLine");522 return statement;523 }524 525 function parseStatements()526 {527 let statement;528 do {529 statement = parseStatement();530 } while (!statement.process || !statement.process.isBlockEnd);531 return statement;532 }533 534 return {535 program()...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.getLocations(function(err, data) {4 if (err) return console.error(err);5 console.log(data);6});7{ "statusCode": 200,8 "data": {9 {10 },11 {12 },13 {14 },15 {16 },17 {18 },19 {
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('API_KEY');3var options = {4};5test.runTest(options, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 test.consumeKind(data.data.testId, 'consoleLog', function(err, data) {9 if (err) return console.error(err);10 console.log(data);11 });12});13var wpt = require('webpagetest');14var test = new wpt('API_KEY');15var options = {16};17test.runTest(options, function(err, data) {18 if (err) return console.error(err);19 console.log(data);20 test.consumeKind(data.data.testId, 'consoleLog', function(err, data) {21 if (err) return console.error(err);22 console.log(data);23 });24});25var wpt = require('webpagetest');26var test = new wpt('API_KEY');27var options = {28};29test.runTest(options, function(err, data) {30 if (err) return console.error(err);31 console.log(data);32 test.consumeKind(data.data.testId, 'consoleLog', function(err, data) {33 if (err) return console.error(err);34 console.log(data);35 });36});37var wpt = require('webpagetest');38var test = new wpt('API_KEY');39var options = {40};41test.runTest(options, function(err, data) {42 if (err) return console.error(err);
Using AI Code Generation
1var wpt = require('wpt.js');2var wpt = new WebPageTest('www.webpagetest.org');3 console.log(data);4});5WebPageTest.prototype.consumeKind = function(url, callback) {6 var that = this;7 var request = that._request('/runtest.php?url=' + url + '&k=' + that._key, function(error, response, body) {8 if (!error && response.statusCode == 200) {9 var json = JSON.parse(body);10 var testId = json.data.testId;11 console.log('Test ID: ' + testId);12 that._testId = testId;13 that._pollResult(callback);14 } else {15 console.log('Error: ' + error);16 }17 });18}
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Albert Einstein');3page.get(function(err, resp) {4 console.log(resp);5 console.log(resp.data);6 console.log(resp.data.image);7 console.log(resp.data.image[0]);8 console.log(resp.data.image[0].url);9 console.log(resp.data.image[0].url[0]);10 console.log(resp.data.image[0].url[0].value);11});
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools('Albert Einstein');3wp.consumeKind('infobox').then(function (page) {4 console.log(page.infobox());5});6var wptools = require('wptools');7var wp = new wptools('Albert Einstein');8wp.consumeKind('infobox').then(function (page) {9 console.log(page.infobox());10});11var wptools = require('wptools');12var wp = new wptools('Albert Einstein');13wp.consumeKind('infobox').then(function (page) {14 console.log(page.infobox());15});16var wptools = require('wptools');17var wp = new wptools('Albert Einstein');18wp.consumeKind('infobox').then(function (page) {19 console.log(page.infobox());20});21var wptools = require('wptools');22var wp = new wptools('Albert Einstein');23wp.consumeKind('infobox').then(function (page) {24 console.log(page.infobox());25});26var wptools = require('wptools');27var wp = new wptools('Albert Einstein');28wp.consumeKind('infobox').then(function (page) {29 console.log(page.infobox());30});31var wptools = require('wptools');32var wp = new wptools('Albert Einstein');33wp.consumeKind('infobox').then(function (page) {34 console.log(page.infobox());35});36var wptools = require('wptools');37var wp = new wptools('Albert Einstein');38wp.consumeKind('infobox').then(function (page) {39 console.log(page.infobox());40});
Using AI Code Generation
1var wpt = require('wpt.js');2var wptObject = new wpt('your-api-key');3wptObject.consumeKind('your-connection-id', function (err, data) {4 console.log(data);5});6### wpt(apiKey)7### .test(options, callback)
Using AI Code Generation
1var wpt = require("wpt");2var wpt = new wpt();3 if (err) {4 } else {5 }6});7Contributions are welcome. Please read [CONTRIBUTING.md](
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.6c9e9c6b8d6c7f6f0b6c1e6f1d6e7f6e');3wpt.consumeKind('Test', function(err, data) {4 if (err) {5 console.log(err);6 }7 console.log(data);8});9var wpt = require('wpt');10var wpt = new WebPageTest('www.webpagetest.org', 'A.6c9e9c6b8d6c7f6f0b6c1e6f1d6e7f6e');11wpt.consumeKind('Test', function(err, data) {12 if (err) {13 console.log(err);14 }15 console.log(data);16});17var wpt = require('wpt');18var wpt = new WebPageTest('www.webpagetest.org', 'A.6c9e9c6b8d6c7f6f0b6c1e6f1d6e7f6e');19wpt.consumeKind('Test', function(err, data) {20 if (err) {21 console.log(err);22 }23 console.log(data);24});25var wpt = require('wpt');26var wpt = new WebPageTest('www.webpagetest.org', 'A.6c9e9c6b8d6c7f6f0b6c1e6f1d6e7f6e');27wpt.consumeKind('Test', function(err, data) {28 if (err) {29 console.log(err);30 }31 console.log(data);32});33var wpt = require('wpt');34var wpt = new WebPageTest('www.webpagetest.org', 'A.6c9e9c6b8d6c7f6f0b6c1e6f1d6e7f6e');
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!!