Best JavaScript code snippet using wpt
Scanner.js
Source:Scanner.js
1"use strict";2exports.__esModule = true;3exports.addToken = exports.scanTokens = exports.Scanner = void 0;4var Token_1 = require("./Token");5var Token_2 = require("./types/Token");6var index_1 = require("./index");7var Scanner = function (source) {8 return {9 current: 0,10 line: 1,11 start: 0,12 __Type: "Scanner",13 source: source,14 tokens: []15 };16};17exports.Scanner = Scanner;18var isAtEnd = function (scanner) {19 return scanner.current >= scanner.source.length;20};21var scanTokens = function (scanner) {22 while (!isAtEnd(scanner)) {23 scanner.start = scanner.current;24 scanToken(scanner);25 }26 scanner.tokens.push((0, Token_1.Token)(Token_2.TokenType.EOF, "", null, scanner.line));27 return scanner.tokens;28};29exports.scanTokens = scanTokens;30var addToken = function (type, literal, scanner) {31 var text = scanner.source.substring(scanner.start, scanner.current);32 scanner.tokens.push((0, Token_1.Token)(type, text, literal, scanner.line));33};34exports.addToken = addToken;35var string = function (scanner) {36 while (peek(scanner) != '"' && !isAtEnd(scanner)) {37 if (peek(scanner) == "\n")38 scanner.line++;39 advance(scanner);40 }41 if (isAtEnd(scanner)) {42 (0, index_1.error)(scanner.line, "Unterminated string");43 return;44 }45 advance(scanner);46 var value = scanner.source.substring(scanner.start + 1, scanner.current - 1);47 (0, exports.addToken)(Token_2.TokenType.STRING, value, scanner);48};49var scanToken = function (scanner) {50 var c = advance(scanner);51 switch (c) {52 case '"':53 string(scanner);54 break;55 case '(':56 (0, exports.addToken)(Token_2.TokenType.LEFT_PAREN, {}, scanner);57 break;58 case ')':59 (0, exports.addToken)(Token_2.TokenType.RIGHT_PAREN, {}, scanner);60 break;61 case '{':62 (0, exports.addToken)(Token_2.TokenType.LEFT_BRACE, {}, scanner);63 break;64 case '}':65 (0, exports.addToken)(Token_2.TokenType.RIGHT_BRACE, {}, scanner);66 break;67 case ',':68 (0, exports.addToken)(Token_2.TokenType.COMMA, {}, scanner);69 break;70 case '.':71 (0, exports.addToken)(Token_2.TokenType.DOT, {}, scanner);72 break;73 case '-':74 (0, exports.addToken)(Token_2.TokenType.MINUS, {}, scanner);75 break;76 case '+':77 (0, exports.addToken)(Token_2.TokenType.PLUS, {}, scanner);78 break;79 case ';':80 (0, exports.addToken)(Token_2.TokenType.SEMICOLON, {}, scanner);81 break;82 case '*':83 (0, exports.addToken)(Token_2.TokenType.STAR, {}, scanner);84 break;85 case '!':86 (0, exports.addToken)(match('=', scanner) ? Token_2.TokenType.BANG_EQUAL : Token_2.TokenType.BANG, {}, scanner);87 break;88 case '=':89 (0, exports.addToken)(match('=', scanner) ? Token_2.TokenType.EQUAL_EQUAL : Token_2.TokenType.EQUAL, {}, scanner);90 break;91 case '<':92 (0, exports.addToken)(match('=', scanner) ? Token_2.TokenType.LESS_EQUAL : Token_2.TokenType.LESS, {}, scanner);93 break;94 case '>':95 (0, exports.addToken)(match('=', scanner) ? Token_2.TokenType.GREATER_EQUAL : Token_2.TokenType.GREATER, {}, scanner);96 break;97 case '/':98 if (match('/', scanner)) {99 while (peek(scanner) != '\n' && !isAtEnd(scanner))100 advance(scanner);101 }102 else {103 (0, exports.addToken)(Token_2.TokenType.SLASH, {}, scanner);104 }105 break;106 case ' ':107 case '\r':108 case '\t':109 // Ignore whitespace.110 break;111 case '\n':112 scanner.line++;113 break;114 default:115 if (isDigit(c)) {116 number(scanner);117 }118 if (isAlpha(c)) {119 identifier(scanner);120 }121 else {122 (0, index_1.error)(scanner.line, "Unexpected Character");123 }124 }125};126var identifier = function (scanner) {127 while (isAlphaNumeric(peek(scanner)))128 advance(scanner);129 var text = scanner.source.substring(scanner.start, scanner.current);130 var tokenType = KEYWORDS.get(text);131 if (!tokenType)132 tokenType = Token_2.TokenType.IDENTIFIER;133 (0, exports.addToken)(tokenType, {}, scanner);134};135var KEYWORDS = new Map();136KEYWORDS.set("and", Token_2.TokenType.AND);137KEYWORDS.set("class", Token_2.TokenType.CLASS);138KEYWORDS.set("else", Token_2.TokenType.ELSE);139KEYWORDS.set("false", Token_2.TokenType.FALSE);140KEYWORDS.set("for", Token_2.TokenType.FOR);141KEYWORDS.set("fun", Token_2.TokenType.FUN);142KEYWORDS.set("if", Token_2.TokenType.IF);143KEYWORDS.set("nil", Token_2.TokenType.NIL);144KEYWORDS.set("or", Token_2.TokenType.OR);145KEYWORDS.set("print", Token_2.TokenType.PRINT);146KEYWORDS.set("return", Token_2.TokenType.RETURN);147KEYWORDS.set("super", Token_2.TokenType.SUPER);148KEYWORDS.set("this", Token_2.TokenType.THIS);149KEYWORDS.set("true", Token_2.TokenType.TRUE);150KEYWORDS.set("var", Token_2.TokenType.VAR);151KEYWORDS.set("while", Token_2.TokenType.WHILE);152var isAlpha = function (c) {153 return (c >= 'a' && c <= 'z') ||154 (c >= 'A' && c <= 'Z') ||155 c == '_';156};157var isAlphaNumeric = function (c) {158 return isAlpha(c) || isDigit(c);159};160var isDigit = function (c) {161 return c >= "0" && c <= '9';162};163var number = function (scanner) {164 while (isDigit(peek(scanner)))165 advance(scanner);166 if (peek(scanner) == '.' && isDigit(peekNext(scanner))) {167 advance(scanner);168 while (isDigit(peek(scanner)))169 advance(scanner);170 }171 (0, exports.addToken)(Token_2.TokenType.NUMBER, scanner.source.substring(scanner.start, scanner.current), scanner);172};173var peekNext = function (scanner) {174 if (scanner.current + 1 >= scanner.source.length)175 return '\0';176 return scanner.source.charAt(scanner.current + 1);177};178var peek = function (scanner) {179 if (isAtEnd(scanner))180 return '\0';181 return scanner.source.charAt(scanner.current);182};183var match = function (expected, scanner) {184 if (isAtEnd(scanner))185 return false;186 if (scanner.source.charAt(scanner.current) != expected)187 return false;188 scanner.current++;189 return true;190};191var advance = function (scanner) {192 return scanner.source.charAt(scanner.current++);...
TestPocketSwap_Fees.js
Source:TestPocketSwap_Fees.js
1const helper = require("./helper");2const ERC20 = artifacts.require("mocks/MockERC20.sol")3const Pocket = artifacts.require("mocks/MockPocket.sol")4const PocketSwapFactory = artifacts.require("pocketswap/PocketSwapFactory.sol")5const PocketSwapRouter = artifacts.require("pocketswap/PocketSwapRouter.sol")6const PocketSwapPair = artifacts.require("pocketswap/PocketSwapPair.sol")7const PocketSwap = artifacts.require("PocketSwap.sol")8contract("PocketSwap Fees", accounts => {9 const burnedLiquidity = 1000;10 let token_111 let token_212 let pocket_token13 let WETH14 let factory15 let pair16 let pocketSwap17 let token2pocket_liq1 = "10000000000000000000000000";18 let token2pocket_liq2 = "10000000000000000000000000";19 const deadline = () => {20 return parseInt(Date.now() / 1000) + 15 * 6021 }22 beforeEach(async () => {23 let router24 await Promise.all([25 ERC20.new(),26 ERC20.new(),27 Pocket.new(),28 ERC20.new(),29 ]).then(([a, b, c, d]) => [token_1, token_2, pocket_token, WETH] = [a, b, c, d])30 .then(() => PocketSwapFactory.new(pocket_token.address))31 .then(a => factory = a)32 .then(() => PocketSwapRouter.new(factory.address, WETH.address, pocket_token.address))33 .then(a => router = a)34 .then(() => PocketSwap.new(router.address, factory.address, pocket_token.address, WETH.address))35 .then(a => pocketSwap = a)36 .then(() => factory.createPair(token_1.address, token_2.address))37 .then(() => factory.getPair(token_1.address, token_2.address))38 .then(a => PocketSwapPair.at(a))39 .then(p => pair = p)40 // .then(p => {41 // console.log(`Factory: ${factory.address}`)42 // console.log(`Router: ${router.address}`)43 // console.log(`PocketSwap: ${pocketSwap.address}`)44 // console.log(`Pocket: ${pocket_token.address}`)45 // console.log(`Token1: ${token_1.address}`)46 // console.log(`Token2: ${token_2.address}`)47 // console.log(`Token1:Token2 pair: ${p.address}`)48 // })49 .then(async () => {50 const acc = accounts[9]51 await factory.createPair(token_2.address, pocket_token.address)52 await pocket_token.excludeFromRewards(acc);53 await AddLiquidity(acc, [token_2, pocket_token], [token2pocket_liq1, token2pocket_liq2])54 })55 })56 it("0.3% fees to LP", async () => {57 await checkFees(0.3)58 })59 it("0.5% fees to LP", async () => {60 await checkFees(0.5)61 })62 it("1.9% fees to LP", async () => {63 await checkFees(1.9)64 })65 it("35% fees to LP", async () => {66 await checkFees(35)67 })68 async function AddLiquidity(liqAcc, tokens, liq) {69 const [token_1, token_2] = tokens70 const [token_1_liq, token_2_liq] = liq71 await token_1.mint(liqAcc, token_1_liq.toString(), {from: liqAcc})72 await token_1.approve(pocketSwap.address, token_1_liq.toString(), {from: liqAcc})73 await token_2.mint(liqAcc, token_2_liq.toString(), {from: liqAcc})74 await token_2.approve(pocketSwap.address, token_2_liq.toString(), {from: liqAcc})75 await pocketSwap.addLiquidity({76 token0: token_1.address,77 token1: token_2.address,78 recipient: liqAcc,79 amount0Desired: token_1_liq.toString(),80 amount1Desired: token_2_liq.toString(),81 amount0Min: token_1_liq.toString(),82 amount1Min: token_2_liq.toString(),83 deadline: deadline(),84 }, {from: liqAcc})85 }86 const checkFees = async (feePercent) => {87 await factory.setFee(feePercent * 1e7)88 const token_1_liq = web3.utils.toWei("1000000")89 const token_2_liq = web3.utils.toWei("1000")90 const tokenIn = web3.utils.toWei("100")91 const totalLiq = Math.sqrt(parseInt(token_1_liq) * parseInt(token_2_liq))92 const liqAcc = accounts[0];93 await AddLiquidity(liqAcc, [token_1, token_2], [token_1_liq, token_2_liq]);94 let acc0LiquidityBalance = await pair.balanceOf(liqAcc)95 assert.equal(acc0LiquidityBalance, totalLiq - burnedLiquidity, 'Liquidity Balance')96 const out = await pocketSwap.getAmountsOut(tokenIn.toString(), [token_1.address, token_2.address]);97 const outRef = out[1]98 let expectedOut = helper.expectOutput(tokenIn, feePercent * 1e7, [token_1_liq, token_2_liq])99 assert.equal(outRef.toString(), expectedOut.toString(), `correct exchange - ${feePercent}%`)100 await token_1.mint(accounts[1], tokenIn.toString(), {from: accounts[1]})101 await token_1.approve(pocketSwap.address, tokenIn.toString(), {from: accounts[1]})102 await pocketSwap.swap({103 tokenIn: token_1.address,104 tokenOut: token_2.address,105 recipient: accounts[1],106 deadline: deadline(),107 amountIn: tokenIn.toString(),108 amountOutMinimum: expectedOut.toString(),109 }, {from: accounts[1]})110 let pairBalance1 = await token_1.balanceOf(pair.address);111 let pairBalance2 = await token_2.balanceOf(pair.address);112 assert.equal(pairBalance1.toString(), web3.utils.toWei((1000000 + 100).toString()), `Incorrect pair token1 balance`)113 let totalLiq1 = await pair.totalSupply();114 let token1BalanceExpected = BigInt(pairBalance1) * BigInt(acc0LiquidityBalance) / BigInt(totalLiq1)115 let token2BalanceExpected = BigInt(pairBalance2) * BigInt(acc0LiquidityBalance) / BigInt(totalLiq1)116 let was_acc0PocketBalance = await pocket_token.balanceOf(liqAcc);117 await pair.approve(pocketSwap.address, acc0LiquidityBalance.toString(), {from: liqAcc})118 await pocketSwap.removeLiquidity({119 tokenA: token_1.address,120 tokenB: token_2.address,121 liquidity: acc0LiquidityBalance.toString(), // liq122 amountAMin: token1BalanceExpected,123 amountBMin: token2BalanceExpected,124 rewards: pocket_token.address,125 recipient: liqAcc,126 deadline: deadline(),127 }, {from: liqAcc})128 let now_acc0PocketBalance = await pocket_token.balanceOf(liqAcc)129 assert.isAbove(parseInt(now_acc0PocketBalance), parseInt(was_acc0PocketBalance), `Pocket: ${feePercent}% Fee to LP`)130 }...
static_class_variable.js
Source:static_class_variable.js
1#!/usr/bin/env node2class Token {3 static number = 0;4 color = '';5}6let token_1 = new Token();7let token_2 = new Token();8let token_3 = new Token();9console.log("Token.number: " + Token.number + "\n");10token_1.number = 1;11token_1.color = "blue";12token_2.number = 2;13token_2.color = "orange";14token_3.number = 3;15token_3.color = "violet";16console.log("token_1.number: " + token_1.number + ", token_1.color: " + token_1.color);17console.log("token_2.number: " + token_2.number + ", token_2.color: " + token_2.color);18console.log("token_3.number: " + token_3.number + ", token_3.color: " + token_3.color);19console.log("\nToken.number: " + Token.number + "\n");20Token.number = 10;21console.log("Token.number: " + Token.number + "\n");22token_1.number = 4;23token_1.color = "petrol";24token_2.number = 5;25token_2.color = "ginger";26token_3.number = 6;27token_3.color = "amaranthine";28console.log("token_1.number: " + token_1.number + ", token_1.color: " + token_1.color);29console.log("token_2.number: " + token_2.number + ", token_2.color: " + token_2.color);30console.log("token_3.number: " + token_3.number + ", token_3.color: " + token_3.color);...
Using AI Code Generation
1const wpt = require('webpagetest');2const wpt_test = wpt('www.webpagetest.org', 'A.6e4a6c0f6f0a6d5b5c5c5d5a5a5b5a5a');3const url = process.argv[2];4wpt_test.runTest(url, {5}, (err, data) => {6 if (err) {7 console.log('Error: ' + err);8 } else {9 console.log('Test ID: ' + data.data.testId);10 }11});
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!!