@polyv/utils 2.13.0 → 2.14.0-beta.1

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/color.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * 本模块提供颜色处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 将十六进制的颜色转换成 r、g、b 格式数组
7
+ * @param hex 十六进制的颜色
8
+ * @returns r、g、b 格式数组
9
+ * @example
10
+ * ```javascript
11
+ * hexToRGB('#f00'); // [255, 0, 0]
12
+ * hexToRGB('#ffff00'); // [255, 255, 0]
13
+ * ```
14
+ */
15
+ export declare function hexToRGB(hex: string): [number, number, number] | null;
package/cjs/color.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";function hexToRGB(e){var t=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e)||/^#([a-f\d])([a-f\d])([a-f\d])$/.exec(e);return t?[t[1],t[2],t[3]].map((function(e){return parseInt(1===e.length?e+e:e,16)})):null}Object.defineProperty(exports,"__esModule",{value:!0}),exports.hexToRGB=hexToRGB;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * 本模块提供加密处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 生成保利威 API 签名(https://help.polyv.net/index.html#/live/api/buildSign)。
7
+ * @param obj 需要签名的对象。
8
+ * @param appSecret 应用密钥。
9
+ * @returns 签名字符串。
10
+ */
11
+ export declare function genAPISign(obj: Record<string, unknown>, appSecret: string): string;
package/cjs/encrypt.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.genAPISign=genAPISign;var md5=require("md5");function genAPISign(e,r){var t=Object.keys(e).filter((function(e){return"sign"!==e})).sort(),n="";return t.forEach((function(r){var t=e[r];"object"==typeof t&&(t=JSON.stringify(t)),n+=r+t})),md5(r+n+r).toString().toUpperCase()}
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(i,n,e,t){return new(e||(e=Promise))((function(r,o){function a(i){try{u(t.next(i))}catch(i){o(i)}}function l(i){try{u(t.throw(i))}catch(i){o(i)}}function u(i){var n;i.done?r(i.value):(n=i.value,n instanceof e?n:new e((function(i){i(n)}))).then(a,l)}u((t=t.apply(i,n||[])).next())}))},__generator=this&&this.__generator||function(i,n){var e,t,r,o,a={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return o={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function l(l){return function(u){return function(l){if(e)throw new TypeError("Generator is already executing.");for(;o&&(o=0,l[0]&&(a=0)),a;)try{if(e=1,t&&(r=2&l[0]?t.return:l[0]?t.throw||((r=t.return)&&r.call(t),0):t.next)&&!(r=r.call(t,l[1])).done)return r;switch(t=0,r&&(l=[2&l[0],r.value]),l[0]){case 0:case 1:r=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,t=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(r=a.trys,(r=r.length>0&&r[r.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!r||l[1]>r[0]&&l[1]<r[3])){a.label=l[1];break}if(6===l[0]&&a.label<r[1]){a.label=r[1],r=l;break}if(r&&a.label<r[2]){a.label=r[2],a.ops.push(l);break}r[2]&&a.ops.pop(),a.trys.pop();continue}l=n.call(i,a)}catch(i){l=[6,i],t=0}finally{e=r=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,u])}}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.LinkJumpWay=exports.LinkType=exports.isIOS=exports.isAndroid=void 0,exports.formatLink=formatLink,exports.navigateToLink=navigateToLink;var LinkType,LinkJumpWay,ua_info_1=require("@just4/ua-info"),querystring_1=require("@just4/querystring"),uaInfo=(0,ua_info_1.getCurrentUAInfo)();function getNativeToPointMallFn(){var i,n,e,t;return exports.isAndroid?null===(i=window.AndroidNative)||void 0===i?void 0:i.toPointMall:exports.isIOS?null===(t=null===(e=null===(n=window.webkit)||void 0===n?void 0:n.messageHandlers)||void 0===e?void 0:e.gotoPointsMall)||void 0===t?void 0:t.postMessage:void 0}function toNativeLink(i){var n=i.androidLink,e=i.iosLink,t=i.otherLink,r=getNativeToPointMallFn();if(r){var o=exports.isAndroid?n:e,a=encodeURIComponent(location.href);return o=(0,querystring_1.concat)(o,{plt_back_uri:a}),void r(JSON.stringify({url:o}))}window.open(t,"_blank","noopener=yes")}function toPlvWebviewBridge(i){return __awaiter(this,void 0,void 0,(function(){var n,e,t,r;return __generator(this,(function(o){switch(o.label){case 0:return n=i.getPlvWebviewSmallWindowSize,e=i.getPlvWebviewBridge,t=(null==n?void 0:n())||{width:90,height:160},[4,null==e?void 0:e()];case 1:return(r=o.sent())?(r.sendData("clickProduct",{width:t.width,height:t.height,newPage:!0,link:i.link,data:i.data}),[2]):[2]}}))}))}function toMultiPlatformLink(i){return __awaiter(this,void 0,void 0,(function(){var n,e,t,r,o,a,l,u,s,k,d;return __generator(this,(function(v){switch(v.label){case 0:n=i.linkData,e=i.isWxMiniProgramEnv,t=i.toWxMiniProgram,r=i.openLink,o=i.getLinkParams,a=i.isMobile,l=n.wxMiniprogramLink,u=n.mobileLink,s=n.pcLink,k=(null==a?void 0:a())||!1,d=!1,v.label=1;case 1:return v.trys.push([1,3,,4]),[4,null==e?void 0:e()];case 2:return d=v.sent()||!1,[3,4];case 3:return v.sent(),d=!1,[3,4];case 4:return d&&l&&t?(t(formatLink(l,o)),[2]):k&&u?(r(formatLink(u,o),LinkJumpWay.NewWindow),[2]):(!k&&s&&r(formatLink(s,o),LinkJumpWay.NewWindow),[2])}}))}))}function formatLink(i,n){var e={};if(n){var t=n(i);e=Object.assign({},e,t)}return(0,querystring_1.concat)(i,e)}function navigateToLink(i){var n=i.linkData,e=i.openLink,t=i.isPlvWebview,r=i.getPlvWebviewSmallWindowSize,o=i.getPlvWebviewBridge,a=i.isWxMiniProgramEnv,l=i.toWxMiniProgram,u=i.getLinkParams,s=i.isMobile,k=n.linkType,d=(null==t?void 0:t())||!1;if(k!==LinkType.Native){if(d){var v="",f=n.link,p=n.mobileLink,c=n.mobileAppLink,L=n.wxMiniprogramOriginalId,m=n.wxMiniprogramLink;switch(k){case LinkType.Normal:v=f;break;case LinkType.MultiPlatform:v=c||p}var g=null;return k!==LinkType.Normal&&(g={mobileLink:formatLink(p,u),wxMiniprogramOriginalId:L,wxMiniprogramLink:formatLink(m,u),mobileAppLink:formatLink(c,u)}),void toPlvWebviewBridge({link:formatLink(v,u),data:g,getPlvWebviewSmallWindowSize:r,getPlvWebviewBridge:o})}if(k!==LinkType.Normal)k===LinkType.MultiPlatform&&toMultiPlatformLink({linkData:n,getLinkParams:u,isWxMiniProgramEnv:a,toWxMiniProgram:l,openLink:e,isMobile:s});else{f=n.link;var w=n.jumpWay;e(formatLink(f,u),w)}}else toNativeLink({androidLink:formatLink(n.androidLink,u),iosLink:formatLink(n.iosLink,u),otherLink:formatLink(n.otherLink,u)})}exports.isAndroid=uaInfo.os.isAndroid,exports.isIOS=uaInfo.os.isIOS,function(i){i[i.Normal=10]="Normal",i[i.MultiPlatform=11]="MultiPlatform",i[i.Native=12]="Native"}(LinkType||(exports.LinkType=LinkType={})),function(i){i.PopUp="POP_UP",i.NewWindow="NEW_WINDOW",i.CurrentWindow="CURRENT_WINDOW"}(LinkJumpWay||(exports.LinkJumpWay=LinkJumpWay={}));
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(i,n,e,o){return new(e||(e=Promise))((function(t,r){function a(i){try{s(o.next(i))}catch(i){r(i)}}function l(i){try{s(o.throw(i))}catch(i){r(i)}}function s(i){var n;i.done?t(i.value):(n=i.value,n instanceof e?n:new e((function(i){i(n)}))).then(a,l)}s((o=o.apply(i,n||[])).next())}))},__generator=this&&this.__generator||function(i,n){var e,o,t,r,a={label:0,sent:function(){if(1&t[0])throw t[1];return t[1]},trys:[],ops:[]};return r={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function l(l){return function(s){return function(l){if(e)throw new TypeError("Generator is already executing.");for(;r&&(r=0,l[0]&&(a=0)),a;)try{if(e=1,o&&(t=2&l[0]?o.return:l[0]?o.throw||((t=o.return)&&t.call(o),0):o.next)&&!(t=t.call(o,l[1])).done)return t;switch(o=0,t&&(l=[2&l[0],t.value]),l[0]){case 0:case 1:t=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,o=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(t=a.trys,(t=t.length>0&&t[t.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!t||l[1]>t[0]&&l[1]<t[3])){a.label=l[1];break}if(6===l[0]&&a.label<t[1]){a.label=t[1],t=l;break}if(t&&a.label<t[2]){a.label=t[2],a.ops.push(l);break}t[2]&&a.ops.pop(),a.trys.pop();continue}l=n.call(i,a)}catch(i){l=[6,i],o=0}finally{e=t=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,s])}}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.LinkJumpWay=exports.LinkType=exports.isIOS=exports.isAndroid=void 0,exports.formatLink=formatLink,exports.navigateToLink=navigateToLink;var LinkType,LinkJumpWay,ua_info_1=require("@just4/ua-info"),querystring_1=require("@just4/querystring"),uaInfo=(0,ua_info_1.getCurrentUAInfo)();function invokeNativePointMall(i){var n,e,o,t,r,a;exports.isAndroid?null===(e=null===(n=window.AndroidNative)||void 0===n?void 0:n.toPointMall)||void 0===e||e.call(n,i):exports.isIOS&&(null===(a=null===(r=null===(t=null===(o=window.webkit)||void 0===o?void 0:o.messageHandlers)||void 0===t?void 0:t.gotoPointsMall)||void 0===r?void 0:r.postMessage)||void 0===a||a.call(r,i))}function toNativeLink(i){var n,e,o,t,r=i.androidLink,a=i.iosLink,l=i.otherLink;if((null===(n=window.AndroidNative)||void 0===n?void 0:n.toPointMall)||(null===(t=null===(o=null===(e=window.webkit)||void 0===e?void 0:e.messageHandlers)||void 0===o?void 0:o.gotoPointsMall)||void 0===t?void 0:t.postMessage)){var s=exports.isAndroid?r:a,u=encodeURIComponent(location.href);s=(0,querystring_1.concat)(s,{plt_back_uri:u}),invokeNativePointMall(JSON.stringify({url:s}))}else window.open(l,"_blank","noopener=yes")}function toPlvWebviewBridge(i){return __awaiter(this,void 0,void 0,(function(){var n,e,o,t;return __generator(this,(function(r){switch(r.label){case 0:return n=i.getPlvWebviewSmallWindowSize,e=i.getPlvWebviewBridge,o=(null==n?void 0:n())||{width:90,height:160},[4,null==e?void 0:e()];case 1:return(t=r.sent())?(t.sendData("clickProduct",{width:o.width,height:o.height,newPage:!0,link:i.link,data:i.data}),[2]):[2]}}))}))}function toMultiPlatformLink(i){return __awaiter(this,void 0,void 0,(function(){var n,e,o,t,r,a,l,s,u,d,v;return __generator(this,(function(k){switch(k.label){case 0:n=i.linkData,e=i.isWxMiniProgramEnv,o=i.toWxMiniProgram,t=i.openLink,r=i.getLinkParams,a=i.isMobile,l=n.wxMiniprogramLink,s=n.mobileLink,u=n.pcLink,d=(null==a?void 0:a())||!1,v=!1,k.label=1;case 1:return k.trys.push([1,3,,4]),[4,null==e?void 0:e()];case 2:return v=k.sent()||!1,[3,4];case 3:return k.sent(),v=!1,[3,4];case 4:return v&&l&&o?(o(formatLink(l,r)),[2]):d&&s?(t(formatLink(s,r),LinkJumpWay.NewWindow),[2]):(!d&&u&&t(formatLink(u,r),LinkJumpWay.NewWindow),[2])}}))}))}function formatLink(i,n){var e={};if(n){var o=n(i);e=Object.assign({},e,o)}return(0,querystring_1.concat)(i,e)}function navigateToLink(i){var n=i.linkData,e=i.openLink,o=i.isPlvWebview,t=i.getPlvWebviewSmallWindowSize,r=i.getPlvWebviewBridge,a=i.isWxMiniProgramEnv,l=i.toWxMiniProgram,s=i.getLinkParams,u=i.isMobile,d=n.linkType,v=(null==o?void 0:o())||!1;if(d!==LinkType.Native){if(v){var k="",c=n.link,p=n.mobileLink,f=n.mobileAppLink,L=n.wxMiniprogramOriginalId,m=n.wxMiniprogramLink;switch(d){case LinkType.Normal:k=c;break;case LinkType.MultiPlatform:k=f||p}var w=null;return d!==LinkType.Normal&&(w={mobileLink:formatLink(p,s),wxMiniprogramOriginalId:L,wxMiniprogramLink:formatLink(m,s),mobileAppLink:formatLink(f,s)}),void toPlvWebviewBridge({link:formatLink(k,s),data:w,getPlvWebviewSmallWindowSize:t,getPlvWebviewBridge:r})}if(d!==LinkType.Normal)d===LinkType.MultiPlatform&&toMultiPlatformLink({linkData:n,getLinkParams:s,isWxMiniProgramEnv:a,toWxMiniProgram:l,openLink:e,isMobile:u});else{c=n.link;var g=n.jumpWay;e(formatLink(c,s),g)}}else toNativeLink({androidLink:formatLink(n.androidLink,s),iosLink:formatLink(n.iosLink,s),otherLink:formatLink(n.otherLink,s)})}exports.isAndroid=uaInfo.os.isAndroid,exports.isIOS=uaInfo.os.isIOS,function(i){i[i.Normal=10]="Normal",i[i.MultiPlatform=11]="MultiPlatform",i[i.Native=12]="Native"}(LinkType||(exports.LinkType=LinkType={})),function(i){i.PopUp="POP_UP",i.NewWindow="NEW_WINDOW",i.CurrentWindow="CURRENT_WINDOW"}(LinkJumpWay||(exports.LinkJumpWay=LinkJumpWay={}));
package/cjs/file.d.ts CHANGED
@@ -47,3 +47,40 @@ export interface FileSizeConversionResult {
47
47
  * @returns 转换结果。
48
48
  */
49
49
  export declare function convertFileSize(sizeInBytes: number, options: FileSizeUnit | FileSizeConversionOptions): FileSizeConversionResult;
50
+ /**
51
+ * 获取扩展名,支持传入文件名或 URL。
52
+ * @param url 文件名或 URL。
53
+ * @returns 扩展名。
54
+ */
55
+ export declare function getExtname(url: string): string;
56
+ /**
57
+ * 文件类型。
58
+ */
59
+ export declare enum FileType {
60
+ /**
61
+ * 演示稿。
62
+ */
63
+ PPT = "ppt",
64
+ /**
65
+ * PDF 文档。
66
+ */
67
+ PDF = "pdf",
68
+ /**
69
+ * Word 文档。
70
+ */
71
+ Word = "word",
72
+ /**
73
+ * 图片。
74
+ */
75
+ Image = "image",
76
+ /**
77
+ * 其他。
78
+ */
79
+ Others = "others"
80
+ }
81
+ /**
82
+ * 获取文件类型,支持传入文件名或 URL。
83
+ * @param url 文件名或 URL。
84
+ * @returns 文件类型。
85
+ */
86
+ export declare function getFileType(url: string): FileType;
package/cjs/file.js CHANGED
@@ -1 +1 @@
1
- "use strict";var FileSizeUnit;function convertFileSize(e,i){var t="object"==typeof i?Object.assign({},i):{unit:i};if(!t.unit)throw new Error("Please specify file size unit");var n=[FileSizeUnit.B,FileSizeUnit.KB,FileSizeUnit.MB,FileSizeUnit.GB,FileSizeUnit.TB],l={value:e,unit:t.unit};return n.some((function(i,r){var o=e/Math.pow(1024,r);return!1===t.allowPureDecimal&&o<1||(l.value=o,l.unit=i,t.unit===i||r===n.length)})),l}Object.defineProperty(exports,"__esModule",{value:!0}),exports.FileSizeUnit=void 0,exports.convertFileSize=convertFileSize,function(e){e.B="B",e.KB="KB",e.MB="MB",e.GB="GB",e.TB="TB"}(FileSizeUnit||(exports.FileSizeUnit=FileSizeUnit={}));
1
+ "use strict";var FileSizeUnit,FileType;function convertFileSize(e,i){var t="object"==typeof i?Object.assign({},i):{unit:i};if(!t.unit)throw new Error("Please specify file size unit");var n=[FileSizeUnit.B,FileSizeUnit.KB,FileSizeUnit.MB,FileSizeUnit.GB,FileSizeUnit.TB],r={value:e,unit:t.unit};return n.some((function(i,p){var l=e/Math.pow(1024,p);return!1===t.allowPureDecimal&&l<1||(r.value=l,r.unit=i,t.unit===i||p===n.length)})),r}function getExtname(e){var i=e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/^([a-z]+:)?\/\/[^/]+/i,"").split("/"),t=/\.([a-z0-9]+)$/.exec(i[i.length-1]);return t?t[1].toLowerCase():""}function getFileType(e){var i=getExtname(e);return-1!==["pdf"].indexOf(i)?FileType.PDF:-1!==["doc","docx"].indexOf(i)?FileType.Word:-1!==["ppt","pptx"].indexOf(i)?FileType.PPT:-1!==["jpg","jpeg","png","gif","bmp","webp","svg"].indexOf(i)?FileType.Image:FileType.Others}Object.defineProperty(exports,"__esModule",{value:!0}),exports.FileType=exports.FileSizeUnit=void 0,exports.convertFileSize=convertFileSize,exports.getExtname=getExtname,exports.getFileType=getFileType,function(e){e.B="B",e.KB="KB",e.MB="MB",e.GB="GB",e.TB="TB"}(FileSizeUnit||(exports.FileSizeUnit=FileSizeUnit={})),function(e){e.PPT="ppt",e.PDF="pdf",e.Word="word",e.Image="image",e.Others="others"}(FileType||(exports.FileType=FileType={}));
package/cjs/image.d.ts CHANGED
@@ -68,3 +68,17 @@ export declare function cosCompress(url: string, options: IOSSCompressOptions |
68
68
  * @returns 处理后的 HTML 代码。
69
69
  */
70
70
  export declare function compressHTMLImgs(html: string, options: IOSSCompressOptions, compressor?: (url: string, options: IOSSCompressOptions | number) => string): string;
71
+ /**
72
+ * 预加载图片。
73
+ * @param url 图片 URL。
74
+ * @returns 图片尺寸和图片元素。
75
+ * @example
76
+ * ```javascript
77
+ * await preloadImg('https://example.com/image.jpg'); // { width, height, element }
78
+ * ```
79
+ */
80
+ export declare function preloadImg(url: string): Promise<{
81
+ width: number;
82
+ height: number;
83
+ element: HTMLImageElement;
84
+ }>;
package/cjs/image.js CHANGED
@@ -1 +1 @@
1
- "use strict";function getExtname(e){var A=(e||"").split("."),t=A[A.length-1];return t?t.toLowerCase():""}function handleCompressOptions(e){var A,t,r="number"==typeof e?{width:e}:e;return r.allowJPG=null===(A=r.allowJPG)||void 0===A||A,r.allowWebP=null!==(t=r.allowWebP)&&void 0!==t?t:"auto",r}function createURLObject(e){var A;if("undefined"!=typeof document){var t=document.createElement("a");t.href=e,A=t}else if("function"==typeof URL)try{A=new URL(/^\/\//.test(e)?"https:"+e:e)}catch(e){}return A}function genOSSCompressParams(e,A){var t="";return null==A.width&&null==A.height||(t+="/resize",A.width&&(t+=",w_"+A.width),A.height&&(t+=",h_"+A.height),t+=",limit_1"),A.allowAVIF&&"gif"!==e?t+="/format,avif":!0===A.allowWebP||"auto"===A.allowWebP&&(0,exports.supportWebP)()?t+="/format,webp/quality,Q_80":A.allowJPG&&"gif"!==e&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(e,A){var t=!1;return e=e.replace(/([?&]x-oss-process)(?:=([^&]*))?/,(function(e,r,a){return t=!0,/^image/.test(a)?r+"="+a.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+A:e})),t?e:e+(-1===e.indexOf("?")?"?":"&")+"x-oss-process=image"+A}function ossCompress(e,A){var t=createURLObject(e);if(!t)return e;var r=getExtname(t.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(t.hostname)||"svg"===r)return e;var a=genOSSCompressParams(r,handleCompressOptions(A));return a&&(t.search=setOSSCompressParams(t.search,a)),t.href}function genCOSCompressParams(e,A){var t="";return null!=A.width&&null!=A.height?t+="/thumbnail/".concat(A.width,"x").concat(A.height):null!=A.width?t+="/thumbnail/".concat(A.width,"x"):null!=A.height&&(t+="/thumbnail/x".concat(A.height)),A.allowAVIF&&"gif"!==e?t+="/format/avif":!0===A.allowWebP||"auto"===A.allowWebP&&(0,exports.supportWebP)()?t+="/format/webp/quality/80":A.allowJPG&&"gif"!==e&&(t+="/format/jpg/quality/80"),t}function setCOSCompressParams(e,A){var t=!1;return e=e.replace(/([?&]imageMogr2)(\/[^&]*)/,(function(e,r,a){return t=!0,r+"="+a.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+A})),t?e:e+(-1===e.indexOf("?")?"?":"&")+"imageMogr2"+A}function cosCompress(e,A){var t=createURLObject(e);if(!t)return e;var r=getExtname(t.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(t.hostname)||"svg"===r)return e;var a=genCOSCompressParams(r,handleCompressOptions(A));return a&&(t.search=setCOSCompressParams(t.search,a)),t.href}function compressHTMLImgs(e,A,t){return void 0===t&&(t=ossCompress),e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(e,r,a,s,o){return r+'"'+t(s,A)+'" data-src="'+s+'"'+o})):""}Object.defineProperty(exports,"__esModule",{value:!0}),exports.supportAVIF=exports.supportWebP=void 0,exports.ossCompress=ossCompress,exports.cosCompress=cosCompress,exports.compressHTMLImgs=compressHTMLImgs,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 A="image/webp";return 0===e.toDataURL(A).indexOf("data:"+A)}return!1}()||function(){var e=!1,A=navigator.userAgent,t=/\bOS\s([\d_.]+)\slike\sMac\sOS\sX\b/.exec(A);if(t)e=parseInt(t[1])>=14;else{var r=/\bVersion\/([\d.]+)\sSafari\b/.exec(A);r&&(e=parseInt(r[1])>=16)}return e}()}catch(A){e=!1}return e}}(),exports.supportAVIF=function(){var e;return function(){return e||(e=new Promise((function(e){var A=new Image;A.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",A.onerror=function(){e(!1)},A.onload=function(){e(!0)},setTimeout((function(){e(!1)}),1e3)}))),e}}();
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}}();
package/cjs/json.d.ts CHANGED
@@ -15,8 +15,8 @@ export declare function cloneJSON(obj: unknown): any;
15
15
  /**
16
16
  * JSON 字符串解析失败时的回调。
17
17
  */
18
- export interface IErrorCallback {
19
- (e: Error): unknown;
18
+ export interface IErrorCallback<T> {
19
+ (e: Error): void | null | T;
20
20
  }
21
21
  /**
22
22
  * 尝试把指定字符串解析为 JSON 对象。
@@ -29,7 +29,7 @@ export interface IErrorCallback {
29
29
  * tryParseJSON('{"a": 1}'); // { a: 1 }
30
30
  * ```
31
31
  */
32
- export declare function tryParseJSON(str: string, onError?: IErrorCallback): unknown;
32
+ export declare function tryParseJSON<T>(str: string, onError?: IErrorCallback<T>): T | null;
33
33
  /**
34
34
  * 尝试把指定字符串解析为 JSON 对象。
35
35
  * @param str 指定字符串。
@@ -40,4 +40,4 @@ export declare function tryParseJSON(str: string, onError?: IErrorCallback): unk
40
40
  * tryParseJSON('12&&**', 1); // 1
41
41
  * ```
42
42
  */
43
- export declare function tryParseJSON(str: string, defaultValue?: unknown): unknown;
43
+ export declare function tryParseJSON<T>(str: string, defaultValue?: T): T | null;
package/cjs/json.js CHANGED
@@ -1 +1 @@
1
- "use strict";function cloneJSON(e){return null==e?e:JSON.parse(JSON.stringify(e))}function tryParseJSON(e,r){var t;try{t=JSON.parse(e)}catch(e){t="function"==typeof r?r(e):r}return t}Object.defineProperty(exports,"__esModule",{value:!0}),exports.cloneJSON=cloneJSON,exports.tryParseJSON=tryParseJSON;
1
+ "use strict";function cloneJSON(e){return null==e?e:JSON.parse(JSON.stringify(e))}function tryParseJSON(e,r){var n,t=null;try{t=JSON.parse(e)}catch(e){t="function"==typeof r?null!==(n=r(e))&&void 0!==n?n:null:null!=r?r:null}return t}Object.defineProperty(exports,"__esModule",{value:!0}),exports.cloneJSON=cloneJSON,exports.tryParseJSON=tryParseJSON;
package/es/color.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * 本模块提供颜色处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 将十六进制的颜色转换成 r、g、b 格式数组
7
+ * @param hex 十六进制的颜色
8
+ * @returns r、g、b 格式数组
9
+ * @example
10
+ * ```javascript
11
+ * hexToRGB('#f00'); // [255, 0, 0]
12
+ * hexToRGB('#ffff00'); // [255, 255, 0]
13
+ * ```
14
+ */
15
+ export declare function hexToRGB(hex: string): [number, number, number] | null;
package/es/color.js ADDED
@@ -0,0 +1 @@
1
+ export function hexToRGB(e){const a=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e)||/^#([a-f\d])([a-f\d])([a-f\d])$/.exec(e);return a?[a[1],a[2],a[3]].map((e=>parseInt(1===e.length?e+e:e,16))):null}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * 本模块提供加密处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 生成保利威 API 签名(https://help.polyv.net/index.html#/live/api/buildSign)。
7
+ * @param obj 需要签名的对象。
8
+ * @param appSecret 应用密钥。
9
+ * @returns 签名字符串。
10
+ */
11
+ export declare function genAPISign(obj: Record<string, unknown>, appSecret: string): string;
package/es/encrypt.js ADDED
@@ -0,0 +1 @@
1
+ import*as md5 from"md5";export function genAPISign(t,e){const o=Object.keys(t).filter((t=>"sign"!==t)).sort();let r="";return o.forEach((e=>{let o=t[e];"object"==typeof o&&(o=JSON.stringify(o)),r+=e+o})),md5(e+r+e).toString().toUpperCase()}
@@ -1 +1 @@
1
- var __awaiter=this&&this.__awaiter||function(i,n,o,t){return new(o||(o=Promise))((function(e,a){function r(i){try{k(t.next(i))}catch(i){a(i)}}function l(i){try{k(t.throw(i))}catch(i){a(i)}}function k(i){var n;i.done?e(i.value):(n=i.value,n instanceof o?n:new o((function(i){i(n)}))).then(r,l)}k((t=t.apply(i,n||[])).next())}))};import{getCurrentUAInfo}from"@just4/ua-info";import{concat}from"@just4/querystring";const uaInfo=getCurrentUAInfo();export const isAndroid=uaInfo.os.isAndroid;export const isIOS=uaInfo.os.isIOS;export var LinkType;!function(i){i[i.Normal=10]="Normal",i[i.MultiPlatform=11]="MultiPlatform",i[i.Native=12]="Native"}(LinkType||(LinkType={}));export var LinkJumpWay;function getNativeToPointMallFn(){var i,n,o,t;return isAndroid?null===(i=window.AndroidNative)||void 0===i?void 0:i.toPointMall:isIOS?null===(t=null===(o=null===(n=window.webkit)||void 0===n?void 0:n.messageHandlers)||void 0===o?void 0:o.gotoPointsMall)||void 0===t?void 0:t.postMessage:void 0}function toNativeLink(i){const{androidLink:n,iosLink:o,otherLink:t}=i,e=getNativeToPointMallFn();if(e){let i=isAndroid?n:o;const t=encodeURIComponent(location.href);i=concat(i,{plt_back_uri:t});e(JSON.stringify({url:i}))}else window.open(t,"_blank","noopener=yes")}function toPlvWebviewBridge(i){return __awaiter(this,void 0,void 0,(function*(){const{getPlvWebviewSmallWindowSize:n,getPlvWebviewBridge:o}=i,t=(null==n?void 0:n())||{width:90,height:160},e=yield null==o?void 0:o();e&&e.sendData("clickProduct",{width:t.width,height:t.height,newPage:!0,link:i.link,data:i.data})}))}function toMultiPlatformLink(i){return __awaiter(this,void 0,void 0,(function*(){const{linkData:n,isWxMiniProgramEnv:o,toWxMiniProgram:t,openLink:e,getLinkParams:a,isMobile:r}=i,{wxMiniprogramLink:l,mobileLink:k,pcLink:d}=n,s=(null==r?void 0:r())||!1;let u=!1;try{u=(yield null==o?void 0:o())||!1}catch(i){u=!1}u&&l&&t?t(formatLink(l,a)):s&&k?e(formatLink(k,a),LinkJumpWay.NewWindow):!s&&d&&e(formatLink(d,a),LinkJumpWay.NewWindow)}))}!function(i){i.PopUp="POP_UP",i.NewWindow="NEW_WINDOW",i.CurrentWindow="CURRENT_WINDOW"}(LinkJumpWay||(LinkJumpWay={}));export function formatLink(i,n){let o={};if(n){const t=n(i);o=Object.assign({},o,t)}return concat(i,o)}export function navigateToLink(i){const{linkData:n,openLink:o,isPlvWebview:t,getPlvWebviewSmallWindowSize:e,getPlvWebviewBridge:a,isWxMiniProgramEnv:r,toWxMiniProgram:l,getLinkParams:k,isMobile:d}=i,{linkType:s}=n,u=(null==t?void 0:t())||!1;if(s!==LinkType.Native){if(u){let i="";const{link:o,mobileLink:t,mobileAppLink:r,wxMiniprogramOriginalId:l,wxMiniprogramLink:d}=n;switch(s){case LinkType.Normal:i=o;break;case LinkType.MultiPlatform:i=r||t}let u=null;return s!==LinkType.Normal&&(u={mobileLink:formatLink(t,k),wxMiniprogramOriginalId:l,wxMiniprogramLink:formatLink(d,k),mobileAppLink:formatLink(r,k)}),void toPlvWebviewBridge({link:formatLink(i,k),data:u,getPlvWebviewSmallWindowSize:e,getPlvWebviewBridge:a})}if(s!==LinkType.Normal)s===LinkType.MultiPlatform&&toMultiPlatformLink({linkData:n,getLinkParams:k,isWxMiniProgramEnv:r,toWxMiniProgram:l,openLink:o,isMobile:d});else{const{link:i,jumpWay:t}=n;o(formatLink(i,k),t)}}else toNativeLink({androidLink:formatLink(n.androidLink,k),iosLink:formatLink(n.iosLink,k),otherLink:formatLink(n.otherLink,k)})}
1
+ var __awaiter=this&&this.__awaiter||function(i,n,o,t){return new(o||(o=Promise))((function(e,a){function r(i){try{d(t.next(i))}catch(i){a(i)}}function l(i){try{d(t.throw(i))}catch(i){a(i)}}function d(i){var n;i.done?e(i.value):(n=i.value,n instanceof o?n:new o((function(i){i(n)}))).then(r,l)}d((t=t.apply(i,n||[])).next())}))};import{getCurrentUAInfo}from"@just4/ua-info";import{concat}from"@just4/querystring";const uaInfo=getCurrentUAInfo();export const isAndroid=uaInfo.os.isAndroid;export const isIOS=uaInfo.os.isIOS;export var LinkType;!function(i){i[i.Normal=10]="Normal",i[i.MultiPlatform=11]="MultiPlatform",i[i.Native=12]="Native"}(LinkType||(LinkType={}));export var LinkJumpWay;function invokeNativePointMall(i){var n,o,t,e,a,r;isAndroid?null===(o=null===(n=window.AndroidNative)||void 0===n?void 0:n.toPointMall)||void 0===o||o.call(n,i):isIOS&&(null===(r=null===(a=null===(e=null===(t=window.webkit)||void 0===t?void 0:t.messageHandlers)||void 0===e?void 0:e.gotoPointsMall)||void 0===a?void 0:a.postMessage)||void 0===r||r.call(a,i))}function toNativeLink(i){var n,o,t,e;const{androidLink:a,iosLink:r,otherLink:l}=i;if(!(null===(n=window.AndroidNative)||void 0===n?void 0:n.toPointMall)&&!(null===(e=null===(t=null===(o=window.webkit)||void 0===o?void 0:o.messageHandlers)||void 0===t?void 0:t.gotoPointsMall)||void 0===e?void 0:e.postMessage))return void window.open(l,"_blank","noopener=yes");let d=isAndroid?a:r;const k=encodeURIComponent(location.href);d=concat(d,{plt_back_uri:k});invokeNativePointMall(JSON.stringify({url:d}))}function toPlvWebviewBridge(i){return __awaiter(this,void 0,void 0,(function*(){const{getPlvWebviewSmallWindowSize:n,getPlvWebviewBridge:o}=i,t=(null==n?void 0:n())||{width:90,height:160},e=yield null==o?void 0:o();e&&e.sendData("clickProduct",{width:t.width,height:t.height,newPage:!0,link:i.link,data:i.data})}))}function toMultiPlatformLink(i){return __awaiter(this,void 0,void 0,(function*(){const{linkData:n,isWxMiniProgramEnv:o,toWxMiniProgram:t,openLink:e,getLinkParams:a,isMobile:r}=i,{wxMiniprogramLink:l,mobileLink:d,pcLink:k}=n,v=(null==r?void 0:r())||!1;let s=!1;try{s=(yield null==o?void 0:o())||!1}catch(i){s=!1}s&&l&&t?t(formatLink(l,a)):v&&d?e(formatLink(d,a),LinkJumpWay.NewWindow):!v&&k&&e(formatLink(k,a),LinkJumpWay.NewWindow)}))}!function(i){i.PopUp="POP_UP",i.NewWindow="NEW_WINDOW",i.CurrentWindow="CURRENT_WINDOW"}(LinkJumpWay||(LinkJumpWay={}));export function formatLink(i,n){let o={};if(n){const t=n(i);o=Object.assign({},o,t)}return concat(i,o)}export function navigateToLink(i){const{linkData:n,openLink:o,isPlvWebview:t,getPlvWebviewSmallWindowSize:e,getPlvWebviewBridge:a,isWxMiniProgramEnv:r,toWxMiniProgram:l,getLinkParams:d,isMobile:k}=i,{linkType:v}=n,s=(null==t?void 0:t())||!1;if(v!==LinkType.Native){if(s){let i="";const{link:o,mobileLink:t,mobileAppLink:r,wxMiniprogramOriginalId:l,wxMiniprogramLink:k}=n;switch(v){case LinkType.Normal:i=o;break;case LinkType.MultiPlatform:i=r||t}let s=null;return v!==LinkType.Normal&&(s={mobileLink:formatLink(t,d),wxMiniprogramOriginalId:l,wxMiniprogramLink:formatLink(k,d),mobileAppLink:formatLink(r,d)}),void toPlvWebviewBridge({link:formatLink(i,d),data:s,getPlvWebviewSmallWindowSize:e,getPlvWebviewBridge:a})}if(v!==LinkType.Normal)v===LinkType.MultiPlatform&&toMultiPlatformLink({linkData:n,getLinkParams:d,isWxMiniProgramEnv:r,toWxMiniProgram:l,openLink:o,isMobile:k});else{const{link:i,jumpWay:t}=n;o(formatLink(i,d),t)}}else toNativeLink({androidLink:formatLink(n.androidLink,d),iosLink:formatLink(n.iosLink,d),otherLink:formatLink(n.otherLink,d)})}
package/es/file.d.ts CHANGED
@@ -47,3 +47,40 @@ export interface FileSizeConversionResult {
47
47
  * @returns 转换结果。
48
48
  */
49
49
  export declare function convertFileSize(sizeInBytes: number, options: FileSizeUnit | FileSizeConversionOptions): FileSizeConversionResult;
50
+ /**
51
+ * 获取扩展名,支持传入文件名或 URL。
52
+ * @param url 文件名或 URL。
53
+ * @returns 扩展名。
54
+ */
55
+ export declare function getExtname(url: string): string;
56
+ /**
57
+ * 文件类型。
58
+ */
59
+ export declare enum FileType {
60
+ /**
61
+ * 演示稿。
62
+ */
63
+ PPT = "ppt",
64
+ /**
65
+ * PDF 文档。
66
+ */
67
+ PDF = "pdf",
68
+ /**
69
+ * Word 文档。
70
+ */
71
+ Word = "word",
72
+ /**
73
+ * 图片。
74
+ */
75
+ Image = "image",
76
+ /**
77
+ * 其他。
78
+ */
79
+ Others = "others"
80
+ }
81
+ /**
82
+ * 获取文件类型,支持传入文件名或 URL。
83
+ * @param url 文件名或 URL。
84
+ * @returns 文件类型。
85
+ */
86
+ export declare function getFileType(url: string): FileType;
package/es/file.js CHANGED
@@ -1 +1 @@
1
- export var FileSizeUnit;!function(i){i.B="B",i.KB="KB",i.MB="MB",i.GB="GB",i.TB="TB"}(FileSizeUnit||(FileSizeUnit={}));export function convertFileSize(i,e){const t="object"==typeof e?Object.assign({},e):{unit:e};if(!t.unit)throw new Error("Please specify file size unit");const n=[FileSizeUnit.B,FileSizeUnit.KB,FileSizeUnit.MB,FileSizeUnit.GB,FileSizeUnit.TB],l={value:i,unit:t.unit};return n.some(((e,o)=>{const B=i/Math.pow(1024,o);return!1===t.allowPureDecimal&&B<1||(l.value=B,l.unit=e,t.unit===e||o===n.length)})),l}
1
+ export var FileSizeUnit;!function(e){e.B="B",e.KB="KB",e.MB="MB",e.GB="GB",e.TB="TB"}(FileSizeUnit||(FileSizeUnit={}));export function convertFileSize(e,i){const t="object"==typeof i?Object.assign({},i):{unit:i};if(!t.unit)throw new Error("Please specify file size unit");const n=[FileSizeUnit.B,FileSizeUnit.KB,FileSizeUnit.MB,FileSizeUnit.GB,FileSizeUnit.TB],p={value:e,unit:t.unit};return n.some(((i,l)=>{const o=e/Math.pow(1024,l);return!1===t.allowPureDecimal&&o<1||(p.value=o,p.unit=i,t.unit===i||l===n.length)})),p}export function getExtname(e){const i=e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/^([a-z]+:)?\/\/[^/]+/i,"").split("/"),t=/\.([a-z0-9]+)$/.exec(i[i.length-1]);return t?t[1].toLowerCase():""}export var FileType;!function(e){e.PPT="ppt",e.PDF="pdf",e.Word="word",e.Image="image",e.Others="others"}(FileType||(FileType={}));export function getFileType(e){const i=getExtname(e);return-1!==["pdf"].indexOf(i)?FileType.PDF:-1!==["doc","docx"].indexOf(i)?FileType.Word:-1!==["ppt","pptx"].indexOf(i)?FileType.PPT:-1!==["jpg","jpeg","png","gif","bmp","webp","svg"].indexOf(i)?FileType.Image:FileType.Others}
package/es/image.d.ts CHANGED
@@ -68,3 +68,17 @@ export declare function cosCompress(url: string, options: IOSSCompressOptions |
68
68
  * @returns 处理后的 HTML 代码。
69
69
  */
70
70
  export declare function compressHTMLImgs(html: string, options: IOSSCompressOptions, compressor?: (url: string, options: IOSSCompressOptions | number) => string): string;
71
+ /**
72
+ * 预加载图片。
73
+ * @param url 图片 URL。
74
+ * @returns 图片尺寸和图片元素。
75
+ * @example
76
+ * ```javascript
77
+ * await preloadImg('https://example.com/image.jpg'); // { width, height, element }
78
+ * ```
79
+ */
80
+ export declare function preloadImg(url: string): Promise<{
81
+ width: number;
82
+ height: number;
83
+ element: HTMLImageElement;
84
+ }>;
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 A="image/webp";return 0===e.toDataURL(A).indexOf("data:"+A)}return!1}()||function(){let e=!1;const A=navigator.userAgent,t=/\bOS\s([\d_.]+)\slike\sMac\sOS\sX\b/.exec(A);if(t)e=parseInt(t[1])>=14;else{const t=/\bVersion\/([\d.]+)\sSafari\b/.exec(A);t&&(e=parseInt(t[1])>=16)}return e}()}catch(A){e=!1}return e}})();export const supportAVIF=(()=>{let e;return()=>(e||(e=new Promise((e=>{const A=new Image;A.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",A.onerror=()=>{e(!1)},A.onload=()=>{e(!0)},setTimeout((()=>{e(!1)}),1e3)}))),e)})();function getExtname(e){const A=(e||"").split("."),t=A[A.length-1];return t?t.toLowerCase():""}function handleCompressOptions(e){var A,t;const n="number"==typeof e?{width:e}:e;return n.allowJPG=null===(A=n.allowJPG)||void 0===A||A,n.allowWebP=null!==(t=n.allowWebP)&&void 0!==t?t:"auto",n}function createURLObject(e){let A;if("undefined"!=typeof document){const t=document.createElement("a");t.href=e,A=t}else if("function"==typeof URL)try{A=new URL(/^\/\//.test(e)?"https:"+e:e)}catch(e){}return A}function genOSSCompressParams(e,A){let t="";return null==A.width&&null==A.height||(t+="/resize",A.width&&(t+=",w_"+A.width),A.height&&(t+=",h_"+A.height),t+=",limit_1"),A.allowAVIF&&"gif"!==e?t+="/format,avif":!0===A.allowWebP||"auto"===A.allowWebP&&supportWebP()?t+="/format,webp/quality,Q_80":A.allowJPG&&"gif"!==e&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(e,A){let t=!1;return e=e.replace(/([?&]x-oss-process)(?:=([^&]*))?/,((e,n,r)=>(t=!0,/^image/.test(r)?n+"="+r.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+A:e))),t?e:e+(-1===e.indexOf("?")?"?":"&")+"x-oss-process=image"+A}export function ossCompress(e,A){const t=createURLObject(e);if(!t)return e;const n=getExtname(t.pathname.split("/").pop());if(!/\.videocc\.net$/i.test(t.hostname)||"svg"===n)return e;const r=genOSSCompressParams(n,handleCompressOptions(A));return r&&(t.search=setOSSCompressParams(t.search,r)),t.href}function genCOSCompressParams(e,A){let t="";return null!=A.width&&null!=A.height?t+=`/thumbnail/${A.width}x${A.height}`:null!=A.width?t+=`/thumbnail/${A.width}x`:null!=A.height&&(t+=`/thumbnail/x${A.height}`),A.allowAVIF&&"gif"!==e?t+="/format/avif":!0===A.allowWebP||"auto"===A.allowWebP&&supportWebP()?t+="/format/webp/quality/80":A.allowJPG&&"gif"!==e&&(t+="/format/jpg/quality/80"),t}function setCOSCompressParams(e,A){let t=!1;return e=e.replace(/([?&]imageMogr2)(\/[^&]*)/,((e,n,r)=>(t=!0,n+"="+r.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+A))),t?e:e+(-1===e.indexOf("?")?"?":"&")+"imageMogr2"+A}export function cosCompress(e,A){const t=createURLObject(e);if(!t)return e;const n=getExtname(t.pathname.split("/").pop());if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(t.hostname)||"svg"===n)return e;const r=genCOSCompressParams(n,handleCompressOptions(A));return r&&(t.search=setCOSCompressParams(t.search,r)),t.href}export function compressHTMLImgs(e,A,t=ossCompress){return e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((e,n,r,s,o)=>n+'"'+t(s,A)+'" data-src="'+s+'"'+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)})();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()}))}
package/es/json.d.ts CHANGED
@@ -15,8 +15,8 @@ export declare function cloneJSON(obj: unknown): any;
15
15
  /**
16
16
  * JSON 字符串解析失败时的回调。
17
17
  */
18
- export interface IErrorCallback {
19
- (e: Error): unknown;
18
+ export interface IErrorCallback<T> {
19
+ (e: Error): void | null | T;
20
20
  }
21
21
  /**
22
22
  * 尝试把指定字符串解析为 JSON 对象。
@@ -29,7 +29,7 @@ export interface IErrorCallback {
29
29
  * tryParseJSON('{"a": 1}'); // { a: 1 }
30
30
  * ```
31
31
  */
32
- export declare function tryParseJSON(str: string, onError?: IErrorCallback): unknown;
32
+ export declare function tryParseJSON<T>(str: string, onError?: IErrorCallback<T>): T | null;
33
33
  /**
34
34
  * 尝试把指定字符串解析为 JSON 对象。
35
35
  * @param str 指定字符串。
@@ -40,4 +40,4 @@ export declare function tryParseJSON(str: string, onError?: IErrorCallback): unk
40
40
  * tryParseJSON('12&&**', 1); // 1
41
41
  * ```
42
42
  */
43
- export declare function tryParseJSON(str: string, defaultValue?: unknown): unknown;
43
+ export declare function tryParseJSON<T>(str: string, defaultValue?: T): T | null;
package/es/json.js CHANGED
@@ -1 +1 @@
1
- export function cloneJSON(t){return null==t?t:JSON.parse(JSON.stringify(t))}export function tryParseJSON(t,r){let n;try{n=JSON.parse(t)}catch(t){n="function"==typeof r?r(t):r}return n}
1
+ export function cloneJSON(n){return null==n?n:JSON.parse(JSON.stringify(n))}export function tryParseJSON(n,l){var r;let t=null;try{t=JSON.parse(n)}catch(n){t="function"==typeof l?null!==(r=l(n))&&void 0!==r?r:null:null!=l?l:null}return t}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/utils",
3
- "version": "2.13.0",
3
+ "version": "2.14.0-beta.1",
4
4
  "description": "Utility functions of Polyv frontend development.",
5
5
  "keywords": [
6
6
  "polyv",
@@ -23,6 +23,8 @@
23
23
  "miniprogram": "./dist/es",
24
24
  "dependencies": {
25
25
  "@just4/querystring": "^2.0.0",
26
- "@just4/ua-info": "^3.2.0"
26
+ "@just4/ua-info": "^3.2.0",
27
+ "@types/md5": "^2.3.5",
28
+ "md5": "^2.3.0"
27
29
  }
28
30
  }