Best JavaScript code snippet using wpt
cache-helper.js
Source:cache-helper.js
1/**2 * cache helper3 */4import settings from './settings';5import DataCache from './DataCache';6// ç¼åæ°æ®å¯¹è±¡ã为äºé¿å
æ··æ·ï¼åªç¼åè³ä¸çº§ç»æ7const dataCache = new DataCache();8// è·åæ¶é´æ³9function getTime(t) {10 return t ? t.getTime() : (new Date()).getTime();11}12/**13 * ä¿®æ£ cacheName14 * @param {String} cacheName åå§çå¼ï¼å¯è½æ¯ä»»ææ ¼å¼15 * @return {String} ä¿®æ£åç cacheNameï¼ä»¥ cachePrefix å¼å¤´16 */17function adjustCacheName(cacheName) {18 if (!cacheName) {19 return '';20 }21 cacheName = encodeURIComponent(('' + cacheName).replace(/\//g, '.').replace(/^\./, '').replace(/(^\s+|\s+$)/g, ''));22 if (cacheName.indexOf(settings.cachePrefix)) {23 // cacheName.indexOf(settings.cachePrefix) !== 0 å ä¸åç¼24 cacheName = settings.cachePrefix + cacheName;25 }26 return cacheName;27}28/**29 * æ ¹æ® cacheType åå¾ cacheStorage 对象30 * @param {String} cacheType31 * @return {Object}32 */33export function getCacheStor(cacheType) {34 let cacheStor = dataCache;35 if (~['sessionStorage', 'localStorage'].indexOf(cacheType)) {36 cacheStor = window[cacheType] || cacheStor;37 }38 return cacheStor;39}40/**41 * æ ¹æ® cacheName å称å±çº§è·åå¯¹åº dataCache ä¸çç¼åæ°æ®42 * @param {String} cacheName - å称ï¼ä»¥ . åå²å±çº§ï¼å¦ ups.pa.query.tags.group43 * @param {String} cacheType - ç¼åç±»åï¼sessionStorageãlocalStorage ã memory(é»è®¤)44 * @return {*} è¿å读åå°çæ°æ®45 */46export function getCacheDataByName(cacheName, cacheType) {47 let data;48 const undefinedVal = void 0;49 const cacheStor = getCacheStor(cacheType);50 if (!(cacheName = adjustCacheName(cacheName))) {51 return data;52 }53 data = cacheStor.getItem(cacheName);54 try {55 data = JSON.parse(data);56 } catch (e) {57 data = data;58 }59 // ç¼åçæ°æ®è®¾ç½®äºæææ data._e60 if (data && data._e) {61 // console.log(getTime() - data._e, getTime(), data._e);62 if (getTime() - data._e < 0) {63 return data.d;64 }65 // å·²è¿æï¼æ°æ®æ æäºï¼ç§»é¤å®66 cacheStor.removeItem(cacheName);67 return undefinedVal;68 }69 return data || undefinedVal;70}71/**72 * æ ¹æ® cacheName å称å°è¯ç§»é¤ç¼åä¸åå¨çæ°æ®73 * @param {String|RegExp} cacheName - å称ï¼ä»¥ . åå²å±çº§ï¼å¦ ups.pa.query.tags.groupãæ¯ææ£åå¹é
74 * @param {String} cacheType - ç¼åç±»åï¼sessionStorageãlocalStorage ã memory(é»è®¤)75 * @return {*}76 */77export function deleteCacheDataByName(cacheName, cacheType) {78 const cacheStor = getCacheStor(cacheType);79 let item,80 i,81 len;82 // 为æ£åï¼æ¯æ模ç³å é¤83 if (cacheName instanceof RegExp) {84 len = cacheStor.length;85 for (i = 0; i < len; i++) {86 item = cacheStor.key(i);87 if (88 !item || // å
¼å®¹89 item.indexOf(settings.cachePrefix) !== 0 || // è¿æ»¤åç¼90 !cacheName.test(item.slice(settings.cachePrefix.length)) // è§åæ£æµ91 ) {92 continue;93 }94 // 符åè§åï¼ç§»é¤95 cacheStor.removeItem(item);96 }97 return;98 }99 // 精确çæ¥æ¾ä¸å é¤100 if (!(cacheName = adjustCacheName(cacheName))) {101 return;102 }103 cacheStor.removeItem(cacheName);104}105/**106 * åå¨æ°æ®å°æ¬å°107 * @param {String} cacheName - ç¨äºåå¨çå称108 * @param {*} data - ä»»æç±»åçæ°æ®109 * @param {String} cacheType - åå¨ç±»åï¼æ¯æä¸ç§æ¹å¼ï¼sessionStorageãlocalStorage åå
åä¸(é»è®¤)110 */111export function saveTOCache(cacheName, data, cfg = {}) {112 if (!(cacheName = adjustCacheName(cacheName))) {113 return;114 }115 // console.log(cacheName, data, cfg);116 const {cache: cacheType, expires} = cfg;117 const cacheStor = getCacheStor(cacheType);118 // expires åºä¸ºæ¯«ç§æ´æ°119 if (+expires) {120 data = {121 d: data,122 _e: (expires instanceof Date) ? getTime(expires) : (getTime() + expires)123 };124 }125 if (cacheStor === dataCache) {126 // åå°å
å dataCache127 cacheStor.setItem(cacheName, data);128 } else {129 cacheStor.setItem(cacheName, JSON.stringify(data));130 }131}132/**133 * æ¯å¦ä¸ºç±»å符串134 */135export function isString(text) {136 const type = typeof text;137 return 'string' === type || 'number' === type;138}139/**140 * è¿åå
è£
done/fail APIè¯æ³ç³ç Promise141 * @param {Boolean} isJquery æ¯å¦ä¸º jQueryï¼ä¸ºtrue åè¿å $.Deferred142 * @return {Promise}143 */144export function getPromise(isJquery) {145 if (isJquery) {146 return $.Deferred();147 }148 let resolve, reject;149 const $p = new window.Promise((rs, rj) => {150 resolve = rs;151 reject = rj;152 });153 $p.resolve = resolve;154 $p.reject = reject;155 $p.done = function (cb) {156 return $p.then(cb);157 };158 $p.fail = function (cb) {159 return $p.then(null, cb);160 };161 $p.always = function (cb) {162 return $p.then(cb, cb);163 };164 return $p;...
index.js
Source:index.js
1'use strict';2var through = require('through2'),3 util = require('gulp-util'),4 pluginName = 'gulp-remember', // name of our plugin for error logging purposes5 caches = {}, // will hold named file caches6 defaultName = '_default'; // name to give a cache if not provided7/**8 * Return a through stream that will:9 * 1. Remember all files that ever pass through it.10 * 2. Add all remembered files back into the stream when not present.11 * @param cacheName {string} Name to give your cache.12 * Caches with different names can know about different sets of files.13 */14function gulpRemember(cacheName) {15 var cache; // the files we've ever put our hands on in the current stream16 if (cacheName !== undefined && typeof cacheName !== 'number' && typeof cacheName !== 'string') {17 throw new util.PluginError(pluginName, 'Usage: require("gulp-remember")(name); where name is undefined, number or string');18 }19 cacheName = cacheName || defaultName; // maybe need to use a default cache20 caches[cacheName] = caches[cacheName] || {}; // maybe initialize the named cache21 cache = caches[cacheName];22 function transform(file, enc, callback) {23 var fileKey = file.path.toLowerCase();24 cache[fileKey] = file; // add file to our cache25 callback();26 }27 function flush(callback) {28 // add all files we've ever seen back into the stream29 for (var key in cache) {30 if (cache.hasOwnProperty(key)) {31 this.push(cache[key]); // add this file back into the current stream32 }33 }34 callback();35 }36 return through.obj(transform, flush);37}38/**39 * Forget about a file.40 * A warning is logged if either the named cache or file do not exist.41 *42 * @param cacheName {string} name of the cache from which to drop the file43 * @param path {string} path of the file to forget44 */45gulpRemember.forget = function (cacheName, path) {46 if (arguments.length === 1) {47 path = cacheName;48 cacheName = defaultName;49 }50 path = path.toLowerCase();51 if (typeof cacheName !== 'number' && typeof cacheName !== 'string') {52 throw new util.PluginError(pluginName, 'Usage: require("gulp-remember").forget(cacheName, path); where cacheName is undefined, number or string and path is a string');53 }54 if (caches[cacheName] === undefined) {55 util.log(pluginName, '- .forget() warning: cache ' + cacheName + ' not found');56 } else if (caches[cacheName][path] === undefined) {57 util.log(pluginName, '- .forget() warning: file ' + path + ' not found in cache ' + cacheName);58 } else {59 delete caches[cacheName][path];60 }61};62/**63 * Forget all files in one cache.64 * A warning is logged if the cache does not exist.65 *66 * @param cacheName {string} name of the cache to wipe67 */68gulpRemember.forgetAll = function (cacheName) {69 if (arguments.length === 0) {70 cacheName = defaultName;71 }72 if (typeof cacheName !== 'number' && typeof cacheName !== 'string') {73 throw new util.PluginError(pluginName, 'Usage: require("gulp-remember").forgetAll(cacheName); where cacheName is undefined, number or string');74 }75 if (caches[cacheName] === undefined) {76 util.log(pluginName, '- .forget() warning: cache ' + cacheName + ' not found');77 } else {78 caches[cacheName] = {};79 }80}81/**82 * Return a raw cache by name.83 * Useful for checking state. Manually adding or removing files is NOT recommended.84 *85 * @param cacheName {string} name of the cache to retrieve86 */87gulpRemember.cacheFor = function (cacheName) {88 if (arguments.length === 0) {89 cacheName = defaultName;90 }91 if (typeof cacheName !== 'number' && typeof cacheName !== 'string') {92 throw new util.PluginError(pluginName, 'Usage: require("gulp-remember").cacheFor(cacheName); where cacheName is undefined, number or string');93 }94 return caches[cacheName];95}...
Using AI Code Generation
1self.addEventListener('fetch', function(event) {2 event.respondWith(3 caches.open('test-cache').then(function(cache) {4 return cache.match(event.request).then(function(response) {5 return response || fetch(event.request);6 });7 })8 );9});
Using AI Code Generation
1var cacheName = __wptBrowsertime.cacheName;2var cache = caches.open(cacheName);3cache.then(function(cache) {4 return response.text();5 }).then(function(text) {6 console.log('cacheName: ' + text);7 });8});9var cacheName = __wptBrowsertime.cacheName;10var cache = caches.open(cacheName);11cache.then(function(cache) {12 return response.text();13 }).then(function(text) {14 console.log('cacheName: ' + text);15 });16});17var cacheName = __wptBrowsertime.cacheName;18var cache = caches.open(cacheName);19cache.then(function(cache) {20 cache.matchAll().then(function(responses) {21 responses.forEach(function(response) {22 response.text().then(function(text) {23 console.log('cacheName: ' + text);24 });25 });26 });27});28var cacheName = __wptBrowsertime.cacheName;29var cache = caches.open(cacheName);30cache.then(function(cache) {31 console.log('cacheName: ' + response);32 });33});34var cacheName = __wptBrowsertime.cacheName;35var cache = caches.open(cacheName);36cache.then(function(cache) {37 cache.keys().then(function(requests) {38 requests.forEach(function(request) {39 console.log('cacheName: ' + request.url);40 });41 });42});43var cacheName = __wptBrowsertime.cacheName;44var cache = caches.open(cacheName);45cache.then(function(cache) {
Using AI Code Generation
1var cacheName = wpt.cacheName;2var cache = caches.open(cacheName);3cache.then(function(cache) {4 cache.addAll([5 ]).then(function() {6 console.log('All resources have been fetched and cached.');7 });8});9var cacheName = wpt.cacheName;10var cache = caches.open(cacheName);11cache.then(function(cache) {12 cache.addAll([13 ]).then(function() {14 console.log('All resources have been fetched and cached.');15 });16});17var cacheName = wpt.cacheName;18var cache = caches.open(cacheName);19cache.then(function(cache) {20 cache.addAll([21 ]).then(function() {22 console.log('All resources have been fetched and cached.');23 });24});25var cacheName = wpt.cacheName;26var cache = caches.open(cacheName);27cache.then(function(cache) {28 cache.addAll([29 ]).then(function() {30 console.log('All resources have been fetched and cached.');31 });32});33var cacheName = wpt.cacheName;34var cache = caches.open(cacheName);35cache.then(function(cache) {36 cache.addAll([37 ]).then(function() {38 console.log('All resources have been fetched and cached.');39 });40});
Using AI Code Generation
1var cacheName = caches.open('cyCache').then(function(cachhe {2 /);3 cacheName.then(function(name) {4 console.log(name/var cacheName = wpt.cacheName;5var cacheName = wpt.cacheName;6var cache = caches.open(cacheName);7cache.then(function(cache) {8 cache.addAll([9 ]).then(function() {10 console.log('All resources have been fetched and cached.');11 });12});13var cacheName = wpt.cacheName;14var cache = caches.open(cacheName);15cache.then(function(cache) {16 cache.addAll([17 ]).then(function() {18 console.log('All resources have been fetched and cached.');19 });20});21var cacheName = wpt.cacheName;22var cache = caches.open(cacheName);23cache.then(function(cache) {24 cache.addAll([25 ]).then(function() {26 console.log('All resources have been fetched and cached.');27 });28});29var cacheName = wpt.cacheName;30var cache = caches.open(cacheName);31cache.then(function(cache) {32 cache.addAll([33 ]).then(function() {34 console.log('All resources have been fetched and cached.');35 });36});37var cacheName = wpt.cacheName;38var cache = caches.open(cacheName);39cache.then(function(cache) {40 cache.addAll([41 ]).then(function() {42 console.log('All resources have been fetched and cached.');43 });44});
Using AI Code Generation
1var cacheName = caches.open('myCache').then(function(cache) {2 return cache.name;3});4cacheName.then(function(name) {5 console.log(name);6});
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!!