@polyv/utils 2.15.0 → 2.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/image.d.ts CHANGED
@@ -12,6 +12,31 @@ export declare const supportWebP: () => boolean;
12
12
  * @returns 当前浏览器是否支持 AVIF 格式。
13
13
  */
14
14
  export declare const supportAVIF: () => Promise<boolean>;
15
+ /**
16
+ * OSS 缩放模式。
17
+ */
18
+ export declare enum OSSResizingMode {
19
+ /**
20
+ * 等比缩放至指定宽高区域内最大图形(默认)。
21
+ */
22
+ LFit = "lfit",
23
+ /**
24
+ * 等比缩放至覆盖指定宽高区域。
25
+ */
26
+ MFit = "mfit",
27
+ /**
28
+ * 等比缩放至覆盖指定宽高区域并居中裁剪。
29
+ */
30
+ Fill = "fill",
31
+ /**
32
+ * 等比缩放至指定宽高内最大图形并填充颜色至指定尺寸。
33
+ */
34
+ Pad = "pad",
35
+ /**
36
+ * 固定宽高,强制缩放。
37
+ */
38
+ Fixed = "fixed"
39
+ }
15
40
  /**
16
41
  * 压缩选项。
17
42
  */
@@ -24,6 +49,10 @@ export interface IOSSCompressOptions {
24
49
  * 压缩后的图片高度。
25
50
  */
26
51
  height?: number;
52
+ /**
53
+ * 缩放模式。
54
+ */
55
+ mode?: OSSResizingMode;
27
56
  /**
28
57
  * 是否允许转换为 JPG。默认为 true。
29
58
  */
package/cjs/image.js CHANGED
@@ -1 +1 @@
1
- "use strict";function getExtname(e){var t=(e||"").split("."),A=t[t.length-1];return A?A.toLowerCase():""}function handleCompressOptions(e){var t,A,r="number"==typeof e?{width:e}:e;return r.allowJPG=null===(t=r.allowJPG)||void 0===t||t,r.allowWebP=null!==(A=r.allowWebP)&&void 0!==A?A:"auto",r}function createURLObject(e){var t;if("undefined"!=typeof document){var A=document.createElement("a");A.href=e,t=A}else if("function"==typeof URL)try{t=new URL(/^\/\//.test(e)?"https:"+e:e)}catch(e){}return t}function genOSSCompressParams(e,t){var A="";return null==t.width&&null==t.height||(A+="/resize",t.width&&(A+=",w_"+t.width),t.height&&(A+=",h_"+t.height),A+=",limit_1"),t.allowAVIF&&"gif"!==e?A+="/format,avif":!0===t.allowWebP||"auto"===t.allowWebP&&(0,exports.supportWebP)()?A+="/format,webp/quality,Q_80":t.allowJPG&&"gif"!==e&&(A+="/format,jpg/quality,Q_80"),A}function setOSSCompressParams(e,t){var A=!1;return e=e.replace(/([?&]x-oss-process)(?:=([^&]*))?/,(function(e,r,o){return A=!0,/^image/.test(o)?r+"="+o.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+t:e})),A?e:e+(-1===e.indexOf("?")?"?":"&")+"x-oss-process=image"+t}function ossCompress(e,t){var A=createURLObject(e);if(!A)return e;var r=getExtname(A.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(A.hostname)||"svg"===r)return e;var o=genOSSCompressParams(r,handleCompressOptions(t));return o&&(A.search=setOSSCompressParams(A.search,o)),A.href}function genCOSCompressParams(e,t){var A="";return null!=t.width&&null!=t.height?A+="/thumbnail/".concat(t.width,"x").concat(t.height):null!=t.width?A+="/thumbnail/".concat(t.width,"x"):null!=t.height&&(A+="/thumbnail/x".concat(t.height)),t.allowAVIF&&"gif"!==e?A+="/format/avif":!0===t.allowWebP||"auto"===t.allowWebP&&(0,exports.supportWebP)()?A+="/format/webp/quality/80":t.allowJPG&&"gif"!==e&&(A+="/format/jpg/quality/80"),A}function setCOSCompressParams(e,t){var A=!1;return e=e.replace(/([?&]imageMogr2)(\/[^&]*)/,(function(e,r,o){return A=!0,r+"="+o.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+t})),A?e:e+(-1===e.indexOf("?")?"?":"&")+"imageMogr2"+t}function cosCompress(e,t){var A=createURLObject(e);if(!A)return e;var r=getExtname(A.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(A.hostname)||"svg"===r)return e;var o=genCOSCompressParams(r,handleCompressOptions(t));return o&&(A.search=setCOSCompressParams(A.search,o)),A.href}function compressHTMLImgs(e,t,A){return void 0===A&&(A=ossCompress),e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(e,r,o,a,n){return r+'"'+A(a,t)+'" data-src="'+a+'"'+n})):""}function preloadImg(e){return new Promise((function(t,A){var r=new Image;function o(){t({width:r.width,height:r.height,element:r})}r.onload=o,r.onerror=function(){A(new Error('"'.concat(e,'" load failed.')))},r.src=e,r.complete&&o()}))}Object.defineProperty(exports,"__esModule",{value:!0}),exports.supportAVIF=exports.supportWebP=void 0,exports.ossCompress=ossCompress,exports.cosCompress=cosCompress,exports.compressHTMLImgs=compressHTMLImgs,exports.preloadImg=preloadImg,exports.supportWebP=function(){var e;return function(){if(null==e)try{e=function(){var e=document.createElement("canvas");if(e.getContext&&e.getContext("2d")){var t="image/webp";return 0===e.toDataURL(t).indexOf("data:"+t)}return!1}()||function(){var e=!1,t=navigator.userAgent,A=/\bOS\s([\d_.]+)\slike\sMac\sOS\sX\b/.exec(t);if(A)e=parseInt(A[1])>=14;else{var r=/\bVersion\/([\d.]+)\sSafari\b/.exec(t);r&&(e=parseInt(r[1])>=16)}return e}()}catch(t){e=!1}return e}}(),exports.supportAVIF=function(){var e;return function(){return e||(e=new Promise((function(e){var t=new Image;t.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",t.onerror=function(){e(!1)},t.onload=function(){e(!0)},setTimeout((function(){e(!1)}),1e3)}))),e}}();
1
+ "use strict";var OSSResizingMode;function getExtname(e){var t=(e||"").split("."),A=t[t.length-1];return A?A.toLowerCase():""}function handleCompressOptions(e){var t,A,r="number"==typeof e?{width:e}:e;return r.allowJPG=null===(t=r.allowJPG)||void 0===t||t,r.allowWebP=null!==(A=r.allowWebP)&&void 0!==A?A:"auto",r}function createURLObject(e){var t;if("undefined"!=typeof document){var A=document.createElement("a");A.href=e,t=A}else if("function"==typeof URL)try{t=new URL(/^\/\//.test(e)?"https:"+e:e)}catch(e){}return t}function genOSSCompressParams(e,t){var A="";return null==t.width&&null==t.height||(A+="/resize",t.width&&(A+=",w_"+t.width),t.height&&(A+=",h_"+t.height),t.mode&&(A+=",m_"+t.mode),A+=",limit_1"),t.allowAVIF&&"gif"!==e?A+="/format,avif":!0===t.allowWebP||"auto"===t.allowWebP&&(0,exports.supportWebP)()?A+="/format,webp/quality,Q_80":t.allowJPG&&"gif"!==e&&(A+="/format,jpg/quality,Q_80"),A}function setOSSCompressParams(e,t){var A=!1;return e=e.replace(/([?&]x-oss-process)(?:=([^&]*))?/,(function(e,r,o){return A=!0,/^image/.test(o)?r+"="+o.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+t:e})),A?e:e+(-1===e.indexOf("?")?"?":"&")+"x-oss-process=image"+t}function ossCompress(e,t){var A=createURLObject(e);if(!A)return e;var r=getExtname(A.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(A.hostname)||"svg"===r)return e;var o=genOSSCompressParams(r,handleCompressOptions(t));return o&&(A.search=setOSSCompressParams(A.search,o)),A.href}function genCOSCompressParams(e,t){var A="";return null!=t.width&&null!=t.height?A+="/thumbnail/".concat(t.width,"x").concat(t.height):null!=t.width?A+="/thumbnail/".concat(t.width,"x"):null!=t.height&&(A+="/thumbnail/x".concat(t.height)),t.allowAVIF&&"gif"!==e?A+="/format/avif":!0===t.allowWebP||"auto"===t.allowWebP&&(0,exports.supportWebP)()?A+="/format/webp/quality/80":t.allowJPG&&"gif"!==e&&(A+="/format/jpg/quality/80"),A}function setCOSCompressParams(e,t){var A=!1;return e=e.replace(/([?&]imageMogr2)(\/[^&]*)/,(function(e,r,o){return A=!0,r+"="+o.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+t})),A?e:e+(-1===e.indexOf("?")?"?":"&")+"imageMogr2"+t}function cosCompress(e,t){var A=createURLObject(e);if(!A)return e;var r=getExtname(A.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(A.hostname)||"svg"===r)return e;var o=genCOSCompressParams(r,handleCompressOptions(t));return o&&(A.search=setCOSCompressParams(A.search,o)),A.href}function compressHTMLImgs(e,t,A){return void 0===A&&(A=ossCompress),e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(e,r,o,n,a){return r+'"'+A(n,t)+'" data-src="'+n+'"'+a})):""}function preloadImg(e){return new Promise((function(t,A){var r=new Image;function o(){t({width:r.width,height:r.height,element:r})}r.onload=o,r.onerror=function(){A(new Error('"'.concat(e,'" load failed.')))},r.src=e,r.complete&&o()}))}Object.defineProperty(exports,"__esModule",{value:!0}),exports.OSSResizingMode=exports.supportAVIF=exports.supportWebP=void 0,exports.ossCompress=ossCompress,exports.cosCompress=cosCompress,exports.compressHTMLImgs=compressHTMLImgs,exports.preloadImg=preloadImg,exports.supportWebP=function(){var e;return function(){if(null==e)try{e=function(){var e=document.createElement("canvas");if(e.getContext&&e.getContext("2d")){var t="image/webp";return 0===e.toDataURL(t).indexOf("data:"+t)}return!1}()||function(){var e=!1,t=navigator.userAgent,A=/\bOS\s([\d_.]+)\slike\sMac\sOS\sX\b/.exec(t);if(A)e=parseInt(A[1])>=14;else{var r=/\bVersion\/([\d.]+)\sSafari\b/.exec(t);r&&(e=parseInt(r[1])>=16)}return e}()}catch(t){e=!1}return e}}(),exports.supportAVIF=function(){var e;return function(){return e||(e=new Promise((function(e){var t=new Image;t.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",t.onerror=function(){e(!1)},t.onload=function(){e(!0)},setTimeout((function(){e(!1)}),1e3)}))),e}}(),function(e){e.LFit="lfit",e.MFit="mfit",e.Fill="fill",e.Pad="pad",e.Fixed="fixed"}(OSSResizingMode||(exports.OSSResizingMode=OSSResizingMode={}));
package/es/image.d.ts CHANGED
@@ -12,6 +12,31 @@ export declare const supportWebP: () => boolean;
12
12
  * @returns 当前浏览器是否支持 AVIF 格式。
13
13
  */
14
14
  export declare const supportAVIF: () => Promise<boolean>;
15
+ /**
16
+ * OSS 缩放模式。
17
+ */
18
+ export declare enum OSSResizingMode {
19
+ /**
20
+ * 等比缩放至指定宽高区域内最大图形(默认)。
21
+ */
22
+ LFit = "lfit",
23
+ /**
24
+ * 等比缩放至覆盖指定宽高区域。
25
+ */
26
+ MFit = "mfit",
27
+ /**
28
+ * 等比缩放至覆盖指定宽高区域并居中裁剪。
29
+ */
30
+ Fill = "fill",
31
+ /**
32
+ * 等比缩放至指定宽高内最大图形并填充颜色至指定尺寸。
33
+ */
34
+ Pad = "pad",
35
+ /**
36
+ * 固定宽高,强制缩放。
37
+ */
38
+ Fixed = "fixed"
39
+ }
15
40
  /**
16
41
  * 压缩选项。
17
42
  */
@@ -24,6 +49,10 @@ export interface IOSSCompressOptions {
24
49
  * 压缩后的图片高度。
25
50
  */
26
51
  height?: number;
52
+ /**
53
+ * 缩放模式。
54
+ */
55
+ mode?: OSSResizingMode;
27
56
  /**
28
57
  * 是否允许转换为 JPG。默认为 true。
29
58
  */
package/es/image.js CHANGED
@@ -1 +1 @@
1
- export const supportWebP=(()=>{let e;return()=>{if(null==e)try{e=function(){const e=document.createElement("canvas");if(e.getContext&&e.getContext("2d")){const t="image/webp";return 0===e.toDataURL(t).indexOf("data:"+t)}return!1}()||function(){let e=!1;const t=navigator.userAgent,A=/\bOS\s([\d_.]+)\slike\sMac\sOS\sX\b/.exec(t);if(A)e=parseInt(A[1])>=14;else{const A=/\bVersion\/([\d.]+)\sSafari\b/.exec(t);A&&(e=parseInt(A[1])>=16)}return e}()}catch(t){e=!1}return e}})();export const supportAVIF=(()=>{let e;return()=>(e||(e=new Promise((e=>{const t=new Image;t.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",t.onerror=()=>{e(!1)},t.onload=()=>{e(!0)},setTimeout((()=>{e(!1)}),1e3)}))),e)})();function getExtname(e){const t=(e||"").split("."),A=t[t.length-1];return A?A.toLowerCase():""}function handleCompressOptions(e){var t,A;const n="number"==typeof e?{width:e}:e;return n.allowJPG=null===(t=n.allowJPG)||void 0===t||t,n.allowWebP=null!==(A=n.allowWebP)&&void 0!==A?A:"auto",n}function createURLObject(e){let t;if("undefined"!=typeof document){const A=document.createElement("a");A.href=e,t=A}else if("function"==typeof URL)try{t=new URL(/^\/\//.test(e)?"https:"+e:e)}catch(e){}return t}function genOSSCompressParams(e,t){let A="";return null==t.width&&null==t.height||(A+="/resize",t.width&&(A+=",w_"+t.width),t.height&&(A+=",h_"+t.height),A+=",limit_1"),t.allowAVIF&&"gif"!==e?A+="/format,avif":!0===t.allowWebP||"auto"===t.allowWebP&&supportWebP()?A+="/format,webp/quality,Q_80":t.allowJPG&&"gif"!==e&&(A+="/format,jpg/quality,Q_80"),A}function setOSSCompressParams(e,t){let A=!1;return e=e.replace(/([?&]x-oss-process)(?:=([^&]*))?/,((e,n,o)=>(A=!0,/^image/.test(o)?n+"="+o.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+t:e))),A?e:e+(-1===e.indexOf("?")?"?":"&")+"x-oss-process=image"+t}export function ossCompress(e,t){const A=createURLObject(e);if(!A)return e;const n=getExtname(A.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(A.hostname)||"svg"===n)return e;const o=genOSSCompressParams(n,handleCompressOptions(t));return o&&(A.search=setOSSCompressParams(A.search,o)),A.href}function genCOSCompressParams(e,t){let A="";return null!=t.width&&null!=t.height?A+=`/thumbnail/${t.width}x${t.height}`:null!=t.width?A+=`/thumbnail/${t.width}x`:null!=t.height&&(A+=`/thumbnail/x${t.height}`),t.allowAVIF&&"gif"!==e?A+="/format/avif":!0===t.allowWebP||"auto"===t.allowWebP&&supportWebP()?A+="/format/webp/quality/80":t.allowJPG&&"gif"!==e&&(A+="/format/jpg/quality/80"),A}function setCOSCompressParams(e,t){let A=!1;return e=e.replace(/([?&]imageMogr2)(\/[^&]*)/,((e,n,o)=>(A=!0,n+"="+o.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+t))),A?e:e+(-1===e.indexOf("?")?"?":"&")+"imageMogr2"+t}export function cosCompress(e,t){const A=createURLObject(e);if(!A)return e;const n=getExtname(A.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(A.hostname)||"svg"===n)return e;const o=genCOSCompressParams(n,handleCompressOptions(t));return o&&(A.search=setCOSCompressParams(A.search,o)),A.href}export function compressHTMLImgs(e,t,A=ossCompress){return e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((e,n,o,r,s)=>n+'"'+A(r,t)+'" data-src="'+r+'"'+s)):""}export function preloadImg(e){return new Promise(((t,A)=>{const n=new Image;function o(){t({width:n.width,height:n.height,element:n})}n.onload=o,n.onerror=function(){A(new Error(`"${e}" load failed.`))},n.src=e,n.complete&&o()}))}
1
+ export const supportWebP=(()=>{let e;return()=>{if(null==e)try{e=function(){const e=document.createElement("canvas");if(e.getContext&&e.getContext("2d")){const t="image/webp";return 0===e.toDataURL(t).indexOf("data:"+t)}return!1}()||function(){let e=!1;const t=navigator.userAgent,A=/\bOS\s([\d_.]+)\slike\sMac\sOS\sX\b/.exec(t);if(A)e=parseInt(A[1])>=14;else{const A=/\bVersion\/([\d.]+)\sSafari\b/.exec(t);A&&(e=parseInt(A[1])>=16)}return e}()}catch(t){e=!1}return e}})();export const supportAVIF=(()=>{let e;return()=>(e||(e=new Promise((e=>{const t=new Image;t.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",t.onerror=()=>{e(!1)},t.onload=()=>{e(!0)},setTimeout((()=>{e(!1)}),1e3)}))),e)})();export var OSSResizingMode;function getExtname(e){const t=(e||"").split("."),A=t[t.length-1];return A?A.toLowerCase():""}function handleCompressOptions(e){var t,A;const n="number"==typeof e?{width:e}:e;return n.allowJPG=null===(t=n.allowJPG)||void 0===t||t,n.allowWebP=null!==(A=n.allowWebP)&&void 0!==A?A:"auto",n}function createURLObject(e){let t;if("undefined"!=typeof document){const A=document.createElement("a");A.href=e,t=A}else if("function"==typeof URL)try{t=new URL(/^\/\//.test(e)?"https:"+e:e)}catch(e){}return t}function genOSSCompressParams(e,t){let A="";return null==t.width&&null==t.height||(A+="/resize",t.width&&(A+=",w_"+t.width),t.height&&(A+=",h_"+t.height),t.mode&&(A+=",m_"+t.mode),A+=",limit_1"),t.allowAVIF&&"gif"!==e?A+="/format,avif":!0===t.allowWebP||"auto"===t.allowWebP&&supportWebP()?A+="/format,webp/quality,Q_80":t.allowJPG&&"gif"!==e&&(A+="/format,jpg/quality,Q_80"),A}function setOSSCompressParams(e,t){let A=!1;return e=e.replace(/([?&]x-oss-process)(?:=([^&]*))?/,((e,n,o)=>(A=!0,/^image/.test(o)?n+"="+o.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+t:e))),A?e:e+(-1===e.indexOf("?")?"?":"&")+"x-oss-process=image"+t}!function(e){e.LFit="lfit",e.MFit="mfit",e.Fill="fill",e.Pad="pad",e.Fixed="fixed"}(OSSResizingMode||(OSSResizingMode={}));export function ossCompress(e,t){const A=createURLObject(e);if(!A)return e;const n=getExtname(A.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(A.hostname)||"svg"===n)return e;const o=genOSSCompressParams(n,handleCompressOptions(t));return o&&(A.search=setOSSCompressParams(A.search,o)),A.href}function genCOSCompressParams(e,t){let A="";return null!=t.width&&null!=t.height?A+=`/thumbnail/${t.width}x${t.height}`:null!=t.width?A+=`/thumbnail/${t.width}x`:null!=t.height&&(A+=`/thumbnail/x${t.height}`),t.allowAVIF&&"gif"!==e?A+="/format/avif":!0===t.allowWebP||"auto"===t.allowWebP&&supportWebP()?A+="/format/webp/quality/80":t.allowJPG&&"gif"!==e&&(A+="/format/jpg/quality/80"),A}function setCOSCompressParams(e,t){let A=!1;return e=e.replace(/([?&]imageMogr2)(\/[^&]*)/,((e,n,o)=>(A=!0,n+"="+o.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+t))),A?e:e+(-1===e.indexOf("?")?"?":"&")+"imageMogr2"+t}export function cosCompress(e,t){const A=createURLObject(e);if(!A)return e;const n=getExtname(A.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(A.hostname)||"svg"===n)return e;const o=genCOSCompressParams(n,handleCompressOptions(t));return o&&(A.search=setCOSCompressParams(A.search,o)),A.href}export function compressHTMLImgs(e,t,A=ossCompress){return e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((e,n,o,r,s)=>n+'"'+A(r,t)+'" data-src="'+r+'"'+s)):""}export function preloadImg(e){return new Promise(((t,A)=>{const n=new Image;function o(){t({width:n.width,height:n.height,element:n})}n.onload=o,n.onerror=function(){A(new Error(`"${e}" load failed.`))},n.src=e,n.complete&&o()}))}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/utils",
3
- "version": "2.15.0",
3
+ "version": "2.16.0",
4
4
  "description": "Utility functions of Polyv frontend development.",
5
5
  "keywords": [
6
6
  "polyv",