Best JavaScript code snippet using wpt
images.js
Source:images.js
1const styleEl = createElement('style')2nestElements(document.head, [styleEl])3const dynamicStyles = styleEl.sheet4dynamicStyles.list = []5let formCount = 06let activeForms7activeForms = 08// Fetch content data on page load and insert into DOM9const renderImages = async (preResponse = null) => {10 const imageDisplay = searchForOne('div.asset-grid')11 let content12 if (searchTargets) {13 empty(imageDisplay, () => {14 searchTargets = null15 })16 }17 const deleteImage = async (eRef) => {18 const request = {19 method: 'DELETE',20 body: JSON.stringify({21 image_id: parseInt(eRef.value)22 }),23 headers: {24 'Content-Type': 'application/json'25 }26 }27 const response = await fetch('/admin/assets/delete', request).then((r) =>28 r.json()29 )30 renderImages(response)31 }32 const handleNotification = (json, asset) => {33 const cancel = (e) => {34 e.currentTarget.parentElement.remove()35 }36 const confirm = (e) => {37 e.currentTarget.parentElement.remove()38 deleteImage({ value: e.currentTarget.value })39 }40 const exitBtn = createElement(41 'button',42 {43 class: 'notif-exit-btn'44 },45 'X'46 )47 const confirmBtn = createElement(48 'button',49 {50 class: 'notif-confirm-btn',51 value: asset.dataset.imageId52 },53 'Confirm'54 )55 exitBtn.addEventListener('click', cancel)56 confirmBtn.addEventListener('click', confirm)57 nestElements(searchForOne('.editors'), [58 nestElements(59 createElement('div', {60 class: 'notification',61 'data-linked-image': asset.dataset.imageId62 }),63 [64 createElement(65 'p',66 { class: 'notification-msg' },67 `${json.length - 1} pieces of content will lose ${68 asset.querySelector('p').innerText69 } image\n\nAre you sure you wish to delete it?`70 ),71 exitBtn,72 confirmBtn73 ]74 )75 ])76 }77 const checkForLinkedContent = async (e) => {78 const thisAsset = e.target.closest('.asset-box')79 const request = {80 method: 'POST',81 body: JSON.stringify({82 image_id: parseInt(e.target.closest('button').value)83 }),84 headers: {85 'Content-Type': 'application/json'86 }87 }88 const response = await fetch('/admin/assets/delete', request).then((r) =>89 r.json()90 )91 if (response[0].message === 1) {92 handleNotification(response, thisAsset)93 } else if (response[0].message === 0) {94 await deleteImage(e.target.closest('button'))95 }96 }97 const viewFullImage = (e) => {98 const { 1: thisImage } = e.target.closest('.asset-box').children99 window.open(thisImage.src, '_blank')100 }101 const selectThisContent = (e) => {102 const div = e.currentTarget103 if (searchForOne(`div[data-linked-image="${div.dataset.imageId}"]`)) return104 const parentDiv = div.parentElement105 parentDiv.querySelectorAll('.selected').forEach((d) => {106 if (d !== div) {107 d.classList.toggle('selected')108 d.querySelector('.asset-box > .btn-group').remove()109 }110 })111 div.classList.toggle('selected')112 if (div.classList[div.classList.length - 1] === 'selected') {113 const deleteBtn = createElement('button', {114 type: 'button',115 value: div.dataset.imageId116 })117 const replaceInput = createElement('input', {118 type: 'file',119 name: 'file',120 'data-image-id': div.dataset.imageId,121 class: 'replace-image',122 autocomplete: 'off',123 onchange: "generateImageEditor(event, 'edit')"124 })125 const replaceBtn = nestElements(126 createElement('label', { class: 'replace' }),127 [128 replaceInput,129 nestElements(130 createElement('div', {131 class: 'replace-image-button',132 value: div.dataset.imageId133 }),134 [135 nestElements(136 createSVG('svg', null, [112.18, 93.927], ['100%', '100%']),137 [138 nestElements(139 createSVG('g', {140 transform: 'translate(-17.601098,6.0505133)',141 fill: 'white',142 stroke: '#000'143 }),144 [145 createSVG('path', {146 d: 'm58.876 48.925-40.5-40.5-0.30907 40.809z',147 'stroke-linecap': 'round',148 'stroke-linejoin': 'round',149 'stroke-width': '.5'150 }),151 createSVG('path', {152 d:153 'm47.719 36.249s12.557-22.668 49.486-41.671c-40.32 5.2933-66.153 25.004-66.153 25.004z',154 'stroke-width': '.5'155 }),156 createSVG('path', {157 d: 'm88.326 32.974 40.5 40.5 0.30908-40.809z',158 'stroke-linecap': 'round',159 'stroke-linejoin': 'round',160 'stroke-width': '.5'161 }),162 createSVG('path', {163 d:164 'm99.483 45.65s-12.557 22.668-49.486 41.671c40.32-5.2933 66.153-25.004 66.153-25.004z',165 'stroke-width': '.5'166 })167 ]168 )169 ]170 )171 ]172 )173 ]174 )175 const previewBtn = nestElements(176 createElement('button', {177 type: 'button'178 }),179 [180 nestElements(181 createSVG('svg', null, [91.81, 44.185], ['100%', '100%']),182 [183 nestElements(184 createSVG('g', {185 fill: 'none',186 stroke: 'white',187 'stroke-width': '6'188 }),189 [190 createSVG('path', {191 d:192 'm7.0794 17.67c8.009-5.149 26.823-16.598 39.695-16.598 12.872 1.482e-4 30.69 11.449 38.275 16.598 7.5848 5.1489 7.5848 5.6992-1.12e-4 10.404s-25.275 15.038-38.278 15.038c-13.003 1.37e-4 -31.682-10.332-39.691-15.038-8.0091-4.7053-8.0091-5.2557-1.34e-4 -10.405z'193 }),194 createSVG('ellipse', {195 cx: '45.905',196 cy: '22.093',197 rx: '14.145',198 ry: '10.543',199 'stroke-linecap': 'round',200 'stroke-linejoin': 'round',201 fill: 'white',202 stroke: 'white'203 })204 ]205 )206 ]207 )208 ]209 )210 const controls = fragmentElements([211 nestElements(createElement('div', { class: 'btn-group' }), [212 previewBtn,213 replaceBtn,214 nestElements(deleteBtn, [215 nestElements(216 createSVG(217 'svg',218 { class: 'trash-can' },219 [105.83, 119.06],220 ['100%', '100%']221 ),222 [223 createSVG('path', {224 d:225 'm22.951 30.289h61.61c0.5235 0 0.99476 0.42382 0.94494 0.94494l-7.4083 77.485c-0.04982 0.52112-5.3265 0.94494-5.85 0.94494h-38.437c-0.5235 0-5.9444-0.42291-5.9836-0.94494l-5.8208-77.485c-0.03922-0.52203 0.42144-0.94494 0.94494-0.94494z',226 style: 'fill-rule:evenodd;fill:#999;stroke-width:0'227 }),228 createSVG('rect', {229 x: 51.815,230 y: 49.944,231 width: 2.0045,232 height: 44.099,233 ry: 1.0023,234 style:235 'fill-rule:evenodd;fill:#ececec;stroke-width:.265;stroke:#999'236 }),237 createSVG('rect', {238 x: 60.142,239 y: 48.893,240 width: 2.0045,241 height: 44.099,242 ry: 1.0023,243 style:244 'fill-rule:evenodd;fill:#ececec;stroke-width:.265;stroke:#999',245 transform: 'rotate(1)'246 }),247 createSVG('rect', {248 x: 68.447,249 y: 47.579,250 width: 2.0045,251 height: 44.099,252 ry: 1.0023,253 style:254 'fill-rule:evenodd;fill:#ececec;stroke-width:.265;stroke:#999',255 transform: 'rotate(2)'256 }),257 createSVG('rect', {258 x: 76.722,259 y: 46.004,260 width: 2.0045,261 height: 44.099,262 ry: 1.0023,263 style:264 'fill-rule:evenodd;fill:#ececec;stroke-width:.265;stroke:#999',265 transform: 'rotate(3)'266 }),267 createSVG('rect', {268 x: 43.472,269 y: 50.733,270 width: 2.0045,271 height: 44.099,272 ry: 1.0023,273 style:274 'fill-rule:evenodd;fill:#ececec;stroke-width:.265;stroke:#999',275 transform: 'rotate(-1)'276 }),277 createSVG('rect', {278 x: 35.12,279 y: 51.26,280 width: 2.0045,281 height: 44.099,282 ry: 1.0023,283 style:284 'fill-rule:evenodd;fill:#ececec;stroke-width:.265;stroke:#999',285 transform: 'rotate(-2)'286 }),287 createSVG('rect', {288 x: 26.764,289 y: 51.523,290 width: 2.0045,291 height: 44.099,292 ry: 1.0023,293 style:294 'fill-rule:evenodd;fill:#ececec;stroke-width:.265;stroke:#999',295 transform: 'rotate(-3)'296 }),297 nestElements(298 createSVG('g', {299 class: 'trash-can-lid',300 transform: 'translate(-.060786 6.35)'301 }),302 [303 createSVG('path', {304 d: 'm33.872 12.653v-9.472h38.437v9.472',305 style: 'fill:none;stroke-width:.26458px;stroke:#999'306 }),307 createSVG('path', {308 d: 'm40.128 12.248v-6.0598h25.862v6.0598',309 style: 'fill:none;stroke-width:.27166;stroke:#999'310 }),311 createSVG('path', {312 d:313 'm34.015 8.0121v-4.6771h38.166v9.3543h-6.5619v-6.1471h-25.042v6.1471h-6.5619z',314 style:315 'fill-rule:evenodd;fill:#999;stroke-width:.13398;stroke:#999'316 }),317 createSVG('rect', {318 x: 10.093,319 y: 22.817,320 width: 85.86,321 height: 2.0713,322 style: 'fill-rule:evenodd;fill:#999;stroke-width:0'323 }),324 createSVG('rect', {325 x: 9.9796,326 y: 12.233,327 width: 85.86,328 height: 2.0713,329 style: 'fill-rule:evenodd;fill:#999;stroke-width:0'330 }),331 createSVG('rect', {332 x: 95.806,333 y: 12.233,334 width: 2.0714,335 height: 12.655,336 style: 'fill-rule:evenodd;fill:#999;stroke-width:0'337 }),338 createSVG('rect', {339 x: 8.077,340 y: 12.233,341 width: 2.0714,342 height: 12.655,343 style: 'fill-rule:evenodd;fill:#999;stroke-width:0'344 }),345 createSVG('path', {346 d: 'm10.168 18.561v-4.2256h85.665v8.4513h-85.665z',347 style:348 'fill-rule:evenodd;fill:#fff;stroke-width:.034606;stroke:#fff'349 })350 ]351 )352 ]353 )354 ])355 ])356 ])357 deleteBtn.addEventListener('click', checkForLinkedContent)358 // replaceInput.addEventListener('onchange', toggleReplaceForm)359 previewBtn.addEventListener('click', viewFullImage)360 div.prepend(controls)361 } else {362 div.querySelector('.asset-box > .btn-group').remove()363 }364 }365 if (preResponse === null) {366 const response = await fetch('/admin/assets/read').then((r) => r.json())367 content = response368 } else {369 content = preResponse370 }371 empty(imageDisplay)372 content.forEach((c) => {373 const img = createElement('img', {374 class: 'thumbnail',375 src: c.image_link,376 alt: c.image_name377 })378 const assetBox = nestElements(379 createElement('div', {380 class: 'asset-box',381 'data-image-id': c.id382 }),383 [384 fragmentElements([385 img,386 createElement('p', { style: `text-align: right;` }, c.image_name)387 ])388 ]389 )390 assetBox.addEventListener('click', selectThisContent)391 imageDisplay.appendChild(assetBox)392 })393}394const generateImageEditor = (e, flag) => {395 const thisInput = e.target396 console.log(thisInput)397 const gate = flag398 const [file] = thisInput.files399 const exitBtn = createElement(400 'button',401 {402 type: 'button',403 class: 'exit-btn'404 },405 'X'406 )407 const boxTitle = createElement(408 'span',409 {410 class: 'box-title'411 },412 gate === 'edit' ? 'Replace Image' : 'Upload New Image'413 )414 const titleBar = nestElements(createElement('div', { class: 'title-bar' }), [415 boxTitle,416 exitBtn417 ])418 const fileNameSpan = createElement('p', { id: 'file-selected' }, file.name)419 const nameLabel = createElement(420 'label',421 { for: 'new-image-name' },422 'New image name'423 )424 const newImageName = createElement('input', {425 type: 'text',426 name: 'new-image-name',427 id: 'new-image-name'428 })429 const uploadButton = createElement(430 'button',431 { class: 'upload-btn' },432 'Upload'433 )434 const displayContainer = nestElements(435 createElement('div', { class: 'display-for-image-info' }),436 [fileNameSpan, nameLabel, newImageName, uploadButton]437 )438 const uploadNewImage = async (e) => {439 const imageEditor = e.target.closest('div.image-uploader')440 const loader = createLoadingSpinner()441 loader.style.cssText = `position: static; transform: translate(0,0);`442 const fd = new FormData()443 if (gate === 'new') {444 fd.append('new_image', file)445 fd.append('image_name', document.querySelector('#new-image-name').value)446 } else if (gate === 'edit') {447 fd.append('new_image', file)448 fd.append('image_name', document.querySelector('#new-image-name').value)449 fd.append('image_id', thisInput.dataset.imageId)450 }451 const url =452 gate === 'edit' ? '/admin/assets/replace' : '/admin/assets/create'453 const request = {454 method: 'POST',455 body: fd456 }457 empty(displayContainer)458 displayContainer.style.cssText = `align-items: center;`459 nestElements(displayContainer, [loader])460 const response = await fetch(url, request).then((r) => r.json())461 exitBtn.click()462 renderImages(response)463 }464 uploadButton.addEventListener('click', uploadNewImage)465 const editNewImageInfo = nestElements(466 createElement('div', { class: 'image-uploader content-editor' }),467 [fragmentElements([titleBar, displayContainer])]468 )469 exitBtn.addEventListener('click', handleExit)470 const existingForms = searchForAll('.editors > .content-editor')471 activeForms = existingForms.length472 dynamicStyles.insertRule(473 `div.form-count${formCount} { transform: matrix(1, 0, 0, 1, -3.37, ${474 activeForms * 35475 }); transition: all 0.2s linear; }`,476 formCount477 )478 dynamicStyles.list.push(formCount)479 console.log('before', dynamicStyles)480 editNewImageInfo.setAttribute('data-form-count', formCount)481 transition(482 'in',483 editNewImageInfo,484 `form-count${formCount}`,485 searchForOne('.editors')486 )487 formCount += 1488 return formCount...
svg.test.ts
Source:svg.test.ts
1import { strictEqual } from 'assert'2import { createSVG, svg, pixel } from './svg'3describe('svg', () => {4 describe('When the balance is 0', () => {5 it('output is -', () => {6 const expect = '-'7 strictEqual(createSVG({ balance: expect, width: pixel(expect) }), svg(0))8 })9 })10 describe('Represent a numerical value with 6 or fewer characters', () => {11 it('0.0001 is 0.000+', () => {12 const expect = '0.000+'13 strictEqual(14 createSVG({ balance: expect, width: pixel(expect) }),15 svg(0.0001)16 )17 })18 it('0.xxx is 0.xxx', () => {19 const expect = '0.123'20 strictEqual(21 createSVG({ balance: expect, width: pixel(expect) }),22 svg(0.123)23 )24 })25 it('9.999 is 9.999', () => {26 const expect = '9.999'27 strictEqual(28 createSVG({ balance: expect, width: pixel(expect) }),29 svg(9.999)30 )31 })32 it('9.9991 is 9.999+', () => {33 const expect = '9.999+'34 strictEqual(35 createSVG({ balance: expect, width: pixel(expect) }),36 svg(9.9991)37 )38 })39 it('99.99 is 99.99', () => {40 const expect = '99.99'41 strictEqual(42 createSVG({ balance: expect, width: pixel(expect) }),43 svg(99.99)44 )45 })46 it('99.991 is 99.99+', () => {47 const expect = '99.99+'48 strictEqual(49 createSVG({ balance: expect, width: pixel(expect) }),50 svg(99.991)51 )52 })53 it('999.9 is 999.9', () => {54 const expect = '999.9'55 strictEqual(56 createSVG({ balance: expect, width: pixel(expect) }),57 svg(999.9)58 )59 })60 it('999.91 is 999.9+', () => {61 const expect = '999.9+'62 strictEqual(63 createSVG({ balance: expect, width: pixel(expect) }),64 svg(999.91)65 )66 })67 it('1000 is 1000', () => {68 const expect = '1000'69 strictEqual(70 createSVG({ balance: expect, width: pixel(expect) }),71 svg(1000)72 )73 })74 it('1000.1 is 1k', () => {75 const expect = '1k'76 strictEqual(77 createSVG({ balance: expect, width: pixel(expect) }),78 svg(1000.1)79 )80 })81 it('1230 is 1.23k', () => {82 const expect = '1.23k'83 strictEqual(84 createSVG({ balance: expect, width: pixel(expect) }),85 svg(1230)86 )87 })88 it('1234.1 is 1.23k', () => {89 const expect = '1.23k'90 strictEqual(91 createSVG({ balance: expect, width: pixel(expect) }),92 svg(1234.1)93 )94 })95 it('10000 is 10k', () => {96 const expect = '10k'97 strictEqual(98 createSVG({ balance: expect, width: pixel(expect) }),99 svg(10000)100 )101 })102 it('12300 is 12.3k', () => {103 const expect = '12.3k'104 strictEqual(105 createSVG({ balance: expect, width: pixel(expect) }),106 svg(12300)107 )108 })109 it('12345 is 12.3k', () => {110 const expect = '12.3k'111 strictEqual(112 createSVG({ balance: expect, width: pixel(expect) }),113 svg(12345)114 )115 })116 it('1000000000 is 1G', () => {117 const expect = '1G'118 strictEqual(119 createSVG({ balance: expect, width: pixel(expect) }),120 svg(1000000000)121 )122 })123 it('1230000000 is 1.23G', () => {124 const expect = '1.23G'125 strictEqual(126 createSVG({ balance: expect, width: pixel(expect) }),127 svg(1230000000)128 )129 })130 it('1234567890 is 1.23G', () => {131 const expect = '1.23G'132 strictEqual(133 createSVG({ balance: expect, width: pixel(expect) }),134 svg(1234567890)135 )136 })137 it('100000000000000 is 100T', () => {138 const expect = '100T'139 strictEqual(140 createSVG({ balance: expect, width: pixel(expect) }),141 svg(100000000000000)142 )143 })144 it('12300000000000 is 12.3T', () => {145 const expect = '12.3T'146 strictEqual(147 createSVG({ balance: expect, width: pixel(expect) }),148 svg(12300000000000)149 )150 })151 it('12345678901230 is 12.3T', () => {152 const expect = '12.3T'153 strictEqual(154 createSVG({ balance: expect, width: pixel(expect) }),155 svg(12345678901230)156 )157 })158 })...
Using AI Code Generation
1var wpt = require('wpt');2 if (err) {3 console.log(err);4 } else {5 console.log(data);6 }7});8var wpt = require('wpt');9wpt.getLocations(function(err, data) {10 if (err) {11 console.log(err);12 } else {13 console.log(data);14 }15});16var wpt = require('wpt');17wpt.getTesters(function(err, data) {18 if (err) {19 console.log(err);20 } else {21 console.log(data);22 }23});24var wpt = require('wpt');25wpt.getTestStatus('140329_5G_1', function(err, data) {26 if (err) {27 console.log(err);28 } else {29 console.log(data);30 }31});32var wpt = require('wpt');33wpt.getTestResults('140329_5G_1', function(err, data) {34 if (err) {35 console.log(err);36 } else {37 console.log(data);38 }39});40var wpt = require('wpt');41wpt.getTestInfo('140329_5G_1', function(err, data) {42 if (err) {43 console.log(err);44 } else {45 console.log(data);46 }47});48var wpt = require('wpt');49 if (err) {50 console.log(err);51 } else {52 console.log(data);53 }54});55var wpt = require('wpt');56wpt.getTestResultsByLocation('
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var path = require('path');4 if (err) {5 console.log(err);6 }7 else {8 fs.writeFile(path.join(__dirname, 'test.svg'), svg, function (err) {9 if (err) {10 console.log(err);11 }12 else {13 console.log('SVG created successfully');14 }15 });16 }17});18### `wptools.createSvg(url, callback)`19### `wptools.createSvgForPage(page, callback)`
Using AI Code Generation
1var wptools = require("wptools");2var fs = require('fs');3 if (err) {4 console.log(err);5 } else {6 fs.writeFile("test.svg", data, function (err) {7 if (err) {8 console.log(err);9 } else {10 console.log("The file was saved!");11 }12 });13 }14});15### createPng(url, callback)16var wptools = require("wptools");17var fs = require('fs');18 if (err) {19 console.log(err);20 } else {21 fs.writeFile("test.png", data, function (err) {22 if (err) {23 console.log(err);24 } else {25 console.log("The file was saved!");26 }27 });28 }29});30### createPdf(url, callback)31var wptools = require("wptools");32var fs = require('fs');33 if (err) {34 console.log(err);35 } else {36 fs.writeFile("test.pdf", data, function (err) {37 if (err) {38 console.log(err);39 } else {40 console.log("The file was saved!");41 }42 });43 }44});45### createXml(url, callback)
Using AI Code Generation
1var wptools = require('wptools');2wptools.createSvg('test.svg', 400, 400, function(err, svg) {3 if (err) throw err;4 console.log('svg created');5});6### wptools.createSvg(filePath, width, height, callback)7### wptools.createSvgStream(width, height)
Using AI Code Generation
1var wpt = require('wpt.js');2var svg = wpt.createSvg();3svg.addCircle(50, 50, 50, 'red');4svg.addCircle(150, 50, 50, 'green');5svg.addCircle(250, 50, 50, 'blue');6console.log(svg.get());
Using AI Code Generation
1var wptools = require("wptools");2var fs = require("fs");3var wp = new wptools("India");4wp.createSvg(function(err, svg){5 if(!err){6 fs.writeFile("test.svg", svg, function(err){7 if(err){8 console.log(err);9 }10 else{11 console.log("File saved successfully");12 }13 });14 }15 else{16 console.log(err);17 }18});19wp.createSvgStream(function(err, stream){20 if(!err){21 stream.pipe(fs.createWriteStream("test.svg"));22 }23 else{24 console.log(err);25 }26});27wp.getCategories(function(err, categories){28 if(!err){29 console.log(categories);30 }31 else{32 console.log(err);33 }34});35wp.getImages(function(err, images){36 if(!err){37 console.log(images);38 }39 else{40 console.log(err);41 }42});43wp.getImagesStream(function(err, stream){44 if(!err){45 stream.pipe(fs.createWriteStream("test.json"));46 }47 else{48 console.log(err);49 }50});51wp.getLinks(function(err, links){52 if(!err){53 console.log(links);54 }55 else{56 console.log(err);57 }58});59wp.getLinksStream(function(err, stream){60 if(!err){61 stream.pipe(fs.createWriteStream("test.json"));62 }63 else{64 console.log(err);65 }66});67wp.getTemplates(function(err, templates){68 if(!err){69 console.log(templates);70 }71 else{72 console.log(err);73 }74});75wp.getTemplatesStream(function(err, stream){76 if(!err){77 stream.pipe(fs.createWriteStream("test.json"));78 }79 else{80 console.log(err);81 }82});83wp.getReferences(function(err, references){84 if(!err){85 console.log(references);86 }87 else{88 console.log(err);89 }90});
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!!