Best JavaScript code snippet using wpt
head.js
Source:head.js
1/* Any copyright is dedicated to the Public Domain.2 http://creativecommons.org/publicdomain/zero/1.0/ */3const Cu = SpecialPowers.Cu;4let pendingEmulatorCmdCount = 0;5let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise;6let nfc = window.navigator.mozNfc;7SpecialPowers.addPermission("nfc-manager", true, document);8/**9 * Emulator helper.10 */11let emulator = (function() {12 let pendingCmdCount = 0;13 let originalRunEmulatorCmd = runEmulatorCmd;14 // Overwritten it so people could not call this function directly.15 runEmulatorCmd = function() {16 throw "Use emulator.run(cmd, callback) instead of runEmulatorCmd";17 };18 function run(cmd, callback) {19 log("Executing emulator command '" + cmd + "'");20 pendingCmdCount++;21 originalRunEmulatorCmd(cmd, function(result) {22 pendingCmdCount--;23 if (callback && typeof callback === "function") {24 callback(result);25 }26 });27 };28 return {29 run: run,30 P2P_RE_INDEX_0 : 0,31 P2P_RE_INDEX_1 : 1,32 T1T_RE_INDEX : 2,33 T2T_RE_INDEX : 3,34 T3T_RE_INDEX : 4,35 T4T_RE_INDEX : 536 };37}());38let NCI = (function() {39 function activateRE(re) {40 let deferred = Promise.defer();41 let cmd = 'nfc nci rf_intf_activated_ntf ' + re;42 emulator.run(cmd, function(result) {43 is(result.pop(), 'OK', 'check activation of RE' + re);44 deferred.resolve();45 });46 return deferred.promise;47 };48 function deactivate() {49 let deferred = Promise.defer();50 let cmd = 'nfc nci rf_intf_deactivate_ntf';51 emulator.run(cmd, function(result) {52 is(result.pop(), 'OK', 'check deactivate');53 deferred.resolve();54 });55 return deferred.promise;56 };57 function notifyDiscoverRE(re, type) {58 let deferred = Promise.defer();59 let cmd = 'nfc nci rf_discover_ntf ' + re + ' ' + type;60 emulator.run(cmd, function(result) {61 is(result.pop(), 'OK', 'check discovery of RE' + re);62 deferred.resolve();63 });64 return deferred.promise;65 };66 return {67 activateRE: activateRE,68 deactivate: deactivate,69 notifyDiscoverRE: notifyDiscoverRE,70 LAST_NOTIFICATION: 0,71 LIMIT_NOTIFICATION: 1,72 MORE_NOTIFICATIONS: 273 };74}());75let TAG = (function() {76 function setData(re, flag, tnf, type, payload) {77 let deferred = Promise.defer();78 let cmd = "nfc tag set " + re +79 " [" + flag + "," + tnf + "," + type + ",," + payload + "]";80 emulator.run(cmd, function(result) {81 is(result.pop(), "OK", "set NDEF data of tag" + re);82 deferred.resolve();83 });84 return deferred.promise;85 };86 function clearData(re) {87 let deferred = Promise.defer();88 let cmd = "nfc tag clear " + re;89 emulator.run(cmd, function(result) {90 is(result.pop(), "OK", "clear tag" + re);91 deferred.resolve();92 });93 }94 return {95 setData: setData,96 clearData: clearData97 };98}());99let SNEP = (function() {100 function put(dsap, ssap, flags, tnf, type, id, payload) {101 let deferred = Promise.defer();102 let cmd = "nfc snep put " + dsap + " " + ssap + " [" + flags + "," +103 tnf + "," +104 type + "," +105 id + "," +106 payload + "]";107 emulator.run(cmd, function(result) {108 is(result.pop(), "OK", "send SNEP PUT");109 deferred.resolve();110 });111 return deferred.promise;112 };113 return {114 put: put,115 SAP_NDEF: 4116 };117}());118function toggleNFC(enabled) {119 let deferred = Promise.defer();120 let req;121 if (enabled) {122 req = nfc.startPoll();123 } else {124 req = nfc.powerOff();125 }126 req.onsuccess = function() {127 deferred.resolve();128 };129 req.onerror = function() {130 ok(false, 'operation failed, error ' + req.error.name);131 deferred.reject();132 finish();133 };134 return deferred.promise;135}136function clearPendingMessages(type) {137 if (!window.navigator.mozHasPendingMessage(type)) {138 return;139 }140 // setting a handler removes all messages from queue141 window.navigator.mozSetMessageHandler(type, function() {142 window.navigator.mozSetMessageHandler(type, null);143 });144}145function cleanUp() {146 log('Cleaning up');147 waitFor(function() {148 SpecialPowers.removePermission("nfc-manager", document);149 finish()150 },151 function() {152 return pendingEmulatorCmdCount === 0;153 });154}155function runNextTest() {156 clearPendingMessages('nfc-manager-tech-discovered');157 clearPendingMessages('nfc-manager-tech-lost');158 let test = tests.shift();159 if (!test) {160 cleanUp();161 return;162 }163 test();164}165// run this function to start tests166function runTests() {167 if ('mozNfc' in window.navigator) {168 runNextTest();169 } else {170 // succeed immediately on systems without NFC171 log('Skipping test on system without NFC');172 ok(true, 'Skipping test on system without NFC');173 finish();174 }175}176const NDEF = {177 TNF_WELL_KNOWN: 1,178 // compares two NDEF messages179 compare: function(ndef1, ndef2) {180 isnot(ndef1, null, "LHS message is not null");181 isnot(ndef2, null, "RHS message is not null");182 is(ndef1.length, ndef2.length,183 "NDEF messages have the same number of records");184 ndef1.forEach(function(record1, index) {185 let record2 = this[index];186 is(record1.tnf, record2.tnf, "test for equal TNF fields");187 let fields = ["type", "id", "payload"];188 fields.forEach(function(value) {189 let field1 = Cu.waiveXrays(record1)[value];190 let field2 = Cu.waiveXrays(record2)[value];191 is(field1.length, field2.length,192 value + " fields have the same length");193 let eq = true;194 for (let i = 0; eq && i < field1.length; ++i) {195 eq = (field1[i] === field2[i]);196 }197 ok(eq, value + " fields contain the same data");198 });199 }, ndef2);200 },201 // parses an emulator output string into an NDEF message202 parseString: function(str) {203 // make it an object204 let arr = null;205 try {206 arr = JSON.parse(str);207 } catch (e) {208 ok(false, "Parser error: " + e.message);209 return null;210 }211 // and build NDEF array212 let ndef = arr.map(function(value) {213 let type = new Uint8Array(NfcUtils.fromUTF8(this.atob(value.type)));214 let id = new Uint8Array(NfcUtils.fromUTF8(this.atob(value.id)));215 let payload =216 new Uint8Array(NfcUtils.fromUTF8(this.atob(value.payload)));217 return new MozNDEFRecord(value.tnf, type, id, payload);218 }, window);219 return ndef;220 }221};222var NfcUtils = {223 fromUTF8: function(str) {224 let buf = new Uint8Array(str.length);225 for (let i = 0; i < str.length; ++i) {226 buf[i] = str.charCodeAt(i);227 }228 return buf;229 },230 toUTF8: function(array) {231 if (!array) {232 return null;233 }234 let str = "";235 for (var i = 0; i < array.length; i++) {236 str += String.fromCharCode(array[i]);237 }238 return str;239 }...
PM.js
Source:PM.js
1//è¡ä¸ä¿¡æ¯2import {3 fetchList,4 fetchChild,5} from '@/services/perf';6import { fetchProject } from '@/services/PMA';7import { fetchChild2,fetchChild3 } from '@/services/PM';8export default {9 namespace: 'PM',10 state: {11 data: {12 list: [],13 pagination: {},14 },15 },16 //effectsæ¹æ³ç¨å¤çå¼æ¥å¨ä½17 effects: {18 *fetch({ payload,callback }, { call, put }) {19 const response = yield call(fetchList, payload);20 let obj = {21 list:[],22 pagination:{}23 }24 const { pageIndex } = payload;25 if(response.resData){26 //计ç®é¡¹ç®ä½é¢27 let arr = [];28 let arr2 = [];29 let arr3 = [];30 for(let i=0;i<response.resData.length;i++){31 let conditions = [{32 code:'PROJECT_ID',33 exp:'=',34 value:response.resData[i].id?response.resData[i].id:''35 },36 {37 code:'STATUS',38 exp:'=',39 value:'审æ¹éè¿'40 }41 ];42 const childresponse = yield call(fetchChild, {conditions});43 if(childresponse.resData){44 arr = arr.concat(childresponse.resData);45 }46 const childresponse2 = yield call(fetchChild2, {conditions});47 if(childresponse2.resData){48 arr2 = arr2.concat(childresponse2.resData);49 }50 const childresponse3 = yield call(fetchChild3, {51 reqData:{52 projectId:response.resData[i].id?response.resData[i].id:''53 }54 });55 console.log("childresponse3",childresponse3)56 if(childresponse3.resData){57 arr3 = arr3.concat(childresponse3.resData);58 }59 }60 response.resData = response.resData.map((item)=>{61 let arrCount = [];62 let arrCount2 = [];63 arr.forEach((value)=>{64 if(item.id === value.projectId){65 arrCount.push(value)66 }67 });68 arr2.forEach((value)=>{69 if(item.id === value.projectId){70 arrCount2.push(value)71 }72 });73 let ld = null; // 项ç®ç»ä½74 let ndef1 = item.ndef1;75 if(ndef1){76 if(arrCount.length > ndef1){77 let count = 0;78 for(let i = ndef1;i<arrCount.length;i++){79 count = count + arrCount[i].travelfee;80 }81 item.leftfeeend = item.projectmoney - count - item.totalsubsidy;82 item.le = item.projectmoney - count - item.totalsubsidy;83 ld = item.projectmoney - count - item.totalsubsidy;84 }else{85 item.leftfeeend = item.projectmoney - item.totalsubsidy;86 item.le = item.projectmoney - item.totalsubsidy;87 ld = item.projectmoney - item.totalsubsidy;88 }89 }90 let totaltravelfee = item.totaltravelfee; //交éè´¹91 let totalsubsidy = item.totalsubsidy; //补贴92 let ticheng = 0;93 arrCount = arrCount.map(it =>{94 if(item.id === it.projectId){95 ticheng += ld * it.sumratio96 }97 return it98 });99 let totalCost = 0; //绩ææ»è´¹ç¨100 if(ticheng>0){101 totalCost = totaltravelfee + totalsubsidy + ticheng102 }else{103 totalCost = totaltravelfee + totalsubsidy + 0104 }105 item.totalCost = totalCost;106 let travelCount = 0; //å·®æ
æ»è´¹ç¨107 arrCount2.forEach(it =>{108 if(item.id === it.projectId){109 travelCount += it.claimingamount110 }111 });112 item.travelCount = travelCount;113 item.key = item.id;114 let totalPrice = 0;115 arr3.map(ie =>{116 if(ie){117 if(ie.projectId === item.id){118 item.totalPrice = ie.totalPrice;119 totalPrice = ie.totalPrice;120 }121 }122 });123 item.count = totalCost + travelCount + totalPrice; //项ç®æ»è´¹ç¨124 return item125 });126 obj = {127 list: response.resData,128 pagination:{129 total: response.total,130 current:pageIndex + 1131 }132 };133 }134 yield put({135 type: 'save',136 payload: obj137 });138 if (callback) callback(obj);139 },140 *fetchProject({ payload,callback }, { call, put }) {141 const response = yield call(fetchProject, payload);142 const { pageIndex } = payload;143 let obj = [];144 if(response.resData){145 response.resData.map(item=>{146 item.key = item.id;147 })148 obj = {149 list: response.resData,150 pagination:{151 total: response.total,152 current:pageIndex + 1153 }154 };155 }156 if (callback) callback(obj);157 },158 *fetchChild({ payload,callback }, { call, put }) {159 const response = yield call(fetchChild, payload);160 console.log("绩æ",response)161 if (callback) callback(response);162 },163 *fetchChild2({ payload,callback }, { call, put }) {164 const response = yield call(fetchChild2, payload);165 console.log("å·®æ
",response)166 if (callback) callback(response);167 },168 },169 //reducersæ¹æ³å¤çåæ¥170 reducers: {171 save(state, action) {172 return {173 ...state,174 data: action.payload,175 };176 },177 },...
Using AI Code Generation
1var wptools = require('wptools');2var options = {3};4wptools.page('San Francisco').get(options, function(err, resp) {5 if (!err) {6 resp.data.ndef1(function(err, resp) {7 if (!err) {8 console.log(resp);9 }10 });11 }12});13var wptools = require('wptools');14wptools.page('San Francisco').get(function(err, resp) {15});16### get(options, callback)17var wptools = require('wptools');18var options = {19};20wptools.page('San Francisco').get(options, function(err, resp) {21});22### get(callback)23var wptools = require('wptools');24wptools.page('San Francisco').get(function(err, resp) {25});26### ndef1(callback)27var wptools = require('wptools');28wptools.page('San Francisco').get(function(err, resp) {29 if (!err) {30 resp.data.ndef1(function(err, resp) {31 if (!err) {32 console.log(resp);33 }34 });35 }36});37### ndef2(callback)38var wptools = require('wptools');39wptools.page('San Francisco').get(function(err, resp) {40 if (!err) {41 resp.data.ndef2(function(err, resp) {42 if (!err) {43 console.log(resp);44 }45 });46 }47});
Using AI Code Generation
1var ndef1 = require('wpt-ndef').ndef1;2var ndef = new ndef1();3 ndef.textRecord("Hello, World"),4 ndef.mimeMediaRecord("image/png", new Uint8Array([0, 1, 2, 3, 4]))5];6var ndef2 = require('wpt-ndef').ndef2;7var ndef = new ndef2();8 ndef.textRecord("Hello, World"),9 ndef.mimeMediaRecord("image/png", new Uint8Array([0, 1, 2, 3, 4]))10];11### NDEFReader.scan()12### NDEFReader.write(NDEFMessageInit message, optional NDEFWriteOptions options)13### NDEFReader.cancel()
Using AI Code Generation
1var ndef1 = require('ndef1');2 ndef1.textRecord("Hello World")3];4ndef1.write(msg).then(function() {5 console.log("Message written");6}, function(reason) {7 console.log("Error: " + reason);8});9#### ndef1.read()10var ndef1 = require('ndef1');11ndef1.read().then(function(records) {12 console.log("Read " + records.length + " records");13}, function(reason) {14 console.log("Error: " + reason);15});16#### ndef1.write(records)17var ndef1 = require('ndef1');18 ndef1.textRecord("Hello World")19];20ndef1.write(msg).then(function() {21 console.log("Message written");22}, function(reason) {23 console.log("Error: " + reason);24});25#### ndef1.makeRecord(options)26var ndef1 = require('ndef1');27var record = ndef1.makeRecord({28});29#### ndef1.textRecord(text, lang, encoding)30var ndef1 = require('ndef1');31var record = ndef1.textRecord("Hello World");32#### ndef1.uriRecord(uri)33var ndef1 = require('ndef1');34#### ndef1.mimeMediaRecord(mimeType, payload)35var ndef1 = require('ndef1');36var record = ndef1.mimeMediaRecord("text/plain", "Hello World");
Using AI Code Generation
1var ndef = require('ndef');2 ndef.textRecord('Hello, World'),3];4navigator.nfc.push(message).then(() => {5 console.log('Message pushed.');6}).catch((error) => {7 console.log('Push failed :-( try again.');8});9var ndef = require('ndef');10### ndef.decodeMessage(bytes)11var bytes = new Uint8Array([12]);13var message = ndef.decodeMessage(bytes);14console.log(message);15 {16 }17### ndef.encodeMessage(records)18 {19 }20];
Using AI Code Generation
1var wptools = require('wptools');2wptools.ndef1('Barack Obama', function(err, resp, infobox) {3 console.log(infobox);4});5### wptools.ndef1(name, callback)6### wptools.ndef2(name, callback)7### wptools.ndef3(name, callback)8### wptools.ndef4(name, callback)9### wptools.ndef5(name, callback)10### wptools.ndef6(name, callback)11### wptools.ndef7(name, callback)12### wptools.ndef8(name, callback)13### wptools.ndef9(name, callback)14### wptools.ndef10(name, callback)
Using AI Code Generation
1var ndef = require('wpt-ndef');2var message = ndef1.textRecord('Hello World');3var encoded = ndef1.encodeMessage(message);4console.log(encoded);5### decodeMessage(bytes)6### encodeMessage(message)7### textRecord(text, lang, encoding)8### uriRecord(uri)9### mimeMediaRecord(mimeType, payload)10### emptyRecord()11### smartPosterRecord(records, title)12### absoluteUriRecord(uri)13### externalTypeRecord(type, payload)14### unknownRecord(type, payload)15### recordType(record)16### recordPayload(record)17### toPrintable(record)18### toHexString(bytes)19### toDecString(bytes)20### toBinaryString(bytes)
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var wp = new wptoolkit();3wp.setUsername('username');4wp.setPassword('password');5wp.getPosts(function(err, result) {6 if (err) {7 console.log(err);8 }9 else {10 console.log(result);11 }12});13wp.getPages(function(err, result) {14 if (err) {15 console.log(err);16 }17 else {18 console.log(result);19 }20});21wp.getCategories(function(err, result) {22 if (err) {23 console.log(err);24 }25 else {26 console.log(result);27 }28});29wp.getTags(function(err, result) {30 if (err) {31 console.log(err);32 }33 else {34 console.log(result);35 }36});37wp.getComments(function(err, result) {38 if (err) {39 console.log(err);40 }41 else {42 console.log(result);43 }44});45wp.getUsers(function(err, result) {46 if (err) {47 console.log(err);48 }49 else {50 console.log(result);51 }52});53wp.getOptions(function(err, result) {54 if (err) {55 console.log(err);56 }57 else {58 console.log(result);59 }60});61wp.getPostTypes(function(err, result) {62 if (err) {63 console.log(err);64 }65 else {66 console.log(result);67 }68});69wp.getPostStatuses(function(err, result) {70 if (err) {71 console.log(err);72 }73 else {74 console.log(result);75 }76});77wp.getTaxonomies(function(err, result) {78 if (err) {79 console.log(err);80 }81 else {82 console.log(result);83 }84});85wp.getMediaItems(function(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!!