Best JavaScript code snippet using wpt
helpers.js
Source:helpers.js
1import { Type } from "./type.js";2import { Argument } from "./argument.js";3import {4 ExtendedAttributes,5 SimpleExtendedAttribute,6} from "./extended-attributes.js";7import { Operation } from "./operation.js";8import { Attribute } from "./attribute.js";9import { Tokeniser } from "../tokeniser.js";10/**11 * @param {string} identifier12 */13export function unescape(identifier) {14 return identifier.startsWith("_") ? identifier.slice(1) : identifier;15}16/**17 * Parses comma-separated list18 * @param {import("../tokeniser").Tokeniser} tokeniser19 * @param {object} args20 * @param {Function} args.parser parser function for each item21 * @param {boolean} [args.allowDangler] whether to allow dangling comma22 * @param {string} [args.listName] the name to be shown on error messages23 */24export function list(tokeniser, { parser, allowDangler, listName = "list" }) {25 const first = parser(tokeniser);26 if (!first) {27 return [];28 }29 first.tokens.separator = tokeniser.consume(",");30 const items = [first];31 while (first.tokens.separator) {32 const item = parser(tokeniser);33 if (!item) {34 if (!allowDangler) {35 tokeniser.error(`Trailing comma in ${listName}`);36 }37 break;38 }39 item.tokens.separator = tokeniser.consume(",");40 items.push(item);41 if (!item.tokens.separator) break;42 }43 return items;44}45/**46 * @param {import("../tokeniser").Tokeniser} tokeniser47 */48export function const_value(tokeniser) {49 return (50 tokeniser.consumeKind("decimal", "integer") ||51 tokeniser.consume("true", "false", "Infinity", "-Infinity", "NaN")52 );53}54/**55 * @param {object} token56 * @param {string} token.type57 * @param {string} token.value58 */59export function const_data({ type, value }) {60 switch (type) {61 case "decimal":62 case "integer":63 return { type: "number", value };64 case "string":65 return { type: "string", value: value.slice(1, -1) };66 }67 switch (value) {68 case "true":69 case "false":70 return { type: "boolean", value: value === "true" };71 case "Infinity":72 case "-Infinity":73 return { type: "Infinity", negative: value.startsWith("-") };74 case "[":75 return { type: "sequence", value: [] };76 case "{":77 return { type: "dictionary" };78 default:79 return { type: value };80 }81}82/**83 * @param {import("../tokeniser").Tokeniser} tokeniser84 */85export function primitive_type(tokeniser) {86 function integer_type() {87 const prefix = tokeniser.consume("unsigned");88 const base = tokeniser.consume("short", "long");89 if (base) {90 const postfix = tokeniser.consume("long");91 return new Type({ source, tokens: { prefix, base, postfix } });92 }93 if (prefix) tokeniser.error("Failed to parse integer type");94 }95 function decimal_type() {96 const prefix = tokeniser.consume("unrestricted");97 const base = tokeniser.consume("float", "double");98 if (base) {99 return new Type({ source, tokens: { prefix, base } });100 }101 if (prefix) tokeniser.error("Failed to parse float type");102 }103 const { source } = tokeniser;104 const num_type = integer_type(tokeniser) || decimal_type(tokeniser);105 if (num_type) return num_type;106 const base = tokeniser.consume(107 "bigint",108 "boolean",109 "byte",110 "octet",111 "undefined"112 );113 if (base) {114 return new Type({ source, tokens: { base } });115 }116}117/**118 * @param {import("../tokeniser").Tokeniser} tokeniser119 */120export function argument_list(tokeniser) {121 return list(tokeniser, {122 parser: Argument.parse,123 listName: "arguments list",124 });125}126/**127 * @param {import("../tokeniser").Tokeniser} tokeniser128 * @param {string} typeName129 */130export function type_with_extended_attributes(tokeniser, typeName) {131 const extAttrs = ExtendedAttributes.parse(tokeniser);132 const ret = Type.parse(tokeniser, typeName);133 if (ret) autoParenter(ret).extAttrs = extAttrs;134 return ret;135}136/**137 * @param {import("../tokeniser").Tokeniser} tokeniser138 * @param {string} typeName139 */140export function return_type(tokeniser, typeName) {141 const typ = Type.parse(tokeniser, typeName || "return-type");142 if (typ) {143 return typ;144 }145 const voidToken = tokeniser.consume("void");146 if (voidToken) {147 const ret = new Type({148 source: tokeniser.source,149 tokens: { base: voidToken },150 });151 ret.type = "return-type";152 return ret;153 }154}155/**156 * @param {import("../tokeniser").Tokeniser} tokeniser157 */158export function stringifier(tokeniser) {159 const special = tokeniser.consume("stringifier");160 if (!special) return;161 const member =162 Attribute.parse(tokeniser, { special }) ||163 Operation.parse(tokeniser, { special }) ||164 tokeniser.error("Unterminated stringifier");165 return member;166}167/**168 * @param {string} str169 */170export function getLastIndentation(str) {171 const lines = str.split("\n");172 // the first line visually binds to the preceding token173 if (lines.length) {174 const match = lines[lines.length - 1].match(/^\s+/);175 if (match) {176 return match[0];177 }178 }179 return "";180}181/**182 * @param {string} parentTrivia183 */184export function getMemberIndentation(parentTrivia) {185 const indentation = getLastIndentation(parentTrivia);186 const indentCh = indentation.includes("\t") ? "\t" : " ";187 return indentation + indentCh;188}189/**190 * @param {object} def191 * @param {import("./extended-attributes.js").ExtendedAttributes} def.extAttrs192 */193export function autofixAddExposedWindow(def) {194 return () => {195 if (def.extAttrs.length) {196 const tokeniser = new Tokeniser("Exposed=Window,");197 const exposed = SimpleExtendedAttribute.parse(tokeniser);198 exposed.tokens.separator = tokeniser.consume(",");199 const existing = def.extAttrs[0];200 if (!/^\s/.test(existing.tokens.name.trivia)) {201 existing.tokens.name.trivia = ` ${existing.tokens.name.trivia}`;202 }203 def.extAttrs.unshift(exposed);204 } else {205 autoParenter(def).extAttrs = ExtendedAttributes.parse(206 new Tokeniser("[Exposed=Window]")207 );208 const trivia = def.tokens.base.trivia;209 def.extAttrs.tokens.open.trivia = trivia;210 def.tokens.base.trivia = `\n${getLastIndentation(trivia)}`;211 }212 };213}214/**215 * Get the first syntax token for the given IDL object.216 * @param {*} data217 */218export function getFirstToken(data) {219 if (data.extAttrs.length) {220 return data.extAttrs.tokens.open;221 }222 if (data.type === "operation" && !data.special) {223 return getFirstToken(data.idlType);224 }225 const tokens = Object.values(data.tokens).sort((x, y) => x.index - y.index);226 return tokens[0];227}228/**229 * @template T230 * @param {T[]} array231 * @param {(item: T) => boolean} predicate232 */233export function findLastIndex(array, predicate) {234 const index = array.slice().reverse().findIndex(predicate);235 if (index === -1) {236 return index;237 }238 return array.length - index - 1;239}240/**241 * Returns a proxy that auto-assign `parent` field.242 * @template T243 * @param {T} data244 * @param {*} [parent] The object that will be assigned to `parent`.245 * If absent, it will be `data` by default.246 * @return {T}247 */248export function autoParenter(data, parent) {249 if (!parent) {250 // Defaults to `data` unless specified otherwise.251 parent = data;252 }253 if (!data) {254 // This allows `autoParenter(undefined)` which again allows255 // `autoParenter(parse())` where the function may return nothing.256 return data;257 }258 return new Proxy(data, {259 get(target, p) {260 const value = target[p];261 if (Array.isArray(value)) {262 // Wraps the array so that any added items will also automatically263 // get their `parent` values.264 return autoParenter(value, target);265 }266 return value;267 },268 set(target, p, value) {269 target[p] = value;270 if (!value) {271 return true;272 } else if (Array.isArray(value)) {273 // Assigning an array will add `parent` to its items.274 for (const item of value) {275 if (typeof item.parent !== "undefined") {276 item.parent = parent;277 }278 }279 } else if (typeof value.parent !== "undefined") {280 value.parent = parent;281 }282 return true;283 },284 });...
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3 console.log(data);4});5var wpt = require('webpagetest');6var client = wpt('www.webpagetest.org');7 console.log(data);8});9var wpt = require('webpagetest');10var client = wpt('www.webpagetest.org');11 console.log(data);12});13var wpt = require('webpagetest');14var client = wpt('www.webpagetest.org');15 console.log(data);16});17var wpt = require('webpagetest');18var client = wpt('www.webpagetest.org');19 console.log(data);20});21var wpt = require('webpagetest');22var client = wpt('www.webpagetest.org');23 console.log(data);24});25var wpt = require('webpagetest');26var client = wpt('www.webpagetest.org');27 console.log(data);28});
Using AI Code Generation
1var editor = CKEDITOR.instances.editor1;2editor.on( 'key', function( evt ) {3 if ( evt.data.keyCode == 9 ) {4 evt.cancel();5 editor.execCommand( 'indentCh' );6 }7} );
Using AI Code Generation
1var wptools = require('wptools');2wp.get(function(err, info) {3 console.log(info);4});5var wptools = require('wptools');6wp.get(function(err, info) {7 console.log(info);8});9var wptools = require('wptools');10wp.get(function(err, info) {11 console.log(info);12});13var wptools = require('wptools');14wp.get(function(err, info) {15 console.log(info);16});17var wptools = require('wptools');18wp.get(function(err, info) {19 console.log(info);20});21var wptools = require('wptools');22wp.get(function(err, info) {23 console.log(info);24});25var wptools = require('wptools');26wp.get(function(err, info) {27 console.log(info);28});29var wptools = require('wptools
Using AI Code Generation
1var wpt = require('wpt');2wpt.indentCh('test.js', function(err, data) {3 console.log(data);4});5var wpt = require('wpt');6wpt.indentStr('test.js', function(err, data) {7 console.log(data);8});9var wpt = require('wpt');10wpt.indentStr('test.js', function(err, data) {11 console.log(data);12});13var wpt = require('wpt');14wpt.inRange(3, 2, 4);15wpt.inRange(4, 8);16wpt.inRange(4, 2);17wpt.inRange(2, 2);18wpt.inRange(1.2, 2);19wpt.inRange(5.2, 4);20wpt.inRange(-3, -2, -6);21var wpt = require('wpt');22wpt.inRangeRight(3, 2, 4);23wpt.inRangeRight(4, 8);24wpt.inRangeRight(4, 2);25wpt.inRangeRight(2, 2);26wpt.inRangeRight(1.2, 2);27wpt.inRangeRight(5.2, 4);28wpt.inRangeRight(-3, -2, -6);29var wpt = require('wpt');30wpt.indexOf([1, 2, 1, 2], 2);31wpt.indexOf([1, 2, 1, 2], 2, 2
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!!