Best JavaScript code snippet using wpt
fonts.js
Source:fonts.js
...1885 }1886 }1887 return charCodes;1888 }1889 function createCharCode(charCodeToGlyphId, glyphId) {1890 for (var charCode in charCodeToGlyphId) {1891 if (glyphId === charCodeToGlyphId[charCode]) {1892 return charCode | 0;1893 }1894 }1895 newMapping.charCodeToGlyphId[newMapping.nextAvailableFontCharCode] = glyphId;1896 return newMapping.nextAvailableFontCharCode++;1897 }1898 var seacs = font.seacs;1899 if (SEAC_ANALYSIS_ENABLED && seacs && seacs.length) {1900 var matrix = properties.fontMatrix || _util.FONT_IDENTITY_MATRIX;1901 var charset = font.getCharset();1902 var seacMap = Object.create(null);1903 for (var glyphId in seacs) {1904 glyphId |= 0;1905 var seac = seacs[glyphId];1906 var baseGlyphName = _encodings.StandardEncoding[seac[2]];1907 var accentGlyphName = _encodings.StandardEncoding[seac[3]];1908 var baseGlyphId = charset.indexOf(baseGlyphName);1909 var accentGlyphId = charset.indexOf(accentGlyphName);1910 if (baseGlyphId < 0 || accentGlyphId < 0) {1911 continue;1912 }1913 var accentOffset = {1914 x: seac[0] * matrix[0] + seac[1] * matrix[2] + matrix[4],1915 y: seac[0] * matrix[1] + seac[1] * matrix[3] + matrix[5]1916 };1917 var charCodes = getCharCodes(mapping, glyphId);1918 if (!charCodes) {1919 continue;1920 }1921 for (var i = 0, ii = charCodes.length; i < ii; i++) {1922 var charCode = charCodes[i];1923 var charCodeToGlyphId = newMapping.charCodeToGlyphId;1924 var baseFontCharCode = createCharCode(charCodeToGlyphId, baseGlyphId);1925 var accentFontCharCode = createCharCode(charCodeToGlyphId, accentGlyphId);1926 seacMap[charCode] = {1927 baseFontCharCode: baseFontCharCode,1928 accentFontCharCode: accentFontCharCode,1929 accentOffset: accentOffset1930 };1931 }1932 }1933 properties.seacMap = seacMap;1934 }1935 var unitsPerEm = 1 / (properties.fontMatrix || _util.FONT_IDENTITY_MATRIX)[0];1936 var builder = new OpenTypeFileBuilder('\x4F\x54\x54\x4F');1937 builder.addTable('CFF ', font.data);1938 builder.addTable('OS/2', createOS2Table(properties, newMapping.charCodeToGlyphId));1939 builder.addTable('cmap', createCmapTable(newMapping.charCodeToGlyphId, numGlyphs));...
index.js
Source:index.js
1import Vue from 'vue';2const vm = new Vue();3/**4 * @param {Array} list5 * @param {Function} f6 * @return {*}7 */8export function find (list, f) {9 return list.filter(f)[0];10}11/**12 * forEach for object13 */14export function forEachValue (obj, fn) {15 Object.keys(obj).forEach(key => fn(obj[key], key));16}17export function isObject (obj) {18 return obj !== null && typeof obj === 'object';19}20// æ°ç»å»é å
ç´ çé®21export function uniqueFunc (arr, uniId) {22 const res = new Map();23 return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));24}25export function isPromise (val) {26 return val && typeof val.then === 'function';27}28export function assert (condition, msg) {29 if (!condition) throw new Error(`[vuex] ${msg}`);30}31export function partial (fn, arg) {32 return function () {33 return fn(arg);34 };35}36export const queryToObj = (key) => {37 let url = window.location.search; // è·åå½åurl38 if (url) {39 let cs = url.split('?')[1]; // è·å?ä¹åçåæ°å符串40 let csArr = cs.split('&'); // åæ°å符串åå²ä¸ºæ°ç»41 const obj = {};42 for (let i = 0; i < csArr.length; i++) { // éåæ°ç»ï¼æ¿å°json对象43 obj[csArr[i].split('=')[0]] = csArr[i].split('=')[1];44 }45 return obj[key];46 }47 return null;48};49// æ ¼å¼åé¾æ¥åæ°50export const getQueryObject = (url) => {51 const search = url.substring(url.lastIndexOf('?') + 1);52 const obj = {};53 const reg = /([^?&=]+)=([^?&=]*)/g;54 search.replace(reg, (rs, $1, $2) => {55 const name = decodeURIComponent($1);56 let val = decodeURIComponent($2);57 val = String(val);58 obj[name] = val;59 return rs;60 });61 return obj;62};63export const objToQuery = (obj) => {64 let str = '';65 for (let key in obj) {66 if (str !== '') {67 str += '&';68 }69 if (key) {70 str += key + '=' + encodeURIComponent(obj[key]);71 }72 }73 return str;74};75let pngMagic = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];76let jpegJfif = [0x4a, 0x46, 0x49, 0x46];77let jpegExif = [0x45, 0x78, 0x69, 0x66];78let jpegMagic = [0xFF, 0xD8, 0xFF, 0xE0];79let gifMagic0 = [0x47, 0x49, 0x46, 0x38, 0x37, 0x61];80let getGifMagic1 = [0x47, 0x49, 0x46, 0x38, 0x39, 0x61];81function arraycopy (src, index, dist, distIndex, size) {82 for (let i = 0; i < size; i++) {83 dist[distIndex + i] = src[index + i];84 }85}86function arrayEquals (arr1, arr2) {87 if (arr1 === 'undefined' || arr2 === 'undefined') {88 return false;89 }90 if (arr1 instanceof Array && arr2 instanceof Array) {91 if (arr1.length !== arr2.length) {92 return false;93 }94 for (let i = 0; i < arr1.length; i++) {95 if (arr1[i] !== arr2[i]) {96 return false;97 }98 }99 return true;100 }101 return false;102}103export const isImage = (buf) => {104 if (buf === null || buf === 'undefined' || buf.length < 8) {105 return null;106 }107 let bytes = [];108 arraycopy(buf, 0, bytes, 0, 6);109 if (isGif(bytes)) {110 return 'image/gif';111 }112 bytes = [];113 arraycopy(buf, 6, bytes, 0, 4);114 if (isJpeg(bytes)) {115 return 'image/jpeg';116 }117 bytes = [];118 arraycopy(buf, 0, bytes, 0, 8);119 if (isPng(bytes)) {120 return 'image/png';121 }122 return null;123};124/**125 * @param data first 6 bytes of file126 * @return gif image file true,other false127 */128function isGif (data) {129 return arrayEquals(data, gifMagic0) || arrayEquals(data, getGifMagic1);130}131/**132 * @param data first 4 bytes of file133 * @return jpeg image file true,other false134 */135function isJpeg (data) {136 return arrayEquals(data, jpegMagic) || arrayEquals(data, jpegJfif) || arrayEquals(data, jpegExif);137}138/**139 * @param data first 8 bytes of file140 * @return png image file true,other false141 */142function isPng (data) {143 return arrayEquals(data, pngMagic);144}145/**146 * 导åº147 *148 * @param {Object} å端è¿åçæ´ä¸ªresponse149 */150export function exportExcel (oRequest) {151 const fileName = decodeURIComponent(152 oRequest.headers['content-disposition']153 ).split('=')[1];154 const blob = new Blob([oRequest.data], { type: 'application/vnd.ms-excel' });155 const elink = document.createElement('a');156 elink.download = fileName;157 elink.style.display = 'none';158 elink.href = URL.createObjectURL(blob);159 document.body.appendChild(elink);160 elink.click();161 URL.revokeObjectURL(elink.href); // éæ¾URL 对象162 document.body.removeChild(elink);163}164/**165 * ä¸è½½æ件æµ166 * @param blob æ件æµ167 * @param filename æ件åå168 * @param type æ件类å "application/excel"169 */170export function downloadStreamBlob (blob, filename, type = 'application/excel') {171 // é²æ¢éå¤æ·»å aé¾æ¥172 const oOldA = document.getElementById('downloadstream');173 if (oOldA) {174 document.body.removeChild(oOldA);175 }176 // https://stackoverflow.com/questions/20310688/blob-download-is-not-working-in-ie177 if (window.navigator && window.navigator.msSaveOrOpenBlob) {178 window.navigator.msSaveBlob(blob, filename);179 } else {180 // let filename = "设å¤å¯¼åº{0}.xlsx".format(vpms.core.date.format("yyyyMMddhhmmss"));181 const oAElement = document.createElement('a');182 oAElement.setAttribute('id', 'downloadstream');183 let binaryData = [];184 binaryData.push(blob);185 const URL = window.URL || window['webkitURL']; // å
¼å®¹å¤ç186 const url = URL.createObjectURL(new Blob(binaryData, { type }));187 oAElement.href = url;188 oAElement.download = filename;189 oAElement.click();190 URL.revokeObjectURL(url);191 }192}193// æå¼urlä¸è½½æ件194export function downloadByUrl (url, fileName) {195 var downloadURL = url;196 let xhr = new XMLHttpRequest();197 xhr.open('GET', downloadURL, true);198 xhr.responseType = 'arraybuffer';199 xhr.onload = function () {200 if (this.status === 200) {201 let blob = new Blob([this.response], { type: 'application/octet-stream"' });202 if (typeof window.navigator.msSaveBlob !== 'undefined') {203 window.navigator.msSaveBlob(blob, fileName);204 } else {205 let URL = window.URL || window.webkitURL;206 let objectUrl = URL.createObjectURL(blob);207 if (fileName) {208 var a = document.createElement('a');209 if (typeof a.download === 'undefined') {210 window.location = objectUrl;211 } else {212 a.href = objectUrl;213 a.download = fileName;214 document.body.appendChild(a);215 a.click();216 a.remove();217 }218 } else {219 window.location = objectUrl;220 }221 }222 }223 };224 xhr.send();225}226// 导åºtxtæ件227export function exportRaw (fileName, data) {228 // Window.URL å±æ§è¿åä¸ä¸ªå¯¹è±¡ï¼å®æä¾äºç¨äºå建å管ç对象URLsçéææ¹æ³ãå®ä¹å¯ä»¥ä½ä¸ºä¸ä¸ªæé å½æ°è¢«è°ç¨æ¥æé URL 对象ã229 const urlObject = window.URL || window.webkitURL || window;230 // Blob 对象表示ä¸ä¸ªä¸å¯åãåå§æ°æ®çç±»æ件对象ã231 const exportBlob = new Blob([data]);232 // å建ä¸ä¸ªå
·ææå®çå½å空é´URIåéå®å称çå
ç´ ã createElementNS() æ¹æ³å建带æå½å空é´çå
ç´ èç¹ã该æ¹æ³è¿å Element 对象ã233 const saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');234 // URL.createObjectURL() éææ¹æ³ä¼å建ä¸ä¸ª DOMStringï¼å
¶ä¸å
å«ä¸ä¸ªè¡¨ç¤ºåæ°ä¸ç»åºç对象çURLã235 // è¿ä¸ª URL ççå½å¨æåå建å®ççªå£ä¸ç document ç»å®ãè¿ä¸ªæ°çURL 对象表示æå®ç File 对象æ Blob 对象ã236 saveLink.href = urlObject.createObjectURL(exportBlob);237 // HTMLAnchorElement.download å±æ§æ¯ä¸ä¸ª DOMString ï¼è¡¨æé¾æ¥çèµæºå°è¢«ä¸è½½ï¼èä¸æ¯æ¾ç¤ºå¨æµè§å¨ä¸ã238 // HTMLAnchorElement æ¥å£è¡¨ç¤ºè¶
é¾æ¥å
ç´ ï¼å¹¶æä¾ä¸äºç¹å«çå±æ§åæ¹æ³ï¼é¤äºé£äºç»§æ¿èªæ®é HTMLElement对象æ¥å£çä¹å¤ï¼ä»¥ç¨äºæä½è¿äºå
ç´ çå¸å±åæ¾ç¤ºã239 // DOMString æ¯ä¸ä¸ªUTF-16å符串ãç±äºJavaScriptå·²ç»ä½¿ç¨äºè¿æ ·çå符串ï¼æ以DOMString ç´æ¥æ å°å° ä¸ä¸ªStringãå°±æ¯ä¸ä¸ªæ®éçå符串240 saveLink.download = fileName;241 // 模æç¹å»äºä»¶242 saveLink.click();243}244/**245 * å¤å¶ææ¬å°ç²246 * åèï¼https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript247 * @param text å¤å¶çææ¬248 */249export function fnPureCopyTextToClipboard (text, sucessMessage) {250 let textArea = document.createElement('textarea');251 textArea.style.position = 'fixed';252 textArea.style.top = 0;253 textArea.style.left = 0;254 textArea.style.width = '2em';255 textArea.style.height = '2em';256 textArea.style.padding = 0;257 textArea.style.border = 'none';258 textArea.style.outline = 'none';259 textArea.style.boxShadow = 'none';260 textArea.style.background = 'transparent';261 textArea.value = text;262 document.body.appendChild(textArea);263 textArea.select();264 try {265 let successful = document.execCommand('copy');266 let msg = successful ? 'successful' : 'unsuccessful';267 if (msg === 'successful') {268 if (sucessMessage) {269 vm.$message.success(sucessMessage);270 } else {271 vm.$message.success('å¤å¶æå');272 }273 } else {274 window.prompt('å¤å¶: Ctrl+C ', text);275 }276 } catch (err) {277 window.prompt('å¤å¶: Ctrl+C ', text);278 }279 document.body.removeChild(textArea);280}281/**282 * æ°å转为ä¸æ283 */284export function fnPureToChinesNum (Num) {285 for (var i = Num.length - 1; i >= 0; i--) {286 Num = Num.replace(',', '');// æ¿æ¢Numä¸çâ,â287 Num = Num.replace(' ', '');// æ¿æ¢Numä¸çç©ºæ ¼288 }289 if (isNaN(Num)) { // éªè¯è¾å
¥çå符æ¯å¦ä¸ºæ°å290 // alert("请æ£æ¥å°åéé¢æ¯å¦æ£ç¡®");291 return;292 }293 // å符å¤çå®æ¯åå¼å§è½¬æ¢ï¼éç¨åå两é¨ååå«è½¬æ¢294 var part = String(Num).split('.');295 var newchar = '';296 // å°æ°ç¹åè¿è¡è½¬å297 for (let i = part[0].length - 1; i >= 0; i--) {298 if (part[0].length > 10) {299 // alert("ä½æ°è¿å¤§ï¼æ æ³è®¡ç®");300 return '';301 }// è¥æ°éè¶
è¿æ¾äº¿åä½ï¼æ示302 var tmpnewchar = '';303 var perchar = part[0].charAt(i);304 switch (perchar) {305 case '0': tmpnewchar = 'é¶' + tmpnewchar; break;306 case '1': tmpnewchar = 'ä¸' + tmpnewchar; break;307 case '2': tmpnewchar = 'äº' + tmpnewchar; break;308 case '3': tmpnewchar = 'ä¸' + tmpnewchar; break;309 case '4': tmpnewchar = 'å' + tmpnewchar; break;310 case '5': tmpnewchar = 'äº' + tmpnewchar; break;311 case '6': tmpnewchar = 'å
' + tmpnewchar; break;312 case '7': tmpnewchar = 'ä¸' + tmpnewchar; break;313 case '8': tmpnewchar = 'å
«' + tmpnewchar; break;314 case '9': tmpnewchar = 'ä¹' + tmpnewchar; break;315 }316 switch (part[0].length - i - 1) {317 case 0: break;318 case 1: if (perchar !== 0) tmpnewchar = tmpnewchar + 'å'; break;319 case 2: if (perchar !== 0) tmpnewchar = tmpnewchar + 'ç¾'; break;320 case 3: if (perchar !== 0) tmpnewchar = tmpnewchar + 'å'; break;321 case 4: tmpnewchar = tmpnewchar + 'ä¸'; break;322 case 5: if (perchar !== 0) tmpnewchar = tmpnewchar + 'å'; break;323 case 6: if (perchar !== 0) tmpnewchar = tmpnewchar + 'ç¾'; break;324 case 7: if (perchar !== 0) tmpnewchar = tmpnewchar + 'å'; break;325 case 8: tmpnewchar = tmpnewchar + '亿'; break;326 case 9: tmpnewchar = tmpnewchar + 'å'; break;327 }328 newchar = tmpnewchar + newchar;329 }330 // æ¿æ¢æææ ç¨æ±åï¼ç´å°æ²¡ææ¤ç±»æ ç¨çæ°å为æ¢331 while (newchar.search('é¶é¶') !== -1 || newchar.search('é¶äº¿') !== -1 || newchar.search('亿ä¸') !== -1 || newchar.search('é¶ä¸') !== -1) {332 newchar = newchar.replace('é¶äº¿', '亿');333 newchar = newchar.replace('亿ä¸', '亿');334 newchar = newchar.replace('é¶ä¸', 'ä¸');335 newchar = newchar.replace('é¶é¶', 'é¶');336 }337 // æ¿æ¢ä»¥âä¸åâå¼å¤´çï¼ä¸ºâåâ338 if (newchar.indexOf('ä¸å') === 0) {339 newchar = newchar.substr(1);340 }341 // æ¿æ¢ä»¥âé¶âç»å°¾çï¼ä¸ºââ342 if (newchar.lastIndexOf('é¶') === newchar.length - 1) {343 newchar = newchar.substr(0, newchar.length - 1);344 }345 return newchar;346}347// éèçµè¯å·ç ä¸é´4ä½348export function hideTel (mobile) {349 var reg = new RegExp('(\\d{3})(\\d{4})(\\d{4})');350 mobile = mobile.toString();351 var tel = mobile.replace(reg, '$1****$3');352 return tel;353}354/*355 * description: é²æå½æ°356 * param fnName {String} å½æ°å357 * param wait {Number} 延è¿æ¶é´358 * return: å¤çåçæ§è¡å½æ°359 */360export function debounce (fnName, wait) {361 let timeout = null;362 return function () {363 let context = this;364 let args = arguments;365 if (timeout) clearTimeout(timeout);366 timeout = setTimeout(() => {367 this[fnName].apply(context, args);368 }, wait);369 };370}371// å段å称 ä¸å线转æ¢é©¼å³°372export function fieldToHump (name) {373 return name.replace(/_(\w)/g, function (all, letter) {374 return letter.toUpperCase();375 });376}377// å段å称 驼峰转æ¢ä¸å线378export function fieldToLine (name) {379 return name.replace(/([A-Z])/g, '_$1').toLowerCase();380}381// 16è¿å¶é¢è²å¼è½¬æ¢ærgb382export const hexToRgbArr = (hex) => {383 return [parseInt('0x' + hex.slice(1, 3)), parseInt('0x' + hex.slice(3, 5)), parseInt('0x' + hex.slice(5, 7))];384};385// å¨ææ·»å èæ¬386export function loadScript (url, callback) {387 const script = document.createElement('script');388 script.type = 'text/javascript';389 if (script.readyState) { // IE390 script.onreadystatechange = function () {391 if (script.readyState === 'loaded' || script.readyState === 'complete') {392 script.onreadystatechange = null;393 callback && callback();394 }395 };396 } else { // Others397 script.onload = function () {398 callback && callback();399 };400 }401 script.src = url;402 document.body.appendChild(script);403}404// è¾åºA-Z 26个大ååæ¯405export function createCharCode () {406 const code = [];407 for (let i = 0; i < 26; i++) {408 code.push(String.fromCharCode(65 + i));409 }410 return code;411}412// ååä½åé符 1000 -> 1,000413export function formatNumOfThousands (num) {414 let reg = /\d{1,3}(?=(\d{3})+$)/g;415 return (num + '').replace(reg, '$&,');...
exercise_5.js
Source:exercise_5.js
1/*2substring is a prototype method on the constructor string3create is a static method on the constructor object4createCharCode is a static method on the constructor string5slice is a prototype method on the constructors array and string6toString is a prototype method on all four constructors object, array, number and string...
Using AI Code Generation
1wp.texturize.createCharCode('’', "'");2wp.texturize.createCharCode('“', '"');3wp.texturize.createCharCode('”', '"');4wp.texturize.createCharCode('–', '-');5wp.texturize.createCharCode('—', '--');6wp.texturize.createCharCode('…', '...');7wp.texturize.createCharCode('′', "'");8wp.texturize.createCharCode('″', '"');9wp.texturize.createCharCode('‘', "'");10wp.texturize.createCharCode('’', "'");11wp.texturize.createCharCode('“', '"');12wp.texturize.createCharCode('”', '"');13wp.texturize.createCharCode('–', '-');14wp.texturize.createCharCode('—', '--');15wp.texturize.createCharCode('…', '...');16wp.texturize.createCharCode('′', "'");17wp.texturize.createCharCode('″', '"');18wp.texturize.createCharCode('‘', "'");19wp.texturize.createCharCode('’', "'");20wp.texturize.createCharCode('“', '"');21wp.texturize.createCharCode('”', '"');22wp.texturize.createCharCode('–', '-');23wp.texturize.createCharCode('—', '--');24wp.texturize.createCharCode('…', '...');25wp.texturize.createCharCode('′', "'");26wp.texturize.createCharCode('″', '"');27wp.texturize.createCharCode('‘', "'");28wp.texturize.createCharCode('’', "'");29wp.texturize.createCharCode('“', '"');30wp.texturize.createCharCode('”', '"');31wp.texturize.createCharCode('–', '-');32wp.texturize.createCharCode('—', '--');33wp.texturize.createCharCode('…', '...');34wp.texturize.createCharCode('′', "'");35wp.texturize.createCharCode('″', '"');36wp.texturize.createCharCode('‘', "'");37wp.texturize.createCharCode('’', "'");38wp.texturize.createCharCode('“', '"');39wp.texturize.createCharCode('”', '"');
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4 videoParams: {5 }6};7wpt.runTest(options, function(err, data) {8 if (err) return console.log(err);9 console.log(data);10});11var wpt = require('wpt');12var wpt = new WebPageTest('www.webpagetest.org');13var options = {14};15wpt.getTestStatus(options, function(err, data) {16 if (err) return console.log(err);17 console.log(data);18});19var wpt = require('wpt');20var wpt = new WebPageTest('www.webpagetest.org');21var options = {22};23wpt.getTestResults(options, function(err, data) {24 if (err) return console.log(err);25 console.log(data);26});27var wpt = require('wpt');28var wpt = new WebPageTest('www.webpagetest.org');29wpt.getLocations(function(err, data) {30 if (err) return console.log(err);31 console.log(data);32});33var wpt = require('wpt');34var wpt = new WebPageTest('www.webpagetest.org');35wpt.getTesters(function(err, data) {36 if (err) return console.log(err);
Using AI Code Generation
1var editor = CKEDITOR.replace('editor1', {2 wptextpattern: {3 {start: '*', end: '*', format: 'italic'},4 {start: '_', end: '_', format: 'underline'},5 {start: '~', end: '~', format: 'strikethrough'},6 {start: '#', format: 'h1'},7 {start: '##', format: 'h2'},8 {start: '###', format: 'h3'},9 {start: '####', format: 'h4'},10 {start: '#####', format: 'h5'},11 {start: '######', format: 'h6'},12 {start: '1. ', cmd: 'InsertOrderedList'},13 {start: '* ', cmd: 'InsertUnorderedList'},14 {start: '- ', cmd: 'InsertUnorderedList'}15 }16});
Using AI Code Generation
1const wptextencoder = require("wptextencoder");2const encoder = new wptextencoder.TextEncoder();3const str = "Hello World";4const arr = encoder.encode(str);5console.log(arr);6const wptextdecoder = require("wptextdecoder");7const decoder = new wptextdecoder.TextDecoder();8const arr = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100];9const str = decoder.decode(arr);10console.log(str);
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!!