Best JavaScript code snippet using wpt
match.js
Source:match.js
1const { SEPARATOR, flatten } = require("../utils/flatten");2const { recursive_multi_replace } = require("../utils/string");3/**4 * Expression placeholders registry created via "_m" method.5 * @type {string[]}6 */7const EXPRESSIONS_REGISTRY = [];8/**9 * Utility function that returns the generic string placeholder of an expression.10 * @returns {string}11 */12function _m() {13 const expr_definition = "<<expr@" + EXPRESSIONS_REGISTRY.length + ">>";14 EXPRESSIONS_REGISTRY.push(expr_definition);15 return expr_definition;16}17/**18 * Returns whether the given value is an expression placeholder.19 *20 * @param value Value to check.21 * @returns {boolean}22 */23function is_matching_expr(value) {24 return "string" === typeof value && value.indexOf("<<expr@") > -1;25}26/**27 * Performs match entry key check.28 *29 * @param key Known value key.30 * @param match_key Key used to match or compare to with the original key.31 * @param resolved_matches Keys that are already resolved.32 * @returns {*[]}33 */34function match_entry_keys(key, match_key, resolved_matches) {35 const key_parts = key.split(SEPARATOR);36 const match_key_parts = match_key.split(SEPARATOR);37 const match_key_expr_parts = match_key_parts.filter(part => is_matching_expr(part));38 const resolved_match_key = recursive_multi_replace(39 match_key,40 match_key_expr_parts,41 0,42 key_parts,43 match_key_parts.findIndex(single_match_key_part => is_matching_expr(single_match_key_part)),44 ""45 );46 const matches = key_parts.filter((_, single_key_part_index) => {47 return is_matching_expr(match_key_parts[single_key_part_index]) &&48 !resolved_matches.includes(key_parts[single_key_part_index])49 });50 return [51 resolved_match_key === key,52 ...matches53 ];54}55/**56 * Performs match entry value check.57 *58 * @param val Known value.59 * @param match_val A value used to match or compare to with the original value.60 * @returns {*[]}61 */62function match_entry_values(val, match_val) {63 const is_match_flag = is_matching_expr(match_val);64 const are_values_matching = is_match_flag || val === match_val;65 if (is_match_flag) {66 return [are_values_matching, val];67 }68 return [are_values_matching];69}70/**71 * Executes key and value matching algorithms on flattened values, passed as Object key-value entries.72 *73 * @param val_entries Object key-value entries of the known value.74 * @param match_val_entries Object key-value entries to match or compare to with the original ones.75 * @param acc Accumulator where the expression matching results will be available.76 * @param matched_keys_cache The cache of already resolved keys.77 * @returns {*[]}78 */79function match_flat_val_entries(val_entries, match_val_entries, acc, matched_keys_cache) {80 const [head, ...tail] = val_entries;81 const [match_head, ...match_tail] = match_val_entries;82 if (undefined === head || undefined === match_head) return acc;83 const [key, val] = head;84 const [match_key, match_val] = match_head;85 const [are_keys_matching, ...key_matches] = match_entry_keys(key, match_key, matched_keys_cache);86 const acc1 = acc.concat([87 [88 [are_keys_matching, ...key_matches],89 match_entry_values(val, match_val)90 ]91 ]);92 const matched_keys_cache1 = matched_keys_cache.concat(key_matches);93 return match_flat_val_entries(tail, match_tail, acc1, matched_keys_cache1);94}95/**96 * Returns whether all match entries are truthy.97 *98 * @param match_entries Entries to check.99 * @returns {boolean}100 */101function is_every_entry_matching(match_entries) {102 return match_entries.every(entry => {103 const [key_match_entries, val_match_entries] = entry;104 const [is_key_matching] = key_match_entries;105 const [is_val_matching] = val_match_entries;106 return is_key_matching && is_val_matching;107 });108}109/**110 * Returns all truthy match entries.111 *112 * @param match_entries Entries to check and return.113 * @returns {*[]}114 */115function get_all_entry_matches(match_entries) {116 return match_entries.reduce((partial, acc) => {117 const [key_match_entries, val_match_entries] = acc;118 const key_matches = key_match_entries.slice(1);119 const val_matches = val_match_entries.slice(1);120 return [121 ...partial,122 ...key_matches,123 ...val_matches124 ]125 }, []);126}127/**128 * Performs expression match based on the passed values.129 *130 * @param val Known value.131 * @param match_val Value to match or compare to the original one.132 * @returns {*[]}133 */134function match_val(val, match_val) {135 const flat_values = [val, match_val].map(single_val => Object.entries(136 flatten(single_val, "", {})137 ));138 const [flat_val, flat_match_val] = flat_values;139 const match_entries = match_flat_val_entries(flat_val, flat_match_val, [], []);140 const match_result = is_every_entry_matching(match_entries);141 if (true === match_result) {142 return [143 match_result,144 get_all_entry_matches(match_entries)145 ];146 }147 return [match_result, []];148}149exports._m = _m;...
mark.any.js
Source:mark.any.js
1// test data2var testThreshold = 20;3var expectedTimes = new Array();4function match_entries(entries, index)5{6 var entry = entries[index];7 var match = self.performance.getEntriesByName("mark")[index];8 assert_equals(entry.name, match.name, "entry.name");9 assert_equals(entry.startTime, match.startTime, "entry.startTime");10 assert_equals(entry.entryType, match.entryType, "entry.entryType");11 assert_equals(entry.duration, match.duration, "entry.duration");12}13function filter_entries_by_type(entryList, entryType)14{15 var testEntries = new Array();16 // filter entryList17 for (var i in entryList)18 {19 if (entryList[i].entryType == entryType)20 {21 testEntries.push(entryList[i]);22 }23 }24 return testEntries;25}26test(function () {27 // create first mark28 self.performance.mark("mark");29 expectedTimes[0] = self.performance.now();30 entries = self.performance.getEntriesByName("mark");31 assert_equals(entries.length, 1);32}, "Entry 0 is properly created");33test(function () {34 // create second, duplicate mark35 self.performance.mark("mark");36 expectedTimes[1] = self.performance.now();37 entries = self.performance.getEntriesByName("mark");38 assert_equals(entries.length, 2);39}, "Entry 1 is properly created");40function test_mark(index) {41 test(function () {42 entries = self.performance.getEntriesByName("mark");43 assert_equals(entries[index].name, "mark", "Entry has the proper name");44 }, "Entry " + index + " has the proper name");45 test(function () {46 entries = self.performance.getEntriesByName("mark");47 assert_approx_equals(entries[index].startTime, expectedTimes[index], testThreshold);48 }, "Entry " + index + " startTime is approximately correct (up to " + testThreshold +49 "ms difference allowed)");50 test(function () {51 entries = self.performance.getEntriesByName("mark");52 assert_equals(entries[index].entryType, "mark");53 }, "Entry " + index + " has the proper entryType");54 test(function () {55 entries = self.performance.getEntriesByName("mark");56 assert_equals(entries[index].duration, 0);57 }, "Entry " + index + " duration == 0");58 test(function () {59 entries = self.performance.getEntriesByName("mark", "mark");60 assert_equals(entries[index].name, "mark");61 }, "getEntriesByName(\"mark\", \"mark\")[" + index + "] returns an " +62 "object containing a \"mark\" mark");63 test(function () {64 entries = self.performance.getEntriesByName("mark", "mark");65 match_entries(entries, index);66 }, "The mark returned by getEntriesByName(\"mark\", \"mark\")[" + index67 + "] matches the mark returned by " +68 "getEntriesByName(\"mark\")[" + index + "]");69 test(function () {70 entries = filter_entries_by_type(self.performance.getEntries(), "mark");71 assert_equals(entries[index].name, "mark");72 }, "getEntries()[" + index + "] returns an " +73 "object containing a \"mark\" mark");74 test(function () {75 entries = filter_entries_by_type(self.performance.getEntries(), "mark");76 match_entries(entries, index);77 }, "The mark returned by getEntries()[" + index78 + "] matches the mark returned by " +79 "getEntriesByName(\"mark\")[" + index + "]");80 test(function () {81 entries = self.performance.getEntriesByType("mark");82 assert_equals(entries[index].name, "mark");83 }, "getEntriesByType(\"mark\")[" + index + "] returns an " +84 "object containing a \"mark\" mark");85 test(function () {86 entries = self.performance.getEntriesByType("mark");87 match_entries(entries, index);88 }, "The mark returned by getEntriesByType(\"mark\")[" + index89 + "] matches the mark returned by " +90 "getEntriesByName(\"mark\")[" + index + "]");91}92for (var i = 0; i < expectedTimes.length; i++) {93 test_mark(i);...
Using AI Code Generation
1var wptools = require('wptools');2var options = {3};4var page = wptools.page('Albert Einstein', options);5page.get(function(err, info) {6 if (err) {7 console.log(err);8 } else {9 var matches = page.match_entries('birth_date');10 console.log(matches);11 }12});
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('New York City');3page.match_entries('population', function(err, resp) {4 if (err) {5 console.log(err);6 } else {7 console.log(resp);8 }9});10### page()11#### page([title], [options])12var page = wptools.page('New York City');13#### page([options])14var page = wptools.page();15#### page([title], [callback])16var page = wptools.page('New York City', function(err, resp) {17 if (err) {18 console.log(err);19 } else {20 console.log(resp);21 }22});23#### page([callback])24var page = wptools.page(function(err, resp) {25 if (err) {26 console.log(err);27 } else {28 console.log(resp);29 }30});31### page.get()32#### page.get([options], [callback])33page.get(function(err, resp) {34 if (err) {35 console.log(err);36 } else {37 console.log(resp);38 }39});40### page.get_categories()41#### page.get_categories([options], [callback])42page.get_categories(function(err, resp) {43 if (err) {44 console.log(err);45 } else {46 console.log(resp);47 }48});49### page.get_images()50#### page.get_images([options], [callback])
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var readline = require('readline');4var stream = require('stream');5var instream = fs.createReadStream('input.txt');6var outstream = new stream;7var rl = readline.createInterface(instream, outstream);8var arr = [];9var i = 0;10rl.on('line', function(line) {11 arr[i] = line;12 i++;13});14rl.on('close', function() {15 var len = arr.length;16 var j = 0;17 for (j = 0; j < len; j++) {18 wptools.match_entries({19 }, function(err, resp) {20 if (err) {21 console.log("Error");22 } else {23 console.log(resp);24 }25 });26 }27});
Using AI Code Generation
1var wptools = require('wptools');2var wiki = wptools.page('Barack Obama');3wiki.match_entries('Barack Obama', function(resp) {4 console.log(resp);5});6{ matches: [ 'Barack Obama', 'Barack Obama Jr.', 'Barack Obama Sr.' ] }
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools.page('Barack Obama');3wp.match_entries(function(err, resp) {4 console.log(resp);5});6var wptools = require('wptools');7var wp = wptools.page('Barack Obama');8wp.get_coordinates(function(err, resp) {9 console.log(resp);10});11var wptools = require('wptools');12var wp = wptools.page('Barack Obama');13wp.get_extract(function(err, resp) {14 console.log(resp);15});16var wptools = require('wptools');17var wp = wptools.page('Barack Obama');18wp.get_info(function(err, resp) {19 console.log(resp);20});21var wptools = require('wptools');22var wp = wptools.page('Barack Obama');23wp.get_pageid(function(err, resp) {24 console.log(resp);25});26var wptools = require('wptools');27var wp = wptools.page('Barack Obama');28wp.get_redirects(function(err, resp) {29 console.log(resp);30});31var wptools = require('wptools');32var wp = wptools.page('Barack Obama');
Using AI Code Generation
1const wptools = require('wptools');2const fs = require('fs');3fs.readFile('test.txt', 'utf-8', (err, data) => {4 if (err) throw err;5 console.log(data);6 var lines = data.split('\n');7 lines.forEach((line) => {8 .match_entries(line)9 .then((response) => {10 console.log('response', response);11 })12 .catch((error) => {13 console.log('error', error);14 });15 });16});17const wptools = require('wptools');18const fs = require('fs');19fs.readFile('test.txt', 'utf-8', (err, data) => {20 if (err) throw err;21 console.log(data);22 var lines = data.split('\n');23 lines.forEach((line) => {24 .page(line)25 .then((response) => {26 console.log('response', response);27 })28 .catch((error) => {29 console.log('error', error);30 });31 });32});
Using AI Code Generation
1const wptools = require('wptools');2const fs = require('fs');3var names = fs.readFileSync('names.txt').toString().split("\n");4var output = fs.createWriteStream('output.txt');5names.forEach(function (name) {6 var page = wptools.page(name);7 page.get_infobox(function (err, info) {8 var id = info.wikidata;9 var page = wptools.page(id);10 page.get_infobox(function (err, info) {11 var image = info.image;12 output.write(name + " : " + image + "\n");13 });14 });15});
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var data = fs.readFileSync('data.json');4var json = JSON.parse(data);5var result = [];6for (var i = 0; i < json.length; i++) {7 var page = wptools.page(json[i].title);8 page.get(function (err, resp) {9 if (err) {10 console.log(err);11 }12 else {13 var res = resp.data.infoboxes[0].match_entries("birth_date");14 if (res) {15 console.log(res);16 }17 }18 });19}20var wptools = require('wptools');21var fs = require('fs');22var page = wptools.page('List of Indian film actresses');23page.get(function (err, resp) {24 if (err) {25 console.log(err);26 }27 else {28 var data = resp.data.infoboxes[0].data;29 var result = [];30 for (var i = 0; i < data.length; i++) {31 var obj = {};32 obj.title = data[i][0];33 result.push(obj);34 }35 var json = JSON.stringify(result);36 fs.writeFile('data.json', json, 'utf8', function (err) {37 if (err) {38 console.log(err);39 }40 else {41 console.log("file created");42 }43 });44 }45});46var wptools = require('wptools');47var fs = require('fs');48var data = fs.readFileSync('data.json');49var json = JSON.parse(data);50var result = [];51for (var i = 0; i < json.length; i++) {52 var page = wptools.page(json[i].title);53 page.get(function (err, resp) {54 if (err) {
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!!