Best JavaScript code snippet using wpt
autocomplete.js
Source:autocomplete.js
1/*2 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one3 * or more contributor license agreements. Licensed under the Elastic License;4 * you may not use this file except in compliance with the Elastic License.5 */6import { uniq } from 'lodash';7import { parse, getByAlias } from '@kbn/interpreter/common';8const MARKER = 'CANVAS_SUGGESTION_MARKER';9/**10 * Generates the AST with the given expression and then returns the function and argument definitions11 * at the given position in the expression, if there are any.12 */13export function getFnArgDefAtPosition(specs, expression, position) {14 const text = expression.substr(0, position) + MARKER + expression.substr(position);15 try {16 const ast = parse(text, { addMeta: true });17 const { ast: newAst, fnIndex, argName } = getFnArgAtPosition(ast, position);18 const fn = newAst.node.chain[fnIndex].node;19 const fnDef = getByAlias(specs, fn.function.replace(MARKER, ''));20 if (fnDef && argName) {21 const argDef = getByAlias(fnDef.args, argName);22 return { fnDef, argDef };23 }24 return { fnDef };25 } catch (e) {26 // Fail silently27 }28 return [];29}30/**31 * Gets a list of suggestions for the given expression at the given position. It does this by32 * inserting a marker at the given position, then parsing the resulting expression. This way we can33 * see what the marker would turn into, which tells us what sorts of things to suggest. For34 * example, if the marker turns into a function name, then we suggest functions. If it turns into35 * an unnamed argument, we suggest argument names. If it turns into a value, we suggest values.36 */37export function getAutocompleteSuggestions(specs, expression, position) {38 const text = expression.substr(0, position) + MARKER + expression.substr(position);39 try {40 const ast = parse(text, { addMeta: true });41 const { ast: newAst, fnIndex, argName, argIndex } = getFnArgAtPosition(ast, position);42 const fn = newAst.node.chain[fnIndex].node;43 if (fn.function.includes(MARKER)) {44 return getFnNameSuggestions(specs, newAst, fnIndex);45 }46 if (argName === '_') {47 return getArgNameSuggestions(specs, newAst, fnIndex, argName, argIndex);48 }49 if (argName) {50 return getArgValueSuggestions(specs, newAst, fnIndex, argName, argIndex);51 }52 } catch (e) {53 // Fail silently54 }55 return [];56}57/**58 * Get the function and argument (if there is one) at the given position.59 */60function getFnArgAtPosition(ast, position) {61 const fnIndex = ast.node.chain.findIndex(fn => fn.start <= position && position <= fn.end);62 const fn = ast.node.chain[fnIndex];63 for (const [argName, argValues] of Object.entries(fn.node.arguments)) {64 for (let argIndex = 0; argIndex < argValues.length; argIndex++) {65 const value = argValues[argIndex];66 if (value.start <= position && position <= value.end) {67 if (value.node !== null && value.node.type === 'expression') {68 return getFnArgAtPosition(value, position);69 }70 return { ast, fnIndex, argName, argIndex };71 }72 }73 }74 return { ast, fnIndex };75}76function getFnNameSuggestions(specs, ast, fnIndex) {77 // Filter the list of functions by the text at the marker78 const { start, end, node: fn } = ast.node.chain[fnIndex];79 const query = fn.function.replace(MARKER, '');80 const matchingFnDefs = specs.filter(({ name }) => textMatches(name, query));81 // Sort by whether or not the function expects the previous function's return type, then by82 // whether or not the function name starts with the text at the marker, then alphabetically83 const prevFn = ast.node.chain[fnIndex - 1];84 const prevFnDef = prevFn && getByAlias(specs, prevFn.node.function);85 const prevFnType = prevFnDef && prevFnDef.type;86 const comparator = combinedComparator(87 prevFnTypeComparator(prevFnType),88 invokeWithProp(startsWithComparator(query), 'name'),89 invokeWithProp(alphanumericalComparator, 'name')90 );91 const fnDefs = matchingFnDefs.sort(comparator);92 return fnDefs.map(fnDef => {93 return { type: 'function', text: fnDef.name + ' ', start, end: end - MARKER.length, fnDef };94 });95}96function getArgNameSuggestions(specs, ast, fnIndex, argName, argIndex) {97 // Get the list of args from the function definition98 const fn = ast.node.chain[fnIndex].node;99 const fnDef = getByAlias(specs, fn.function);100 if (!fnDef) {101 return [];102 }103 // We use the exact text instead of the value because it is always a string and might be quoted104 const { text, start, end } = fn.arguments[argName][argIndex];105 // Filter the list of args by the text at the marker106 const query = text.replace(MARKER, '');107 const matchingArgDefs = Object.values(fnDef.args).filter(({ name }) => textMatches(name, query));108 // Filter the list of args by those which aren't already present (unless they allow multi)109 const argEntries = Object.entries(fn.arguments).map(([name, values]) => {110 return [name, values.filter(value => !value.text.includes(MARKER))];111 });112 const unusedArgDefs = matchingArgDefs.filter(argDef => {113 if (argDef.multi) {114 return true;115 }116 return !argEntries.some(([name, values]) => {117 return values.length && (name === argDef.name || argDef.aliases.includes(name));118 });119 });120 // Sort by whether or not the arg is also the unnamed, then by whether or not the arg name starts121 // with the text at the marker, then alphabetically122 const comparator = combinedComparator(123 unnamedArgComparator,124 invokeWithProp(startsWithComparator(query), 'name'),125 invokeWithProp(alphanumericalComparator, 'name')126 );127 const argDefs = unusedArgDefs.sort(comparator);128 return argDefs.map(argDef => {129 return { type: 'argument', text: argDef.name + '=', start, end: end - MARKER.length, argDef };130 });131}132function getArgValueSuggestions(specs, ast, fnIndex, argName, argIndex) {133 // Get the list of values from the argument definition134 const fn = ast.node.chain[fnIndex].node;135 const fnDef = getByAlias(specs, fn.function);136 if (!fnDef) {137 return [];138 }139 const argDef = getByAlias(fnDef.args, argName);140 if (!argDef) {141 return [];142 }143 // Get suggestions from the argument definition, including the default144 const { start, end, node } = fn.arguments[argName][argIndex];145 const query = node.replace(MARKER, '');146 const suggestions = uniq(argDef.options.concat(argDef.default || []));147 // Filter the list of suggestions by the text at the marker148 const filtered = suggestions.filter(option => textMatches(String(option), query));149 // Sort by whether or not the value starts with the text at the marker, then alphabetically150 const comparator = combinedComparator(startsWithComparator(query), alphanumericalComparator);151 const sorted = filtered.sort(comparator);152 return sorted.map(value => {153 const text = maybeQuote(value) + ' ';154 return { start, end: end - MARKER.length, type: 'value', text };155 });156}157function textMatches(text, query) {158 return text.toLowerCase().includes(query.toLowerCase().trim());159}160function maybeQuote(value) {161 if (typeof value === 'string') {162 if (value.match(/^\{.*\}$/)) {163 return value;164 }165 return `"${value.replace(/"/g, '\\"')}"`;166 }167 return value;168}169function prevFnTypeComparator(prevFnType) {170 return (a, b) =>171 Boolean(b.context.types && b.context.types.includes(prevFnType)) -172 Boolean(a.context.types && a.context.types.includes(prevFnType));173}174function unnamedArgComparator(a, b) {175 return b.aliases.includes('_') - a.aliases.includes('_');176}177function alphanumericalComparator(a, b) {178 if (a < b) {179 return -1;180 }181 if (a > b) {182 return 1;183 }184 return 0;185}186function startsWithComparator(query) {187 return (a, b) => String(b).startsWith(query) - String(a).startsWith(query);188}189function combinedComparator(...comparators) {190 return (a, b) =>191 comparators.reduce((acc, comparator) => {192 if (acc !== 0) {193 return acc;194 }195 return comparator(a, b);196 }, 0);197}198function invokeWithProp(fn, prop) {199 return (...args) => fn(...args.map(arg => arg[prop]));...
index.js
Source:index.js
1// @ts-check2export class ECMAssembly {3 table4 __pin5 __unpin6 get wasmExports() {7 return this._exports8 }9 set wasmExports(e) {10 this.table = e.table11 this.__pin = e.__pin12 this.__unpin = e.__unpin13 this._exports = e14 }15 _exports = null16 __deferWithArg_pinnedRefCount = new Map()17 wasmImports = {18 requestAnimationFrame: {19 _requestAnimationFrame: fnIndex => {20 return requestAnimationFrame(time => {21 this.getFn(fnIndex)(time)22 })23 },24 // _cancelAnimationFrame: id => {25 // cancelAnimationFrame(id)26 // },27 cancelAnimationFrame,28 },29 setTimeout: {30 _setTimeout: (fnIndex, ms) => {31 return setTimeout(this.getFn(fnIndex), ms)32 },33 clearTimeout,34 },35 setInterval: {36 _setInterval: (fnIndex, ms) => {37 return setInterval(this.getFn(fnIndex), ms)38 },39 clearInterval,40 },41 defer: {42 _defer: callbackIndex => {43 Promise.resolve().then(this.getFn(callbackIndex))44 },45 _deferWithArg: (callbackIndex, argPtr) => {46 let refCount = this.__deferWithArg_pinnedRefCount.get(argPtr)47 refCount ?? this.__deferWithArg_pinnedRefCount.set(argPtr, (refCount = 0))48 // Prevent the thing pointed to by argPtr from being collectd, because the callback needs it later.49 if (refCount++ === 0) this.__pin(argPtr)50 this.__deferWithArg_pinnedRefCount.set(argPtr, refCount)51 Promise.resolve().then(() => {52 // At this point, is the callback collected? Did we need to53 // __pin the callback too? Does it currently works by54 // accident?55 this.getFn(callbackIndex)(argPtr)56 let refCount = this.__deferWithArg_pinnedRefCount.get(argPtr)57 if (refCount == null) throw new Error('We should always have a ref count at this point!')58 if (refCount-- === 0) {59 this.__unpin(argPtr)60 this.__deferWithArg_pinnedRefCount.delete(argPtr)61 } else {62 this.__deferWithArg_pinnedRefCount.set(argPtr, refCount)63 }64 })65 },66 },67 }68 getFn(fnIndex) {69 if (!this.wasmExports)70 throw new Error(71 'Make sure you set .wasmExports after instantiating the Wasm module but before running the Wasm module.',72 )73 return this.table.get(fnIndex)74 }...
Using AI Code Generation
1var wpt = require('wpt');2var options = {3};4wpt.fnIndex(options, function(err, data) {5 if (err) {6 console.log(err);7 } else {8 console.log(data);9 }10});11### fnGetLocations(fnGetLocationsCallback)12var wpt = require('wpt');13var options = {14};15wpt.fnGetLocations(options, function(err, data) {16 if (err) {17 console.log(err);18 } else {19 console.log(data);20 }21});22### fnGetTesters(fnGetTestersCallback)23var wpt = require('wpt');24var options = {25};26wpt.fnGetTesters(options, function(err, data) {27 if (err) {28 console.log(err);29 } else {30 console.log(data);31 }32});33### fnGetLocations(fnGetLocationsCallback)34var wpt = require('wpt
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log(data);5 wpt.getTestStatus(data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 });9});10var wpt = require('wpt');11var wpt = new WebPageTest('www.webpagetest.org');12 if (err) return console.error(err);13 console.log(data);14 wpt.getTestStatus(data.testId, function(err, data) {15 if (err) return console.error(err);16 console.log(data);17 });18});19var wpt = require('wpt');20var wpt = new WebPageTest('www.webpagetest.org');21 if (err) return console.error(err);22 console.log(data);23 wpt.getTestStatus(data.testId, function(err, data) {24 if (err) return console.error(err);25 console.log(data);26 });27});28var wpt = require('wpt');29var wpt = new WebPageTest('www.webpagetest.org');30 if (err) return console.error(err);31 console.log(data);32 wpt.getTestStatus(data.testId, function(err, data) {33 if (err) return console.error(err);34 console.log(data);35 });36});37var wpt = require('wpt');38var wpt = new WebPageTest('www.webpagetest.org');39 if (err) return console.error(err);40 console.log(data);
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.fnIndex();3module.exports = {4 fnIndex: function(){5 console.log("I am index method of wpt module.");6 }7}8var wpt = require('./wpt.js');9wpt.fnIndex();10wpt.fnAbout();11module.exports = {12 fnIndex: function(){13 console.log("I am index method of wpt module.");14 },15 fnAbout: function(){16 console.log("I am about method of wpt module.");17 }18}19var wpt = require('./wpt.js');20wpt.fnIndex();21wpt.fnAbout();22module.exports = {23 fnIndex: function(){24 console.log("I am index method of wpt module.");25 },26 fnAbout: function(){27 console.log("I am about method of wpt module.");28 }29}
Using AI Code Generation
1var wpt = require('wpt.js');2var wpt = new wpt('API_KEY');3wpt.fnIndex(function (err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10var wpt = require('wpt.js');11var wpt = new wpt('API_KEY');12wpt.fnList(function (err, data) {13 if (err) {14 console.log(err);15 } else {16 console.log(data);17 }18});19var wpt = require('wpt.js');20var wpt = new wpt('API_KEY');21wpt.fnOptions(function (err, data) {22 if (err) {23 console.log(err);24 } else {25 console.log(data);26 }27});28var wpt = require('wpt.js');29var wpt = new wpt('API_KEY');30wpt.fnStatus(function (err, data) {31 if (err) {32 console.log(err);33 } else {34 console.log(data);35 }36});37var wpt = require('wpt.js');38var wpt = new wpt('API_KEY');39wpt.getLocations(function (err, data) {40 if (err) {41 console.log(err);42 } else {43 console.log(data);44 }45});46var wpt = require('wpt.js');47var wpt = new wpt('API_KEY');48wpt.getTesters(function (err, data) {49 if (err) {50 console.log(err);51 } else {52 console.log(data);53 }54});55var wpt = require('wpt.js');56var wpt = new wpt('API_KEY');57wpt.getTesters(function (err, data) {58 if (err) {59 console.log(err);60 } else {61 console.log(data);62 }63});
Using AI Code Generation
1var wpt = require('./wpt.js');2var index = wpt.fnIndex([1,2,3,4,5,6,7,8,9,10], 7);3console.log(index);4module.exports = {5 fnIndex: function(arr, value){6 return arr.indexOf(value);7 }8}
Using AI Code Generation
1var wpt = require('wpt');2wpt.fnIndex(function(err, data) {3 if (err) {4 } else {5 }6});7var wpt = require('wpt');8wpt.fnIndex(function(err, data) {9 if (err) {10 } else {11 console.log(data);12 }13});14var wpt = require('wpt');15wpt.fnIndex(function(err, data) {16 if (err) {17 } else {18 console.log(data);19 }20});21var wpt = require('wpt');22wpt.fnIndex(function(err, data) {23 if (err) {24 } else {25 console.log(data);26 }27});28var wpt = require('wpt');29wpt.fnIndex(function(err, data) {30 if (err) {31 } else {32 console.log(data);33 }34});35var wpt = require('wpt');36wpt.fnIndex(function(err, data) {37 if (err) {
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.fnIndex(1, 2, function(data) {3 console.log(data);4});5{ statusCode: 200,6 standardDeviation: 0 }7var wpt = require('./wpt.js');8wpt.fnIndex(1, 2, function(data) {9 console.log(data);10});11{ statusCode: 200,12 standardDeviation: 0 }13{14}15{16}17{18}19{20}21{22}23{
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!!