@polyv/utils 2.11.0-beta.2 → 2.11.0

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/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/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.2",
3
+ "version": "2.11.0",
4
4
  "description": "Utility functions of Polyv frontend development.",
5
5
  "keywords": [
6
6
  "polyv",