Best JavaScript code snippet using wpt
global.js
Source:global.js
1import Ember from 'ember';2export default Ember.View.extend({3 isCertificate: true,4 teams: [],5 currentTeamIndex: 0,6 loading: false,7 reload: false,8 currentTeam: Ember.computed('teams', 'currentTeamIndex', function () {9 var t = this.get('teams').objectAt(this.get('currentTeamIndex'));10 if (t) {11 return t;12 } else {13 return null;14 }15 }),16 17 config: Ember.computed('currentTeam.configs', function () {18 if (this.get('currentTeam')) {19 return this.get('currentTeam').get('configs.content').objectAt(0);20 } else {21 return null;22 }23 }),24 instance: Ember.computed('config.instances', function () {25 return this.get('config').get('instances.content').objectAt(0);26 }), 27 28 boardsCompletedPercent: Ember.computed('meta', function () {29 var p = 0;30 if (this.get('meta')) {31 p = (this.get('meta.completed') / this.get('meta.total') * 100).toFixed(2);32 return p;33 }34 return p;35 }),36 lastUpdated: Ember.computed('boards.@each', function () {37 var _this = this;38 if (this.get('boards')) { 39 if (this.get('boards').objectAt(0)) {40 if (_this.get('interval')) {41 clearInterval(_this.get('interval'));42 }43 var interval = setInterval(function () {44 clearInterval(_this.get('interval'));45 _this.set('refreshTime', !_this.get('refreshTime'));46 if (_this.get('boards').objectAt(0)) {47 _this.set('lu', _this.get('boards').objectAt(0).get('updatedAt'));48 } 49 }, 5000);50 _this.set('interval', interval);51 this.set('lu', this.get('boards').objectAt(0).get('updatedAt'));52 return this.get('boards').objectAt(0).get('updatedAt');53 }54 }55 }),56 forces: Ember.computed('votes.@each', 'votes', function () { 57 var forces = [];58 var total= 0;59 if (this.get('votes')) {60 this.get('votes').forEach(function(result) {61 if (result) {62 if (!result.get('totalVotes')) { 63 result.set('totalVotes', 0);64 }65 var force = forces.findProperty('id', result.get('force').get('id'));66 if (!force) {67 force = Ember.Object.create({68 id: result.get('force').get('id'),69 force: result.get('force'),70 total: result.get('totalVotes'),71 candidates: [],72 votes: 0,73 });74 forces.pushObject(force);75 }76 var candidate = force.get('candidates').findProperty('id', result.get('candidate').get('id'));77 if (!candidate) {78 candidate = Ember.Object.create({79 id: result.get('candidate').get('id'),80 candidate: result.get('candidate'),81 votes: 082 });83 force.get('candidates').pushObject(candidate);84 }85 if (!result.get('votes')) { 86 result.set('votes', 0);87 }88 candidate.votes += parseInt(result.get('votes'));89 force.votes += parseInt(result.get('votes'));90 total += parseInt(result.get('votes')); 91 }92 });93 forces.forEach(function (force) {94 force.get('candidates').forEach(function (candidate) {95 if (!force.total) { 96 force.total = 0;97 } 98 var p = (candidate.votes / total * 100).toFixed(2);99 var p2 = (candidate.votes / force.total * 100).toFixed(2);100 101 if (!parseFloat(p2)) {102 p2 = (0).toFixed(2);103 }104 if (!parseFloat(p)) {105 p = (0).toFixed(2);106 }107 candidate.set('percent', p);108 candidate.set('totalPercent', p2);109 }); 110 var bp = (force.get('votes') / total * 100).toFixed(2);111 if (!parseFloat(bp)) {112 bp = (0).toFixed(2);113 } 114 force.set('percent', bp);115 });116 if (forces.get('candidates')) forces.get('candidates').sort(function(a, b){return b.get('percent') - a.get('percent')});117 forces.sort(function(a, b){return b.get('percent') - a.get('percent')});118 }119 return forces;120 }),121 candidates: Ember.computed('votes.@each', function () {122 var candidates = [];123 var total= 0;124 if (this.get('votes')) {125 this.get('votes').forEach(function(result) {126 if (result) {127 var candidate = candidates.findProperty('id', result.get('candidate').get('id'));128 if (!candidate) {129 candidate = Ember.Object.create({130 id: result.get('candidate').get('id'),131 candidate: result.get('candidate'),132 votes: 0,133 validTotal: 0,134 });135 candidates.pushObject(candidate);136 }137 if (!result.get('votes')) { 138 result.set('votes', 0);139 }140 if (!result.get('totalVotes')) { 141 result.set('totalVotes', 0);142 } 143 candidate.votes += parseInt(result.get('votes'));144 total += parseInt(result.get('votes')); 145 candidate.validTotal += parseInt(result.get('totalVotes')); 146 }147 });148 var fpvVotes = 0;149 var validTotal = 0;150 candidates.forEach(function (candidate) {151 var p = (candidate.votes / total * 100).toFixed(2);152 var pt = (candidate.votes / candidate.validTotal * 100).toFixed(2);153 if (!parseFloat(pt)) {154 pt = (0).toFixed(2);155 }156 if (!parseFloat(p)) {157 p = (0).toFixed(2);158 }159 fpvVotes += candidate.votes;160 validTotal = candidate.validTotal;161 candidate.set('percent', p);162 candidate.set('totalPercent', pt);163 }); 164 var fpv = (fpvVotes / validTotal * 100).toFixed(2);165 if (!parseFloat(fpv)) {166 fpv = (0).toFixed(2);167 } 168 this.set('votosFPV', fpv);169 candidates.sort(function(a, b){return b.get('votes') - a.get('votes')});170 }171 return candidates;172 }), 173 _setUpdateTime: function (updateAt) {174 var _this = this;175 this.set('lu', null);176 Ember.run.next(function () {177 _this.set('lu', updateAt);178 })179 }, 180 votesChanged: function () {181 var _this = this;182 if (this.get('config') && this.get('instance') && !this.get('loading')) {183 _this.set('loading', true);184 this.get('store').find('result', { id: this.get('config').get('id'), instance: this.get('instance').get('id'), isCertificate: this.get('isCertificate')}).then(function (votes) {185 if (votes) {186 _this.set('votes', []);187 _this.set('votes', votes);188 _this.set('meta', votes.get('meta'));189 var p = (_this.get('meta.completed') / _this.get('meta.total') * 100).toFixed(2);190 _this.set('ba', _this.get('meta.total'))191 _this.set('bc', _this.get('meta.completed'))192 _this.set('bcPercent', p);193 _this.get('store').find('result', { id: _this.get('config').get('id'), instance: _this.get('instance').get('id'), isBoards: true, isCertificate: _this.get('isCertificate')}).then(function (boards) {194 if (boards) {195 _this.set('lastBoardsLoaded', false);196 _this.set('boards', boards);197 if (_this.get('boards').objectAt(0)) {198 if (_this.get('interval')) {199 clearInterval(_this.get('interval'));200 }201 var interval = setInterval(function () {202 //clearInterval(_this.get('interval'));203 _this.set('refreshTime', !_this.get('refreshTime'));204 if (_this.get('boards').objectAt(0)) {205 _this.set('lu', _this.get('boards').objectAt(0).get('updatedAt'));206 _this._setUpdateTime(_this.get('boards').objectAt(0).get('updatedAt'));207 } 208 }, 5000);209 _this.set('interval', interval);210 if (_this.get('boards').objectAt(0)) {211 _this.set('lu', _this.get('boards').objectAt(0).get('updatedAt'));212 }213 } 214 Ember.run.later(function () {215 _this.set('lastBoardsLoaded', true);216 _this.set('loading', false);217 Ember.run.later(function (){218 _this.set('currentTeamIndexLoaded', _this.get('currentTeamIndex'));219 if (_this.get('currentTeamIndex') == _this.get('teams.length') - 1) {220 _this.set('currentTeamIndex', 0);221 } else {222 _this.set('currentTeamIndex', _this.get('currentTeamIndex') + 1);223 }224 _this.set('votes', [])225 }, 25000) 226 }, 200);227 } 228 }, function (err) {229 console.log('error server');230 _this.set('loading', false);231 _this.toggleProperty('reload');232 });233 } 234 }, function (err) {235 console.log('error server');236 _this.set('loading', false);237 _this.toggleProperty('reload'); 238 });239 }240 241 }.observes('config', 'instance', 'reload'),242 didInsertElement: function () {243 this._super();244 this.votesChanged();245 var _this = this;246 }, 247 winningForces: Ember.computed('forces', function () {248 return this.get('forces').slice(0, 5);249 }),250 restForces: Ember.computed('forces', function () {251 return this.get('forces').slice(5, -1);252 }),...
util.js
Source:util.js
1const XLSX = require('xlsx');2const _ = require('lodash');3const rateTable = require('./rate');4const config = require('./config');5const baseRate = config.getBaseRate();6const bonusRate = config.getBonusRate();7const bindingBonusRate = config.getBindingBonusRate();8const maxBonus = config.getBonusLimit();9const parseXLS = (path) => {10 try {11 const workbook = XLSX.readFile(path);12 const worksheet = workbook.Sheets[workbook.SheetNames[0]];13 const wsjson = XLSX.utils.sheet_to_json(worksheet, {14 header: [15 'transaction_date',16 'posting_date',17 'card',18 'amount',19 'detail',20 'currency',21 'category',22 'comment'23 ]24 });25 const bills = _.drop(wsjson, 1);26 bills.success = true;27 return bills;28 } catch (ex) {29 return { success: false, reason: ex };30 }31};32const convertCurrency = (bills) => {33 return _.map(bills, (bill) => {34 bill.amount = parseInt(bill.amount.replace('NT$', '').replace(',', ''), 10);35 return bill;36 });37};38const sortByPostingDate = (bills) => {39 return _.sortBy(bills, ['posting_date']);40};41const processBills = (bills) => {42 let processedBills = bills;43 processedBills = convertCurrency(processedBills);44 return sortByPostingDate(processedBills);45};46const calculateRebate = (bill, amount, bounsRate) => {47 const outputBill = bill;48 outputBill.cash = {};49 outputBill.cash.base = Math.round(amount * baseRate);50 outputBill.cash.binding = Math.round(amount * bindingBonusRate);51 outputBill.cash.bonus = Math.round(amount * bounsRate);52 return outputBill;53};54const calculateTotalRebate = (bills, start = 0, end = bills.length) => {55 const slicedBills = _.slice(bills, start, end);56 const finalBills = [];57 let validTotal = 0;58 let validBonus = 0;59 _.each(slicedBills, (bill) => {60 const amount = 0 - bill.amount;61 let isCashBack = false;62 _.each(rateTable, (rate) => {63 if (rate.match.test(bill.detail)) {64 if (rate.rate !== 0) {65 validTotal += amount;66 validBonus += amount;67 finalBills.push(calculateRebate(bill, amount, rate.rate));68 } else {69 finalBills.push(calculateRebate(bill, 0, 0));70 }71 if (rate.note) {72 bill.rateNote = rate.note;73 }74 isCashBack = true;75 return false;76 }77 return true;78 });79 if (!isCashBack) {80 validTotal += amount;81 finalBills.push(calculateRebate(bill, amount, 0));82 }83 });84 const basicRebate = Math.round(validTotal * baseRate);85 const bonusRebate = Math.round(validBonus * bonusRate) > maxBonus86 ? maxBonus : Math.round(validBonus * bonusRate);87 const bindingRebate = (Math.round(validTotal * bindingBonusRate) + bonusRebate) > maxBonus88 ? maxBonus - bonusRebate : Math.round(validTotal * bindingBonusRate);89 const totalBonusRebate = Math.round(validBonus * bonusRate)90 + Math.round(validTotal * bindingBonusRate);91 const totalRebate = basicRebate + (totalBonusRebate > maxBonus ? maxBonus : totalBonusRebate);92 return {93 basicRebate,94 bindingRebate,95 bonusRebate,96 totalRebate,97 totalBonusRebate,98 overBounsLimit: (totalBonusRebate > maxBonus),99 bills: finalBills100 };101};102module.exports = {103 parseXLS,104 processBills,105 calculateTotalRebate,106 _: {107 convertCurrency,108 sortByPostingDate,109 processBills,110 calculateRebate111 }...
index.js
Source:index.js
1import { parserV1, parserV2 } from "../tools/parser";2/**3 * Part One Solution4 */5export const passwordValidatorV1 = (input) => {6 let validTotal = 0;7 const formattedInput = parserV1(input);8 formattedInput.forEach(({ min, max, character, password }) => {9 const matchingCharacters = password10 .split("")11 .filter((passwordCharacter) => passwordCharacter === character).length;12 if (matchingCharacters >= min && matchingCharacters <= max) {13 validTotal += 1;14 }15 });16 return validTotal;17};18/**19 * Part Two Solution20 */21export const passwordValidatorV2 = (input) => {22 let validTotal = 0;23 const formattedInput = parserV2(input);24 formattedInput.forEach(({ firstIndex, secondIndex, character, password }) => {25 if (26 (password[firstIndex] === character &&27 password[secondIndex] !== character) ||28 (password[firstIndex] !== character &&29 password[secondIndex] === character)30 ) {31 validTotal += 1;32 }33 });34 return validTotal;...
Using AI Code Generation
1var wpt = require('./wpt.js');2var validTotal = wpt.validTotal(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);3console.log(validTotal);4var validTotal = function() {5 var total = 0;6 for (var i = 0; i < arguments.length; i++) {7 total += arguments[i];8 }9 return total;10}11module.exports.validTotal = validTotal;12var wpt = require('./wpt.js');13var validTotal = wpt.validTotal(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);14console.log(validTotal);15var validTotal = function() {16 var total = 0;17 for (var i = 0; i < arguments.length; i++) {18 total += arguments[i];19 }20 return total;21}22module.exports.validTotal = validTotal;23var wpt = require('./wpt.js');24var validTotal = wpt.validTotal(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);25console.log(validTotal);26var validTotal = function() {27 var total = 0;28 for (var i = 0; i < arguments.length; i++) {29 total += arguments[i];30 }31 return total;32}33module.exports.validTotal = validTotal;34var wpt = require('./wpt.js');35var validTotal = wpt.validTotal(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);36console.log(validTotal);37var validTotal = function() {38 var total = 0;
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new wpt('yourkeyhere');3wpt.validTotal(function(err, data) {4 if (err) {5 console.log('Error: ' + err);6 } else {7 console.log(data);8 }9});10{11 "data": {12 }13}14var wpt = require('wpt');15var wpt = new wpt('yourkeyhere');16wpt.validTotal(function(err, data) {17 if (err) {18 console.log('Error: ' + err);19 } else {20 console.log(data);21 }22});23{24 "data": {25 }26}27var wpt = require('wpt');28var wpt = new wpt('yourkeyhere');29wpt.validTotal(function(err, data) {30 if (err) {31 console.log('Error: ' + err);32 } else {33 console.log(data);34 }35});
Using AI Code Generation
1var wpt = require('wpt');2var wptInstance = new wpt('your_key');3wptInstance.validTotal(function(err, data) {4 console.log(data);5});6var wpt = require('wpt');7var wptInstance = new wpt('your_key');8wptInstance.validTotal(function(err, data) {9 console.log(data);10});11var wpt = require('wpt');12var wptInstance = new wpt('your_key');13wptInstance.validTotal(function(err, data) {14 console.log(data);15});16var wpt = require('wpt');17var wptInstance = new wpt('your_key');18wptInstance.validTotal(function(err, data) {19 console.log(data);20});21var wpt = require('wpt');22var wptInstance = new wpt('your_key');23wptInstance.validTotal(function(err, data) {24 console.log(data);25});26var wpt = require('wpt');27var wptInstance = new wpt('your_key');28wptInstance.validTotal(function(err, data) {29 console.log(data);30});31var wpt = require('wpt');32var wptInstance = new wpt('your_key');33wptInstance.validTotal(function(err, data) {34 console.log(data);35});36var wpt = require('wpt');37var wptInstance = new wpt('your_key');38wptInstance.validTotal(function(err, data) {39 console.log(data);40});41var wpt = require('wpt');42var wptInstance = new wpt('your_key');43wptInstance.validTotal(function(err, data) {44 console.log(data);45});
Using AI Code Generation
1const wpt = require('webpagetest');2const validTotal = wpt.validTotal;3const total = 10;4const isValid = validTotal(total);5console.log(isValid);6 * @param {Number} total - total number of tests running on the server7 * @returns {Boolean}8WebPageTest.validTotal = function(total) {9 if (total < 500) {10 return true;11 } else {12 return false;13 }14};15 * @param {Number} total - total number of tests running on the server16 * @returns {Boolean}17WebPageTest.validTotal = function(total) {18 if (total < 500) {19 return true;20 } else {21 return false;22 }23};24 * @param {Number} total - total number of tests running on the server25 * @returns {Boolean}26WebPageTest.validTotal = function(total) {27 if (total < 500) {28 return true;29 } else {30 return false;31 }32};
Using AI Code Generation
1var wpt = require('wpt-api');2var wpt = new wpt('API_KEY');3wpt.validTotal(function(err, data) {4 console.log(data);5});6var wpt = require('wpt-api');7var wpt = new wpt('API_KEY');8wpt.validTotal(function(err, data) {9 console.log(data);10});11var wpt = require('wpt-api');12var wpt = new wpt('API_KEY');13wpt.validTotal(function(err, data) {14 console.log(data);15});16var wpt = require('wpt-api');17var wpt = new wpt('API_KEY');18wpt.validTotal(function(err, data) {19 console.log(data);20});21var wpt = require('wpt-api');22var wpt = new wpt('API_KEY');23wpt.validTotal(function(err, data) {24 console.log(data);25});26var wpt = require('wpt-api');27var wpt = new wpt('API_KEY');28wpt.validTotal(function(err, data) {29 console.log(data);30});
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!!