@polyv/utils 2.11.0-beta.1 → 2.11.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/cjs/image.d.ts CHANGED
@@ -38,7 +38,7 @@ export interface IOSSCompressOptions {
38
38
  allowAVIF?: boolean;
39
39
  }
40
40
  /**
41
- * 如果指定图片 URL 的域名是 OSS 域名,且没有任何 OSS 处理参数,则根据压缩选项追加 OSS 图片压缩处理参数。
41
+ * 追加或替换 OSS 图片压缩参数。
42
42
  * @param url 指定图片 URL。
43
43
  * @param options 压缩选项或缩放的宽度。
44
44
  * @returns 处理后的图片 URL。
@@ -53,10 +53,18 @@ export interface IOSSCompressOptions {
53
53
  * ```
54
54
  */
55
55
  export declare function ossCompress(url: string, options: IOSSCompressOptions | number): string;
56
+ /**
57
+ * 追加或替换 COS 图片压缩参数。
58
+ * @param url 指定图片 URL。
59
+ * @param options 压缩选项或缩放的宽度。
60
+ * @returns 处理后的图片 URL。
61
+ */
62
+ export declare function cosCompress(url: string, options: IOSSCompressOptions | number): string;
56
63
  /**
57
64
  * 对指定 HTML 代码中 img 标签的图片地址做 OSS 压缩处理。
58
65
  * @param html 指定 HTML 代码。
59
66
  * @param options 压缩选项。
67
+ * @param compressor 压缩器。可以使用本模块下的 ossCompress 或 cosCompress,默认为 ossCompress。
60
68
  * @returns 处理后的 HTML 代码。
61
69
  */
62
- export declare function compressHTMLImgs(html: string, options: IOSSCompressOptions): string;
70
+ export declare function compressHTMLImgs(html: string, options: IOSSCompressOptions, compressor?: (url: string, options: IOSSCompressOptions | number) => string): string;
package/cjs/image.js CHANGED
@@ -1 +1 @@
1
- "use strict";function genOSSCompressParams(A,e){var t="";return null==e.width&&null==e.height||(t+="/resize",e.width&&(t+=",w_"+e.width),e.height&&(t+=",h_"+e.height),t+=",limit_1"),e.allowAVIF&&"gif"!==A?t+="/format,avif":!0===e.allowWebP||"auto"===e.allowWebP&&(0,exports.supportWebP)()?t+="/format,webp/quality,Q_80":e.allowJPG&&"gif"!==A&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(A,e){var t=!1;return A=A.replace(/([?&]x-oss-process)(?:=([^&]*))?/,(function(A,r,o){return t=!0,/^image/.test(o)?r+"="+o.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A})),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}function ossCompress(A,e){var t,r,o;if("undefined"!=typeof document){var s=document.createElement("a");s.href=A,o=s}else if("function"==typeof URL)try{o=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}if(!o)return A;if(!/\.videocc\.net$/i.test(o.hostname))return A;var a=(o.pathname.split("/").pop()||"").split("."),n=a[a.length-1].toLowerCase(),i="number"==typeof e?{width:e}:e;i.allowJPG=null===(t=i.allowJPG)||void 0===t||t,i.allowWebP=null!==(r=i.allowWebP)&&void 0!==r?r:"auto";var u=genOSSCompressParams(n,i);return u&&(o.search=setOSSCompressParams(o.search,u)),o.href}function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(A,t,r,o,s){return t+'"'+ossCompress(o,e)+'" data-src="'+o+'"'+s})):""}Object.defineProperty(exports,"__esModule",{value:!0}),exports.supportAVIF=exports.supportWebP=void 0,exports.ossCompress=ossCompress,exports.compressHTMLImgs=compressHTMLImgs,exports.supportWebP=function(){var A;return function(){return null==A&&(A=function(){var A=document.createElement("canvas");if(A.getContext&&A.getContext("2d")){var e="image/webp";return 0===A.toDataURL(e).indexOf("data:"+e)}return!1}()),A}}(),exports.supportAVIF=function(){var A;return function(){return A||(A=new Promise((function(A){var e=new Image;e.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",e.onerror=function(){A(!1)},e.onload=function(){A(!0)},setTimeout((function(){A(!1)}),1e3)}))),A}}();
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,o){return t=!0,/^image/.test(o)?r+"="+o.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;if(!/\.videocc\.net$/i.test(t.hostname))return e;var r=genOSSCompressParams(getExtname(t.pathname.split("/").pop()),handleCompressOptions(A));return r&&(t.search=setOSSCompressParams(t.search,r)),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,o){return t=!0,r+"="+o.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;if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(t.hostname))return e;var r=genCOSCompressParams(getExtname(t.pathname.split("/").pop()),handleCompressOptions(A));return r&&(t.search=setCOSCompressParams(t.search,r)),t.href}function compressHTMLImgs(e,A,t){return void 0===t&&(t=ossCompress),e?e.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,(function(e,r,o,a,n){return r+'"'+t(a,A)+'" data-src="'+a+'"'+n})):""}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}()}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}}();
package/cjs/string.d.ts CHANGED
@@ -98,3 +98,35 @@ export declare function uuidV4(): string;
98
98
  * 等于 0 时,表示两个版本号一致。
