UNPKG

@polyv/utils

Version:

Utility functions of Polyv frontend development.

1 lines 3.39 kB
export const supportWebP=(()=>{let A;return()=>{if(null==A)try{A=function(){const A=document.createElement("canvas");if(A.getContext&&A.getContext("2d")){const e="image/webp";return 0===A.toDataURL(e).indexOf("data:"+e)}return!1}()}catch(e){A=!1}return A}})();export const supportAVIF=(()=>{let A;return()=>(A||(A=new Promise((A=>{const e=new Image;e.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",e.onerror=()=>{A(!1)},e.onload=()=>{A(!0)},setTimeout((()=>{A(!1)}),1e3)}))),A)})();function getExtname(A){const e=(A||"").split("."),t=e[e.length-1];return t?t.toLowerCase():""}function handleCompressOptions(A){var e,t;const o="number"==typeof A?{width:A}:A;return o.allowJPG=null===(e=o.allowJPG)||void 0===e||e,o.allowWebP=null!==(t=o.allowWebP)&&void 0!==t?t:"auto",o}function createURLObject(A){let e;if("undefined"!=typeof document){const t=document.createElement("a");t.href=A,e=t}else if("function"==typeof URL)try{e=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}return e}function genOSSCompressParams(A,e){let t="";return null==e.width&&null==e.height||(t+="/resize",e.width&&(t+=",w_"+e.width),e.height&&(t+=",h_"+e.height),t+=",limit_1"),e.allowAVIF&&"gif"!==A?t+="/format,avif":!0===e.allowWebP||"auto"===e.allowWebP&&supportWebP()?t+="/format,webp/quality,Q_80":e.allowJPG&&"gif"!==A&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(A,e){let t=!1;return A=A.replace(/([?&]x-oss-process)(?:=([^&]*))?/,((A,o,r)=>(t=!0,/^image/.test(r)?o+"="+r.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}export function ossCompress(A,e){const t=createURLObject(A);if(!t)return A;const o=getExtname(t.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(t.hostname)||"svg"===o)return A;const r=genOSSCompressParams(o,handleCompressOptions(e));return r&&(t.search=setOSSCompressParams(t.search,r)),t.href}function genCOSCompressParams(A,e){let t="";return null!=e.width&&null!=e.height?t+=`/thumbnail/${e.width}x${e.height}`:null!=e.width?t+=`/thumbnail/${e.width}x`:null!=e.height&&(t+=`/thumbnail/x${e.height}`),e.allowAVIF&&"gif"!==A?t+="/format/avif":!0===e.allowWebP||"auto"===e.allowWebP&&supportWebP()?t+="/format/webp/quality/80":e.allowJPG&&"gif"!==A&&(t+="/format/jpg/quality/80"),t}function setCOSCompressParams(A,e){let t=!1;return A=A.replace(/([?&]imageMogr2)(\/[^&]*)/,((A,o,r)=>(t=!0,o+"="+r.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+e))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"imageMogr2"+e}export function cosCompress(A,e){const t=createURLObject(A);if(!t)return A;const o=getExtname(t.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(t.hostname)||"svg"===o)return A;const r=genCOSCompressParams(o,handleCompressOptions(e));return r&&(t.search=setCOSCompressParams(t.search,r)),t.href}export function compressHTMLImgs(A,e,t=ossCompress){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((A,o,r,a,n)=>o+'"'+t(a,e)+'" data-src="'+a+'"'+n)):""}