@polyv/utils 2.13.0-beta.5 → 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 +15 -0
- package/cjs/color.js +1 -0
- package/cjs/encrypt.d.ts +11 -0
- package/cjs/encrypt.js +1 -0
- package/cjs/external-link.d.ts +0 -9
- package/cjs/external-link.js +1 -1
- package/cjs/file.d.ts +37 -0
- package/cjs/file.js +1 -1
- package/cjs/image.d.ts +14 -0
- package/cjs/image.js +1 -1
- package/cjs/json.d.ts +4 -4
- package/cjs/json.js +1 -1
- package/es/color.d.ts +15 -0
- package/es/color.js +1 -0
- package/es/encrypt.d.ts +11 -0
- package/es/encrypt.js +1 -0
- package/es/external-link.d.ts +0 -9
- package/es/external-link.js +1 -1
- package/es/file.d.ts +37 -0
- package/es/file.js +1 -1
- package/es/image.d.ts +14 -0
- package/es/image.js +1 -1
- package/es/json.d.ts +4 -4
- package/es/json.js +1 -1
- package/package.json +4 -2
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;
|
package/cjs/encrypt.d.ts
ADDED
|
@@ -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()}
|
package/cjs/external-link.d.ts
CHANGED
|
@@ -160,15 +160,6 @@ export declare function formatLink(url: string, getLinkParams?: (url: string) =>
|
|
|
160
160
|
/**
|
|
161
161
|
* 根据链接类型和当前环境进行跳转处理
|
|
162
162
|
* @param options 跳转配置项
|
|
163
|
-
* @param options.linkData 链接数据,包含各种平台的链接和跳转方式
|
|
164
|
-
* @param options.getLinkParams 获取链接参数的函数
|
|
165
|
-
* @param options.openLink 通用链接打开处理器
|
|
166
|
-
* @param options.isPlvWebview 判断是否处于保利威 webview 中的函数
|
|
167
|
-
* @param options.isMobile 判断是否移动端的函数
|
|
168
|
-
* @param options.getPlvWebviewSmallWindowSize 获取保利威 webview 小窗尺寸的函数
|
|
169
|
-
* @param options.getPlvWebviewBridge 获取保利威 webview 桥接器的函数
|
|
170
|
-
* @param options.isWxMiniProgramEnv 判断是否处于微信小程序环境的函数
|
|
171
|
-
* @param options.toWxMiniProgram 跳转微信小程序的函数
|
|
172
163
|
*/
|
|
173
164
|
export declare function navigateToLink(options: NavigateToLinkOptions): void;
|
|
174
165
|
export {};
|
package/cjs/external-link.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __awaiter=this&&this.__awaiter||function(i,n,e,
|
|
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],
|
|
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
|
|
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):
|
|
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):
|
|
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?:
|
|
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}
|
package/es/encrypt.d.ts
ADDED
|
@@ -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()}
|
package/es/external-link.d.ts
CHANGED
|
@@ -160,15 +160,6 @@ export declare function formatLink(url: string, getLinkParams?: (url: string) =>
|
|
|
160
160
|
/**
|
|
161
161
|
* 根据链接类型和当前环境进行跳转处理
|
|
162
162
|
* @param options 跳转配置项
|
|
163
|
-
* @param options.linkData 链接数据,包含各种平台的链接和跳转方式
|
|
164
|
-
* @param options.getLinkParams 获取链接参数的函数
|
|
165
|
-
* @param options.openLink 通用链接打开处理器
|
|
166
|
-
* @param options.isPlvWebview 判断是否处于保利威 webview 中的函数
|
|
167
|
-
* @param options.isMobile 判断是否移动端的函数
|
|
168
|
-
* @param options.getPlvWebviewSmallWindowSize 获取保利威 webview 小窗尺寸的函数
|
|
169
|
-
* @param options.getPlvWebviewBridge 获取保利威 webview 桥接器的函数
|
|
170
|
-
* @param options.isWxMiniProgramEnv 判断是否处于微信小程序环境的函数
|
|
171
|
-
* @param options.toWxMiniProgram 跳转微信小程序的函数
|
|
172
163
|
*/
|
|
173
164
|
export declare function navigateToLink(options: NavigateToLinkOptions): void;
|
|
174
165
|
export {};
|
package/es/external-link.js
CHANGED
|
@@ -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{
|
|
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(
|
|
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
|
|
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):
|
|
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):
|
|
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?:
|
|
43
|
+
export declare function tryParseJSON<T>(str: string, defaultValue?: T): T | null;
|
package/es/json.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export function cloneJSON(
|
|
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.
|
|
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
|
}
|