Best JavaScript code snippet using apimocker
regexpjs.js
Source:regexpjs.js
1// ==++==2// 3// 4// Copyright (c) 2006 Microsoft Corporation. All rights reserved.5// 6// The use and distribution terms for this software are contained in the file7// named license.txt, which can be found in the root of this distribution.8// By using this software in any fashion, you are agreeing to be bound by the9// terms of this license.10// 11// You must not remove this notice, or any other, from this software.12// 13// 14// ==--==15/////////////////////////////////////////////////////////////////////////16import System17function WriteLine(str)18{19 print(str);20}21//*****************************************************************************22function Right(str, len)23{24 return str.substr(str.length - len);25}26//*****************************************************************************27function RegExpTest(regExp, srcStr, replStr, count, matchExp, searchExp, replExp)28{29 var good = true;30 if (count != null || matchExp != null) {31 var matches = srcStr.match(regExp);32 var matchCount = matches == null ? 0 : matches.length;33 if (count != null && matchCount != count) {34 if (good) {35 WriteLine(regExp + ":");36 good = false;37 }38 WriteLine("Count=" + matchCount);39 }40 if (matchExp != null) {41 var matchRes = matches.toString();42 if (matchRes != matchExp) {43 if (good) {44 WriteLine(regExp + ":");45 good = false;46 }47 WriteLine(matchRes);48 }49 }50 }51 if (searchExp != null) {52 var searchRes = srcStr.search(regExp);53 if (searchRes != searchExp) {54 if (good) {55 WriteLine(regExp + ":");56 good = false;57 }58 WriteLine("Search=" + searchRes);59 }60 }61 if (replExp != null) {62 var replRes = srcStr.replace(regExp, replStr);63 if (replRes != replExp) {64 if (good) {65 WriteLine(regExp + ":");66 good = false;67 }68 WriteLine("{" + replRes + "}");69 }70 }71}72//*****************************************************************************73function RegExpTest1()74{75 WriteLine("***** RegExp Test #1: Basic Functionality *****");76 var regExp = /is/ig;77 var msg = regExp.toString();78 if (msg != "/is/ig")79 WriteLine("RegExp=" + msg);80 RegExpTest(regExp, "THIS is it", "***",81 2, "IS,is", 2, "TH*** *** it");82 RegExpTest(regExp, "THAT was it", "***",83 0, null, -1, "THAT was it");84}85//*****************************************************************************86function RegExpTest2()87{88 WriteLine("***** RegExp Test #2: Long Source Strings *****");89 var str1 = "fix fox ";90 var str2 = "fox fix ";91 for (var i = 0; i < 10; i++) {92 str1 += str1;93 str2 += str2;94 }95 var matches = str1.match(/fox/g);96 var matchCount = matches == null ? 0 : matches.length;97 if (matchCount != 1024)98 WriteLine("Count=" + matchCount);99 str1 = str1.replace(/fox/g, "temp");100 str1 = str1.replace(/fix/g, "fox");101 str1 = str1.replace(/temp/g, "fix");102 if (str1 != str2)103 WriteLine("MISMATCH");104}105//*****************************************************************************106function RegExpTest3()107{108 WriteLine("***** RegExp Test #3: IgnoreCase and Global Flags *****");109 var src = "xXxX";110 var repl = "^";111 RegExpTest(/X/, src, repl, 1, "X", 1, "x^xX");112 RegExpTest(/X/g, src, repl, 2, "X,X", 1, "x^x^");113 RegExpTest(/X/i, src, repl, 1, "x", 0, "^XxX");114 RegExpTest(/X/ig, src, repl, 4, "x,X,x,X", 0, "^^^^");115}116//*****************************************************************************117function RegExpTest4()118{119 WriteLine("***** RegExp Test #4: Beginning, End, and Newline *****");120 var src = "foo\nbar";121 var repl = "X";122 RegExpTest(/\n/g, src, repl, 1, null, null, "fooXbar");123 RegExpTest(/^/g, src, repl, 1, null, null, "X" + src);124 RegExpTest(/$/g, src, repl, 1, null, null, src + "X");125}126//*****************************************************************************127function RegExpTest5()128{129 WriteLine("***** RegExp Test #5: Shorthands and Ranges *****");130 var src = "ABCabc:: :::01:::: :::234: ::De5 d5E:";131 var repl = "[$1]";132 RegExpTest(/(\w+)/ig, src, repl, 5, null, null,133 "[ABCabc]:: :::[01]:::: :::[234]: ::[De5] [d5E]:");134 RegExpTest(/(\W+)/ig, src, repl, 5, null, null,135 "ABCabc[:: :::]01[:::: :::]234[: ::]De5[ ]d5E[:]");136 RegExpTest(/(\d+)/ig, src, repl, 4, null, null,137 "ABCabc:: :::[01]:::: :::[234]: ::De[5] d[5]E:");138 RegExpTest(/(\D+)/ig, src, repl, 5, null, null,139 "[ABCabc:: :::]01[:::: :::]234[: ::De]5[ d]5[E:]");140 RegExpTest(/(\s+)/ig, src, repl, 4, null, null,141 "ABCabc::[ ]:::01::::[ ]:::234:[ ]::De5[ ]d5E:");142 RegExpTest(/(\S+)/ig, src, repl, 5, null, null,143 "[ABCabc::] [:::01::::] [:::234:] [::De5] [d5E:]");144 RegExpTest(/\b/ig, src, "^", 10, null, null,145 "^ABCabc^:: :::^01^:::: :::^234^: ::^De5^ ^d5E^:");146 RegExpTest(/\B/ig, src, "^", 34, null, null,147 "A^B^C^a^b^c:^:^ ^:^:^:0^1:^:^:^:^ ^ ^:^:^:2^3^4:^ ^ ^ ^:^:D^e^5 ^ ^ ^ d^5^E:^");148 RegExpTest(/([A-Z]+)/g, src, repl, 3, null, null,149 "[ABC]abc:: :::01:::: :::234: ::[D]e5 d5[E]:");150 RegExpTest(/([^A-Z]+)/g, src, repl, 3, null, null,151 "ABC[abc:: :::01:::: :::234: ::]D[e5 d5]E[:]");152 RegExpTest(/([a-z]+)/g, src, repl, 3, null, null,153 "ABC[abc]:: :::01:::: :::234: ::D[e]5 [d]5E:");154 RegExpTest(/([^a-z]+)/g, src, repl, 4, null, null,155 "[ABC]abc[:: :::01:::: :::234: ::D]e[5 ]d[5E:]");156}157//*****************************************************************************158function RegExpTest6()159{160 WriteLine("***** RegExp Test #6: ASCII Escape Values *****");161 var src = "";162 for (var i = 0; i < 128; i++)163 src += String.fromCharCode(i);164 for (i = 0; i < 4; i++)165 src += src;166 for (i = 0; i < 128; i++) {167 var patrn1 = "00" + i.toString(8);168 patrn1 = "\\" + Right(patrn1, 3);169 var regExp = new RegExp(patrn1, "g");170 var matches = src.match(regExp);171 if (matches == null) {172 var count1 = 0;173 var msg1 = "0 {}";174 } else {175 count1 = matches.length;176 msg1 = matches.toString();177 }178 var patrn2 = "0" + i.toString(16);179 patrn2 = "\\x" + Right(patrn2, 2);180 regExp = new RegExp(patrn2, "g");181 matches = src.match(regExp);182 if (matches == null) {183 var count2 = 0;184 var msg2 = "0 {}";185 } else {186 count2 = matches.length;187 msg2 = matches.toString();188 }189 var patrn3 = "000" + i.toString(16);190 patrn3 = "\\u" + Right(patrn3, 4);191 regExp = new RegExp(patrn3, "g");192 matches = src.match(regExp);193 if (matches == null) {194 var count3 = 0;195 var msg3 = "0 {}";196 } else {197 count3 = matches.length;198 msg3 = matches.toString();199 }200 if (count1 != 16 || count2 != 16 || count3 != 16201 || msg1 != msg2 || msg1 != msg3) {202 WriteLine(patrn1 + ": " + count1);203 WriteLine(patrn2 + ": " + count2);204 WriteLine(patrn3 + ": " + count3);205 }206 }207 var chs = ["b","B","d","D","f","n","r","s","S","t","v","w","W"];208 var cts = [128,1921,160,1888,16,16,16,96,1952,16,16,1008,1040];209 for (i = 0; i < 13; i++) {210 patrn1 = "\\" + chs[i];211 regExp = new RegExp(patrn1, "g");212 matches = src.match(regExp);213 count1 = matches == null ? 0 : matches.length;214 if (count1 != cts[i])215 WriteLine(patrn1 + ": " + count1);216 }217 for (i = 0; i < 26; i++) {218 patrn1 = "\\c" + String.fromCharCode(i+65);219 regExp = new RegExp(patrn1, "g");220 matches = src.match(regExp);221 if (matches == null) {222 count1 = 0;223 msg1 = "0 {}";224 } else {225 count1 = matches.length;226 msg1 = matches.toString();227 }228 patrn2 = "\\c" + String.fromCharCode(i+97);229 regExp = new RegExp(patrn2, "g");230 matches = src.match(regExp);231 if (matches == null) {232 count2 = 0;233 msg2 = "0 {}";234 } else {235 count2 = matches.length;236 msg2 = matches.toString();237 }238 if (count1 != 16 || count2 != 16 || msg1 != msg2) {239 WriteLine(patrn1 + ": " + count1);240 WriteLine(patrn2 + ": " + count2);241 }242 }243}244//*****************************************************************************245function RegExpTest7()246{247 WriteLine("***** RegExp Test #7: Bar *****");248 RegExpTest(/([A-Z]+)|([a-z]+)|(\d+)/g,249 "The 61 QUICK brOwn f0x",250 "[$1$2$3]", 10, null, null,251 "[T][he] [61] [QUICK] [br][O][wn] [f][0][x]")252}253//*****************************************************************************254function RepeatTest(src, min, max, exp)255{256 if (min > 0) {257 var regExp = new RegExp("\\b\\w{" + min + "}\\b", "g");258 var matches = src.match(regExp);259 var msg = matches == null ? 0 : matches.length;260 } else261 msg = "-";262 regExp = new RegExp("\\b\\w{" + min + ",}\\b", "g");263 matches = src.match(regExp);264 msg += "," + (matches == null ? 0 : matches.length);265 for (var i = min; i <= max; i++) {266 regExp = new RegExp("\\b\\w{" + min + "," + i + "}\\b", "g");267 matches = src.match(regExp);268 msg += "," + (matches == null ? 0 : matches.length);269 }270 if (msg != exp)271 WriteLine("(" + min + "," + max + "): " + msg);272}273function CompareTest(patrn1, patrn2, src)274{275 var regExp = new RegExp(patrn1, "g");276 var matches = src.match(regExp);277 var msg1 = matches == null ? "0 {}" : matches.toString();278 var msg2 = src.replace(regExp, "[$1]");279 regExp = new RegExp(patrn2, "g");280 matches = src.match(regExp);281 var msg3 = matches == null ? "0 {}" : matches.toString();282 var msg4 = src.replace(regExp, "[$1]");283 if (msg1 != msg3) {284 WriteLine(patrn1 + ": " + msg1);285 WriteLine(patrn2 + ": " + msg3);286 System.Environment.ExitCode = 1;287 }288 if (msg2 != msg4) {289 WriteLine(patrn1 + ": " + msg2);290 WriteLine(patrn2 + ": " + msg4);291 System.Environment.ExitCode = 1;292 }293}294function RegExpTest8()295{296 WriteLine("***** RegExp Test #8: Repetition *****");297 var src = "";298 for (var i = 1; i <= 15; i++) {299 var ch = i.toString(16);300 for (var j = 0; j < i; j++)301 src += ch;302 src += " ";303 }304 RepeatTest(src, 0, 16, "-,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30");305 RepeatTest(src, 1, 16, "1,15,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15");306 RepeatTest(src, 2, 16, "1,14,1,2,3,4,5,6,7,8,9,10,11,12,13,14,14");307 RepeatTest(src, 3, 16, "1,13,1,2,3,4,5,6,7,8,9,10,11,12,13,13");308 RepeatTest(src, 4, 16, "1,12,1,2,3,4,5,6,7,8,9,10,11,12,12");309 RepeatTest(src, 5, 16, "1,11,1,2,3,4,5,6,7,8,9,10,11,11");310 RepeatTest(src, 6, 16, "1,10,1,2,3,4,5,6,7,8,9,10,10");311 RepeatTest(src, 7, 16, "1,9,1,2,3,4,5,6,7,8,9,9");312 RepeatTest(src, 8, 16, "1,8,1,2,3,4,5,6,7,8,8");313 RepeatTest(src, 9, 16, "1,7,1,2,3,4,5,6,7,7");314 RepeatTest(src, 10, 16, "1,6,1,2,3,4,5,6,6");315 RepeatTest(src, 11, 16, "1,5,1,2,3,4,5,5");316 RepeatTest(src, 12, 16, "1,4,1,2,3,4,4");317 RepeatTest(src, 13, 16, "1,3,1,2,3,3");318 RepeatTest(src, 14, 16, "1,2,1,2,2");319 RepeatTest(src, 15, 16, "1,1,1,1");320 RepeatTest(src, 16, 16, "0,0,0");321 CompareTest("(\\b\\w{0,1}\\b)", "(\\b\\w?\\b)", src);322 CompareTest("(\\b\\w{0,}\\b)", "(\\b\\w*\\b)", src);323 CompareTest("(\\b\\w{1,}\\b)", "(\\b\\w+\\b)", src);324}325//*****************************************************************************326function RegExpTest9()327{328 WriteLine("***** RegExp Test #9: Backreferences *****");329 var f1 = 1;330 var f2 = 1;331 var src = "11";332 for (var i = 2; i <= 40; i++) {333 var f = f1 + f2;334 f2 = f1;335 f1 = f;336 src += f.toString(2);337 }338 RegExpTest(/(.)\1/g, src, null, 191, null, null, null);339 RegExpTest(/(.{2})\1/g, src, null, 69, null, null, null);340 RegExpTest(/(.{3})\1/g, src, null, 34, null, null, null);341 RegExpTest(/(.{4})\1/g, src, null, 16, null, null, null);342 RegExpTest(/(.{5})\1/g, src, null, 8, null, null, null);343 RegExpTest(/(.{6})\1/g, src, null, 5, null, null, null);344 RegExpTest(/(.{7})\1/g, src, null, 2, null, null, null);345 RegExpTest(/(.{8})\1/g, src, null, 1, null, null, null);346 RegExpTest(/(.{9})\1/g, src, null, 1, null, null, null);347 RegExpTest(/(.{10})\1/g, src, null, 0, null, null, null);348 RegExpTest(/(.+)\1\1/g, src, null, 83, null, null, null);349 RegExpTest(/(.+)(.+)\2\1/g, src, null, 62, null, null, null); 350}351//*****************************************************************************352System.Environment.ExitCode = 0;353RegExpTest1();354RegExpTest2();355RegExpTest3();356RegExpTest4();357RegExpTest5();358RegExpTest6();359RegExpTest7();360RegExpTest8();...
escape.js
Source:escape.js
1var elTest = /[&<]/;2var elTestReplace = /[&<]/g;3var attrTest = /[&<"\n]/;4var attrReplace = /[&<"\n]/g;5var replacements = {6 "<": "<",7 "&": "&",8 '"': """,9 "'": "'",10 "\n": " " //Preserve new lines so that they don't get normalized as space11};12function replaceChar(match) {13 return replacements[match];14}15function escapeString(str, regexpTest, regexpReplace) {16 return regexpTest.test(str) ? str.replace(regexpReplace, replaceChar) : str;17}18function escapeXmlHelper(value, regexpTest, regexpReplace) {19 // check for most common case first20 if (typeof value === "string") {21 return escapeString(value, regexpTest, regexpReplace);22 } else if (value == null) {23 return "";24 } else if (typeof value === "object") {25 if (value.toHTML) {26 return value.toHTML();27 }28 } else if (value === true || value === false || typeof value === "number") {29 return value.toString();30 }31 return escapeString(value.toString(), regexpTest, regexpReplace);32}33function escapeXml(value) {34 return escapeXmlHelper(value, elTest, elTestReplace);35}36function escapeXmlAttr(value) {37 return escapeXmlHelper(value, attrTest, attrReplace);38}39exports.escapeString = escapeString;40exports.escapeXml = escapeXml;...
Using AI Code Generation
1var apimocker = require('apimocker');2var path = require('path');3var fs = require('fs');4var http = require('http');5var express = require('express');6var app = express();7var bodyParser = require('body-parser');8var url = require('url');9var querystring = require('querystring');10var request = require('request');11var server = http.createServer(app);12var port = 8080;13var mock = apimocker.middleware(mockPath);14app.use(mock);15app.use(bodyParser.json());16app.use(bodyParser.urlencoded({ extended: true }));17app.use(express.static(__dirname + '/public'));18app.listen(port);19console.log("server started at port " + port);20app.get('/test', function (req, res) {21 var url_parts = url.parse(req.url, true);22 var query = url_parts.query;23 var name = query.name;24 var data = fs.readFileSync('C:/Users/IBM_ADMIN/Desktop/apimocker-master/mock/test.json', 'utf8');25 var obj = JSON.parse(data);26 var test = obj.test;27 var i = 0;28 var len = test.length;29 for (i = 0; i < len; i++) {30 var testname = test[i].name;31 var testurl = test[i].url;32 var testmethod = test[i].method;33 var teststatus = test[i].status;34 var testresponse = test[i].response;35 var testrequest = test[i].request;36 if (name == testname) {37 var url = testurl;38 var method = testmethod;39 var status = teststatus;40 var response = testresponse;41 var request = testrequest;
Using AI Code Generation
1var apimocker = require('apimocker');2apimocker.regexpTest('/api/v1/employee/([0-9]+)', '/api/v1/employee/1234');3apimocker.regexpTest('/api/v1/employee/([0-9]+)', '/api/v1/employee/abc');4apimocker.regexpTest('/api/v1/employee/([0-9]+)', '/api/v1/employee/1234/abc');5### apimocker.getMockDataForRequest( url, method )6var apimocker = require('apimocker');7var mockData = apimocker.getMockDataForRequest('/api/v1/employee/1234', 'GET');8console.log(mockData);9### apimocker.getMockDataForRequest( url, method, headers )10var apimocker = require('apimocker');11var mockData = apimocker.getMockDataForRequest('/api/v1/employee/1234', 'GET', { 'Accept': 'application/json' });12console.log(mockData);13### apimocker.getMockDataForRequest( url, method, headers, body )14var apimocker = require('apimocker');15var mockData = apimocker.getMockDataForRequest('/api/v1/employee/1234', 'GET', { 'Accept': 'application/json' }, '{"firstName":"John","lastName":"Doe"}');16console.log(mockData);
Using AI Code Generation
1var apimocker = require('apimocker');2apimocker.setConfigFile('./config.json');3apimocker.loadMockFiles();4apimocker.regexpTest('/api/v1/users/\\d+', '/api/v1/users/12345');5apimocker.regexpTest('/api/v1/users/\\d+', '/api/v1/users/12345/');6apimocker.regexpTest('/api/v1/users/\\d+', '/api/v1/users/12345/67890');7apimocker.regexpTest('/api/v1/users/\\d+', '/api/v1/users/12345?name=John');8apimocker.regexpTest('/api/v1/users/\\d+', '/api/v1/users/12345?name=John&age=25');9### apimocker.addMockFile(filePath, callback)10var apimocker = require('apimocker');11apimocker.setConfigFile('./config.json');12apimocker.loadMockFiles();13apimocker.addMockFile('./mocks/mock1.json', function(err) {14 if (err) {15 console.log('Error adding mock file');16 } else {17 console.log('Mock file added successfully');18 }19});20### apimocker.removeMockFile(filePath, callback)21var apimocker = require('apimocker');22apimocker.setConfigFile('./config.json');23apimocker.loadMockFiles();24apimocker.removeMockFile('./mocks/mock1.json', function(err) {25 if (err) {26 console.log('Error removing mock file');27 } else {28 console.log('Mock file removed successfully');29 }30});
Using AI Code Generation
1var apimocker = require('apimocker');2var assert = require('assert');3describe('test', function () {4 it('should test the api', function () {5 var req = {6 headers: {7 }8 };9 var res = {10 status: function (code) {11 assert.equal(code, 200);12 return this;13 },14 json: function (data) {15 assert.equal(data.employeeId, 123);16 }17 };18 apimocker.regexpTest(req, res);19 });20});
Using AI Code Generation
1var apimocker = require('apimocker');2apimocker.regexpTest('/api/.*', '/api/123', function (err, result) {3 console.log(result);4});5 var apimocker = require('apimocker');6 gulp.task('apimocker', function () {7 apimocker.run({
Using AI Code Generation
1var apimocker = require('apimocker');2var options = {3};4apimocker.loadMockFilesIntoOptions(options);5apimocker.createServer(options).listen(options.port, options.host, function () {6 console.log('Press Ctrl+C to stop');7});8var server = apimocker.createServer(options);9server.listen(options.port, options.host, function () {10 console.log('Press Ctrl+C to stop');11});12server.on('request', function (req, res) {13 console.log('Request received: ' + req.url);14});15server.on('response', function (req, res) {16 console.log('Response sent: ' + req.url);17});18server.on('error', function (err) {19 console.log('Error: ' + err);20});21server.on('close', function () {22 console.log('Server closed');23});24server.close();25var regexpTest = function (regexp, url) {26 var matches = regexp.exec(url);27 if (matches && matches.length > 1) {28 return matches[1];29 }30 return null;31};32var test = regexpTest(/\/api\/test\/(.*)/, '/api/test/123');33console.log(test);
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!!