@polyv/utils 2.9.0 → 2.10.0-beta.2

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/image.d.ts CHANGED
@@ -24,6 +24,10 @@ export interface IOSSCompressOptions {
24
24
  * 压缩后的图片高度。
25
25
  */
26
26
  height?: number;
27
+ /**
28
+ * 是否允许转换为 JPG。
29
+ */
30
+ allowJPG?: boolean;
27
31
  /**
28
32
  * 是否允许转换为 WebP。
29
33
  */
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 ossCompress(A,e){var t="",s="";if("undefined"!=typeof document){var o=document.createElement("a");o.href=A,t=o.search,s=o.hostname}else if("function"==typeof URL){var r=new URL(/^\/\//.test(A)?"https:"+A:A);t=r.search,s=r.hostname}if(s=s.toLowerCase(),-1===["liveimages.videocc.net","vod-assets.videocc.net","polyvschool.videocc.net","img.videocc.net"].indexOf(s)||/(?:\?|&)x-oss-process(?:=|&|$)/.test(t))return A;var n="";return null==e.width&&null==e.height||(n+="/resize",e.width&&(n+=",w_"+e.width),e.height&&(n+=",h_"+e.height),n+=",limit_1"),e.allowAVIF?n+="/format,avif":e.allowWebP&&(n+="/format,webp"),n&&(n="x-oss-process=image"+n,A+=(-1===A.indexOf("?")?"?":"&")+n),A}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 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)||/(?:\?|&)x-oss-process(?:=|&|$)/.test(t.search))return A;var o=(t.pathname.split("/").pop()||"").split("."),r=genOssProcessParams(o[o.length-1].toLowerCase(),e);if(r){var n=t.search;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;
package/es/image.d.ts CHANGED
@@ -24,6 +24,10 @@ export interface IOSSCompressOptions {
24
24
  * 压缩后的图片高度。
25
25
  */
26
26
  height?: number;
27
+ /**
28
+ * 是否允许转换为 JPG。
29
+ */
30
+ allowJPG?: boolean;
27
31
  /**
28
32
  * 是否允许转换为 WebP。
29
33
  */
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)}))}export function ossCompress(A,e){let t="",o="";if("undefined"!=typeof document){const e=document.createElement("a");e.href=A,t=e.search,o=e.hostname}else if("function"==typeof URL){const e=new URL(/^\/\//.test(A)?"https:"+A:A);t=e.search,o=e.hostname}if(o=o.toLowerCase(),-1===["liveimages.videocc.net","vod-assets.videocc.net","polyvschool.videocc.net","img.videocc.net"].indexOf(o)||/(?:\?|&)x-oss-process(?:=|&|$)/.test(t))return A;let s="";return null==e.width&&null==e.height||(s+="/resize",e.width&&(s+=",w_"+e.width),e.height&&(s+=",h_"+e.height),s+=",limit_1"),e.allowAVIF?s+="/format,avif":e.allowWebP&&(s+="/format,webp"),s&&(s="x-oss-process=image"+s,A+=(-1===A.indexOf("?")?"?":"&")+s),A}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 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)||/(?:\?|&)x-oss-process(?:=|&|$)/.test(t.search))return A;const s=(t.pathname.split("/").pop()||"").split("."),o=genOssProcessParams(s[s.length-1].toLowerCase(),e);if(o){let A=t.search;A+=(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+o,t.search=A}return t.href}export function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((A,t,s,o,n)=>t+'"'+ossCompress(o,e)+'" data-src="'+o+'"'+n)):""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/utils",
3
- "version": "2.9.0",
3
+ "version": "2.10.0-beta.2",
4
4
  "description": "Utility functions of Polyv frontend development.",
5
5
  "keywords": [
6
6
  "polyv",