Best JavaScript code snippet using mountebank
comed-rrtp.js
Source:comed-rrtp.js
...24 var hourRegEx = /\[Date.UTC\(\d{4},\d{1,2},\d{1,2},(\d{1,2})/g;25 var minRegEx = /\[Date.UTC\(\d{4},\d{1,2},\d{1,2},\d{1,2},(\d{1,2})/g;26 var secRegEx = /\[Date.UTC\(\d{4},\d{1,2},\d{1,2},\d{1,2},\d{1,2},(\d{1,2})\)/g;27 var priceRegEx = /\[Date.UTC\(\d{4},\d{1,2},\d{1,2},\d{1,2},\d{1,2},\d{1,2}\),\s(-?\d{1,4}.\d{1,4})\]/g;28 function getMatches(string, regex, index) {29 index || (index = 1); // default to the first capturing group30 var matches = [];31 var match;32 while (match = regex.exec(string)) {33 matches.push(match[index]);34 }35 return matches;36 }37 function TomPredPrices(n) {38 RED.nodes.createNode(this, n);39 var node = this;40 this.name = n.name;41 const options = {42 uri: 'https://hourlypricing.comed.com/rrtp/ServletFeed?type=daynexttomorrow',43 json: true44 };45 node.on("input", function (msg) {46 var outmsg = {47 topic: msg.topic48 };49 request(options, function (error, response, body) {50 if (!error && response.statusCode == 200) {51 var matches, year = [], month = [], day = [], hour = [], min = [], sec = [], price = [];52 // Get an array containing the year53 matches = getMatches(body, yearRegEx, 1);54 for (var i = 0; i < matches.length; i++) {55 year[i] = parseInt(matches[i]);56 }57 // Get an array containing the month58 matches = getMatches(body, monthRegEx, 1);59 for (var i = 0; i < matches.length; i++) {60 month[i] = parseInt(matches[i]);61 }62 // Get an array containing the day63 matches = getMatches(body, dayRegEx, 1);64 for (var i = 0; i < matches.length; i++) {65 day[i] = parseInt(matches[i]);66 }67 // Get an array containing the hour68 matches = getMatches(body, hourRegEx, 1);69 for (var i = 0; i < matches.length; i++) {70 hour[i] = parseInt(matches[i]);71 }72 // Get an array containing the minute73 matches = getMatches(body, minRegEx, 1);74 for (var i = 0; i < matches.length; i++) {75 min[i] = parseInt(matches[i]);76 }77 // Get an array containing the second78 matches = getMatches(body, secRegEx, 1);79 for (var i = 0; i < matches.length; i++) {80 sec[i] = parseInt(matches[i]);81 }82 // Get an array containing the price83 matches = getMatches(body, priceRegEx, 1);84 for (var i = 0; i < matches.length; i++) {85 price[i] = matches[i];86 }87 outmsg.payload = [];88 for (var i = 0; i < matches.length; i++) {89 outmsg.payload[i] = {90 year: parseInt(year[i]),91 month: parseInt(month[i]) + 1, //January is 0!92 day: parseInt(day[i]),93 hour: parseInt(hour[i]),94 minute: parseInt(min[i]),95 second: parseInt(sec[i]),96 tom_pred_hourly_price: parseFloat(price[i])97 };98 }99 node.send(outmsg);100 }101 })102 });103 }104 RED.nodes.registerType("TomPredPrices", TomPredPrices);105 function PredPrices(n) {106 RED.nodes.createNode(this, n);107 var node = this;108 this.name = n.name;109 node.on("input", function (msg) {110 var outmsg = {111 topic: msg.topic112 };113 var today = new Date();114 var dd = today.getDate();115 var mm = today.getMonth() + 1; //January is 0!116 var yyyy = today.getFullYear();117 if (dd < 10) {118 dd = '0' + dd;119 }120 if (mm < 10) {121 mm = '0' + mm;122 }123 var date = yyyy.toString() + mm.toString() + dd.toString();124 /*console.log('year: '+yyyy); 125 console.log('month: '+mm);126 console.log('day: '+ dd);127 console.log('date: ' +date);*/128 const options = {129 uri: 'https://hourlypricing.comed.com/rrtp/ServletFeed?type=daynexttoday&date=' + date,130 json: true131 };132 request(options, function (error, response, body) {133 if (!error && response.statusCode == 200) {134 var matches, year = [], month = [], day = [], hour = [], min = [], sec = [], price = [];135 // Get an array containing the year136 matches = getMatches(body, yearRegEx, 1);137 for (var i = 0; i < matches.length; i++) {138 year[i] = parseInt(matches[i]);139 }140 // Get an array containing the month141 matches = getMatches(body, monthRegEx, 1);142 for (var i = 0; i < matches.length; i++) {143 month[i] = parseInt(matches[i]);144 }145 // Get an array containing the day146 matches = getMatches(body, dayRegEx, 1);147 for (var i = 0; i < matches.length; i++) {148 day[i] = parseInt(matches[i]);149 }150 // Get an array containing the hour151 matches = getMatches(body, hourRegEx, 1);152 for (var i = 0; i < matches.length; i++) {153 hour[i] = parseInt(matches[i]);154 }155 // Get an array containing the minute156 matches = getMatches(body, minRegEx, 1);157 for (var i = 0; i < matches.length; i++) {158 min[i] = parseInt(matches[i]);159 }160 // Get an array containing the second161 matches = getMatches(body, secRegEx, 1);162 for (var i = 0; i < matches.length; i++) {163 sec[i] = parseInt(matches[i]);164 }165 // Get an array containing the price166 matches = getMatches(body, priceRegEx, 1);167 for (var i = 0; i < matches.length; i++) {168 price[i] = matches[i];169 }170 outmsg.payload = [];171 for (var i = 0; i < matches.length; i++) {172 outmsg.payload[i] = {173 year: parseInt(year[i]),174 month: parseInt(month[i]) + 1, //January is 0!175 day: parseInt(day[i]),176 hour: parseInt(hour[i]),177 minute: parseInt(min[i]),178 second: parseInt(sec[i]),179 predicted_hourly_price: parseFloat(price[i])180 };181 }182 node.send(outmsg);183 }184 })185 });186 }187 RED.nodes.registerType("PredPrices", PredPrices);188 function CurntPredPrice(n) {189 RED.nodes.createNode(this, n);190 var node = this;191 this.name = n.name;192 node.on("input", function (msg) {193 var outmsg = {194 topic: msg.topic195 };196 let today = new Date();197 let dd = today.getDate();198 let mm = today.getMonth() + 1; //January is 0!199 let yyyy = today.getFullYear();200 if (dd < 10) {201 dd = '0' + dd;202 }203 if (mm < 10) {204 mm = '0' + mm;205 }206 var date = yyyy.toString() + mm.toString() + dd.toString();207 const options = {208 uri: 'https://hourlypricing.comed.com/rrtp/ServletFeed?type=daynexttoday&date=' + date,209 json: true210 };211 request(options, function (error, response, body) {212 if (!error && response.statusCode == 200) {213 var d = new Date();214 var currhour = d.getHours();215 var matches, year = [], month = [], day = [], hour = [], min = [], sec = [], price = [], tmp = [];216 // Get an array containing the year217 matches = getMatches(body, yearRegEx, 1);218 for (var i = 0; i < matches.length; i++) {219 year[i] = parseInt(matches[i]);220 }221 // Get an array containing the month222 matches = getMatches(body, monthRegEx, 1);223 for (var i = 0; i < matches.length; i++) {224 month[i] = parseInt(matches[i]);225 }226 // Get an array containing the day227 matches = getMatches(body, dayRegEx, 1);228 for (var i = 0; i < matches.length; i++) {229 day[i] = parseInt(matches[i]);230 }231 // Get an array containing the hour232 matches = getMatches(body, hourRegEx, 1);233 for (var i = 0; i < matches.length; i++) {234 hour[i] = parseInt(matches[i]);235 }236 // Get an array containing the minute237 matches = getMatches(body, minRegEx, 1);238 for (var i = 0; i < matches.length; i++) {239 min[i] = parseInt(matches[i]);240 }241 // Get an array containing the second242 matches = getMatches(body, secRegEx, 1);243 for (var i = 0; i < matches.length; i++) {244 sec[i] = parseInt(matches[i]);245 }246 // Get an array containing the price247 matches = getMatches(body, priceRegEx, 1);248 for (var i = 0; i < matches.length; i++) {249 price[i] = matches[i];250 }251 tmp.payload = [];252 for (var i = 0; i < matches.length; i++) {253 tmp.payload[i] = {254 year: parseInt(year[i]),255 month: parseInt(month[i]) + 1, //January is 0!256 day: parseInt(day[i]),257 hour: parseInt(hour[i]) + 1, //12 am is 0!258 minute: parseInt(min[i]),259 second: parseInt(sec[i]),260 predicted_hourly_price: parseFloat(price[i])261 };262 }263 outmsg.payload = tmp.payload[currhour];264 node.send(outmsg);265 }266 })267 });268 }269 RED.nodes.registerType("CurntPredPrice", CurntPredPrice);270 function CurntPrices(n) {271 RED.nodes.createNode(this, n);272 var node = this;273 this.name = n.name;274 node.on("input", function (msg) {275 var outmsg = {276 topic: msg.topic277 };278 var today = new Date();279 var dd = today.getDate();280 var mm = today.getMonth() + 1; //January is 0!281 var yyyy = today.getFullYear();282 if (dd < 10) {283 dd = '0' + dd;284 }285 if (mm < 10) {286 mm = '0' + mm;287 }288 var date = yyyy.toString() + mm.toString() + dd.toString();289 var options = {290 uri: 'https://hourlypricing.comed.com/rrtp/ServletFeed?type=day&date=' + date,291 json: true292 };293 request(options, function (error, response, body) {294 if (!error && response.statusCode == 200) {295 var matches, year = [], month = [], day = [], hour = [], min = [], sec = [], price = [];296 // Get an array containing the year297 matches = getMatches(body, yearRegEx, 1);298 for (var i = 0; i < matches.length; i++) {299 year[i] = parseInt(matches[i]);300 }301 // Get an array containing the month302 matches = getMatches(body, monthRegEx, 1);303 for (var i = 0; i < matches.length; i++) {304 month[i] = parseInt(matches[i]);305 }306 // Get an array containing the day307 matches = getMatches(body, dayRegEx, 1);308 for (var i = 0; i < matches.length; i++) {309 day[i] = parseInt(matches[i]);310 }311 // Get an array containing the hour312 matches = getMatches(body, hourRegEx, 1);313 for (var i = 0; i < matches.length; i++) {314 hour[i] = parseInt(matches[i]);315 }316 // Get an array containing the minute317 matches = getMatches(body, minRegEx, 1);318 for (var i = 0; i < matches.length; i++) {319 min[i] = parseInt(matches[i]);320 }321 // Get an array containing the second322 matches = getMatches(body, secRegEx, 1);323 for (var i = 0; i < matches.length; i++) {324 sec[i] = parseInt(matches[i]);325 }326 // Get an array containing the price327 matches = getMatches(body, priceRegEx, 1);328 for (var i = 0; i < matches.length; i++) {329 price[i] = matches[i];330 }331 outmsg.payload = [];332 for (var i = 0; i < matches.length; i++) {333 outmsg.payload[i] = {334 year: parseInt(year[i]),335 month: parseInt(month[i]) + 1, //January is 0!336 day: parseInt(day[i]),337 hour: parseInt(hour[i]),338 minute: parseInt(min[i]),339 second: parseInt(sec[i]),340 hourly_price: parseFloat(price[i])341 };342 }343 node.send(outmsg);344 }345 })346 });347 }348 RED.nodes.registerType("CurntPrices", CurntPrices);349 function CurntPrice(n) {350 RED.nodes.createNode(this, n);351 var node = this;352 this.name = n.name;353 const options = {354 uri: 'https://hourlypricing.comed.com/rrtp/ServletFeed?type=instanthourly',355 json: true356 };357 node.on("input", function (msg) {358 var outmsg = {359 topic: msg.topic360 };361 request(options, function (error, response, body) {362 if (!error && response.statusCode == 200) {363 var priceRegEx = /(-?\d{1,4}.\d{1,4})/g;364 var timeRegEx = /(\d{1,2}-\d{1,2}\s\w{1,2}\s\w{1,3})/g;365 var $ = cheerio.load(body);366 var list = $('p').map(function () {367 return $(this).text();368 }).get();369 var price = getMatches(list[0], priceRegEx, 1);370 var time = getMatches(list[1], timeRegEx, 1);371 var ftime = time[0];372 outmsg.payload = {373 price: parseFloat(price),374 time: ftime,375 };376 }377 node.send(outmsg);378 })379 });380 }381 RED.nodes.registerType("CurntPrice", CurntPrice);382 function CurntHrAvgPrice(n) {383 RED.nodes.createNode(this, n);384 var node = this;385 this.name = n.name;386 const options = {387 uri: 'https://hourlypricing.comed.com/rrtp/ServletFeed?type=instant',388 json: true389 };390 node.on("input", function (msg) {391 var outmsg = {392 topic: msg.topic393 };394 request(options, function (error, response, body) {395 if (!error && response.statusCode == 200) {396 var priceRegEx = /(-?\d{1,4}.\d{1,4})/g;397 var timeRegEx = /(\d{1,2}\:\d{1,2}\s\w{1,2}\s\w{1,3})/g;398 var $ = cheerio.load(body);399 var list = $('p').map(function () {400 return $(this).text();401 }).get();402 var price = getMatches(list[0], priceRegEx, 1);403 var time = getMatches(list[1], timeRegEx, 1);404 var ftime = time[0];405 outmsg.payload = {406 price: parseFloat(price),407 time: ftime,408 };409 }410 node.send(outmsg);411 })412 });413 }414 RED.nodes.registerType("CurntHrAvgPrice", CurntHrAvgPrice);415 function FiveMinPrices(n) {416 RED.nodes.createNode(this, n);417 var node = this;418 this.name = n.name;419 const options = {420 uri: 'https://hourlypricing.comed.com/rrtp/ServletFeed?type=pricechartfiveminute',421 json: true422 };423 node.on("input", function (msg) {424 var outmsg = {425 topic: msg.topic426 };427 request(options, function (error, response, body) {428 if (!error && response.statusCode == 200) {429 var matches, year = [], month = [], day = [], hour = [], min = [], sec = [], price = [];430 // Get an array containing the year431 matches = getMatches(body, yearRegEx, 1);432 for (var i = 0; i < matches.length; i++) {433 year[i] = parseInt(matches[i]);434 }435 // Get an array containing the month436 matches = getMatches(body, monthRegEx, 1);437 for (var i = 0; i < matches.length; i++) {438 month[i] = parseInt(matches[i]);439 }440 // Get an array containing the day441 matches = getMatches(body, dayRegEx, 1);442 for (var i = 0; i < matches.length; i++) {443 day[i] = parseInt(matches[i]);444 }445 // Get an array containing the hour446 matches = getMatches(body, hourRegEx, 1);447 for (var i = 0; i < matches.length; i++) {448 hour[i] = parseInt(matches[i]);449 }450 // Get an array containing the minute451 matches = getMatches(body, minRegEx, 1);452 for (var i = 0; i < matches.length; i++) {453 min[i] = parseInt(matches[i]);454 }455 // Get an array containing the second456 matches = getMatches(body, secRegEx, 1);457 for (var i = 0; i < matches.length; i++) {458 sec[i] = parseInt(matches[i]);459 }460 // Get an array containing the price461 matches = getMatches(body, priceRegEx, 1);462 for (var i = 0; i < matches.length; i++) {463 price[i] = matches[i];464 }465 outmsg.payload = [];466 for (var i = 0; i < matches.length; i++) {467 outmsg.payload[i] = {468 year: parseInt(year[i]),469 month: parseInt(month[i]) + 1, //January is 0!470 day: parseInt(day[i]),471 hour: parseInt(hour[i]),472 minute: parseInt(min[i]),473 second: parseInt(sec[i]),474 five_minute_price: parseFloat(price[i])475 };...
getMatches.test.js
Source:getMatches.test.js
...27]}28test('getMatches returns all cmd args and pos args on unknown input', () => {29 const line = ''30 const values = []31 const res = getMatches(line, values, cmd, {only: true})32 const exp = [['Bat', 'Cat', '-d', '--dot', '-e', '--eat', '--fat', 'Put', 'GUT'], line]33 expect(res).toStrictEqual(exp)34})35test('getMatches returns all cmd args and pos args on unknown input', () => {36 const line = 'cvbnkl'37 const values = [38 {values: ['cvbnkl']}39 ]40 const res = getMatches(line, values, cmd, {only: true})41 const exp = [['Bat', 'Cat', '-d', '--dot', '-e', '--eat', '--fat', 'Put', 'GUT'], line]42 expect(res).toStrictEqual(exp)43})44test('getMatches returns full options args on partial input for subcommands', () => {45 const line = 'Ba'46 const values = [47 {values: ['Ba']}48 ]49 const res = getMatches(line, values, cmd, {only: true})50 const exp = [['Bat'], line]51 expect(res).toStrictEqual(exp)52})53test('getMatches completes full options args on partial input for primitive options', () => {54 const line = '--do'55 const values = [56 {values: ['--do']}57 ]58 const res = getMatches(line, values, cmd, {only: true})59 const exp = [['--dot'], line]60 expect(res).toStrictEqual(exp)61})62test('getMatches completes full options args on partial input for primitive arrays', () => {63 const line = '--ea'64 const values = [65 {values: ['--ea']}66 ]67 const res = getMatches(line, values, cmd, {only: true})68 const exp = [['--eat'], line]69 expect(res).toStrictEqual(exp)70})71test('getMatches completes full options args on partial input for flag options', () => {72 const line = '--f'73 const values = [74 {values: ['--f']}75 ]76 const res = getMatches(line, values, cmd, {only: true})77 const exp = [['--fat'], line]78 expect(res).toStrictEqual(exp)79})80test('getMatches completes full options args on partial input for primitive options in subcommands', () => {81 const line = 'Cat --je'82 const values = [83 {...Cat, values: [84 {values: ['--je']}85 ]}86 ]87 const res = getMatches(line, values, cmd, {only: true})88 const exp = [['Cat --jet'], line]89 expect(res).toStrictEqual(exp)90})91test('getMatches completes full options args on partial input for primitive arrays in subcommands', () => {92 const line = 'Cat --ki'93 const values = [94 {...Cat, values: [95 {values: ['--ki']}96 ]}97 ]98 const res = getMatches(line, values, cmd, {only: true})99 const exp = [['Cat --kit'], line]100 expect(res).toStrictEqual(exp)101})102test('getMatches completes full options args on partial input for flag options in subcommands', () => {103 const line = 'Cat --lo'104 const values = [105 {...Cat, values: [106 {values: ['--lo']}107 ]}108 ]109 const res = getMatches(line, values, cmd, {only: true})110 const exp = [['Cat --lot'], line]111 expect(res).toStrictEqual(exp)112})113test('getMatches completes full options args on partial input with shortest common substring', () => {114 const line = 'Put -'115 const values = [116 {...Put, values: [117 {values: ['-']}118 ]}119 ]120 const res = getMatches(line, values, cmd, {only: true})121 const exp = [['Put --'], line]122 expect(res).toStrictEqual(exp)123})124test('getMatches completes full options args on partial input for primitive positional arguments in subcommands', () => {125 const line = 'Cat OA'126 const values = [127 {...Cat, values: [128 {values: ['OA']}129 ]}130 ]131 const res = getMatches(line, values, cmd, {only: true})132 const exp = [['Cat OAK'], line]133 expect(res).toStrictEqual(exp)134})135test('getMatches uses only to suggest possible value of array option in subcommands', () => {136 const line = 'Cat --mad'137 const values = [138 {...Cat, values: [139 {values: ['--mad']}140 ]}141 ]142 const res = getMatches(line, values, cmd, {only: true})143 const exp = [['AAA', 'AAB'], line]144 expect(res).toStrictEqual(exp)145})146test('getMatches uses only values to suggest possible value of array option in subcommands 1', () => {147 const line = 'Cat --mad A'148 const values = [149 {...Cat, values: [150 {...mad, values: ['A']}151 ]}152 ]153 const res = getMatches(line, values, cmd, {only: true})154 const exp = [['Cat --mad AA'], line]155 expect(res).toStrictEqual(exp)156})157test('getMatches uses only values to suggest possible value of array option in subcommands 2', () => {158 const line = 'Cat --mad AA'159 const values = [160 {...Cat, values: [161 {...mad, values: ['AA']}162 ]}163 ]164 const res = getMatches(line, values, cmd, {only: true})165 const exp = [['AAA', 'AAB'], line]166 expect(res).toStrictEqual(exp)167})168test('getMatches uses only to complete value of array option in subcommands', () => {169 const line = 'Cat --nut'170 const values = [171 {...Cat, values: [172 {values: ['--nut']}173 ]}174 ]175 const res = getMatches(line, values, cmd, {only: true})176 const exp = [['Cat --nut C'], line]177 expect(res).toStrictEqual(exp)178})179test('getMatches returns all cmd args and pos args on "-- "', () => {180 const line = 'Cat --jet A --lot -- '181 const values = [182 {...Cat, values: [183 {...jet, values: ['A']},184 {...lot, values: [1]},185 {values: ['--']}186 ]}187 ]188 const res = getMatches(line, values, cmd, {only: true})189 const exp = [['Bat', 'Cat', '-d', '--dot', '-e', '--eat', '--fat', 'Put', 'GUT'], line]190 expect(res).toStrictEqual(exp)191})192test('getMatches returns all subcommand args and pos args with no rest', () => {193 const line = 'Cat --jet A --lot'194 const values = [195 {...Cat, values: [196 {...jet, values: ['A']},197 {...lot, values: [1]}198 ]}199 ]200 const res = getMatches(line, values, cmd, {only: true})201 const exp = [['--jet', '--kit', '--lot', '--mad', '--nut', 'OAK'], line]202 expect(res).toStrictEqual(exp)203})204test('getMatches returns only first positional argument if the line is empty', () => {205 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}206 const posB = {key: 'posB', types: ['string'], only: ['baz', 'bat']}207 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}208 const cmd = {209 key: 'posArgs',210 opts: [posA, posB, posC]211 }212 const line = ''213 const values = [214 posA,215 posB,216 posC217 ]218 const res = getMatches(line, values, cmd, {only: true})219 const exp = [['foo', 'bar'], line]220 expect(res).toStrictEqual(exp)221})222test('getMatches returns only first positional argument if the line is empty in a subcommand', () => {223 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}224 const posB = {key: 'posB', types: ['string'], only: ['baz', 'bat']}225 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}226 const sub = {227 key: 'sub',228 args: ['sub'],229 opts: [posA, posB, posC]230 }231 const cmd = {232 key: 'posArgs',233 opts: [sub]234 }235 const line = 'sub'236 const values = [237 {...sub, values: [238 posA,239 posB,240 posC241 ]}242 ]243 const res = getMatches(line, values, cmd, {only: true})244 const exp = [['foo', 'bar'], line]245 expect(res).toStrictEqual(exp)246})247test('getMatches returns only the second positional argument if the first is already present', () => {248 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}249 const posB = {key: 'posB', types: ['string'], only: ['baz', 'bat']}250 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}251 const sub = {252 key: 'sub',253 args: ['sub'],254 opts: [posA, posB, posC]255 }256 const cmd = {257 key: 'posArgs',258 opts: [sub]259 }260 const line = 'sub foo'261 const values = [262 {...sub, values: [263 {...posA, values: ['foo']},264 posB,265 posC266 ]}267 ]268 const res = getMatches(line, values, cmd, {only: true})269 const exp = [['baz', 'bat'], line]270 expect(res).toStrictEqual(exp)271})272test('getMatches returns only the second positional argument if the first is already present in a subcommand', () => {273 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}274 const posB = {key: 'posB', types: ['string'], only: ['baz', 'bat']}275 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}276 const cmd = {277 key: 'posArgs',278 opts: [posA, posB, posC]279 }280 const line = 'foo'281 const values = [282 {...posA, values: ['foo']},283 posB,284 posC285 ]286 const res = getMatches(line, values, cmd, {only: true})287 const exp = [['baz', 'bat'], line]288 expect(res).toStrictEqual(exp)289})290test('getMatches returns only the second positional argument if the first is already present in a subcommand', () => {291 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}292 const posB = {key: 'posB', types: ['string'], only: ['baz', 'bat']}293 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}294 const sub = {295 key: 'sub',296 args: ['sub'],297 opts: [posA, posB, posC]298 }299 const cmd = {300 key: 'posArgs',301 opts: [sub]302 }303 const line = 'sub foo'304 const values = [305 {...sub, values: [306 {...posA, values: ['foo']},307 posB,308 posC309 ]}310 ]311 const res = getMatches(line, values, cmd, {only: true})312 const exp = [['baz', 'bat'], line]313 expect(res).toStrictEqual(exp)314})315test('getMatches returns only the first positional argument if the first is already present, but is variadic', () => {316 const posA = {key: 'posA', only: ['foo', 'bar']}317 const posB = {key: 'posB', types: ['string'], only: ['baz', 'bat']}318 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}319 const cmd = {320 key: 'posArgs',321 opts: [posA, posB, posC]322 }323 const line = 'foo'324 const values = [325 {...posA, values: ['foo']},326 posB,327 posC328 ]329 const res = getMatches(line, values, cmd, {only: true})330 const exp = [['foo', 'bar'], line]331 expect(res).toStrictEqual(exp)332})333test('getMatches returns only the first positional argument if the first is already present, but is variadic, in a subcommand', () => {334 const posA = {key: 'posA', only: ['foo', 'bar']}335 const posB = {key: 'posB', types: ['string'], only: ['baz', 'bat']}336 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}337 const sub = {338 key: 'sub',339 args: ['sub'],340 opts: [posA, posB, posC]341 }342 const cmd = {343 key: 'posArgs',344 opts: [sub]345 }346 const line = 'sub foo'347 const values = [348 {...sub, values: [349 {...posA, values: ['foo']},350 posB,351 posC352 ]}353 ]354 const res = getMatches(line, values, cmd, {only: true})355 const exp = [['foo', 'bar'], line]356 expect(res).toStrictEqual(exp)357})358test('getMatches returns only the third positional argument if the first and second are already present', () => {359 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}360 const posB = {key: 'posB', types: ['string'], only: ['baz', 'bat']}361 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}362 const cmd = {363 key: 'posArgs',364 opts: [posA, posB, posC]365 }366 const line = 'foo baz'367 const values = [368 {...posA, values: ['foo']},369 {...posB, values: ['baz']},370 posC371 ]372 const res = getMatches(line, values, cmd, {only: true})373 const exp = [['bam', 'ban'], line]374 expect(res).toStrictEqual(exp)375})376test('getMatches returns only the third positional argument if the first and second are already present in a subcommand', () => {377 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}378 const posB = {key: 'posB', types: ['string'], only: ['baz', 'bat']}379 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}380 const sub = {381 key: 'sub',382 args: ['sub'],383 opts: [posA, posB, posC]384 }385 386 const cmd = {387 key: 'posArgs',388 opts: [sub]389 }390 const line = 'sub foo baz'391 const values = [392 {...sub, values: [393 {...posA, values: ['foo']},394 {...posB, values: ['baz']},395 posC396 ]}397 ]398 const res = getMatches(line, values, cmd, {only: true})399 const exp = [['bam', 'ban'], line]400 expect(res).toStrictEqual(exp)401})402test('getMatches returns only the second positional argument if the first and second are already present, but the second is variadic', () => {403 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}404 const posB = {key: 'posB', only: ['baz', 'bat']}405 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}406 const cmd = {407 key: 'posArgs',408 opts: [posA, posB, posC]409 }410 const line = 'foo baz'411 const values = [412 {...posA, values: ['foo']},413 {...posB, values: ['baz']},414 posC415 ]416 const res = getMatches(line, values, cmd, {only: true})417 const exp = [['baz', 'bat'], line]418 expect(res).toStrictEqual(exp)419})420test('getMatches returns only the second positional argument if the first and second are already present, but the second is variadic, in a subcommand', () => {421 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}422 const posB = {key: 'posB', only: ['baz', 'bat']}423 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}424 const sub = {425 key: 'sub',426 args: ['sub'],427 opts: [posA, posB, posC]428 }429 const cmd = {430 key: 'posArgs',431 opts: [sub]432 }433 const line = 'sub foo baz'434 const values = [435 {...sub, values: [436 {...posA, values: ['foo']},437 {...posB, values: ['baz']},438 posC439 ]}440 ]441 const res = getMatches(line, values, cmd, {only: true})442 const exp = [['baz', 'bat'], line]443 expect(res).toStrictEqual(exp)444})445test('getMatches returns only the second positional argument if the first and second are already present, but the second is variadic, in a subcommand of a subcommand', () => {446 const posA = {key: 'posA', types: ['string'], only: ['foo', 'bar']}447 const posB = {key: 'posB', only: ['baz', 'bat']}448 const posC = {key: 'posC', types: ['string'], only: ['bam', 'ban']}449 const subB = {450 key: 'subB',451 args: ['subB'],452 opts: [posA, posB, posC]453 }454 const subA = {455 key: 'subA',456 args: ['subA'],457 opts: [subB]458 }459 const cmd = {460 key: 'posArgs',461 opts: [subA]462 }463 const line = 'subA subB foo baz'464 const values = [465 {...subA, values: [466 {...subB, values: [467 {...posA, values: ['foo']},468 {...posB, values: ['baz']},469 posC470 ]}471 ]}472 ]473 const res = getMatches(line, values, cmd, {only: true})474 const exp = [['baz', 'bat'], line]475 expect(res).toStrictEqual(exp)...
ParseFile.js
Source:ParseFile.js
1import { GetMatches } from './GetMatches';2export const ParseFile = (file) =>{3 // var ParametersGroupNameRegex = /^(\d{3}|\d{1})\,([0])\,(.*)/gm; //001,0,General or 1,0,Compressor4 // // var ParametersGroupNames = [5 // // GetMatches(file, ParametersGroupNameRegex, 1),6 // // GetMatches(file, ParametersGroupNameRegex, 2),7 // // GetMatches(file, ParametersGroupNameRegex, 3)8 // // ];9 var ParametersNamesRegex = /^(\d{1,3})\,([0-9]|[0-9][0-9])\,(.*)/gm;//001,1,Voltage Supply,(Terminal 15),Specifcation: 0.0...15.0 V or 1,1,Switch OFF status,0=OK 7=Man. OFF10 var ParametersNames = [11 GetMatches(file, ParametersNamesRegex, 1),12 GetMatches(file, ParametersNamesRegex, 2),13 GetMatches(file, ParametersNamesRegex, 3)14 ];15 // var BasicSettingsGroupNamesRegex = /[B](\d{3}|\d{2})\,([0])\,(.*)/gm;//B001,0,Adjustment Position (Step 1)16 // var BasicSettingsGroupnames = [17 // GetMatches(file, BasicSettingsGroupNamesRegex, 1),18 // GetMatches(file, BasicSettingsGroupNamesRegex, 2),19 // GetMatches(file, BasicSettingsGroupNamesRegex, 3),20 // ];21 var BasicSettingsNamesRegex = /[B](\d{3}|\d{2})\,([0-4])\,(.*)/gm;//B001,1,Status, ,22 var BasicSettingsNames = [23 GetMatches(file, BasicSettingsNamesRegex, 1),24 GetMatches(file, BasicSettingsNamesRegex, 2),25 GetMatches(file, BasicSettingsNamesRegex, 3)26 ];27 // var AdaptationsGroupNamesRegex = /^\A(\d{3}|\d{2})\,([0])\,(.*)/gm; //A01,0,Reception Optimization or A001,0,Reception Optimization28 // var AdaptationsGroupNames = [29 // GetMatches(file, AdaptationsGroupNamesRegex, 1),30 // GetMatches(file, AdaptationsGroupNamesRegex, 2),31 // GetMatches(file, AdaptationsGroupNamesRegex, 3),32 // ];33 var AdaptationsNamesRegex = /^\A(\d{3}|\d{2}|\d{1})\,([0-9]|[0-9][0-9])\,(.*)/gm;34 var AdaptationsNames = [35 GetMatches(file, AdaptationsNamesRegex, 1),36 GetMatches(file, AdaptationsNamesRegex, 2),37 GetMatches(file, AdaptationsNamesRegex, 3),38 ];39 var CodingsNamesRegex = /^C(\d\d|\d)\,(.*)/gm; //C01,??xxx: Equipment40 var CodingsNames = [41 GetMatches(file, CodingsNamesRegex, 2)42 ];43 var LongCodingsRegex = /\L\C\,(\d{2})\,(.{0,})\,(.*)/gm; //LC,00,1,CD-Changer/iPod/USB Connection Monitoring active44 var LongCodingsNames = [45 GetMatches(file, LongCodingsRegex, 1),46 GetMatches(file, LongCodingsRegex, 2),47 GetMatches(file, LongCodingsRegex, 3)48 ];49 var SelectiveActivationsNamesRegex = /^\T(\d*)\,(.*)/gm; //T1543,Parking Aid Warning Chime (H15)50 var SelectiveActivationsNames = [51 GetMatches(file, SelectiveActivationsNamesRegex, 1),52 [],53 GetMatches(file, SelectiveActivationsNamesRegex, 2)54 ];55 var SecurityAccessLoginsRegex = /(L|S)(\d\d)\,(.*)/gm; //S01,13861 = Adaptation Enabling or L01,11463 = Cruise Control System (CCS) Activation56 var SecurityNames = [57 GetMatches(file, SecurityAccessLoginsRegex, 3)58 ];59 return {60 //ParamGroupNames : ParametersGroupNames,61 ParamNames : ParametersNames,62 //BSGroupNames : BasicSettingsGroupnames,63 BSSettingsNames : BasicSettingsNames,64 //AdaptGroupNames : AdaptationsGroupNames,65 AdaptNames: AdaptationsNames,66 CONames : CodingsNames,67 LCNames : LongCodingsNames,68 SelACNames : SelectiveActivationsNames,69 SANames : SecurityNames70 };...
Using AI Code Generation
1const mb = require('mountebank');2const imposter = {3 {4 {5 is: {6 headers: {7 },8 body: {9 }10 }11 }12 }13};14mb.create(imposter).then(() => {15 return mb.getMatches({ port: 3000, protocol: 'http' });16}).then((matches) => {17 console.log(JSON.stringify(matches, null, 2));18}).catch((error) => {19 console.error(error);20});21 {22 {
Using AI Code Generation
1var mb = require('mountebank');2var imposter = {3 {4 {5 is: {6 }7 }8 }9};10mb.create(imposter).then(function (result) {11 return mb.getMatches(result.port);12}).then(function (result) {13 console.log(JSON.stringify(result, null, 2));14}).finally(mb.stopAll);15{16 {17 {18 {19 "is": {20 }21 }22 }23 {24 "request": {25 "query": {},26 "headers": {27 }28 },29 "response": {30 "headers": {31 },32 }33 }34 }35}36var mb = require('mountebank');37var imposter = {38 {39 {40 is: {41 }42 }43 }44};45mb.create(imposter).then(function (result) {46 return mb.getImposters();47}).then(function (result) {48 console.log(JSON.stringify(result, null, 2));49}).finally(mb.stopAll);50{51 {52 {
Using AI Code Generation
1const request = require('request');2const options = {3};4request(options, (error, response, body) => {5 if (!error && response.statusCode == 200) {6 console.log(body);7 }8});9const request = require('request');10const options = {11};12request(options, (error, response, body) => {13 if (!error && response.statusCode == 200) {14 console.log(body);15 }16});17const request = require('request');18const options = {19};20request(options, (error, response, body) => {21 if (!error && response.statusCode == 200) {22 console.log(body);23 }24});25const request = require('request');26const options = {27};28request(options, (error, response, body) => {29 if (!error && response.statusCode == 200) {30 console.log(body);31 }32});33const request = require('request');34const options = {35};36request(options, (error, response, body) => {37 if (!error && response.statusCode == 200) {38 console.log(body);39 }40});41const request = require('request');42const options = {43};44request(options, (error, response, body) => {45 if (!error && response.statusCode == 200) {46 console.log(body);47 }48});
Using AI Code Generation
1var mb = require('mountebank'),2 assert = require('assert');3var options = {4};5mb.create(options, function (error, mbServer) {6 if (error) {7 console.log(error);8 }9 mbServer.get('/test', function (req, res) {10 res.send({test: 'test'});11 });12 mbServer.get('/test1', function (req, res) {13 res.send({test: 'test1'});14 });15 mbServer.get('/test2', function (req, res) {16 res.send({test: 'test2'});17 });18 mbServer.get('/test3', function (req, res) {19 res.send({test: 'test3'});20 });21 mbServer.get('/test4', function (req, res) {22 res.send({test: 'test4'});23 });24 mbServer.get('/test5', function (req, res) {25 res.send({test: 'test5'});26 });27 mbServer.get('/test6', function (req, res) {28 res.send({test: 'test6'});29 });30 mbServer.get('/test7', function (req, res) {31 res.send({test: 'test7'});32 });33 mbServer.get('/test8', function (req, res) {34 res.send({test: 'test8'});35 });36 mbServer.get('/test9', function (req, res) {37 res.send({test: 'test9'});38 });39 mbServer.get('/test10', function (req, res) {40 res.send({test: 'test10'});41 });42 mbServer.get('/test11', function (req, res) {43 res.send({test: 'test11'});44 });45 mbServer.get('/test12', function (req, res) {46 res.send({test: 'test12'});47 });48 mbServer.get('/test13', function (req, res) {49 res.send({test: 'test13'});50 });51 mbServer.get('/test14', function (req, res) {52 res.send({test: 'test14'});53 });54 mbServer.get('/test15', function (req, res) {55 res.send({test: 'test15'});56 });
Using AI Code Generation
1var mb = require('mountebank');2mb.getMatches({port: 2525, protocol: 'http', stubs: 1, predicates: 1}, function (error, result) {3 if (error) {4 console.log(error);5 }6 else {7 console.log(result);8 }9});10[ { protocol: 'http',11 responses: [ [Object] ] } ]12var mb = require('mountebank');13mb.getMatches({port: 2525, protocol: 'http', stubs: 1, predicates: 1}, function (error, result) {14 if (error) {15 console.log(error);16 }17 else {18 console.log(result[0].requests[0].data);19 console.log(result[0].responses[0].data);20 }21});22{ headers: { host: 'localhost:2525', 'user-agent': 'curl/7.43.0', accept: '*/*' },23 body: '' }24{ headers: { 'content-type': 'text/plain' }, body: 'Hello, World!' }25var mb = require('mountebank');26mb.getMatches({port: 2525,
Using AI Code Generation
1const imposter = require('mountebank').createImposter(3000, 'http')2imposter.getMatches().then(matches => console.log(matches))3const imposter = require('mountebank').createImposter(3000, 'http')4imposter.getMatches().then(matches => console.log(matches))5const imposter = require('mountebank').createImposter(3000, 'http')6imposter.getMatches().then(matches => console.log(matches))7const imposter = require('mountebank').createImposter(3000, 'http')8imposter.getMatches().then(matches => console.log(matches))9const imposter = require('mountebank').createImposter(3000, 'http')10imposter.getMatches().then(matches => console.log(matches))11const imposter = require('mountebank').createImposter(3000, 'http')12imposter.getMatches().then(matches => console.log(matches))13const imposter = require('mountebank').createImposter(3000, 'http')14imposter.getMatches().then(matches => console.log(matches))15const imposter = require('mountebank').createImposter(3000, 'http')16imposter.getMatches().then(matches => console.log(matches))17const imposter = require('mountebank').createImposter(3000, 'http')18imposter.getMatches().then(matches => console.log(matches))19const imposter = require('mountebank').createImposter(3000, 'http')20imposter.getMatches().then(matches => console.log(matches))
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!!