@polyv/utils 2.10.0-beta.4 → 2.10.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
@@ -40,10 +40,11 @@ export interface IOSSCompressOptions {
40
40
  /**
41
41
  * 如果指定图片 URL 的域名是 OSS 域名,且没有任何 OSS 处理参数,则根据压缩选项追加 OSS 图片压缩处理参数。
42
42
  * @param url 指定图片 URL。
43
- * @param options 压缩选项。
43
+ * @param options 压缩选项或缩放的宽度。
44
44
  * @returns 处理后的图片 URL。
45
45
  * @example
46
46
  * ```javascript
47
+ * ossCompress(url, 300);
47
48
  * ossCompress(url, {
48
49
  * width: 300,
49
50
  * allowJPG: true,
@@ -51,7 +52,7 @@ export interface IOSSCompressOptions {
51
52
  * });
52
53
  * ```
53
54
  */
54
- export declare function ossCompress(url: string, options: IOSSCompressOptions): string;
55
+ export declare function ossCompress(url: string, options: IOSSCompressOptions | number): string;
55
56
  /**
56
57
  * 对指定 HTML 代码中 img 标签的图片地址做 OSS 压缩处理。
57
58
  * @param html 指定 HTML 代码。
package/cjs/image.js CHANGED
@@ -1 +1 @@
1
- "use strict";function genOSSCompressParams(A,e){var 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&&(0,exports.supportWebP)()?t+="/format,webp/quality,Q_80":e.allowJPG&&"gif"!==A&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(A,e){var t=!1;return A=A.replace(/([?&]x-oss-process)(?:=([^&]*))?/,(function(A,r,o){return t=!0,/^image/.test(o)?r+"="+o.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A})),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}function ossCompress(A,e){var t,r,o;if("undefined"!=typeof document){var s=document.createElement("a");s.href=A,o=s}else if("function"==typeof URL)try{o=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}if(!o)return A;if(!/\.videocc\.net$/i.test(o.hostname))return A;var a=(o.pathname.split("/").pop()||"").split("."),n=a[a.length-1].toLowerCase();e.allowJPG=null===(t=e.allowJPG)||void 0===t||t,e.allowWebP=null!==(r=e.allowWebP)&&void 0!==r?r:"auto";var i=genOSSCompressParams(n,e);return i&&(o.search=setOSSCompressParams(o.search,i)),o.href}function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(A,t,r,o,s){return t+'"'+ossCompress(o,e)+'" data-src="'+o+'"'+s})):""}Object.defineProperty(exports,"__esModule",{value:!0}),exports.supportAVIF=exports.supportWebP=void 0,exports.ossCompress=ossCompress,exports.compressHTMLImgs=compressHTMLImgs,exports.supportWebP=function(){var A;return function(){return null==A&&(A=function(){var A=document.createElement("canvas");if(A.getContext&&A.getContext("2d")){var e="image/webp";return 0===A.toDataURL(e).indexOf("data:"+e)}return!1}()),A}}(),exports.supportAVIF=function(){var A;return function(){return A||(A=new Promise((function(A){var e=new Image;e.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",e.onerror=function(){A(!1)},e.onload=function(){A(!0)},setTimeout((function(){A(!1)}),1e3)}))),A}}();
1
+ "use strict";function genOSSCompressParams(A,e){var 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&&(0,exports.supportWebP)()?t+="/format,webp/quality,Q_80":e.allowJPG&&"gif"!==A&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(A,e){var t=!1;return A=A.replace(/([?&]x-oss-process)(?:=([^&]*))?/,(function(A,r,o){return t=!0,/^image/.test(o)?r+"="+o.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A})),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}function ossCompress(A,e){var t,r,o;if("undefined"!=typeof document){var s=document.createElement("a");s.href=A,o=s}else if("function"==typeof URL)try{o=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}if(!o)return A;if(!/\.videocc\.net$/i.test(o.hostname))return A;var a=(o.pathname.split("/").pop()||"").split("."),n=a[a.length-1].toLowerCase(),i="number"==typeof e?{width:e}:e;i.allowJPG=null===(t=i.allowJPG)||void 0===t||t,i.allowWebP=null!==(r=i.allowWebP)&&void 0!==r?r:"auto";var u=genOSSCompressParams(n,i);return u&&(o.search=setOSSCompressParams(o.search,u)),o.href}function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(A,t,r,o,s){return t+'"'+ossCompress(o,e)+'" data-src="'+o+'"'+s})):""}Object.defineProperty(exports,"__esModule",{value:!0}),exports.supportAVIF=exports.supportWebP=void 0,exports.ossCompress=ossCompress,exports.compressHTMLImgs=compressHTMLImgs,exports.supportWebP=function(){var A;return function(){return null==A&&(A=function(){var A=document.createElement("canvas");if(A.getContext&&A.getContext("2d")){var e="image/webp";return 0===A.toDataURL(e).indexOf("data:"+e)}return!1}()),A}}(),exports.supportAVIF=function(){var A;return function(){return A||(A=new Promise((function(A){var e=new Image;e.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",e.onerror=function(){A(!1)},e.onload=function(){A(!0)},setTimeout((function(){A(!1)}),1e3)}))),A}}();
package/es/image.d.ts CHANGED
@@ -40,10 +40,11 @@ export interface IOSSCompressOptions {
40
40
  /**
41
41
  * 如果指定图片 URL 的域名是 OSS 域名,且没有任何 OSS 处理参数,则根据压缩选项追加 OSS 图片压缩处理参数。
42
42
  * @param url 指定图片 URL。
43
- * @param options 压缩选项。
43
+ * @param options 压缩选项或缩放的宽度。
44
44
  * @returns 处理后的图片 URL。
45
45
  * @example
46
46
  * ```javascript
47
+ * ossCompress(url, 300);
47
48
  * ossCompress(url, {
48
49
  * width: 300,
49
50
  * allowJPG: true,
@@ -51,7 +52,7 @@ export interface IOSSCompressOptions {
51
52
  * });
52
53
  * ```
53
54
  */
54
- export declare function ossCompress(url: string, options: IOSSCompressOptions): string;
55
+ export declare function ossCompress(url: string, options: IOSSCompressOptions | number): string;
55
56
  /**
56
57
  * 对指定 HTML 代码中 img 标签的图片地址做 OSS 压缩处理。
57
58
  * @param html 指定 HTML 代码。
package/es/image.js CHANGED
@@ -1 +1 @@
1
- export const supportWebP=(()=>{let A;return()=>(null==A&&(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}()),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 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,s)=>(t=!0,/^image/.test(s)?o+"="+s.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}export function ossCompress(A,e){var t,o;let s;if("undefined"!=typeof document){const e=document.createElement("a");e.href=A,s=e}else if("function"==typeof URL)try{s=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}if(!s)return A;if(!/\.videocc\.net$/i.test(s.hostname))return A;const a=(s.pathname.split("/").pop()||"").split("."),r=a[a.length-1].toLowerCase();e.allowJPG=null===(t=e.allowJPG)||void 0===t||t,e.allowWebP=null!==(o=e.allowWebP)&&void 0!==o?o:"auto";const n=genOSSCompressParams(r,e);return n&&(s.search=setOSSCompressParams(s.search,n)),s.href}export function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((A,t,o,s,a)=>t+'"'+ossCompress(s,e)+'" data-src="'+s+'"'+a)):""}
1
+ export const supportWebP=(()=>{let A;return()=>(null==A&&(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}()),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 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,s)=>(t=!0,/^image/.test(s)?o+"="+s.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}export function ossCompress(A,e){var t,o;let s;if("undefined"!=typeof document){const e=document.createElement("a");e.href=A,s=e}else if("function"==typeof URL)try{s=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}if(!s)return A;if(!/\.videocc\.net$/i.test(s.hostname))return A;const a=(s.pathname.split("/").pop()||"").split("."),r=a[a.length-1].toLowerCase(),n="number"==typeof e?{width:e}:e;n.allowJPG=null===(t=n.allowJPG)||void 0===t||t,n.allowWebP=null!==(o=n.allowWebP)&&void 0!==o?o:"auto";const l=genOSSCompressParams(r,n);return l&&(s.search=setOSSCompressParams(s.search,l)),s.href}export function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((A,t,o,s,a)=>t+'"'+ossCompress(s,e)+'" data-src="'+s+'"'+a)):""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/utils",
3
- "version": "2.10.0-beta.4",
3
+ "version": "2.10.0",
4
4
  "description": "Utility functions of Polyv frontend development.",
5
5
  "keywords": [
6
6
  "polyv",