@polyv/utils 2.18.0 → 2.19.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/date.d.ts +14 -0
- package/cjs/date.js +1 -1
- package/cjs/string.js +1 -1
- package/es/date.d.ts +14 -0
- package/es/date.js +1 -1
- package/es/string.js +1 -1
- package/package.json +1 -1
package/cjs/date.d.ts
CHANGED
|
@@ -43,3 +43,17 @@ export interface FormatSecondsOptions {
|
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
45
|
export declare function formatSeconds(secs: number, options?: FormatSecondsOptions): string;
|
|
46
|
+
/**
|
|
47
|
+
* 把“时:分:秒.毫秒”格式的字符串转换为总秒数。如存在毫秒部分,则四舍五入。
|
|
48
|
+
* @param time “时:分:秒.毫秒”格式的字符串。
|
|
49
|
+
* @returns 总秒数。
|
|
50
|
+
* @example
|
|
51
|
+
* ```javascript
|
|
52
|
+
* timeToSeconds('00:01'); // 1
|
|
53
|
+
* timeToSeconds('10:01'); // 601
|
|
54
|
+
* timeToSeconds('06:10:01'); // 22201
|
|
55
|
+
* timeToSeconds('06:10:01.123'); // 22201
|
|
56
|
+
* timeToSeconds('06:10:01.666'); // 22202
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function timeToSeconds(time: string): number;
|
package/cjs/date.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function ensureDate(e){return"object"!=typeof e&&(e=new Date(e)),e}function formatDate(e,t){var r={Y:(e=ensureDate(e)).getFullYear(),M:e.getMonth()+1,D:e.getDate(),h:e.getHours(),m:e.getMinutes(),s:e.getSeconds()};return t.replace(/([YMDhms])\1*/g,(function(e){var t=r[e[0]].toString(),n=e.length;return n>t.length&&(t=new Array(n-t.length+1).join("0")+t),t}))}function formatSeconds(e,t){if(void 0===t&&(t={segments:2,digits:2}),e=Number(e),isNaN(e)||e<0)throw new Error('"secs" must be a positive integer');var r=2;"auto"===t.segments&&e>=3600?r=3:"number"==typeof t.segments&&-1!==[2,3].indexOf(t.segments)&&(r=t.segments);var n=0|(t.digits||2);n=Math.max(1,n);var o=new Array(n+1).join("0"),s=[60,1];return 3===r&&s.unshift(3600),s.map((function(t){var r=Math.floor(e/t),s=r.toString().length;return e%=t,(o+r).slice(-Math.max(s,n))})).join(":")}Object.defineProperty(exports,"__esModule",{value:!0}),exports.formatDate=formatDate,exports.formatSeconds=formatSeconds;
|
|
1
|
+
"use strict";function ensureDate(e){return"object"!=typeof e&&(e=new Date(e)),e}function formatDate(e,t){var r={Y:(e=ensureDate(e)).getFullYear(),M:e.getMonth()+1,D:e.getDate(),h:e.getHours(),m:e.getMinutes(),s:e.getSeconds()};return t.replace(/([YMDhms])\1*/g,(function(e){var t=r[e[0]].toString(),n=e.length;return n>t.length&&(t=new Array(n-t.length+1).join("0")+t),t}))}function formatSeconds(e,t){if(void 0===t&&(t={segments:2,digits:2}),e=Number(e),isNaN(e)||e<0)throw new Error('"secs" must be a positive integer');var r=2;"auto"===t.segments&&e>=3600?r=3:"number"==typeof t.segments&&-1!==[2,3].indexOf(t.segments)&&(r=t.segments);var n=0|(t.digits||2);n=Math.max(1,n);var o=new Array(n+1).join("0"),s=[60,1];return 3===r&&s.unshift(3600),s.map((function(t){var r=Math.floor(e/t),s=r.toString().length;return e%=t,(o+r).slice(-Math.max(s,n))})).join(":")}function timeToSeconds(e){var t=/^(?:(\d+):)*?(?:(\d+):)?(\d+)(?:\.(\d+))?$/.exec(e);if(!t)return NaN;var r=[3600,60,1,.001],n=0;return[t[1],t[2],t[3],t[4]].some((function(e,t){if(null!=e){var o=Number(e);if(isNaN(o))return n=NaN,!0;n+=Math.round(o*r[t])}return!1})),n}Object.defineProperty(exports,"__esModule",{value:!0}),exports.formatDate=formatDate,exports.formatSeconds=formatSeconds,exports.timeToSeconds=timeToSeconds;
|
package/cjs/string.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
|
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){var t;(r=r||{}).enLen=Number(r.enLen)||1,r.nonEnLen=Number(r.nonEnLen)||2,r.ellipsis=null!==(t=r.ellipsis)&&void 0!==t?t:"...";var i=strLen(n=String(n),r);if(i<=e)return n;e-=strLen(r.ellipsis,r);for(var s="",o=-1;e>0&&++o<i;)(e-=n.charCodeAt(o)>255?r.nonEnLen:r.enLen)>=0&&(s+=n.charAt(o));return s+=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=[""","'","&","<",">"," "],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/date.d.ts
CHANGED
|
@@ -43,3 +43,17 @@ export interface FormatSecondsOptions {
|
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
45
|
export declare function formatSeconds(secs: number, options?: FormatSecondsOptions): string;
|
|
46
|
+
/**
|
|
47
|
+
* 把“时:分:秒.毫秒”格式的字符串转换为总秒数。如存在毫秒部分,则四舍五入。
|
|
48
|
+
* @param time “时:分:秒.毫秒”格式的字符串。
|
|
49
|
+
* @returns 总秒数。
|
|
50
|
+
* @example
|
|
51
|
+
* ```javascript
|
|
52
|
+
* timeToSeconds('00:01'); // 1
|
|
53
|
+
* timeToSeconds('10:01'); // 601
|
|
54
|
+
* timeToSeconds('06:10:01'); // 22201
|
|
55
|
+
* timeToSeconds('06:10:01.123'); // 22201
|
|
56
|
+
* timeToSeconds('06:10:01.666'); // 22202
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function timeToSeconds(time: string): number;
|
package/es/date.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function ensureDate(e){return"object"!=typeof e&&(e=new Date(e)),e}export function formatDate(e,t){const n={Y:(e=ensureDate(e)).getFullYear(),M:e.getMonth()+1,D:e.getDate(),h:e.getHours(),m:e.getMinutes(),s:e.getSeconds()};return t.replace(/([YMDhms])\1*/g,(function(e){let t=n[e[0]].toString();const r=e.length;return r>t.length&&(t=new Array(r-t.length+1).join("0")+t),t}))}export function formatSeconds(e,t={segments:2,digits:2}){if(e=Number(e),isNaN(e)||e<0)throw new Error('"secs" must be a positive integer');let n=2;"auto"===t.segments&&e>=3600?n=3:"number"==typeof t.segments&&-1!==[2,3].indexOf(t.segments)&&(n=t.segments);let r=0|(t.digits||2);r=Math.max(1,r);const
|
|
1
|
+
function ensureDate(e){return"object"!=typeof e&&(e=new Date(e)),e}export function formatDate(e,t){const n={Y:(e=ensureDate(e)).getFullYear(),M:e.getMonth()+1,D:e.getDate(),h:e.getHours(),m:e.getMinutes(),s:e.getSeconds()};return t.replace(/([YMDhms])\1*/g,(function(e){let t=n[e[0]].toString();const r=e.length;return r>t.length&&(t=new Array(r-t.length+1).join("0")+t),t}))}export function formatSeconds(e,t={segments:2,digits:2}){if(e=Number(e),isNaN(e)||e<0)throw new Error('"secs" must be a positive integer');let n=2;"auto"===t.segments&&e>=3600?n=3:"number"==typeof t.segments&&-1!==[2,3].indexOf(t.segments)&&(n=t.segments);let r=0|(t.digits||2);r=Math.max(1,r);const o=new Array(r+1).join("0"),s=[60,1];return 3===n&&s.unshift(3600),s.map((t=>{const n=Math.floor(e/t),s=n.toString().length;return e%=t,(o+n).slice(-Math.max(s,r))})).join(":")}export function timeToSeconds(e){const t=/^(?:(\d+):)*?(?:(\d+):)?(\d+)(?:\.(\d+))?$/.exec(e);if(!t)return NaN;const n=[3600,60,1,.001];let r=0;return[t[1],t[2],t[3],t[4]].some(((e,t)=>{if(null!=e){const o=Number(e);if(isNaN(o))return r=NaN,!0;r+=Math.round(o*n[t])}return!1})),r}
|
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
|
|
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){var r;(t=t||{}).enLen=Number(t.enLen)||1,t.nonEnLen=Number(t.nonEnLen)||2,t.ellipsis=null!==(r=t.ellipsis)&&void 0!==r?r:"...";const o=strLen(n=String(n),t);if(o<=e)return n;e-=strLen(t.ellipsis,t);let i="",l=-1;for(;e>0&&++l<o;)(e-=n.charCodeAt(l)>255?t.nonEnLen:t.enLen)>=0&&(i+=n.charAt(l));return i+=t.ellipsis,i}const htmlChars=['"',"'","&","<",">"," "],htmlEntities=[""","'","&","<",">"," "],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}
|