Best JavaScript code snippet using wpt
pdfassembler.ts
Source:pdfassembler.ts
...391 const encodeChar = (char: string) => '\0\t\n\f\r #%()/<>[]{}'.indexOf(char) === -1 ?392 char : `#${`0${char.charCodeAt(0).toString(16)}`.slice(-2)}`;393 pdfObject = `/${jsObject.slice(1).replace(/./g, encodeChar)}`;394 } else if (firstChar === '(' && lastChar === ')') {395 const byteArray = Array.from(arraysToBytes(jsObject.slice(1, -1)));396 const stringEncode = byteArray.map((byte: number) => stringByteMap[byte]).join('');397 if (stringEncode.length < byteArray.length * 2) {398 pdfObject = `(${stringEncode})`;399 } else {400 const hexEncode = byteArray.map((byte: number) => `0${byte.toString(16)}`.slice(-2)).join('');401 pdfObject = `<${hexEncode}>`;402 }403 } else {404 pdfObject = jsObject;405 }406 // convert true, false, and null to string407 } else if (typeof jsObject !== 'object' || jsObject === null) {408 pdfObject = jsObject === null || jsObject === undefined ? 'null' :409 jsObject === true ? 'true' :410 jsObject === false ? 'false' :411 jsObject + '';412 // format array413 } else if (jsObject instanceof Array) {414 const arrayItems = jsObject415 .map((item, index) => newPdfObject(item, depth + 1, !!space || !!index))416 .join('');417 pdfObject = `[${arrayItems}${newline}${space.repeat(depth)}]`;418 // if an indirect object has already been saved, just return a reference to it419 } else if (typeof jsObject.num === 'number' && indirectObjects[jsObject.num] !== undefined) {420 pdfObject = `${jsObject.num} ${jsObject.gen} R`;421 // format dictionary, as either a direct or indirect object422 } else {423 // new indirect object424 if (typeof jsObject.num === 'number') {425 indirectObjects[jsObject.num] = null; // save placeholder to stop recursive loops426 pdfObject = `${jsObject.num} ${jsObject.gen} obj${newline}`;427 depth = 0;428 // compress stream?429 if (jsObject.stream && jsObject.stream.length) {430 if (this.compress && !jsObject['/Filter']) {431 // If stream is not already compressed, compress it432 const compressedStream = deflate(arraysToBytes([jsObject.stream]));433 // but use compressed version only if it is smaller overall434 // (+ 19 for additional '/Filter/FlateDecode' dict entry)435 if (compressedStream.length + 19 < jsObject.stream.length) {436 jsObject.stream = compressedStream;437 jsObject['/Filter'] = '/FlateDecode';438 }439 }440 jsObject['/Length'] = jsObject.stream.length;441 }442 }443 // format object dictionary entries444 const dictItems = Object.keys(jsObject)445 .filter((key) => key[0] === '/')446 .map(key =>447 newPdfObject(key, depth + 1) +448 newPdfObject(jsObject[key], depth + 1, !!space ? ' ' : '')449 )450 .join('');451 pdfObject += `<<${dictItems}${newline}${space.repeat(depth)}>>`;452 // finish and save indirect object453 if (typeof jsObject.num === 'number') {454 if (jsObject.stream && jsObject.stream.length) {455 const streamPrefix = `${pdfObject}${newline}stream\n`;456 const streamSuffix = `${newline}endstream\nendobj\n`;457 pdfObject = arraysToBytes([streamPrefix, jsObject.stream, streamSuffix]);458 } else {459 pdfObject += `${newline}endobj\n`;460 }461 // save indirect object in object cache462 indirectObjects[jsObject.num] = pdfObject;463 // return object reference464 pdfObject = `${jsObject.num} ${jsObject.gen} R`;465 }466 // otherwise, return inline object467 }468 // add indentation or space?469 const prefix =470 // if nextIndent is set, indent item471 nextIndent ? nextIndent :472 // otherwise, check if item is first in an array, or starts with a delimiter character473 // if not (nextIndent = ''), add a space to separate it from the previous item474 nextIndent === false || ['/', '[', '(', '<'].includes(pdfObject[0]) ? '' : ' ';475 return prefix + pdfObject;476 };477 const rootRef = newPdfObject(this.pdfTree['/Root'], 0, false);478 const infoRef = this.pdfTree['/Info'] && Object.keys(this.pdfTree['/Info']).length ?479 newPdfObject(this.pdfTree['/Info'], 0, false) : null;480 const header =481 `%PDF-${this.pdfVersion}\n` + // default: 1.7482 `%âãÃÃ\n`;483 let offset = 0;484 const xref =485 `xref\n` +486 `0 ${indirectObjects.length}\n` +487 `0000000000 65535 f \n` +488 [header, ...indirectObjects]489 .filter(o => o)490 .map(o => (`0000000000${offset += o.length} 00000 n \n`).slice(-20))491 .slice(0, -1)492 .join('');493 const trailer =494 `trailer\n` +495 `<<${newline}` +496 `${space}/Root ${rootRef}${newline}` +497 (infoRef ? `${space}/Info ${infoRef}${newline}` : '') +498 `${space}/Size ${indirectObjects.length}${newline}` +499 `>>\n` +500 `startxref\n` +501 `${offset}\n` +502 `%%EOF\n`;503 const pdfData = arraysToBytes([header, ...indirectObjects.filter(o => o), xref, trailer]);504 switch (nameOrOutputFormat) {505 case 'ArrayBuffer': return pdfData.buffer;506 case 'Uint8Array': return pdfData;507 default:508 if (nameOrOutputFormat.slice(-4) !== '.pdf') { nameOrOutputFormat += '.pdf'; }509 return new File([pdfData], nameOrOutputFormat, { type: 'application/pdf' });510 }511 });512 }513 // utility functions from js.pdf:514 arraysToBytes(arrays) {515 return arraysToBytes(arrays);516 }517 bytesToString(bytes) {518 return bytesToString(bytes);519 }...
pdfassembler.js
Source:pdfassembler.js
...420 char : `#${`0${char.charCodeAt(0).toString(16)}`.slice(-2)}`;421 pdfObject = `/${jsObject.slice(1).replace(/./g, encodeChar)}`;422 }423 else if (firstChar === '(' && lastChar === ')') {424 const byteArray = Array.from(arraysToBytes(jsObject.slice(1, -1)));425 const stringEncode = byteArray.map((byte) => stringByteMap[byte]).join('');426 if (stringEncode.length < byteArray.length * 2) {427 pdfObject = `(${stringEncode})`;428 }429 else {430 const hexEncode = byteArray.map((byte) => `0${byte.toString(16)}`.slice(-2)).join('');431 pdfObject = `<${hexEncode}>`;432 }433 }434 else {435 pdfObject = jsObject;436 }437 }438 else if (typeof jsObject !== 'object' || jsObject === null) {439 pdfObject = jsObject === null || jsObject === undefined ? 'null' :440 jsObject === true ? 'true' :441 jsObject === false ? 'false' :442 jsObject + '';443 }444 else if (jsObject instanceof Array) {445 const arrayItems = jsObject446 .map((item, index) => newPdfObject(item, depth + 1, !!space || !!index))447 .join('');448 pdfObject = `[${arrayItems}${newline}${space.repeat(depth)}]`;449 }450 else if (typeof jsObject.num === 'number' && indirectObjects[jsObject.num] !== undefined) {451 pdfObject = `${jsObject.num} ${jsObject.gen} R`;452 }453 else {454 if (typeof jsObject.num === 'number') {455 indirectObjects[jsObject.num] = null;456 pdfObject = `${jsObject.num} ${jsObject.gen} obj${newline}`;457 depth = 0;458 if (typeof jsObject.stream !== 'undefined') {459 if (jsObject.stream.length) {460 if (this.compress && !jsObject['/Filter']) {461 const compressedStream = deflate(arraysToBytes([jsObject.stream]));462 if (compressedStream.length + 19 < jsObject.stream.length) {463 jsObject.stream = compressedStream;464 jsObject['/Filter'] = '/FlateDecode';465 }466 }467 }468 jsObject['/Length'] = jsObject.stream.length;469 }470 }471 const dictItems = Object.keys(jsObject)472 .filter((key) => key[0] === '/')473 .map(key => newPdfObject(key, depth + 1) +474 newPdfObject(jsObject[key], depth + 1, !!space ? ' ' : ''))475 .join('');476 pdfObject += `<<${dictItems}${newline}${space.repeat(depth)}>>`;477 if (typeof jsObject.num === 'number') {478 if (typeof jsObject.stream !== 'undefined') {479 if (jsObject.stream.length) {480 const streamPrefix = `${pdfObject}${newline}stream\n`;481 const streamSuffix = `${newline}endstream\nendobj\n`;482 pdfObject = arraysToBytes([streamPrefix, jsObject.stream, streamSuffix]);483 }484 else {485 pdfObject += `${newline}stream\nendstream\nendobj\n`;486 }487 }488 else {489 pdfObject += `${newline}endobj\n`;490 }491 indirectObjects[jsObject.num] = pdfObject;492 pdfObject = `${jsObject.num} ${jsObject.gen} R`;493 }494 }495 const prefix = nextIndent ? nextIndent :496 nextIndent === false || ['/', '[', '(', '<'].includes(pdfObject[0]) ? '' : ' ';497 return prefix + pdfObject;498 };499 const rootRef = newPdfObject(this.pdfTree['/Root'], 0, false);500 this.pdfTree['/Info'].gen = 0;501 this.pdfTree['/Info'].num = this.nextNodeNum++;502 const infoRef = this.pdfTree['/Info'] && Object.keys(this.pdfTree['/Info']).length ?503 newPdfObject(this.pdfTree['/Info'], 0, false) : null;504 const header = `%PDF-${this.pdfVersion}\n` +505 `%âãÃÃ\n`;506 let offset = 0;507 const xref = `xref\n` +508 `0 ${indirectObjects.length}\n` +509 `0000000000 65535 f \n` +510 [header, ...indirectObjects]511 .filter(o => o)512 .map(o => (`0000000000${offset += o.length} 00000 n \n`).slice(-20))513 .slice(0, -1)514 .join('');515 const trailer = `trailer\n` +516 `<<${newline}` +517 `${space}/Root ${rootRef}${newline}` +518 (infoRef ? `${space}/Info ${infoRef}${newline}` : '') +519 `${space}/Size ${indirectObjects.length}${newline}` +520 `>>\n` +521 `startxref\n` +522 `${offset}\n` +523 `%%EOF\n`;524 const pdfData = arraysToBytes([header, ...indirectObjects.filter(o => o), xref, trailer]);525 switch (nameOrOutputFormat) {526 case 'ArrayBuffer':527 return pdfData.buffer;528 break;529 case 'Uint8Array':530 return pdfData;531 break;532 default:533 if (nameOrOutputFormat.slice(-4) !== '.pdf') {534 nameOrOutputFormat += '.pdf';535 }536 return pdfData;537 }538 }539 arraysToBytes(arrays) {540 return arraysToBytes(arrays);541 }542 bytesToString(bytes) {543 return bytesToString(bytes);544 }545}...
Using AI Code Generation
1const wptb = require('wptb');2const array = [1, 2, 3, 4, 5, 6, 7, 8, 9];3const bytes = wptb.arraysToBytes(array);4console.log(bytes);5const wptb = require('wptb');6const bytes = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);7const array = wptb.bytesToArrays(bytes);8console.log(array);9const wptb = require('wptb');10const array = [1, 2, 3, 4, 5, 6, 7, 8, 9];11const base64 = wptb.arraysToBase64(array);12console.log(base64);13const wptb = require('wptb');14const base64 = 'AQIDBAUGBwgJCg==';15const array = wptb.base64ToArrays(base64);16console.log(array);17const wptb = require('wptb');
Using AI Code Generation
1var wptools = require('wptools');2var bytes = wptools.arraysToBytes([[1, 2, 3], [4, 5, 6]]);3console.log(bytes);4var wptools = require('wptools');5var bytes = wptools.bytesToArrays(new Buffer([1, 2, 3, 4, 5, 6]));6console.log(bytes);7var wptools = require('wptools');8var bytes = wptools.bytesToHex(new Buffer([1, 2, 3, 4, 5, 6]));9console.log(bytes);10var wptools = require('wptools');11var bytes = wptools.hexToBytes('010203040506');12console.log(bytes);13var wptools = require('wptools');14var bytes = wptools.bytesToBase64(new Buffer([1, 2, 3, 4, 5, 6]));15console.log(bytes);16var wptools = require('wptools');17var bytes = wptools.base64ToBytes('AQIDBAUGBw==');18console.log(bytes);19var wptools = require('wptools');20var bytes = wptools.bytesToUtf8(new Buffer([1, 2, 3, 4, 5, 6]));
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var array = [0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7];3var bytes = wptoolkit.arraysToBytes(array);4console.log(bytes);5var wptoolkit = require('wptoolkit');6var bytes = [0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7];7var arrays = wptoolkit.bytesToArrays(bytes);8console.log(arrays);9var wptoolkit = require('wptoolkit');10var array = [0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7];11var words = wptoolkit.arraysToWords(array);12console.log(words);13var wptoolkit = require('wptoolkit');14var words = [0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7];15var arrays = wptoolkit.wordsToArrays(words);16console.log(arrays);17var wptoolkit = require('wptoolkit');18var array = [0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7];19var dwords = wptoolkit.arraysToDWords(array);20console.log(dwords);21var wptoolkit = require('w
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var path = require('path');4var array = [1, 2, 3, 4, 5];5var bytes = wptools.arraysToBytes(array);6var array2 = wptools.bytesToArrays(bytes);7console.log(array2);8### wptools.bytesToArrays(bytes)9var wptools = require('wptools');10var fs = require('fs');11var path = require('path');12var array = [1, 2, 3, 4, 5];13var bytes = wptools.arraysToBytes(array);14var array2 = wptools.bytesToArrays(bytes);15console.log(array2);16### wptools.bytesToHex(bytes)17var wptools = require('wptools');18var fs = require('fs');19var path = require('path');20var array = [1, 2, 3, 4, 5];21var bytes = wptools.arraysToBytes(array);22var hex = wptools.bytesToHex(bytes);23console.log(hex);24### wptools.bytesToString(bytes)25var wptools = require('wptools');26var fs = require('fs');27var path = require('path');28var array = [1, 2, 3, 4, 5];29var bytes = wptools.arraysToBytes(array);30var string = wptools.bytesToString(bytes);31console.log(string);32### wptools.bytesToWords(bytes)
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!!