@polyv/utils 2.10.0 → 2.11.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 +4 -3
- package/cjs/date.js +1 -1
- package/cjs/image.d.ts +10 -2
- package/cjs/image.js +1 -1
- package/es/date.d.ts +4 -3
- package/es/date.js +1 -1
- package/es/image.d.ts +10 -2
- package/es/image.js +1 -1
- package/package.json +1 -1
package/cjs/date.d.ts
CHANGED
|
@@ -18,11 +18,12 @@ export declare function formatDate(date: number | Date, formation: string): stri
|
|
|
18
18
|
*/
|
|
19
19
|
export interface FormatSecondsOptions {
|
|
20
20
|
/**
|
|
21
|
-
* 段数,2
|
|
21
|
+
* 段数,2、3 或 'auto'。
|
|
22
22
|
* 为 3 时,格式化样式为「时:分:秒」。
|
|
23
|
-
* 为 2
|
|
23
|
+
* 为 2 时,格式化样式为「分:秒」;
|
|
24
|
+
* 为 'auto' 时,不超过 1 小时按 2 段处理,否则按 3 段处理。
|
|
24
25
|
*/
|
|
25
|
-
segments?: number;
|
|
26
|
+
segments?: number | 'auto';
|
|
26
27
|
/**
|
|
27
28
|
* 每一段数字的最小位数,不足位数时补 0。
|
|
28
29
|
*/
|
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=
|
|
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;
|
package/cjs/image.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export interface IOSSCompressOptions {
|
|
|
38
38
|
allowAVIF?: boolean;
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
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
|
|
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/es/date.d.ts
CHANGED
|
@@ -18,11 +18,12 @@ export declare function formatDate(date: number | Date, formation: string): stri
|
|
|
18
18
|
*/
|
|
19
19
|
export interface FormatSecondsOptions {
|
|
20
20
|
/**
|
|
21
|
-
* 段数,2
|
|
21
|
+
* 段数,2、3 或 'auto'。
|
|
22
22
|
* 为 3 时,格式化样式为「时:分:秒」。
|
|
23
|
-
* 为 2
|
|
23
|
+
* 为 2 时,格式化样式为「分:秒」;
|
|
24
|
+
* 为 'auto' 时,不超过 1 小时按 2 段处理,否则按 3 段处理。
|
|
24
25
|
*/
|
|
25
|
-
segments?: number;
|
|
26
|
+
segments?: number | 'auto';
|
|
26
27
|
/**
|
|
27
28
|
* 每一段数字的最小位数,不足位数时补 0。
|
|
28
29
|
*/
|
package/es/date.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function ensureDate(
|
|
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 s=new Array(r+1).join("0"),o=[60,1];return 3===n&&o.unshift(3600),o.map((t=>{const n=Math.floor(e/t),o=n.toString().length;return e%=t,(s+n).slice(-Math.max(o,r))})).join(":")}
|
package/es/image.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export interface IOSSCompressOptions {
|
|
|
38
38
|
allowAVIF?: boolean;
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
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
|
|
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)):""}
|