@polyv/utils 2.0.0-beta.5 → 2.2.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/dist/cjs/event.d.ts +113 -0
- package/dist/cjs/event.js +1 -0
- package/dist/cjs/image.d.ts +49 -0
- package/dist/cjs/image.js +1 -0
- package/dist/cjs/net.d.ts +12 -1
- package/dist/cjs/net.js +1 -1
- package/dist/es/event.d.ts +113 -0
- package/dist/es/event.js +1 -0
- package/dist/es/image.d.ts +49 -0
- package/dist/es/image.js +1 -0
- package/dist/es/net.d.ts +12 -1
- package/dist/es/net.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本模块提供事件触发器类。
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```js
|
|
7
|
+
* import { EventEmitter } from '@polyv/utils/es/event';
|
|
8
|
+
*
|
|
9
|
+
* const emitter = new EventEmitter();
|
|
10
|
+
* const context = { name: 'my name' };
|
|
11
|
+
*
|
|
12
|
+
* function eventHandler(e) {
|
|
13
|
+
* console.log(e);
|
|
14
|
+
* console.log(this === context); // true
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* // listen event
|
|
18
|
+
* emitter.on('foo', eventHandler, context);
|
|
19
|
+
*
|
|
20
|
+
* // listen once event
|
|
21
|
+
* emitter.once('foo', eventHandler, context);
|
|
22
|
+
*
|
|
23
|
+
* // fire event
|
|
24
|
+
* emitter.emit('foo', { a: 1 });
|
|
25
|
+
*
|
|
26
|
+
* // unlisten event
|
|
27
|
+
* emitter.off('foo', eventHandler);
|
|
28
|
+
*
|
|
29
|
+
* // ---------- use typescript define event parameter types ---------- //
|
|
30
|
+
* type EventParams = {
|
|
31
|
+
* foo: string;
|
|
32
|
+
* boo: number;
|
|
33
|
+
* };
|
|
34
|
+
*
|
|
35
|
+
* const emitter = new EventEmitter<EventParams>();
|
|
36
|
+
*
|
|
37
|
+
* emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'
|
|
38
|
+
*
|
|
39
|
+
* emitter.emit('foo', 18); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
|
|
40
|
+
*
|
|
41
|
+
* // ---------- use typescript define event enum ---------- //
|
|
42
|
+
* enum TestEvent {
|
|
43
|
+
* Foo = 'foo',
|
|
44
|
+
* Bar = 'bar',
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* type TestEventParams = {
|
|
48
|
+
* [TestEvent.Foo]: string;
|
|
49
|
+
* [TestEvent.Bar]: number;
|
|
50
|
+
* };
|
|
51
|
+
*
|
|
52
|
+
* const emitter = new EventEmitter<TestEventParams, TestEvent>();
|
|
53
|
+
*
|
|
54
|
+
* emitter.emit(TestEvent.Foo, 'abc'); // ok
|
|
55
|
+
*
|
|
56
|
+
* emitter.emit('foo', 'abc'); // Error: Argument of type "'foo'" is not assignable to parameter of type 'TestEvent'. (2345)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
/**
|
|
60
|
+
* 事件类型
|
|
61
|
+
*/
|
|
62
|
+
export declare type EventType = string | symbol;
|
|
63
|
+
/**
|
|
64
|
+
* 事件参数关系类型
|
|
65
|
+
*/
|
|
66
|
+
export declare type EventRelationsType = Record<EventType, unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* 事件回调函数类型
|
|
69
|
+
*/
|
|
70
|
+
export declare type EventHandler<Relations extends EventRelationsType, E extends EventType> = (params: Relations[E]) => unknown;
|
|
71
|
+
export declare class EventEmitter<Relations extends EventRelationsType = EventRelationsType, Events extends EventType = string> {
|
|
72
|
+
/**
|
|
73
|
+
* 事件回调存储器
|
|
74
|
+
* @ignore
|
|
75
|
+
*/
|
|
76
|
+
private __eventStore;
|
|
77
|
+
/**
|
|
78
|
+
* 添加监听事件
|
|
79
|
+
* @ignore
|
|
80
|
+
*/
|
|
81
|
+
private __addOnEvent;
|
|
82
|
+
/**
|
|
83
|
+
* 监听事件
|
|
84
|
+
* @param event 事件名
|
|
85
|
+
* @param handler 回调函数
|
|
86
|
+
* @param context this 上下文
|
|
87
|
+
*/
|
|
88
|
+
on<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
|
|
89
|
+
/**
|
|
90
|
+
* 监听事件(一次性)
|
|
91
|
+
* @param event 事件名
|
|
92
|
+
* @param handler 回调函数
|
|
93
|
+
* @param context this 上下文
|
|
94
|
+
*/
|
|
95
|
+
once<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
|
|
96
|
+
/**
|
|
97
|
+
* 移除事件监听
|
|
98
|
+
* @param event 事件名
|
|
99
|
+
* @param handler 回调函数
|
|
100
|
+
*/
|
|
101
|
+
off<E extends Events>(event: E, handler: unknown): void;
|
|
102
|
+
/**
|
|
103
|
+
* 触发事件
|
|
104
|
+
* @param event 事件名
|
|
105
|
+
* @param params 回调参数
|
|
106
|
+
*/
|
|
107
|
+
emit<E extends Events>(event: E, params: Relations[E]): void;
|
|
108
|
+
emit<E extends Events>(event: undefined extends Relations[E] ? E : never): void;
|
|
109
|
+
/**
|
|
110
|
+
* 销毁实例
|
|
111
|
+
*/
|
|
112
|
+
destroy(): void;
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.EventEmitter=void 0;var EventEmitter=function(){function t(){this.__eventStore={}}return t.prototype.__addOnEvent=function(t,e,n,o){if(e&&n){var r=this.__eventStore[e];r||(r=[]),r.push({type:t,handler:n,callbackHandler:n.bind(o)}),this.__eventStore[e]=r}},t.prototype.on=function(t,e,n){void 0===n&&(n=this),this.__addOnEvent("normal",t,e,n)},t.prototype.once=function(t,e,n){void 0===n&&(n=this),this.__addOnEvent("once",t,e,n)},t.prototype.off=function(t,e){var n=this.__eventStore[t];n&&(n=n.filter((function(t){return t.handler!==e})),this.__eventStore[t]=n)},t.prototype.emit=function(t,e){var n=this,o=this.__eventStore[t];o&&o.forEach((function(o){var r=o.type,i=o.handler,v=o.callbackHandler;"function"==typeof v&&v(e),"once"===r&&n.off(t,i)}))},t.prototype.destroy=function(){this.__eventStore={}},t}();exports.EventEmitter=EventEmitter;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本模块提供图片操作相关方法。
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 检查当前浏览器是否支持 WebP 格式。
|
|
7
|
+
* @returns 当前浏览器是否支持 WebP 格式。
|
|
8
|
+
*/
|
|
9
|
+
export declare function supportWebP(): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* 检查当前浏览器是否支持 AVIF 格式(注意,本函数是异步函数)。
|
|
12
|
+
* @returns 当前浏览器是否支持 AVIF 格式。
|
|
13
|
+
*/
|
|
14
|
+
export declare function supportAVIF(): Promise<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* 压缩选项。
|
|
17
|
+
*/
|
|
18
|
+
export interface IOSSCompressOptions {
|
|
19
|
+
/**
|
|
20
|
+
* 压缩后的图片宽度。
|
|
21
|
+
*/
|
|
22
|
+
width?: number;
|
|
23
|
+
/**
|
|
24
|
+
* 压缩后的图片高度。
|
|
25
|
+
*/
|
|
26
|
+
height?: number;
|
|
27
|
+
/**
|
|
28
|
+
* 是否允许转换为 WebP。
|
|
29
|
+
*/
|
|
30
|
+
allowWebP?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* 是否允许转换为 AVIF。
|
|
33
|
+
*/
|
|
34
|
+
allowAVIF?: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 如果指定图片 URL 的域名是 OSS 域名,则追加 OSS 图片压缩参数。
|
|
38
|
+
* @param url 指定图片 URL。
|
|
39
|
+
* @param options 压缩选项。
|
|
40
|
+
* @returns 处理后的图片 URL。
|
|
41
|
+
* @example
|
|
42
|
+
* ```javascript
|
|
43
|
+
* ossCompress(url, {
|
|
44
|
+
* width: 300,
|
|
45
|
+
* allowWebp: true
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function ossCompress(url: string, options: IOSSCompressOptions): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function supportWebP(){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}function supportAVIF(){return 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)}))}function ossCompress(A,e){var t=document.createElement("a");t.href=A;var o=t.search;if("liveimages.videocc.net"!==t.hostname.toLowerCase()||/(?:\?|&)x-oss-process(?:=|&|$)/.test(o))return A;var s="";return null==e.width&&null==e.height||(s+="/resize,mfit",e.width&&(s+=",w_"+e.width),e.height&&(s+=",h_"+e.height),s+=",limit_1"),e.allowAVIF?s+="/format,avif":e.allowWebP&&(s+="/format,webp"),s&&(s="x-oss-process=image"+s,A+=(-1===A.indexOf("?")?"?":"&")+s),A}Object.defineProperty(exports,"__esModule",{value:!0}),exports.ossCompress=exports.supportAVIF=exports.supportWebP=void 0,exports.supportWebP=supportWebP,exports.supportAVIF=supportAVIF,exports.ossCompress=ossCompress;
|
package/dist/cjs/net.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare function startsWithProtocol(str: string, protocols?: string[]): b
|
|
|
21
21
|
/**
|
|
22
22
|
* 替换目标字符串中的 URL 协议。如果字符串中不包含协议,则加上协议。
|
|
23
23
|
* @param url 目标字符串。
|
|
24
|
-
* @param protocol
|
|
24
|
+
* @param protocol 协议(不含冒号和斜杠)。
|
|
25
25
|
* @return 替换结果。
|
|
26
26
|
* @example
|
|
27
27
|
* ```javascript
|
|
@@ -30,3 +30,14 @@ export declare function startsWithProtocol(str: string, protocols?: string[]): b
|
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
32
|
export declare function changeProtocol(url: string, protocol: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* 标准化(如果当前页面协议为 http,则为 http,否则为 https)目标字符串中的 URL 协议。
|
|
35
|
+
* @param url 目标字符串。
|
|
36
|
+
* @returns 标准化结果。
|
|
37
|
+
* ```javascript
|
|
38
|
+
* // 当前页面协议为 http 时,结果是 'http://abc.com'
|
|
39
|
+
* // 否则结果是 'https://abc.com'
|
|
40
|
+
* normalizeProtocol('http://abc.com');
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function normalizeProtocol(url: string): string;
|
package/dist/cjs/net.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.changeProtocol=exports.startsWithProtocol=void 0;var reProtocol=/^(?:([a-z]+):)?\/{2,3}/i;function startsWithProtocol(o,t){var r=reProtocol.test(o);if(r&&t){for(var e=(RegExp.$1||"").toLowerCase(),c=t.length-1;c>=0;c--)if(e===t[c].toLowerCase())return!0;return!1}return r}function changeProtocol(o,t){return reProtocol.test(t)||(t+="://"),startsWithProtocol(o)?o.replace(reProtocol,t):t+o}exports.startsWithProtocol=startsWithProtocol,exports.changeProtocol=changeProtocol;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.normalizeProtocol=exports.changeProtocol=exports.startsWithProtocol=void 0;var reProtocol=/^(?:([a-z]+):)?\/{2,3}/i;function startsWithProtocol(o,t){var r=reProtocol.test(o);if(r&&t){for(var e=(RegExp.$1||"").toLowerCase(),c=t.length-1;c>=0;c--)if(e===t[c].toLowerCase())return!0;return!1}return r}function changeProtocol(o,t){return reProtocol.test(t)||(t+="://"),startsWithProtocol(o)?o.replace(reProtocol,t):t+o}function normalizeProtocol(o){return changeProtocol(o,"http:"===window.location.protocol?"http":"https")}exports.startsWithProtocol=startsWithProtocol,exports.changeProtocol=changeProtocol,exports.normalizeProtocol=normalizeProtocol;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本模块提供事件触发器类。
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```js
|
|
7
|
+
* import { EventEmitter } from '@polyv/utils/es/event';
|
|
8
|
+
*
|
|
9
|
+
* const emitter = new EventEmitter();
|
|
10
|
+
* const context = { name: 'my name' };
|
|
11
|
+
*
|
|
12
|
+
* function eventHandler(e) {
|
|
13
|
+
* console.log(e);
|
|
14
|
+
* console.log(this === context); // true
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* // listen event
|
|
18
|
+
* emitter.on('foo', eventHandler, context);
|
|
19
|
+
*
|
|
20
|
+
* // listen once event
|
|
21
|
+
* emitter.once('foo', eventHandler, context);
|
|
22
|
+
*
|
|
23
|
+
* // fire event
|
|
24
|
+
* emitter.emit('foo', { a: 1 });
|
|
25
|
+
*
|
|
26
|
+
* // unlisten event
|
|
27
|
+
* emitter.off('foo', eventHandler);
|
|
28
|
+
*
|
|
29
|
+
* // ---------- use typescript define event parameter types ---------- //
|
|
30
|
+
* type EventParams = {
|
|
31
|
+
* foo: string;
|
|
32
|
+
* boo: number;
|
|
33
|
+
* };
|
|
34
|
+
*
|
|
35
|
+
* const emitter = new EventEmitter<EventParams>();
|
|
36
|
+
*
|
|
37
|
+
* emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'
|
|
38
|
+
*
|
|
39
|
+
* emitter.emit('foo', 18); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
|
|
40
|
+
*
|
|
41
|
+
* // ---------- use typescript define event enum ---------- //
|
|
42
|
+
* enum TestEvent {
|
|
43
|
+
* Foo = 'foo',
|
|
44
|
+
* Bar = 'bar',
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* type TestEventParams = {
|
|
48
|
+
* [TestEvent.Foo]: string;
|
|
49
|
+
* [TestEvent.Bar]: number;
|
|
50
|
+
* };
|
|
51
|
+
*
|
|
52
|
+
* const emitter = new EventEmitter<TestEventParams, TestEvent>();
|
|
53
|
+
*
|
|
54
|
+
* emitter.emit(TestEvent.Foo, 'abc'); // ok
|
|
55
|
+
*
|
|
56
|
+
* emitter.emit('foo', 'abc'); // Error: Argument of type "'foo'" is not assignable to parameter of type 'TestEvent'. (2345)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
/**
|
|
60
|
+
* 事件类型
|
|
61
|
+
*/
|
|
62
|
+
export declare type EventType = string | symbol;
|
|
63
|
+
/**
|
|
64
|
+
* 事件参数关系类型
|
|
65
|
+
*/
|
|
66
|
+
export declare type EventRelationsType = Record<EventType, unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* 事件回调函数类型
|
|
69
|
+
*/
|
|
70
|
+
export declare type EventHandler<Relations extends EventRelationsType, E extends EventType> = (params: Relations[E]) => unknown;
|
|
71
|
+
export declare class EventEmitter<Relations extends EventRelationsType = EventRelationsType, Events extends EventType = string> {
|
|
72
|
+
/**
|
|
73
|
+
* 事件回调存储器
|
|
74
|
+
* @ignore
|
|
75
|
+
*/
|
|
76
|
+
private __eventStore;
|
|
77
|
+
/**
|
|
78
|
+
* 添加监听事件
|
|
79
|
+
* @ignore
|
|
80
|
+
*/
|
|
81
|
+
private __addOnEvent;
|
|
82
|
+
/**
|
|
83
|
+
* 监听事件
|
|
84
|
+
* @param event 事件名
|
|
85
|
+
* @param handler 回调函数
|
|
86
|
+
* @param context this 上下文
|
|
87
|
+
*/
|
|
88
|
+
on<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
|
|
89
|
+
/**
|
|
90
|
+
* 监听事件(一次性)
|
|
91
|
+
* @param event 事件名
|
|
92
|
+
* @param handler 回调函数
|
|
93
|
+
* @param context this 上下文
|
|
94
|
+
*/
|
|
95
|
+
once<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
|
|
96
|
+
/**
|
|
97
|
+
* 移除事件监听
|
|
98
|
+
* @param event 事件名
|
|
99
|
+
* @param handler 回调函数
|
|
100
|
+
*/
|
|
101
|
+
off<E extends Events>(event: E, handler: unknown): void;
|
|
102
|
+
/**
|
|
103
|
+
* 触发事件
|
|
104
|
+
* @param event 事件名
|
|
105
|
+
* @param params 回调参数
|
|
106
|
+
*/
|
|
107
|
+
emit<E extends Events>(event: E, params: Relations[E]): void;
|
|
108
|
+
emit<E extends Events>(event: undefined extends Relations[E] ? E : never): void;
|
|
109
|
+
/**
|
|
110
|
+
* 销毁实例
|
|
111
|
+
*/
|
|
112
|
+
destroy(): void;
|
|
113
|
+
}
|
package/dist/es/event.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class EventEmitter{constructor(){this.__eventStore={}}__addOnEvent(t,e,n,o){if(!e||!n)return;let r=this.__eventStore[e];r||(r=[]),r.push({type:t,handler:n,callbackHandler:n.bind(o)}),this.__eventStore[e]=r}on(t,e,n=this){this.__addOnEvent("normal",t,e,n)}once(t,e,n=this){this.__addOnEvent("once",t,e,n)}off(t,e){let n=this.__eventStore[t];n&&(n=n.filter((t=>t.handler!==e)),this.__eventStore[t]=n)}emit(t,e){const n=this.__eventStore[t];n&&n.forEach((n=>{const{type:o,handler:r,callbackHandler:_}=n;"function"==typeof _&&_(e),"once"===o&&this.off(t,r)}))}destroy(){this.__eventStore={}}}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本模块提供图片操作相关方法。
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 检查当前浏览器是否支持 WebP 格式。
|
|
7
|
+
* @returns 当前浏览器是否支持 WebP 格式。
|
|
8
|
+
*/
|
|
9
|
+
export declare function supportWebP(): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* 检查当前浏览器是否支持 AVIF 格式(注意,本函数是异步函数)。
|
|
12
|
+
* @returns 当前浏览器是否支持 AVIF 格式。
|
|
13
|
+
*/
|
|
14
|
+
export declare function supportAVIF(): Promise<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* 压缩选项。
|
|
17
|
+
*/
|
|
18
|
+
export interface IOSSCompressOptions {
|
|
19
|
+
/**
|
|
20
|
+
* 压缩后的图片宽度。
|
|
21
|
+
*/
|
|
22
|
+
width?: number;
|
|
23
|
+
/**
|
|
24
|
+
* 压缩后的图片高度。
|
|
25
|
+
*/
|
|
26
|
+
height?: number;
|
|
27
|
+
/**
|
|
28
|
+
* 是否允许转换为 WebP。
|
|
29
|
+
*/
|
|
30
|
+
allowWebP?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* 是否允许转换为 AVIF。
|
|
33
|
+
*/
|
|
34
|
+
allowAVIF?: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 如果指定图片 URL 的域名是 OSS 域名,则追加 OSS 图片压缩参数。
|
|
38
|
+
* @param url 指定图片 URL。
|
|
39
|
+
* @param options 压缩选项。
|
|
40
|
+
* @returns 处理后的图片 URL。
|
|
41
|
+
* @example
|
|
42
|
+
* ```javascript
|
|
43
|
+
* ossCompress(url, {
|
|
44
|
+
* width: 300,
|
|
45
|
+
* allowWebp: true
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function ossCompress(url: string, options: IOSSCompressOptions): string;
|
package/dist/es/image.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function supportWebP(){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}export function supportAVIF(){return 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)}))}export function ossCompress(A,e){const t=document.createElement("a");t.href=A;const o=t.search;if("liveimages.videocc.net"!==t.hostname.toLowerCase()||/(?:\?|&)x-oss-process(?:=|&|$)/.test(o))return A;let a="";return null==e.width&&null==e.height||(a+="/resize,mfit",e.width&&(a+=",w_"+e.width),e.height&&(a+=",h_"+e.height),a+=",limit_1"),e.allowAVIF?a+="/format,avif":e.allowWebP&&(a+="/format,webp"),a&&(a="x-oss-process=image"+a,A+=(-1===A.indexOf("?")?"?":"&")+a),A}
|
package/dist/es/net.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare function startsWithProtocol(str: string, protocols?: string[]): b
|
|
|
21
21
|
/**
|
|
22
22
|
* 替换目标字符串中的 URL 协议。如果字符串中不包含协议,则加上协议。
|
|
23
23
|
* @param url 目标字符串。
|
|
24
|
-
* @param protocol
|
|
24
|
+
* @param protocol 协议(不含冒号和斜杠)。
|
|
25
25
|
* @return 替换结果。
|
|
26
26
|
* @example
|
|
27
27
|
* ```javascript
|
|
@@ -30,3 +30,14 @@ export declare function startsWithProtocol(str: string, protocols?: string[]): b
|
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
32
|
export declare function changeProtocol(url: string, protocol: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* 标准化(如果当前页面协议为 http,则为 http,否则为 https)目标字符串中的 URL 协议。
|
|
35
|
+
* @param url 目标字符串。
|
|
36
|
+
* @returns 标准化结果。
|
|
37
|
+
* ```javascript
|
|
38
|
+
* // 当前页面协议为 http 时,结果是 'http://abc.com'
|
|
39
|
+
* // 否则结果是 'https://abc.com'
|
|
40
|
+
* normalizeProtocol('http://abc.com');
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function normalizeProtocol(url: string): string;
|
package/dist/es/net.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const reProtocol=/^(?:([a-z]+):)?\/{2,3}/i;export function startsWithProtocol(o,t){const r=reProtocol.test(o);if(r&&t){const o=(RegExp.$1||"").toLowerCase();for(let r=t.length-1;r>=0;r--)if(o===t[r].toLowerCase())return!0;return!1}return r}export function changeProtocol(o,t){return reProtocol.test(t)||(t+="://"),startsWithProtocol(o)?o.replace(reProtocol,t):t+o}
|
|
1
|
+
const reProtocol=/^(?:([a-z]+):)?\/{2,3}/i;export function startsWithProtocol(o,t){const r=reProtocol.test(o);if(r&&t){const o=(RegExp.$1||"").toLowerCase();for(let r=t.length-1;r>=0;r--)if(o===t[r].toLowerCase())return!0;return!1}return r}export function changeProtocol(o,t){return reProtocol.test(t)||(t+="://"),startsWithProtocol(o)?o.replace(reProtocol,t):t+o}export function normalizeProtocol(o){return changeProtocol(o,"http:"===window.location.protocol?"http":"https")}
|