Best JavaScript code snippet using wpt
Logger.js
Source:Logger.js
1'use strict';2describe('yeap_logger.Logger', ()=>{3 const assert = require('assert');4 let api;5 before(()=>{6 const re = (module)=>{ return require('../' + module); }7 api = re('index');8 api.Logger._getInstance()._cout = (message)=>{9 lastLine = message;10 };11 });12 describe('static methods', ()=>{13 let lastLine;14 before(()=>{15 api.Logger._getInstance()._cout = (message)=>{16 lastLine = message;17 };18 });19 it('Check instantiation', ()=>{20 const i1 = api.Logger._getInstance();21 const i2 = api.Logger._getInstance();22 assert.equal(i1, i2);23 const i3 = new api.Logger();24 assert.notEqual(i1, i3);25 });26 it('lib.Logger.system', ()=>{27 api.Logger.system('AAAAA');28 assert.notEqual(-1, lastLine.indexOf('system'));29 assert.notEqual(-1, lastLine.indexOf('AAAAA'));30 });31 it('lib.Logger.fatal', ()=>{32 api.Logger.fatal('BBBBB');33 assert.notEqual(-1, lastLine.indexOf('fatal'));34 assert.notEqual(-1, lastLine.indexOf('BBBBB'));35 });36 it('lib.Logger.error', ()=>{37 api.Logger.error('CCCCC');38 assert.notEqual(-1, lastLine.indexOf('error'));39 assert.notEqual(-1, lastLine.indexOf('CCCCC'));40 });41 it('lib.Logger.warn', ()=>{42 api.Logger.warn('DDDDD');43 assert.notEqual(-1, lastLine.indexOf('warn'));44 assert.notEqual(-1, lastLine.indexOf('DDDDD'));45 });46 it('lib.Logger.info', ()=>{47 api.Logger.info('EEEEE');48 assert.notEqual(-1, lastLine.indexOf('info'));49 assert.notEqual(-1, lastLine.indexOf('EEEEE'));50 });51 it('lib.Logger.debug', ()=>{52 api.Logger.debug('FFFFF');53 assert.notEqual(-1, lastLine.indexOf('debug'));54 assert.notEqual(-1, lastLine.indexOf('FFFFF'));55 });56 it('context switching', ()=>{57 api.Logger.ctx('CTXA').debug('aaa1');58 assert.notEqual(-1, lastLine.indexOf('CTXA'));59 assert.notEqual(-1, lastLine.indexOf('aaa1'));60 api.Logger.ctx('CTXA').info('aaa2');61 assert.notEqual(-1, lastLine.indexOf('CTXA'));62 assert.notEqual(-1, lastLine.indexOf('aaa2'));63 api.Logger.debug('aaa3');64 assert.equal(-1, lastLine.indexOf('CTXA'));65 assert.notEqual(-1, lastLine.indexOf('aaa3'));66 api.Logger.ctx('CTXB').warn('bbb1');67 assert.notEqual(-1, lastLine.indexOf('CTXB'));68 assert.notEqual(-1, lastLine.indexOf('bbb1'));69 api.Logger.ctx('CTXB').error('bbb2');70 assert.notEqual(-1, lastLine.indexOf('CTXB'));71 assert.notEqual(-1, lastLine.indexOf('bbb2'));72 api.Logger.debug('bbb3');73 assert.equal(-1, lastLine.indexOf('CTXB'));74 assert.notEqual(-1, lastLine.indexOf('bbb3'));75 });76 it('2 conextes at once', ()=>{77 api.Logger.ctx('CTX1').ctx('CTX2').debug('yyy');78 const ctx1Index = lastLine.indexOf('CTX1');79 assert.notEqual(-1, ctx1Index);80 const ctx2Index = lastLine.indexOf('CTX2');81 assert.notEqual(-1, ctx2Index);82 assert(ctx1Index < ctx2Index);83 });84 it('3 conextes at once', ()=>{85 api.Logger.ctx('CTX1').ctx('CTX2').ctx('CTX3').debug('zzz');86 const ctx1Index = lastLine.indexOf('CTX1');87 assert.notEqual(-1, ctx1Index);88 const ctx2Index = lastLine.indexOf('CTX2');89 assert.notEqual(-1, ctx2Index);90 const ctx3Index = lastLine.indexOf('CTX3');91 assert.notEqual(-1, ctx3Index);92 assert(ctx1Index < ctx2Index);93 assert(ctx2Index < ctx3Index);94 });95 it('Dumping object', ()=>{96 const obj = {name:'Julia', phone:'12345', age:37, kids:[{name:'John', age:3}, {name:'Paola', age:13}]};97 api.Logger.ctx('CTX1').ctx('CTX2').ctx('CTX3').debug(obj);98 const expected = "{\n" +99 " name: 'Julia',\n" +100 " phone: '12345',\n" +101 ' age: 37,\n' +102 " kids: [ { name: 'John', age: 3 }, { name: 'Paola', age: 13 } ]\n" +103 "}";104 const index = lastLine.indexOf(expected);105 assert.notEqual(-1, index);106 });107 });108 describe('instance methods', ()=>{109 let logger, lastLine;110 before(()=>{111 logger = new api.Logger();112 logger._cout = (message)=>{113 lastLine = message;114 };115 });116 it('logger.system', ()=>{117 logger.system('11111');118 assert.notEqual(-1, lastLine.indexOf('system'));119 assert.notEqual(-1, lastLine.indexOf('11111'));120 });121 it('logger.fatal', ()=>{122 logger.fatal('22222');123 assert.notEqual(-1, lastLine.indexOf('fatal'));124 assert.notEqual(-1, lastLine.indexOf('22222'));125 });126 it('logger.error', ()=>{127 logger.error('33333');128 assert.notEqual(-1, lastLine.indexOf('error'));129 assert.notEqual(-1, lastLine.indexOf('33333'));130 });131 it('logger.warn', ()=>{132 logger.warn('44444');133 assert.notEqual(-1, lastLine.indexOf('warn'));134 assert.notEqual(-1, lastLine.indexOf('44444'));135 });136 it('logger.info', ()=>{137 logger.info('55555');138 assert.notEqual(-1, lastLine.indexOf('info'));139 assert.notEqual(-1, lastLine.indexOf('55555'));140 });141 it('logger.debug', ()=>{142 logger.debug('66666');143 assert.notEqual(-1, lastLine.indexOf('debug'));144 assert.notEqual(-1, lastLine.indexOf('66666'));145 });146 it('context switching', ()=>{147 logger.ctx('CTXA').debug('aaa1');148 assert.notEqual(-1, lastLine.indexOf('CTXA'));149 assert.notEqual(-1, lastLine.indexOf('aaa1'));150 logger.ctx('CTXA').info('aaa2');151 assert.notEqual(-1, lastLine.indexOf('CTXA'));152 assert.notEqual(-1, lastLine.indexOf('aaa2'));153 logger.debug('aaa3');154 assert.equal(-1, lastLine.indexOf('CTXA'));155 assert.notEqual(-1, lastLine.indexOf('aaa3'));156 logger.ctx('CTXB').warn('bbb1');157 assert.notEqual(-1, lastLine.indexOf('CTXB'));158 assert.notEqual(-1, lastLine.indexOf('bbb1'));159 logger.ctx('CTXB').error('bbb2');160 assert.notEqual(-1, lastLine.indexOf('CTXB'));161 assert.notEqual(-1, lastLine.indexOf('bbb2'));162 logger.debug('bbb3');163 assert.equal(-1, lastLine.indexOf('CTXB'));164 assert.notEqual(-1, lastLine.indexOf('bbb3'));165 });166 it('2 conextes at once', ()=>{167 logger.ctx('CTX1').ctx('CTX2').debug('yyy');168 const ctx1Index = lastLine.indexOf('CTX1');169 assert.notEqual(-1, ctx1Index);170 const ctx2Index = lastLine.indexOf('CTX2');171 assert.notEqual(-1, ctx2Index);172 assert(ctx1Index < ctx2Index);173 });174 it('3 conextes at once', ()=>{175 logger.ctx('CTX1').ctx('CTX2').ctx('CTX3').debug('zzz');176 const ctx1Index = lastLine.indexOf('CTX1');177 assert.notEqual(-1, ctx1Index);178 const ctx2Index = lastLine.indexOf('CTX2');179 assert.notEqual(-1, ctx2Index);180 const ctx3Index = lastLine.indexOf('CTX3');181 assert.notEqual(-1, ctx3Index);182 assert(ctx1Index < ctx2Index);183 assert(ctx2Index < ctx3Index);184 });185 it('Dumping object', ()=>{186 const obj = {name:'Julia', phone:'12345', age:37, kids:[{name:'John', age:3}, {name:'Paola', age:13}]};187 logger.ctx('CTX1').ctx('CTX2').ctx('CTX3').debug(obj);188 const expected = "{\n" +189 " name: 'Julia',\n" +190 " phone: '12345',\n" +191 ' age: 37,\n' +192 " kids: [ { name: 'John', age: 3 }, { name: 'Paola', age: 13 } ]\n" +193 "}";194 const index = lastLine.indexOf(expected);195 assert.notEqual(-1, index);196 });197 });...
printOrder.ts
Source:printOrder.ts
1import jsPDF from 'jspdf';2import { IOrderCourse, IStorageCourse } from '../../../types';3const printOrder = (4 storageCourses: IStorageCourse[],5 courses: IOrderCourse[],6 orderNum: number,7 revenue: number,8 people: number9) => {10 // prep useful data11 const today = new Date();12 const year = today.getFullYear();13 const date = `${year}/${today.getMonth() + 1}/${today.getDate()}`;14 const time = `${today.getHours()}:${today.getMinutes()}`;15 const listXStart = 30;16 const doc = new jsPDF();17 doc.setFontType('bold');18 doc.setTextColor('#4caf50');19 doc.setFontSize(30);20 let lastLine = 22; // initial line21 doc.text(10, lastLine, 'CNGEI - Sagra del pesto 2020');22 lastLine += 12;23 doc.setTextColor('#ff5722');24 doc.setFontType('normal');25 doc.setFontSize(20);26 doc.text(10, lastLine, `Ord. ${orderNum || 0}`);27 doc.text(50, lastLine, `Tot: â¬${revenue || 0}`);28 doc.text(90, lastLine, `Coperti ${people || 0}`);29 doc.text(140, lastLine, `${date} - ${time}`);30 lastLine += 10;31 doc.setTextColor('#000000');32 storageCourses.forEach(({ courseName, dishes }) => {33 doc.setFontType('bold');34 doc.setFontSize(15);35 doc.text(listXStart, lastLine, `${courseName}`);36 lastLine += 7;37 doc.setFontType('normal');38 doc.setFontSize(14);39 dishes.forEach(({ qt, price, name, shortName }) => {40 const orderQt = courses41 .find(course => course.courseName === courseName)42 ?.dishes.find(dish => dish.shortName === shortName)?.qt;43 doc.text(listXStart + 15, lastLine, `${name}`);44 doc.text(listXStart + 85, lastLine, `⬠${price.toFixed(2)}`);45 doc.text(listXStart + 120, lastLine, `${orderQt || '-'}`);46 lastLine += 6;47 });48 lastLine += 4;49 });50 doc.autoPrint();51 doc.output('dataurlnewwindow');52};...
Utils.ts
Source:Utils.ts
1import { TimelineRegionKeyframe } from "../../Types";2export const visualizeLifespans = (keyframes: TimelineRegionKeyframe[], step: number) => {3 if (keyframes.length === 0) return [];4 const lines = [];5 const start = keyframes[0].frame - 1;6 for(let i = 0, l = keyframes.length; i < l; i++) {7 const lastLine = lines[lines.length - 1];8 const point = keyframes[i];9 const prevPoint = keyframes[i-1];10 const offset = (point.frame - start - 1) * step;11 if (!lastLine || !lastLine?.enabled) {12 lines.push({13 offset,14 width: 0,15 length: 0,16 enabled: point.enabled,17 start: point.frame,18 points: [point],19 });20 } else if (prevPoint?.enabled) {21 lastLine.width = (point.frame - lastLine.points[0].frame) * step;22 lastLine.length = point.frame - lastLine.start;23 lastLine.enabled = point.enabled;24 lastLine.points.push(point);25 }26 }27 return lines;28};29export const findClosestKeypoint = (frames: number[], position: number, direction: -1 | 1) => {30 const targetFrames = frames.filter(f => direction === -1 ? f < position : f > position);31 return targetFrames[direction === -1 ? targetFrames.length - 1 : 0] ?? position;...
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!!