Best JavaScript code snippet using wpt
matchkw.js
Source:matchkw.js
1(function(mod) {2 if (typeof exports == "object" && typeof module == "object") // CommonJS3 mod(require("../../lib/codemirror"), require("../fold/pyret-fold"));4 else if (typeof define == "function" && define.amd) // AMD5 define(["../../lib/codemirror", "../fold/pyret-fold"], mod);6 else // Plain browser env7 mod(CodeMirror);8})(function(CodeMirror) {9 "use strict";10 CodeMirror.defineOption("matchKeywords", false, function(cm, val, old) {11 if (old && old != CodeMirror.Init) {12 cm.off("cursorActivity", doMatchKeywords);13 cm.off("viewportChange", maybeUpdateMatch);14 clear(cm);15 }16 if (val) {17 cm.state.matchBothKeywords = typeof val == "object" && val.bothKeywords;18 cm.on("cursorActivity", doMatchKeywords);19 cm.on("viewportChange", maybeUpdateMatch);20 doMatchKeywords(cm);21 }22 });23 function clear(cm) {24 if (cm.state.keywordMarks)25 for (var i = 0; i < cm.state.keywordMarks.length; i++)26 cm.state.keywordMarks[i].clear();27 cm.state.keywordMarks = [];28 }29 function doMatchKeywords(cm) {30 cm.state.failedKeywordMatch = false;31 cm.operation(function() {32 clear(cm);33 if (cm.somethingSelected()) return;34 var cur = cm.getCursor(), range = cm.getViewport();35 range.from = Math.min(range.from, cur.line);36 range.to = Math.max(cur.line + 1, range.to);37 var match = CodeMirror.findMatchingKeyword(cm, cur, range);38 if (!match) return;39 var hit = match.at == "open" ? match.open : match.close;40 var other = match.at == "close" ? match.open : match.close;41 var wordStyle; // = match.matches ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";42 if (match.matches) {43 wordStyle = other ? "CodeMirror-matchingbracket" : "CodeMirror-pyret-no-match-start";44 } else {45 wordStyle = other ? "CodeMirror-nonmatchingbracket" : "CodeMirror-pyret-no-match-end";46 }47 var regionStyle = wordStyle + "-region";48 cm.state.failedKeywordMatch = !match.matches;49 cm.state.keywordMarks.push(cm.markText(hit.from, hit.to, {className: wordStyle + " " + (match.at == 'open' ? 'open' : 'close')}));50 match.extra.forEach(function(tok){51 cm.state.keywordMarks.push(cm.markText(tok.from, tok.to, {className: wordStyle}));52 });53 match.extraBad.forEach(function(tok){54 cm.state.keywordMarks.push(cm.markText(tok.from, tok.to, {className: "CodeMirror-nonmatchingbracket"}));55 });56 if (other) {57 cm.state.keywordMarks.push(cm.markText(other.from, other.to, {className: wordStyle + " " + (match.at == 'close' ? 'open' : 'close')}));58 cm.state.keywordMarks.push(cm.markText(match.open.from, match.close.to, {className: regionStyle}));59 }60 });61 }62 function maybeUpdateMatch(cm) {63 if (cm.state.failedKeywordMatch) doMatchKeywords(cm);64 }65 CodeMirror.commands.toMatchingKeyword = function(cm) {66 var found = CodeMirror.findMatchingKeyword(cm, cm.getCursor());67 if (found) {68 var other = found.at == "close" ? found.open : found.close;69 if (other) cm.extendSelection(other.to, other.from);70 }71 };72 function nonBlankToken(tok) {73 return !(tok.type === null || /^\s*$/.test(tok.string));74 }75 76 function nextNonblankTokenAfter(cm, pos, allowAtCurrent) {77 var line = pos.line;78 var toks = cm.getLineTokens(line);79 if (allowAtCurrent) {80 for (var i = 0; i < toks.length; i++) {81 if (toks[i].start == pos.ch && nonBlankToken(toks[i])) {82 toks[i].line = line;83 return toks[i];84 }85 }86 }87 for (var i = 0; i < toks.length; i++) {88 if (toks[i].start <= pos.ch && toks[i].end > pos.ch) {89 if (nonBlankToken(toks[i])) i++;90 for (; i < toks.length; i++) {91 if (nonBlankToken(toks[i])) {92 toks[i].line = line;93 return toks[i];94 }95 }96 }97 }98 for (line = line + 1; line <= cm.lastLine(); line++) {99 var toks = cm.getLineTokens(line);100 for (var i = 0; i < toks.length; i++) {101 if (nonBlankToken(toks[i])) {102 toks[i].line = line;103 return toks[i];104 }105 }106 }107 return undefined;108 }109 function prevNonblankTokenBefore(cm, pos) {110 var line = pos.line;111 var toks = cm.getLineTokens(line);112 for (var i = toks.length - 1; i >= 0; i--) {113 if (toks[i].start < pos.ch && toks[i].end >= pos.ch) {114 if (nonBlankToken(toks[i])) i--;115 for (; i >= 0; i--) {116 if (nonBlankToken(toks[i])) {117 toks[i].line = line;118 return toks[i];119 }120 }121 }122 }123 for (line = line - 1; line >= cm.firstLine(); line--) {124 var toks = cm.getLineTokens(line);125 for (var i = toks.length - 1; i >= 0; i--) {126 if (nonBlankToken(toks[i])) {127 toks[i].line = line;128 return toks[i];129 }130 }131 }132 return undefined;133 }134 function goToOpenComment(cm, pos, nestingDepth) {135 var line = pos.line;136 var toks = cm.getLineTokens(line);137 for (var i = toks.length - 1; i >= 0; i--) {138 if (toks[i].start < pos.ch139 && toks[i].state.lastToken === "COMMENT-START"140 && toks[i].state.commentNestingDepth == nestingDepth) {141 var pos = {line: line, ch: toks[i].start};142 cm.extendSelection(pos, pos);143 return;144 }145 }146 for (line = line - 1; line >= cm.firstLine(); line--) {147 var toks = cm.getLineTokens(line);148 for (var i = toks.length - 1; i >= 0; i--) {149 if (toks[i].state.lastToken === "COMMENT-START"150 && toks[i].state.commentNestingDepth == nestingDepth) {151 var pos = {line: line, ch: toks[i].start};152 cm.extendSelection(pos, pos);153 return;154 }155 }156 }157 var pos = {line: cm.firstLine(), ch: 0};158 cm.extendSelection(pos, pos);159 }160 function goToCloseComment(cm, pos, nestingDepth) {161 var line = pos.line;162 var toks = cm.getLineTokens(line);163 for (var i = 0; i < toks.length; i++) {164 if (toks[i].start > pos.ch165 && toks[i].state.lastToken === "COMMENT-END"166 && toks[i].state.commentNestingDepth == nestingDepth - 1) {167 var pos = {line: line, ch: toks[i].end};168 cm.extendSelection(pos, pos);169 return;170 }171 }172 for (line = line + 1; line <= cm.lastLine(); line++) {173 var toks = cm.getLineTokens(line);174 for (var i = 0; i < toks.length; i++) {175 if (toks[i].state.lastToken === "COMMENT-END"176 && toks[i].state.commentNestingDepth == nestingDepth - 1) {177 var pos = {line: line, ch: toks[i].end};178 cm.extendSelection(pos, pos);179 return;180 }181 }182 }183 var pos = {line: cm.lastLine(), ch: null};184 cm.extendSelection(pos, pos);185 }186 CodeMirror.commands.goBackwardSexp = function(cm) {187 var cursor = cm.getCursor();188 var cur = cm.getTokenAt(cursor);189 var prev = prevNonblankTokenBefore(cm, cursor);190 if (prev &&191 ((cursor.ch === cur.start) // we're at the start of this token and should really be at the previous192 || (prev.state.lastToken === "COMMENT-END"))) {193 cur = prev;194 cursor = {line: prev.line, ch: prev.start};195 }196 if (cur && cur.type === "comment") {197 if (cur.state.lastToken === "COMMENT-END") {198 goToOpenComment(cm, cursor, cur.state.commentNestingDepth + 1);199 return;200 } else {201 // line comment or inside block comment; do usual token thing202 }203 }204 var found = CodeMirror.findMatchingKeyword(cm, cursor);205 if (found && found.open.from.line == cursor.line && found.open.from.ch == cursor.ch) {206 if (prev) {207 prev.ch = prev.start;208 found = CodeMirror.findMatchingKeyword(cm, prev);209 }210 }211 if (found) {212 cm.extendSelection(found.open.from, found.open.from);213 } else {214 CodeMirror.commands.goBackwardToken(cm);215 }216 };217 CodeMirror.commands.goForwardSexp = function(cm) {218 var cursor = cm.getCursor();219 var cur = cm.getTokenAt(cursor);220 var next = nextNonblankTokenAfter(cm, cursor, true);221 if (next &&222 ((cursor.ch === cur.end) // we're done with this token; we should be on the next one223 || (next.start === cursor.ch && next.line === cur.line) // we're really at the start of the next one224 || (next.state.lastToken === "COMMENT-START"))) {225 cur = next; // needed because getTokenAt is left-biased226 cursor = {line: next.line, ch: next.start};227 }228 if (cur && cur.type === "comment") {229 if (cur.state.lastToken === "COMMENT-START") {230 goToCloseComment(cm, cursor, cur.state.commentNestingDepth);231 return;232 } else {233 // line comment or inside block comment; do usual token thing234 }235 }236 var found = CodeMirror.findMatchingKeyword(cm, cursor);237 if (found && found.close.to.line == cursor.line && found.close.to.ch == cursor.ch) {238 if (next) {239 next.ch = next.end;240 found = CodeMirror.findMatchingKeyword(cm, next);241 }242 }243 if (found) {244 cm.extendSelection(found.close.to, found.close.to);245 } else {246 CodeMirror.commands.goForwardToken(cm);247 }248 };249 CodeMirror.commands.goBackwardToken = function(cm) {250 var cursor = cm.getCursor();251 var cur = cm.getTokenAt(cursor);252 var pos;253 if (!cur) return;254 if (cur.type === "comment") { CodeMirror.commands.goWordLeft(cm); return; }255 if (cur.type && cur.start < cursor.ch) {256 pos = {line: cursor.line, ch: cur.start};257 } else {258 var prev = prevNonblankTokenBefore(cm, cursor);259 if (!prev) return;260 pos = {line: prev.line, ch: prev.start};261 }262 cm.extendSelection(pos, pos);263 };264 CodeMirror.commands.goForwardToken = function(cm) {265 var cursor = cm.getCursor();266 var cur = cm.getTokenAt(cursor);267 var next = nextNonblankTokenAfter(cm, cursor, true);268 var pos;269 if (next &&270 ((cursor.ch === cur.end) // we're done with this token; we should be on the next one271 || (next.start === cursor.ch && next.line === cur.line))) {272 cur = next;273 cursor = {line: next.line, ch: next.start};274 }275 if (cur) {276 if (!nonBlankToken(cur)) { cur = nextNonblankTokenAfter(cm, cursor, true); }277 else if (cur.type === "comment") {278 var index = cursor.ch - cur.start;279 for (; index < cur.string.length; index++) {280 if (cur.string[index] !== " " && cur.string[index] !== "\t"281 && (cur.string[index + 1] === " " || cur.string[index + 1] == "\t")) {282 index++;283 break;284 }285 }286 pos = {line: cursor.line, ch: cur.start + index};287 cm.extendSelection(pos, pos);288 return;289 }290 }291 if (!cur) return;292 if (cur.type && cur.end > cursor.ch) {293 pos = {line: cursor.line, ch: cur.end};294 } else {295 var next = nextNonblankTokenAfter(cm, cursor);296 if (!next) return;297 pos = {line: next.line, ch: next.end};298 }299 cm.extendSelection(pos, pos);300 };...
app.controllers.js
Source:app.controllers.js
1function TopicCtrl ($scope, $rootScope, $sce, $window, $location, $q, TransformService, TocService, SearchService) {2 that = this;3 this.conf = {};4 this.help_app_spec = {};5 this.topiccontent = "";6 this.toccontent = "";7 this.searchcontent = "";8 this.xslLoc = "xsl/topicToWeb.xsl";9 $window.onload = function(e) {10 if(undefined !== that.conf.winStr && "" !== that.conf.winStr) {11 console.log("SETTING LOCATION FOR UNDEFINED: "+that.conf.winStr);12 $window.location=that.conf.winStr;13 }14 console.log("ONLOAD!!!! "+history.length);15 that.showDiv('toc');16 console.log("BUILDING TOC...");17 that.buildToc().then(console.log("TOC BUILT!!!! "+history.length));18 console.log("LOADING SEARCH...");19 that.loadSearch().then(function(){20 console.log("ARF SEARCH BUILT!!!! "+history.length);21 //SearchService.doSearch("open", "And");22 });23 //console.log("TOC BUILT!!!! "+history.length);24 };25 $rootScope.$on('$locationChangeSuccess',function(event, inUrl ) {26 console.log("\n\nLocation Change Success Event START!!!");27 var toks = inUrl.split('?');28 if(undefined === toks || 0 === toks.length) {29 console.error("Location change but no URL to parse.");30 return;31 }32 var url = toks[0];33 var params = toks[1];34 var pObj;35 if(undefined !== params) {36 pObj = that.getParamsObj(params);37 }38 console.log("$locationChangeSuccess URL is: "+url);39 that.conf = $window.conf; // Leaving open the option to load conf from an external file40 that.showTopicFromUrl(url, pObj);41 if(undefined !== that.conf.tocUrl) {42 //console.log("USING TOC URL!!! "+that.conf.tocUrl);43 TocService.highlightNodeForDefaultTopic(that.conf.tocUrl);44 } else {45 //console.log("USING NORMAL URL!!! "+url);46 TocService.highlightNodeForDefaultTopic(url);47 }48 console.log("CHANGE SUCCESS END!!!!\n\n"+history.length);49 });50 $rootScope.$on('tocResponse', function(event, response) {51 that.conf = $window.conf; // Leaving open the option to load conf from an external file52 $location.replace();53 $window.location = response.data;54 });55 this.setRootTopicDir = function(path){56 var toks = path.split('/');57 var len = toks.length;58 for(var i = 0; i<len; i++) {59 if(toks[i] === "..") {60 that.conf.rootTopicDir = toks[i + 1];61 console.log("SETTING ROOT TOPIC DIR TO: "+that.conf.rootTopicDir);62 return;63 }64 }65 //console.log("NOT DOTDOT ROOT TOPIC URL!!!"+ path);66 toks = path.split('/');67 if(toks.length === 2){68 that.conf.rootTopicDir = toks[0];69 console.log("SETTING ROOT TOPIC DIR TO: "+that.conf.rootTopicDir);70 } else if(toks.length > 2){71 that.conf.rootTopicDir = "";72 for(var i = 0; i<toks.length - 1; i++) {73 that.conf.rootTopicDir += toks[i];74 if(i < toks.length - 2) {75 that.conf.rootTopicDir += "/";76 }77 }78 console.warn("TOKS Length: "+toks.length);79 console.warn("PATH IS: "+path);80 console.log("SETTING ROOT TOPIC DIR TO: "+that.conf.rootTopicDir);81 }else if(toks.length === 1) {82 that.conf.rootTopicDir = $window.conf.rootTopicDir;83 console.log("SETTING ROOT TOPIC DIR TO: "+$window.conf.rootTopicDir);84 }else {85 console.warn("NO VALID ROOT TOPIC DIR FOR: "+path);86 console.warn("TOKS Length: "+toks.length);87 }88 };89 this.getParamsObj = function(params) {90 toks = params.split("&");91 var paramsObj = {"BOGUS":"BOGUS"};92 for(var i=0; i<toks.length; i++) {93 subToks = toks[i].split("=");94 console.log("PARAMS SETTING -- "+subToks[0]+":"+subToks[1]);95 paramsObj[subToks[0]] = subToks[1];96 }97 return(paramsObj);98 };99 this.showDiv = function(divName) {100 //101 // TO DO: Set divs to show in the parameters102 //103 var divs = ['search', 'toc'];104 for(var x=0; x<divs.length; x++) {105 jQuery('#'+divs[x]).css('display', 'none');106 }107 jQuery('#'+divName).css('display', 'block');108 };109 this.expandToc = function() {110 TocService.expandAll();111 };112 this.collapseToc = function() {113 TocService.collapseAll();114 };115 this.goBack = function() {116 history.back();117 };118 this.goForward = function() {119 history.forward();120 };121 this.showTopic = function() {122 alert(that.topiccontent);123 };124 this.showToc = function() {125 alert(that.toccontent);126 };127 this.loadSearch = function() {128 var deferred = $q.defer();129 var searchCallback = function(response) {130 console.log("LOADING SEARCH IN CALLBACK FROM CONTROLLER!!!");131 SearchService.loadSearch(response.resp);132 SearchService.setRootDocName(that.conf.rootDocName);133 console.log("SEARCH ROOT DOC NAME IS: "+SearchService.getRootDocName());134 //SearchService.initSearch(jQuery("#searchDiv"));135 //TocService.buildMainToc(response.resp,"index.html",that.rootTopicDir);136 };137 var errorCallback = function(response) {138 console.warn("ERROR TRANSFORMING SEARCH!!!\n"+response.resp.data);139 deferred.reject("FAILURE");140 };141 //console.log("LOADING SEARCH - DATA: "+that.conf.searchFile);142 //console.log("LOADING SEARCH - TRANSFORM: "+that.conf.searchTransform);143 TransformService.initTransformChain(that.conf.searchTransform, that.conf.searchFile)144 .then(TransformService.loadXslDoc)145 .then(TransformService.getTopic)146 .then(TransformService.doTransform)147 .then(searchCallback, errorCallback)148 ;149 deferred.resolve("SUCESS");150 return deferred.promise;151 };152 this.buildToc = function() {153 var deferred = $q.defer();154 var tocCallback = function(response) {155 console.log("TOC CALLBACK RESPONSE: "+response.resp);156 TocService.initToc(jQuery("#tocresult"));157 TocService.buildMainToc(response.resp,"index.html",that.rootTopicDir);158 };159 var errorCallback = function(response) {160 jQuery.ui.fancytree.error("ERROR TRANSFORMING TOC!!!\n"+response.resp.data);161 deferred.reject("FAILURE");162 };163 console.log("BUILDING TOC FOR: "+that.conf.defaultMap);164 console.log("TRANSFORMING TOC WITH: "+that.conf.mapTransform);165 TransformService.initTransformChain(that.conf.mapTransform, that.conf.defaultMap)166 .then(TransformService.loadXslDoc)167 .then(TransformService.getTopic)168 .then(TransformService.doTransform)169 .then(tocCallback, errorCallback)170 ;171 deferred.resolve("SUCESS");172 return deferred.promise;173 };174 this.showTopicFromUrl = function(inUrl, pObj) {175 console.log("SHOWING TOPIC FROM URL: " + inUrl);176 if(undefined === inUrl || "" === inUrl) {177 return;178 }179 var toks = inUrl.split('#');180 if(undefined === toks[1] || "" === toks[1]) {181 return;182 }183 var topicPath = toks[1];184 if('/' === topicPath.charAt(0)) { // needed hack...185 topicPath = topicPath.substr(1);186 }187 if('!' === topicPath.charAt(0)) { // needed hack...188 topicPath = topicPath.substr(1);189 }190 if(topicPath.substring(0,6) === 'MAPPED') { // Look up in the list of mapped topics.191 toks = topicPath.split('&');192 topicPath = getMapEntry(toks[1]).topic;193 console.log("MAPPED PATH: "+topicPath);194 } else195 if(topicPath.substring(0,6) === 'topic=') { // Old-school xref196 toks = topicPath.split('=');197 var sub = toks[1];198 var subToks = sub.split('/');199 var subLen = subToks.length;200 //console.log("TOPIC PATH TOKS LEN: "+subLen+" for topic path: "+sub);201 if(subToks[0] === '..') {202 topicPath = that.conf.rootTopicDir+"/"+sub; // This is relative to a root topic dir203 console.log("OLDSCHOOL XREF PATH 1: "+topicPath);204 that.setRootTopicDir(topicPath);205 } else if(subToks[0] !== that.conf.rootTopicDir && subLen === 1) { // Make this relative to the root topic dir.206 topicPath = that.conf.rootTopicDir+"/"+sub;207 console.log("OLDSCHOOL XREF PATH 2: "+topicPath);208 } else if(subToks[0] !== that.conf.rootTopicDir) { // Make this relative to the root topic dir.209 topicPath = that.conf.rootTopicDir+"/"+sub;210 console.log("OLDSCHOOL XREF PATH 3: "+topicPath);211 } else {212 topicPath = sub;213 console.log("OLDSCHOOL XREF PATH 4: "+topicPath);214 }215 } else { // Normal URL216 console.log("NORMAL URL: "+topicPath);217 if(undefined !== that.conf.mapUrlPrefix && "" !== that.conf.mapUrlPrefix) {218 topicPath = that.conf.mapUrlPrefix+topicPath;219 }220 that.setRootTopicDir(topicPath);221 }222 console.log("FINAL TOPIC PATH: "+topicPath);223 that.populateTopicContent(topicPath, that.conf.topicTransform, pObj);224 };225 this.setTocUrl = function(url) {226 var toks = url.split("../");227 for(var i=0; i<toks.length; i++) {228 console.log("TOC URL TOK: "+toks[i]);229 }230 if(toks.length > 1) {231 that.conf.tocUrl = "../"+toks[1];232 } else {233 that.conf.tocUrl = "../"+url;234 }235 console.log("SETTING TOC URL: "+that.conf.tocUrl);236 };237 this.populateTopicContent = function(xmlFilePath, xslFilePath, pObj){238 console.log("populateTopicContent: TRANSFORMING FILE!!!");239 console.log("XML FILE PATH: "+xmlFilePath);240 console.log("XSL FILE PATH "+xslFilePath);241 that.setTocUrl(xmlFilePath);242 var topicCallback = function(response) {243 //alert("TOPIC: "+response.resp);244 that.topiccontent = $sce.trustAsHtml(response.resp);245 if(undefined !== pObj) {246 console.log("Location Change Success Event -- setting highlight for pObj: "+pObj);247 SearchService.setHighlightForUrl(pObj);248 }249 };250 var errorCallback = function(response) {251 console.error("populateTopicContent: ERROR TRANSFORMING FILE!!!\n"+response.resp.data);252 console.error("XML FILE PATH: "+xmlFilePath);253 console.error("XSL FILE PATH "+xslFilePath);254 };255 TransformService.initTransformChain(xslFilePath, xmlFilePath)256 .then(TransformService.loadXslDoc)257 .then(TransformService.getTopic)258 .then(TransformService.doTransform)259 .then(topicCallback, errorCallback)260 ;261 };262}263angular264 .module ('4D')...
prelude.ts
Source:prelude.ts
1import VarManager from "../manager/VarManager"2import FunToken from "../tokens/FunToken"3import NumberToken from "../tokens/NumberToken"4import BoolToken from "../tokens/BoolToken"5import arrayInit from "./arrayModule"6import stringInit from "./stringModule"7import structInit from "./structModule"8const prelude = () => {9 const stdOut = { content: "" }10 const vars = VarManager.get()11 // Imports12 vars.setVar("import", FunToken.native(toks => {13 const mod = toks[0].request("symbol")14 switch (mod) {15 case "Array":16 return arrayInit()17 case "String":18 return stringInit()19 case "Struct":20 return structInit()21 default:22 throw new Error(`unknown module ${mod}`)23 }24 }), true)25 // Constants26 vars.setVar("unit", VarManager.unit, false)27 vars.setVar("true", new BoolToken(true), false)28 vars.setVar("false", new BoolToken(false), false)29 // Conditionnals30 vars.setVar("if", FunToken.native(toks => {31 const cond = toks[0].request("bool")32 const block = toks[1]33 const other = toks[2]34 return cond ? block.calculate() : other.calculate()35 }), true)36 // Constructors37 vars.setVar("fun", FunToken.native(toks => {38 const args = toks[0].request("array")39 const block = toks[1]40 return FunToken.custom(args, block)41 }), true)42 // IO43 vars.setVar("println", FunToken.native(toks => {44 stdOut.content += toks[0].request("string") + "\n"45 return VarManager.unit46 }), true)47 vars.setVar("print", FunToken.native(toks => {48 stdOut.content += toks[0].request("string")49 return VarManager.unit50 }), true)51 vars.setVar("debug", FunToken.native(toks => {52 stdOut.content += JSON.stringify(toks[0]) + "\n"53 console.log(toks[0])54 return VarManager.unit55 }), true)56 /*vars.setVar("stack", FunToken.native(toks => {57 console.log(VarManager.get().getStack())58 return VarManager.unit59 }), true)*/60 // Defining61 vars.setVar("def", FunToken.native(toks => {62 const name = toks[0].request("symbol")63 const value = toks[1]64 vars.setVar(name, value, false)65 return value66 }), true)67 vars.setVar("deffun", FunToken.native(toks => {68 const name = toks[0].request("symbol")69 const args = toks[1].request("array")70 const block = toks[2]71 const fn = FunToken.custom(args, block)72 vars.setVar(name, fn, false)73 return fn74 }), true)75 // Numerics76 vars.setVar("+", FunToken.native(toks => {77 const x = toks[0].request("number")78 const y = toks[1].request("number")79 return new NumberToken(x + y)80 }), true)81 vars.setVar("-", FunToken.native(toks => {82 const x = toks[0].request("number")83 const y = toks[1].request("number")84 return new NumberToken(x - y)85 }), true)86 vars.setVar("*", FunToken.native(toks => {87 const x = toks[0].request("number")88 const y = toks[1].request("number")89 return new NumberToken(x * y)90 }), true)91 vars.setVar("/", FunToken.native(toks => {92 const x = toks[0].request("number")93 const y = toks[1].request("number")94 return new NumberToken(x / y)95 }), true)96 vars.setVar("// ", FunToken.native(toks => {97 const x = toks[0].request("number")98 const y = toks[1].request("number")99 return new NumberToken(Math.floor(x / y))100 }), true)101 vars.setVar("**", FunToken.native(toks => {102 const x = toks[0].request("number")103 const y = toks[1].request("number")104 return new NumberToken(x ** y)105 }), true)106 vars.setVar("%", FunToken.native(toks => {107 const x = toks[0].request("number")108 const y = toks[1].request("number")109 return new NumberToken(x % y)110 }), true)111 // Comparisons112 vars.setVar("==", FunToken.native(toks => {113 const tok = toks[0]114 const other = toks[1]115 return new BoolToken(tok.compare(other) === 0)116 }), true)117 vars.setVar("!=", FunToken.native(toks => {118 const tok = toks[0]119 const other = toks[1]120 return new BoolToken(tok.compare(other) !== 0)121 }), true)122 vars.setVar("<", FunToken.native(toks => {123 const tok = toks[0]124 const other = toks[1]125 return new BoolToken(tok.compare(other) === -1)126 }), true)127 vars.setVar(">", FunToken.native(toks => {128 const tok = toks[0]129 const other = toks[1]130 return new BoolToken(tok.compare(other) === 1)131 }), true)132 vars.setVar("<=", FunToken.native(toks => {133 const tok = toks[0]134 const other = toks[1]135 return new BoolToken(tok.compare(other) !== 1)136 }), true)137 vars.setVar(">=", FunToken.native(toks => {138 const tok = toks[0]139 const other = toks[1]140 return new BoolToken(tok.compare(other) !== -1)141 }), true)142 // Logical operation143 vars.setVar("&&", FunToken.native(toks => {144 const bool = toks[0].request("bool")145 const boolb = toks[1].request("bool")146 return new NumberToken(bool && boolb)147 }), true)148 vars.setVar("||", FunToken.native(toks => {149 const bool = toks[0].request("bool")150 const boolb = toks[1].request("bool")151 return new NumberToken(bool || boolb)152 }), true)153 // Miscs154 vars.setVar("eval", FunToken.native(toks => {155 const block = toks[0]156 return block.calculate()157 }), true)158 return stdOut159}...
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3var fs = require('fs');4var options = {5};6api.runTest(url, options, function(err, data) {7 if (err) return console.error(err);8 console.log('Test %d from %s at %s', data.data.testId, data.data.from, data.data.location);9 api.getTestResults(data.data.testId, function(err, data) {10 if (err) return console.error(err);11 console.log('First View: %s', data.data.average.firstView.TTFB);12 console.log('Repeat View: %s', data.data.average.repeatView.TTFB);13 var video = data.data.runs[1].firstView.videoFrames;14 var frames = [];15 for (var i = 0; i < video.length; i++) {16 frames.push(video[i].image);17 }18 fs.writeFile('frames.json', JSON.stringify(frames), function(err) {19 if (err) return console.error(err);20 console.log('Frames written to file.');21 });22 });23});
Using AI Code Generation
1const wptools = require('wptools');2var fs = require('fs');3var json2csv = require('json2csv');4var fields = ['title', 'abstract', 'categories', 'links', 'images', 'infobox', 'coordinates', 'text', 'sections', 'references', 'langlinks'];5var csv = json2csv({ data: '', fields: fields });6var csv2 = json2csv({ data: '', fields: fields });7var csv3 = json2csv({ data: '', fields: fields });8var csv4 = json2csv({ data: '', fields: fields });9var csv5 = json2csv({ data: '', fields: fields });10var csv6 = json2csv({ data: '', fields: fields });11var csv7 = json2csv({ data: '', fields: fields });12var csv8 = json2csv({ data: '', fields: fields });13var csv9 = json2csv({ data: '', fields: fields });14var csv10 = json2csv({ data: '', fields: fields });15var csv11 = json2csv({ data: '', fields: fields });16var csv12 = json2csv({ data: '', fields: fields });17var csv13 = json2csv({ data: '', fields: fields });18var csv14 = json2csv({ data: '', fields: fields });19var csv15 = json2csv({ data: '', fields: fields });20var csv16 = json2csv({ data: '', fields: fields });21var csv17 = json2csv({ data: '', fields: fields });22var csv18 = json2csv({ data: '', fields: fields });23var csv19 = json2csv({ data: '', fields: fields });24var csv20 = json2csv({ data: '', fields: fields });25var csv21 = json2csv({ data: '', fields: fields });26var csv22 = json2csv({ data: '', fields: fields });27var csv23 = json2csv({ data: '', fields: fields });28var csv24 = json2csv({ data: '', fields: fields });29var csv25 = json2csv({ data: '', fields: fields });30var csv26 = json2csv({ data: '', fields: fields });31var csv27 = json2csv({ data: '', fields: fields });32var csv28 = json2csv({ data: '', fields: fields });33var csv29 = json2csv({ data: '',
Using AI Code Generation
1var wptools = require('wptools');2wptools.toks({page:'Barack Obama', lang:'en'}, function(err, resp) {3 console.log(resp);4});5var wptools = require('wptools');6wptools.toks({page:'Barack Obama', lang:'en'}, function(err, resp) {7 console.log(resp);8});9var wptools = require('wptools');10wptools.toks({page:'Barack Obama', lang:'en'}, function(err, resp) {11 console.log(resp);12});13var wptools = require('wptools');14wptools.toks({page:'Barack
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!!