@polyv/utils 2.10.0-beta.1 → 2.10.0-beta.3

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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  ## 安装
6
6
 
7
7
  ```
8
- npm install @polyv/utils@next
8
+ npm install @polyv/utils
9
9
  ```
10
10
 
11
11
  NPM 包同时提供了 ES 模块以及 CommonJS 模块,分别位于 `es` 和 `cjs` 两个文件夹。它们使用上的区别和优缺点在于:
package/cjs/browser.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * @packageDocumentation
4
4
  */
5
5
  /**
6
- * 检测当前浏览器是否运行在移动设备上(User-Agent 识别为主,特征判断为辅)。
6
+ * 检测当前浏览器是否运行在移动设备上(仅使用 User-Agent 识别)。
7
7
  * @returns 当前浏览器是否运行在移动设备上。
8
8
  */
9
9
  export declare function isMobile(): boolean;
package/cjs/browser.js CHANGED
@@ -1 +1 @@
1
- "use strict";function isMobile(){var t=navigator.userAgent;return!!/mobile|android/i.test(t)||!/\b(Windows\sNT|Macintosh|x86(_(32|64))?|amd64|i[1-6]86)\b/.test(t)&&("onorientationchange"in window&&"number"==typeof window.orientation)}function hasMobileFeature(){var t=navigator.platform;return"onorientationchange"in window&&"number"==typeof window.orientation||(!!/^(iPhone|iPod|iPad)/.test(t)||"Win32"!==t&&!/^Linux\s*(x86|i686)/.test(t)&&("MacIntel"===t||"Macintosh"===t?navigator.maxTouchPoints>0:"function"==typeof matchMedia&&(matchMedia("(hover: none)").matches&&matchMedia("(pointer: coarse)").matches&&!matchMedia("(pointer: fine)").matches)))}function isMaxthon(){var t=navigator.userAgent;if(/\bMaxthon\b/i.test(t))return!0;if("maxthon"in window){var e=window.maxthon;return"function"==typeof e.IsMaxthon&&e.IsMaxthon()}return!1}function supportMSEH264(){var t=window.MediaSource;return t&&"function"==typeof t.isTypeSupported&&t.isTypeSupported('video/mp4; codecs="avc1.42E01E,mp4a.40.2"')}Object.defineProperty(exports,"__esModule",{value:!0}),exports.isMobile=isMobile,exports.hasMobileFeature=hasMobileFeature,exports.isMaxthon=isMaxthon,exports.supportMSEH264=supportMSEH264;
1
+ "use strict";function isMobile(){var t=navigator.userAgent;return!/\(X11[;)]/i.test(t)&&!/\b(Windows\sNT|Macintosh)\b/.test(t)&&/mobile|tablet|android/i.test(t)}function hasMobileFeature(){var t=navigator.platform;return"onorientationchange"in window&&"number"==typeof window.orientation||(!!/^(iPhone|iPod|iPad)/.test(t)||"Win32"!==t&&!/^Linux\s*(x86|i686)/.test(t)&&("MacIntel"===t||"Macintosh"===t?navigator.maxTouchPoints>0:"function"==typeof matchMedia&&(matchMedia("(hover: none)").matches&&matchMedia("(pointer: coarse)").matches&&!matchMedia("(pointer: fine)").matches)))}function isMaxthon(){var t=navigator.userAgent;if(/\bMaxthon\b/i.test(t))return!0;if("maxthon"in window){var e=window.maxthon;return"function"==typeof e.IsMaxthon&&e.IsMaxthon()}return!1}function supportMSEH264(){var t=window.MediaSource;return t&&"function"==typeof t.isTypeSupported&&t.isTypeSupported('video/mp4; codecs="avc1.42E01E,mp4a.40.2"')}Object.defineProperty(exports,"__esModule",{value:!0}),exports.isMobile=isMobile,exports.hasMobileFeature=hasMobileFeature,exports.isMaxthon=isMaxthon,exports.supportMSEH264=supportMSEH264;
package/cjs/image.d.ts CHANGED
@@ -6,12 +6,12 @@
6
6
  * 检查当前浏览器是否支持 WebP 格式。
7
7
  * @returns 当前浏览器是否支持 WebP 格式。
8
8
  */
9
- export declare function supportWebP(): boolean;
9
+ export declare const supportWebP: () => boolean;
10
10
  /**
11
11
  * 检查当前浏览器是否支持 AVIF 格式(注意,本函数是异步函数)。
12
12
  * @returns 当前浏览器是否支持 AVIF 格式。
13
13
  */