99
99
  */
100
100
  export declare function compareVersions(verA: string, verB: string): number;
101
+ /**
102
+ * 遮蔽字符的选项。
103
+ */
104
+ export interface IMaskCharsOptions {
105
+ /**
106
+ * 掩码字符,默认为 '*'。
107
+ */
108
+ maskChar?: string;
109
+ /**
110
+ * 遮蔽的开始位置(从 0 开始算),默认为 0。如果不指定,则 end 位置前的字符都被遮蔽。
111
+ */
112
+ start?: number;
113
+ /**
114
+ * 遮蔽的结束位置(从 0 开始算),默认为字符串的最后一位。如果不指定,则 start 位置后的字符都被遮蔽。
115
+ */
116
+ end?: number;
117
+ }
118
+ /**
119
+ * 以特定遮蔽字符遮蔽指定字符串的部分字符。
120
+ * @param str 指定字符串。
121
+ * @param options 遮蔽选项。
122
+ * @returns 遮蔽后的结果。
123
+ * @example
124
+ * ```javascript
125
+ * maskChars('1234567890', { start: 3, end: 6 }); // '123****890'
126
+ * maskChars('1234567890', { start: 3 }); // '123*******'
127
+ * maskChars('1234567890', { end: 6 }); // '*******890'
128
+ * maskChars('1234567890'); // '**********'
129
+ * maskChars('1234567890', { maskChar: '#' }); // '##########'
130
+ * ```
131
+ */
132
+ export declare function maskChars(str: string, options: IMaskCharsOptions): string;
package/cjs/string.js CHANGED
@@ -1 +1 @@
1
- "use strict";function strLen(e,n){var r,t;"number"==typeof n?n={enLen:1,nonEnLen:n}:((n=n||{}).enLen=null!==(r=n.enLen)&&void 0!==r?r:1,n.nonEnLen=null!==(t=n.nonEnLen)&&void 0!==t?t:2);for(var o=0,i=e.length-1;i>=0;i--)o+=e.charCodeAt(i)>255?n.nonEnLen||0:n.enLen||0;return o}function cutStr(e,n,r){(r=r||{}).enLen=Number(r.enLen)||1,r.nonEnLen=Number(r.nonEnLen)||2,r.ellipsis=r.ellipsis||"...";var t=strLen(e=String(e),r);if(t<=n)return e;n-=strLen(r.ellipsis,r);for(var o="",i=-1;n>0&&++i<t;)(n-=e.charCodeAt(i)>255?r.nonEnLen:r.enLen)>=0&&(o+=e.charAt(i));return o+=r.ellipsis}Object.defineProperty(exports,"__esModule",{value:!0}),exports.strLen=strLen,exports.cutStr=cutStr,exports.escapeHTML=escapeHTML,exports.unescapeHTML=unescapeHTML,exports.removeTags=removeTags,exports.nl2br=nl2br,exports.randomStr=randomStr,exports.uuidV4=uuidV4,exports.compareVersions=compareVersions;var htmlChars=['"',"'","&","<",">"],htmlEntities=["&quot;","&#39;","&amp;","&lt;","&gt;"],charsToEntities=Object.create(null),entitiesToChars=Object.create(null);htmlChars.forEach((function(e,n){charsToEntities[e]=htmlEntities[n],entitiesToChars[htmlEntities[n]]=e}));var reHTMLChars=new RegExp("["+htmlChars.join("")+"]","g"),reHTMLEntities=new RegExp("("+htmlEntities.join("|")+")","g");function escapeHTML(e){return null==e?e:String(e).replace(reHTMLChars,(function(e){return charsToEntities[e]}))}function unescapeHTML(e){return null==e?e:String(e).replace(reHTMLEntities,(function(e){return entitiesToChars[e]}))}function removeTags(e){return null==e?"":String(e).replace(/<.+?>/g,"")}function nl2br(e){return null==e?e:String(e).replace(/\r?\n/g,"<br />")}function randomStr(e,n){if(!(e|=0)||e<0)throw new Error('"length" must be a number greater than 0');var r="";do{r+=Math.random().toString(36).substr(2)}while(r.length<e);return r=r.substr(0,e),null!=n&&(r=n+r),r}function uuidV4(){var e=Date.now();return"undefined"!=typeof performance&&"function"==typeof performance.now&&(e+=performance.now()),"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(n){var r=(e+16*Math.random())%16|0;return e=Math.floor(e/16),("x"===n?r:3&r|8).toString(16)}))}function compareVersions(e,n){if(!e||!n)throw new Error("Please specify both verA and verB");for(var r=/(\.0+)+$/,t=String(e).replace(r,"").split("."),o=String(n).replace(r,"").split("."),i=Math.min(t.length,o.length),s=0;s<i;s++){var a=parseInt(t[s])-parseInt(o[s]);if(a)return a}return t.length-o.length}
1
+ "use strict";function strLen(n,e){var r,t;"number"==typeof e?e={enLen:1,nonEnLen:e}:((e=e||{}).enLen=null!==(r=e.enLen)&&void 0!==r?r:1,e.nonEnLen=null!==(t=e.nonEnLen)&&void 0!==t?t:2);for(var i=0,s=n.length-1;s>=0;s--)i+=n.charCodeAt(s)>255?e.nonEnLen||0:e.enLen||0;return i}function cutStr(n,e,r){(r=r||{}).enLen=Number(r.enLen)||1,r.nonEnLen=Number(r.nonEnLen)||2,r.ellipsis=r.ellipsis||"...";var t=strLen(n=String(n),r);if(t<=e)return n;e-=strLen(r.ellipsis,r);for(var i="",s=-1;e>0&&++s<t;)(e-=n.charCodeAt(s)>255?r.nonEnLen:r.enLen)>=0&&(i+=n.charAt(s));return i+=r.ellipsis}Object.defineProperty(exports,"__esModule",{value:!0}),exports.strLen=strLen,exports.cutStr=cutStr,exports.escapeHTML=escapeHTML,exports.unescapeHTML=unescapeHTML,exports.removeTags=removeTags,exports.nl2br=nl2br,exports.randomStr=randomStr,exports.uuidV4=uuidV4,exports.compareVersions=compareVersions,exports.maskChars=maskChars;var htmlChars=['"',"'","&","<",">"],htmlEntities=["&quot;","&#39;","&amp;","&lt;","&gt;"],charsToEntities=Object.create(null),entitiesToChars=Object.create(null);htmlChars.forEach((function(n,e){charsToEntities[n]=htmlEntities[e],entitiesToChars[htmlEntities[e]]=n}));var reHTMLChars=new RegExp("["+htmlChars.join("")+"]","g"),reHTMLEntities=new RegExp("("+htmlEntities.join("|")+")","g");function escapeHTML(n){return null==n?n:String(n).replace(reHTMLChars,(function(n){return charsToEntities[n]}))}function unescapeHTML(n){return null==n?n:String(n).replace(reHTMLEntities,(function(n){return entitiesToChars[n]}))}function removeTags(n){return null==n?"":String(n).replace(/<.+?>/g,"")}function nl2br(n){return null==n?n:String(n).replace(/\r?\n/g,"<br />")}function randomStr(n,e){if(!(n|=0)||n<0)throw new Error('"length" must be a number greater than 0');var r="";do{r+=Math.random().toString(36).substr(2)}while(r.length<n);return r=r.substr(0,n),null!=e&&(r=e+r),r}function uuidV4(){var n=Date.now();return"undefined"!=typeof performance&&"function"==typeof performance.now&&(n+=performance.now()),"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var r=(n+16*Math.random())%16|0;return n=Math.floor(n/16),("x"===e?r:3&r|8).toString(16)}))}function compareVersions(n,e){if(!n||!e)throw new Error("Please specify both verA and verB");for(var r=/(\.0+)+$/,t=String(n).replace(r,"").split("."),i=String(e).replace(r,"").split("."),s=Math.min(t.length,i.length),o=0;o<s;o++){var a=parseInt(t[o])-parseInt(i[o]);if(a)return a}return t.length-i.length}function maskChars(n,e){var r,t,i;e=e||{};var s=n.length,o=null!==(r=e.start)&&void 0!==r?r:0,a=null!==(t=e.end)&&void 0!==t?t:s-1,u=null!==(i=e.maskChar)&&void 0!==i?i:"*";if(o>=0&&a>=0&&o<=a&&a<s){var l=n.substring(0,o);return l+=new Array(a-o+2).join(u),a<s-1&&(l+=n.substring(a+1)),l}if(o>=0)return o>=s?n:n.substring(0,o)+new Array(s-o+1).join(u);if(a>=0){l=new Array(Math.min(a,s)+2).join(u);return a<s-1&&(l+=n.substring(a+1)),l}return n}
package/es/image.d.ts CHANGED
@@ -38,7 +38,7 @@ export interface IOSSCompressOptions {
38
38
  allowAVIF?: boolean;
39
39
  }
40
40
  /**
41
- * 如果指定图片 URL 的域名是 OSS 域名,且没有任何 OSS 处理参数,则根据压缩选项追加 OSS 图片压缩处理参数。
41
+ * 追加或替换 OSS 图片压缩参数。
42
42
  * @param url 指定图片 URL。
43
43
  * @param options 压缩选项或缩放的宽度。
44
44
  * @returns 处理后的图片 URL。
@@ -53,10 +53,18 @@ export interface IOSSCompressOptions {
53
53
  * ```
54
54
  */
55
55
  export declare function ossCompress(url: string, options: IOSSCompressOptions | number): string;
56
+ /**
57
+ * 追加或替换 COS 图片压缩参数。
58
+ * @param url 指定图片 URL。
59
+ * @param options 压缩选项或缩放的宽度。
60
+ * @returns 处理后的图片 URL。
61
+ */
62
+ export declare function cosCompress(url: string, options: IOSSCompressOptions | number): string;
56
63
  /**
57
64
  * 对指定 HTML 代码中 img 标签的图片地址做 OSS 压缩处理。
58
65
  * @param html 指定 HTML 代码。
59
66
  * @param options 压缩选项。
67
+ * @param compressor 压缩器。可以使用本模块下的 ossCompress 或 cosCompress,默认为 ossCompress。
60
68
  * @returns 处理后的 HTML 代码。
61
69
  */
62
- export declare function compressHTMLImgs(html: string, options: IOSSCompressOptions): string;
70
+ export declare function compressHTMLImgs(html: string, options: IOSSCompressOptions, compressor?: (url: string, options: IOSSCompressOptions | number) => string): string;
package/es/image.js CHANGED
@@ -1 +1 @@
1
- export const supportWebP=(()=>{let A;return()=>(null==A&&(A=function(){const A=document.createElement("canvas");if(A.getContext&&A.getContext("2d")){const e="image/webp";return 0===A.toDataURL(e).indexOf("data:"+e)}return!1}()),A)})();export const supportAVIF=(()=>{let A;return()=>(A||(A=new Promise((A=>{const e=new Image;e.src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=",e.onerror=()=>{A(!1)},e.onload=()=>{A(!0)},setTimeout((()=>{A(!1)}),1e3)}))),A)})();function genOSSCompressParams(A,e){let t="";return null==e.width&&null==e.height||(t+="/resize",e.width&&(t+=",w_"+e.width),e.height&&(t+=",h_"+e.height),t+=",limit_1"),e.allowAVIF&&"gif"!==A?t+="/format,avif":!0===e.allowWebP||"auto"===e.allowWebP&&supportWebP()?t+="/format,webp/quality,Q_80":e.allowJPG&&"gif"!==A&&(t+="/format,jpg/quality,Q_80"),t}function setOSSCompressParams(A,e){let t=!1;return A=A.replace(/([?&]x-oss-process)(?:=([^&]*))?/,((A,o,s)=>(t=!0,/^image/.test(s)?o+"="+s.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}export function ossCompress(A,e){var t,o;let s;if("undefined"!=typeof document){const e=document.createElement("a");e.href=A,s=e}else if("function"==typeof URL)try{s=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}if(!s)return A;if(!/\.videocc\.net$/i.test(s.hostname))return A;const a=(s.pathname.split("/").pop()||"").split("."),r=a[a.length-1].toLowerCase(),n="number"==typeof e?{width:e}:e;n.allowJPG=null===(t=n.allowJPG)||void 0===t||t,n.allowWebP=null!==(o=n.allowWebP)&&void 0!==o?o:"auto";const l=genOSSCompressParams(r,n);return l&&(s.search=setOSSCompressParams(s.search,l)),s.href}export function compressHTMLImgs(A,e){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((A,t,o,s,a)=>t+'"'+ossCompress(s,e)+'" data-src="'+s+'"'+a)):""}
1
+ export const supportWebP=(()=>{let A;return()=>{if(null==A)try{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}()}catch(e){A=!1}return 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 getExtname(A){const e=(A||"").split("."),t=e[e.length-1];return t?t.toLowerCase():""}function handleCompressOptions(A){var e,t;const r="number"==typeof A?{width:A}:A;return r.allowJPG=null===(e=r.allowJPG)||void 0===e||e,r.allowWebP=null!==(t=r.allowWebP)&&void 0!==t?t:"auto",r}function createURLObject(A){let e;if("undefined"!=typeof document){const t=document.createElement("a");t.href=A,e=t}else if("function"==typeof URL)try{e=new URL(/^\/\//.test(A)?"https:"+A:A)}catch(A){}return e}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,r,o)=>(t=!0,/^image/.test(o)?r+"="+o.replace(/\/(?:resize|format|quality)[^/]*/gi,"")+e:A))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"x-oss-process=image"+e}export function ossCompress(A,e){const t=createURLObject(A);if(!t)return A;if(!/\.videocc\.net$/i.test(t.hostname))return A;const r=genOSSCompressParams(getExtname(t.pathname.split("/").pop()),handleCompressOptions(e));return r&&(t.search=setOSSCompressParams(t.search,r)),t.href}function genCOSCompressParams(A,e){let t="";return null!=e.width&&null!=e.height?t+=`/thumbnail/${e.width}x${e.height}`:null!=e.width?t+=`/thumbnail/${e.width}x`:null!=e.height&&(t+=`/thumbnail/x${e.height}`),e.allowAVIF&&"gif"!==A?t+="/format/avif":!0===e.allowWebP||"auto"===e.allowWebP&&supportWebP()?t+="/format/webp/quality/80":e.allowJPG&&"gif"!==A&&(t+="/format/jpg/quality/80"),t}function setCOSCompressParams(A,e){let t=!1;return A=A.replace(/([?&]imageMogr2)(\/[^&]*)/,((A,r,o)=>(t=!0,r+"="+o.replace(/\/(?:thumbnail|format|quality)\/[^/]+/gi,"")+e))),t?A:A+(-1===A.indexOf("?")?"?":"&")+"imageMogr2"+e}export function cosCompress(A,e){const t=createURLObject(A);if(!t)return A;if(!/(?:\.videocc\.net|\.kingswayvideo\.com)$/i.test(t.hostname))return A;const r=genCOSCompressParams(getExtname(t.pathname.split("/").pop()),handleCompressOptions(e));return r&&(t.search=setCOSCompressParams(t.search,r)),t.href}export function compressHTMLImgs(A,e,t=ossCompress){return A?A.replace(/(<img.*?\ssrc=)(["']?)(.+?)\2(.*?>)/gi,((A,r,o,a,n)=>r+'"'+t(a,e)+'" data-src="'+a+'"'+n)):""}
package/es/string.d.ts CHANGED
@@ -98,3 +98,35 @@ export declare function uuidV4(): string;
98
98
  * 等于 0 时,表示两个版本号一致。
99
99
  */
100
100
  export declare function compareVersions(verA: string, verB: string): number;
101
+ /**
102
+ * 遮蔽字符的选项。
103
+ */
104
+ export interface IMaskCharsOptions {
105
+ /**
106
+ * 掩码字符,默认为 '*'。
107
+ */
108
+ maskChar?: string;
109
+ /**
110
+ * 遮蔽的开始位置(从 0 开始算),默认为 0。如果不指定,则 end 位置前的字符都被遮蔽。
111
+ */
112
+ start?: number;
113
+ /**
114
+ * 遮蔽的结束位置(从 0 开始算),默认为字符串的最后一位。如果不指定,则 start 位置后的字符都被遮蔽。
115
+ */
116
+ end?: number;
117
+ }
118
+ /**
119
+ * 以特定遮蔽字符遮蔽指定字符串的部分字符。
120
+ * @param str 指定字符串。
121
+ * @param options 遮蔽选项。
122
+ * @returns 遮蔽后的结果。
123
+ * @example
124
+ * ```javascript
125
+ * maskChars('1234567890', { start: 3, end: 6 }); // '123****890'
126
+ * maskChars('1234567890', { start: 3 }); // '123*******'
127
+ * maskChars('1234567890', { end: 6 }); // '*******890'
128
+ * maskChars('1234567890'); // '**********'
129
+ * maskChars('1234567890', { maskChar: '#' }); // '##########'
130
+ * ```
131
+ */
132
+ export declare function maskChars(str: string, options: IMaskCharsOptions): string;
package/es/string.js CHANGED
@@ -1 +1 @@
1
- export function strLen(n,e){var t,r;"number"==typeof e?e={enLen:1,nonEnLen:e}:((e=e||{}).enLen=null!==(t=e.enLen)&&void 0!==t?t:1,e.nonEnLen=null!==(r=e.nonEnLen)&&void 0!==r?r:2);let o=0;for(let t=n.length-1;t>=0;t--)o+=n.charCodeAt(t)>255?e.nonEnLen||0:e.enLen||0;return o}export function cutStr(n,e,t){(t=t||{}).enLen=Number(t.enLen)||1,t.nonEnLen=Number(t.nonEnLen)||2,t.ellipsis=t.ellipsis||"...";const r=strLen(n=String(n),t);if(r<=e)return n;e-=strLen(t.ellipsis,t);let o="",i=-1;for(;e>0&&++i<r;)(e-=n.charCodeAt(i)>255?t.nonEnLen:t.enLen)>=0&&(o+=n.charAt(i));return o+=t.ellipsis,o}const htmlChars=['"',"'","&","<",">"],htmlEntities=["&quot;","&#39;","&amp;","&lt;","&gt;"],charsToEntities=Object.create(null),entitiesToChars=Object.create(null);htmlChars.forEach(((n,e)=>{charsToEntities[n]=htmlEntities[e],entitiesToChars[htmlEntities[e]]=n}));const reHTMLChars=new RegExp("["+htmlChars.join("")+"]","g"),reHTMLEntities=new RegExp("("+htmlEntities.join("|")+")","g");export function escapeHTML(n){return null==n?n:String(n).replace(reHTMLChars,(n=>charsToEntities[n]))}export function unescapeHTML(n){return null==n?n:String(n).replace(reHTMLEntities,(n=>entitiesToChars[n]))}export function removeTags(n){return null==n?"":String(n).replace(/<.+?>/g,"")}export function nl2br(n){return null==n?n:String(n).replace(/\r?\n/g,"<br />")}export function randomStr(n,e){if(!(n|=0)||n<0)throw new Error('"length" must be a number greater than 0');let t="";do{t+=Math.random().toString(36).substr(2)}while(t.length<n);return t=t.substr(0,n),null!=e&&(t=e+t),t}export function uuidV4(){let n=Date.now();return"undefined"!=typeof performance&&"function"==typeof performance.now&&(n+=performance.now()),"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){const t=(n+16*Math.random())%16|0;return n=Math.floor(n/16),("x"===e?t:3&t|8).toString(16)}))}export function compareVersions(n,e){if(!n||!e)throw new Error("Please specify both verA and verB");const t=/(\.0+)+$/,r=String(n).replace(t,"").split("."),o=String(e).replace(t,"").split("."),i=Math.min(r.length,o.length);for(let n=0;n<i;n++){const e=parseInt(r[n])-parseInt(o[n]);if(e)return e}return r.length-o.length}
1
+ export function strLen(n,e){var t,r;"number"==typeof e?e={enLen:1,nonEnLen:e}:((e=e||{}).enLen=null!==(t=e.enLen)&&void 0!==t?t:1,e.nonEnLen=null!==(r=e.nonEnLen)&&void 0!==r?r:2);let o=0;for(let t=n.length-1;t>=0;t--)o+=n.charCodeAt(t)>255?e.nonEnLen||0:e.enLen||0;return o}export function cutStr(n,e,t){(t=t||{}).enLen=Number(t.enLen)||1,t.nonEnLen=Number(t.nonEnLen)||2,t.ellipsis=t.ellipsis||"...";const r=strLen(n=String(n),t);if(r<=e)return n;e-=strLen(t.ellipsis,t);let o="",i=-1;for(;e>0&&++i<r;)(e-=n.charCodeAt(i)>255?t.nonEnLen:t.enLen)>=0&&(o+=n.charAt(i));return o+=t.ellipsis,o}const htmlChars=['"',"'","&","<",">"],htmlEntities=["&quot;","&#39;","&amp;","&lt;","&gt;"],charsToEntities=Object.create(null),entitiesToChars=Object.create(null);htmlChars.forEach(((n,e)=>{charsToEntities[n]=htmlEntities[e],entitiesToChars[htmlEntities[e]]=n}));const reHTMLChars=new RegExp("["+htmlChars.join("")+"]","g"),reHTMLEntities=new RegExp("("+htmlEntities.join("|")+")","g");export function escapeHTML(n){return null==n?n:String(n).replace(reHTMLChars,(n=>charsToEntities[n]))}export function unescapeHTML(n){return null==n?n:String(n).replace(reHTMLEntities,(n=>entitiesToChars[n]))}export function removeTags(n){return null==n?"":String(n).replace(/<.+?>/g,"")}export function nl2br(n){return null==n?n:String(n).replace(/\r?\n/g,"<br />")}export function randomStr(n,e){if(!(n|=0)||n<0)throw new Error('"length" must be a number greater than 0');let t="";do{t+=Math.random().toString(36).substr(2)}while(t.length<n);return t=t.substr(0,n),null!=e&&(t=e+t),t}export function uuidV4(){let n=Date.now();return"undefined"!=typeof performance&&"function"==typeof performance.now&&(n+=performance.now()),"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){const t=(n+16*Math.random())%16|0;return n=Math.floor(n/16),("x"===e?t:3&t|8).toString(16)}))}export function compareVersions(n,e){if(!n||!e)throw new Error("Please specify both verA and verB");const t=/(\.0+)+$/,r=String(n).replace(t,"").split("."),o=String(e).replace(t,"").split("."),i=Math.min(r.length,o.length);for(let n=0;n<i;n++){const e=parseInt(r[n])-parseInt(o[n]);if(e)return e}return r.length-o.length}export function maskChars(n,e){var t,r,o;e=e||{};const i=n.length,l=null!==(t=e.start)&&void 0!==t?t:0,s=null!==(r=e.end)&&void 0!==r?r:i-1,a=null!==(o=e.maskChar)&&void 0!==o?o:"*";if(l>=0&&s>=0&&l<=s&&s<i){let e=n.substring(0,l);return e+=new Array(s-l+2).join(a),s<i-1&&(e+=n.substring(s+1)),e}if(l>=0)return l>=i?n:n.substring(0,l)+new Array(i-l+1).join(a);if(s>=0){let e=new Array(Math.min(s,i)+2).join(a);return s<i-1&&(e+=n.substring(s+1)),e}return n}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/utils",
3
- "version": "2.11.0-beta.1",
3
+ "version": "2.11.0-beta.3",
4
4
  "description": "Utility functions of Polyv frontend development.",
5
5
  "keywords": [
6
6
  "polyv",