14
- export declare function supportAVIF(): Promise<boolean>;
14
+ export declare const supportAVIF: () => Promise<boolean>;
15
15
  /**
16
16
  * 压缩选项。
17
17
  */
@@ -29,9 +29,9 @@ export interface IOSSCompressOptions {
29
29
  */
30
30
  allowJPG?: boolean;
31
31
  /**
32
- * 是否允许转换为 WebP
32
+ * 是否允许转换为 WebP。设为 'auto' 时,只要当前浏览器支持 WebP,就进行转换。
33
33
  */
34
- allowWebP?: boolean;
34
+ allowWebP?: boolean | 'auto';
35
35
  /**
36
36
  * 是否允许转换为 AVIF。
37
37
  */
package/cjs/image.js CHANGED
@@ -1 +1 @@
1
- "use strict";function supportWebP(){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}function supportAVIF(){return 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)}))}function genOssProcessParams(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"),"gif"!==A&&(e.allowAVIF?t+="/format,avif":e.allowWebP?t+="/format,webp/quality,Q_80":e.allowJPG&&(t+="/format,jpg/quality,Q_80")),t}function ossCompress(A,e){var t;if("undefined"!=typeof document){var s=document.createElement("a");s.href=A,t=s}else"function"==typeof URL&&(t=new URL(/^\/\//.test(A)?"https:"+A:A));if(!t)return A;if(-1===["liveimages.videocc.net","vod-assets.videocc.net","polyvschool.videocc.net","img.videocc.net"].indexOf(t.hostname))return A;var o=(t.pathname.split("/").pop()||"").split("."),r=genOssProcessParams(o[o.length-1].toLowerCase(),e);if(r){var n=t.search.replace(/(?:\?|&)x-oss-process=[^&]+/g,"");n+=(-1===n.indexOf("?")?"?":"&")+"x-oss-process=image"+r,t.search=n}return t.href}function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(A,t,s,o,r){return t+'"'+ossCompress(o,e)+'" data-src="'+o+'"'+r})):""}Object.defineProperty(exports,"__esModule",{value:!0}),exports.supportWebP=supportWebP,exports.supportAVIF=supportAVIF,exports.ossCompress=ossCompress,exports.compressHTMLImgs=compressHTMLImgs;
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,s){return t=!0,/^image/.test(s)?r+"="+s.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A})),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}function ossCompress(A,e){var t;if("undefined"!=typeof document){var r=document.createElement("a");r.href=A,t=r}else if("function"==typeof URL)try{t=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}if(!t)return A;if(!/\.videocc\.net$/i.test(t.hostname))return A;var s=(t.pathname.split("/").pop()||"").split("."),o=genOSSCompressParams(s[s.length-1].toLowerCase(),e);return o&&(t.search=setOSSCompressParams(t.search,o)),t.href}function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(A,t,r,s,o){return t+'"'+ossCompress(s,e)+'" data-src="'+s+'"'+o})):""}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/browser.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * @packageDocumentation
4
4
  */
5
5
  /**
6
- * 检测当前浏览器是否运行在移动设备上(User-Agent 识别为主,特征判断为辅)。
6
+ * 检测当前浏览器是否运行在移动设备上(仅使用 User-Agent 识别)。
7
7
  * @returns 当前浏览器是否运行在移动设备上。
8
8
  */
9
9
  export declare function isMobile(): boolean;
package/es/browser.js CHANGED
@@ -1 +1 @@
1
- export function isMobile(){const n=navigator.userAgent;return!!/mobile|android/i.test(n)||!/\b(Windows\sNT|Macintosh|x86(_(32|64))?|amd64|i[1-6]86)\b/.test(n)&&("onorientationchange"in window&&"number"==typeof window.orientation)}export function hasMobileFeature(){const n=navigator.platform;return"onorientationchange"in window&&"number"==typeof window.orientation||(!!/^(iPhone|iPod|iPad)/.test(n)||"Win32"!==n&&!/^Linux\s*(x86|i686)/.test(n)&&("MacIntel"===n||"Macintosh"===n?navigator.maxTouchPoints>0:"function"==typeof matchMedia&&(matchMedia("(hover: none)").matches&&matchMedia("(pointer: coarse)").matches&&!matchMedia("(pointer: fine)").matches)))}export function isMaxthon(){const n=navigator.userAgent;if(/\bMaxthon\b/i.test(n))return!0;if("maxthon"in window){const n=window.maxthon;return"function"==typeof n.IsMaxthon&&n.IsMaxthon()}return!1}export function supportMSEH264(){const n=window.MediaSource;return n&&"function"==typeof n.isTypeSupported&&n.isTypeSupported('video/mp4; codecs="avc1.42E01E,mp4a.40.2"')}
1
+ export function isMobile(){const t=navigator.userAgent;return!/\(X11[;)]/i.test(t)&&!/\b(Windows\sNT|Macintosh)\b/.test(t)&&/mobile|tablet|android/i.test(t)}export function hasMobileFeature(){const t=navigator.platform;return"onorientationchange"in window&&"number"==typeof window.orientation||(!!/^(iPhone|iPod|iPad)/.test(t)||"Win32"!==t&&!/^Linux\s*(x86|i686)/.test(t)&&("MacIntel"===t||"Macintosh"===t?navigator.maxTouchPoints>0:"function"==typeof matchMedia&&(matchMedia("(hover: none)").matches&&matchMedia("(pointer: coarse)").matches&&!matchMedia("(pointer: fine)").matches)))}export function isMaxthon(){const t=navigator.userAgent;if(/\bMaxthon\b/i.test(t))return!0;if("maxthon"in window){const t=window.maxthon;return"function"==typeof t.IsMaxthon&&t.IsMaxthon()}return!1}export function supportMSEH264(){const t=window.MediaSource;return t&&"function"==typeof t.isTypeSupported&&t.isTypeSupported('video/mp4; codecs="avc1.42E01E,mp4a.40.2"')}
package/es/image.d.ts CHANGED
@@ -6,12 +6,12 @@
6
6
  * 检查当前浏览器是否支持 WebP 格式。
7
7
  * @returns 当前浏览器是否支持 WebP 格式。
8
8
  */
9
- export declare function supportWebP(): boolean;
9
+ export declare const supportWebP: () => boolean;
10
10
  /**
11
11
  * 检查当前浏览器是否支持 AVIF 格式(注意,本函数是异步函数)。
12
12
  * @returns 当前浏览器是否支持 AVIF 格式。
13
13
  */
14
- export declare function supportAVIF(): Promise<boolean>;
14
+ export declare const supportAVIF: () => Promise<boolean>;
15
15
  /**
16
16
  * 压缩选项。
17
17
  */
@@ -29,9 +29,9 @@ export interface IOSSCompressOptions {
29
29
  */
30
30
  allowJPG?: boolean;
31
31
  /**
32
- * 是否允许转换为 WebP
32
+ * 是否允许转换为 WebP。设为 'auto' 时,只要当前浏览器支持 WebP,就进行转换。
33
33
  */
34
- allowWebP?: boolean;
34
+ allowWebP?: boolean | 'auto';
35
35
  /**
36
36
  * 是否允许转换为 AVIF。
37
37
  */
package/es/image.js CHANGED
@@ -1 +1 @@
1
- export function supportWebP(){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}export function supportAVIF(){return 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)}))}function genOssProcessParams(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"),"gif"!==A&&(e.allowAVIF?t+="/format,avif":e.allowWebP?t+="/format,webp/quality,Q_80":e.allowJPG&&(t+="/format,jpg/quality,Q_80")),t}export function ossCompress(A,e){let t;if("undefined"!=typeof document){const e=document.createElement("a");e.href=A,t=e}else"function"==typeof URL&&(t=new URL(/^\/\//.test(A)?"https:"+A:A));if(!t)return A;if(-1===["liveimages.videocc.net","vod-assets.videocc.net","polyvschool.videocc.net","img.videocc.net"].indexOf(t.hostname))return A;const o=(t.pathname.split("/").pop()||"").split("."),s=genOssProcessParams(o[o.length-1].toLowerCase(),e);if(s){let A=t.search.replace(/(?:\?|&)x-oss-process=[^&]+/g,"");A+=(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+s,t.search=A}return t.href}export function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((A,t,o,s,n)=>t+'"'+ossCompress(s,e)+'" data-src="'+s+'"'+n)):""}
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,s,r)=>(t=!0,/^image/.test(r)?s+"="+r.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}export function ossCompress(A,e){let t;if("undefined"!=typeof document){const e=document.createElement("a");e.href=A,t=e}else if("function"==typeof URL)try{t=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}if(!t)return A;if(!/\.videocc\.net$/i.test(t.hostname))return A;const s=(t.pathname.split("/").pop()||"").split("."),r=genOSSCompressParams(s[s.length-1].toLowerCase(),e);return r&&(t.search=setOSSCompressParams(t.search,r)),t.href}export function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((A,t,s,r,o)=>t+'"'+ossCompress(r,e)+'" data-src="'+r+'"'+o)):""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/utils",
3
- "version": "2.10.0-beta.1",
3
+ "version": "2.10.0-beta.3",
4
4
  "description": "Utility functions of Polyv frontend development.",
5
5
  "keywords": [
6
6
  "polyv",