@daysnap/utils 0.0.44 → 0.0.46

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/es/index.d.ts CHANGED
@@ -23,6 +23,9 @@ export * from './getImageInfo';
23
23
  export * from './getRandomColor';
24
24
  export * from './getVideoInfo';
25
25
  export * from './inBrowser';
26
+ export * from './insertLink';
27
+ export * from './insertScript';
28
+ export * from './insertStyle';
26
29
  export * from './isAndroid';
27
30
  export * from './isArray';
28
31
  export * from './isBoolean';
@@ -48,7 +51,6 @@ export * from './isURL';
48
51
  export * from './isUndefined';
49
52
  export * from './isWeixin';
50
53
  export * from './isWindow';
51
- export * from './loadResource';
52
54
  export * from './omit';
53
55
  export * from './padding';
54
56
  export * from './parseDate';
package/es/index.js CHANGED
@@ -24,6 +24,9 @@ export * from './getImageInfo';
24
24
  export * from './getRandomColor';
25
25
  export * from './getVideoInfo';
26
26
  export * from './inBrowser';
27
+ export * from './insertLink';
28
+ export * from './insertScript';
29
+ export * from './insertStyle';
27
30
  export * from './isAndroid';
28
31
  export * from './isArray';
29
32
  export * from './isBoolean';
@@ -49,7 +52,6 @@ export * from './isURL';
49
52
  export * from './isUndefined';
50
53
  export * from './isWeixin';
51
54
  export * from './isWindow';
52
- export * from './loadResource';
53
55
  export * from './omit';
54
56
  export * from './padding';
55
57
  export * from './parseDate';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 插入 link
3
+ */
4
+ export declare function insertLink(href: string): Promise<HTMLLinkElement>;
5
+ export declare function insertLink(href: string, callback: (err: unknown, el: HTMLLinkElement) => void): void;
@@ -0,0 +1,15 @@
1
+ export function insertLink(href, callback) {
2
+ const linkElement = document.createElement('link');
3
+ linkElement.href = href;
4
+ const insert = (success, fail) => {
5
+ document.body.appendChild(linkElement);
6
+ linkElement.onload = success;
7
+ linkElement.onerror = fail;
8
+ };
9
+ if (callback) {
10
+ return insert(() => callback(null, linkElement), (err) => callback(err, linkElement));
11
+ }
12
+ return new Promise((resolve, reject) => {
13
+ insert(() => resolve(linkElement), reject);
14
+ });
15
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 插入外部 script 标签
3
+ */
4
+ export declare function insertScript(src: string): Promise<HTMLScriptElement>;
5
+ export declare function insertScript(src: string, callback: (err: unknown, el: HTMLScriptElement) => void): void;
@@ -0,0 +1,15 @@
1
+ export function insertScript(src, callback) {
2
+ const scriptElement = document.createElement('script');
3
+ scriptElement.src = src;
4
+ const insert = (success, fail) => {
5
+ document.body.appendChild(scriptElement);
6
+ scriptElement.onload = success;
7
+ scriptElement.onerror = fail;
8
+ };
9
+ if (callback) {
10
+ return insert(() => callback(null, scriptElement), (err) => callback(err, scriptElement));
11
+ }
12
+ return new Promise((resolve, reject) => {
13
+ insert(() => resolve(scriptElement), reject);
14
+ });
15
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 插入样式
3
+ */
4
+ export declare function insertStyle(content: string): HTMLStyleElement;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 插入样式
3
+ */
4
+ export function insertStyle(content) {
5
+ const styleElement = document.createElement('style');
6
+ document.head.appendChild(styleElement);
7
+ styleElement.innerText = content;
8
+ return styleElement;
9
+ }
package/es/throttle.d.ts CHANGED
@@ -2,4 +2,4 @@
2
2
  * 节流函数
3
3
  * 减少事件执行次数,有规律的执行
4
4
  */
5
- export declare function throttle<T extends (...args: []) => any>(fn: T, ms: number): (this: unknown, ...args: Parameters<T>) => void;
5
+ export declare function throttle<T extends (...args: any[]) => any>(fn: T, ms: number): (this: unknown, ...args: Parameters<T>) => void;
package/lib/index.d.ts CHANGED
@@ -23,6 +23,9 @@ export * from './getImageInfo';
23
23
  export * from './getRandomColor';
24
24
  export * from './getVideoInfo';
25
25
  export * from './inBrowser';
26
+ export * from './insertLink';
27
+ export * from './insertScript';
28
+ export * from './insertStyle';
26
29
  export * from './isAndroid';
27
30
  export * from './isArray';
28
31
  export * from './isBoolean';
@@ -48,7 +51,6 @@ export * from './isURL';
48
51
  export * from './isUndefined';
49
52
  export * from './isWeixin';
50
53
  export * from './isWindow';
51
- export * from './loadResource';
52
54
  export * from './omit';
53
55
  export * from './padding';
54
56
  export * from './parseDate';
package/lib/index.js CHANGED
@@ -40,6 +40,9 @@ __exportStar(require("./getImageInfo"), exports);
40
40
  __exportStar(require("./getRandomColor"), exports);
41
41
  __exportStar(require("./getVideoInfo"), exports);
42
42
  __exportStar(require("./inBrowser"), exports);
43
+ __exportStar(require("./insertLink"), exports);
44
+ __exportStar(require("./insertScript"), exports);
45
+ __exportStar(require("./insertStyle"), exports);
43
46
  __exportStar(require("./isAndroid"), exports);
44
47
  __exportStar(require("./isArray"), exports);
45
48
  __exportStar(require("./isBoolean"), exports);
@@ -65,7 +68,6 @@ __exportStar(require("./isURL"), exports);
65
68
  __exportStar(require("./isUndefined"), exports);
66
69
  __exportStar(require("./isWeixin"), exports);
67
70
  __exportStar(require("./isWindow"), exports);
68
- __exportStar(require("./loadResource"), exports);
69
71
  __exportStar(require("./omit"), exports);
70
72
  __exportStar(require("./padding"), exports);
71
73
  __exportStar(require("./parseDate"), exports);
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 插入 link
3
+ */
4
+ export declare function insertLink(href: string): Promise<HTMLLinkElement>;
5
+ export declare function insertLink(href: string, callback: (err: unknown, el: HTMLLinkElement) => void): void;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.insertLink = void 0;
4
+ function insertLink(href, callback) {
5
+ const linkElement = document.createElement('link');
6
+ linkElement.href = href;
7
+ const insert = (success, fail) => {
8
+ document.body.appendChild(linkElement);
9
+ linkElement.onload = success;
10
+ linkElement.onerror = fail;
11
+ };
12
+ if (callback) {
13
+ return insert(() => callback(null, linkElement), (err) => callback(err, linkElement));
14
+ }
15
+ return new Promise((resolve, reject) => {
16
+ insert(() => resolve(linkElement), reject);
17
+ });
18
+ }
19
+ exports.insertLink = insertLink;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 插入外部 script 标签
3
+ */
4
+ export declare function insertScript(src: string): Promise<HTMLScriptElement>;
5
+ export declare function insertScript(src: string, callback: (err: unknown, el: HTMLScriptElement) => void): void;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.insertScript = void 0;
4
+ function insertScript(src, callback) {
5
+ const scriptElement = document.createElement('script');
6
+ scriptElement.src = src;
7
+ const insert = (success, fail) => {
8
+ document.body.appendChild(scriptElement);
9
+ scriptElement.onload = success;
10
+ scriptElement.onerror = fail;
11
+ };
12
+ if (callback) {
13
+ return insert(() => callback(null, scriptElement), (err) => callback(err, scriptElement));
14
+ }
15
+ return new Promise((resolve, reject) => {
16
+ insert(() => resolve(scriptElement), reject);
17
+ });
18
+ }
19
+ exports.insertScript = insertScript;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 插入样式
3
+ */
4
+ export declare function insertStyle(content: string): HTMLStyleElement;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.insertStyle = void 0;
4
+ /**
5
+ * 插入样式
6
+ */
7
+ function insertStyle(content) {
8
+ const styleElement = document.createElement('style');
9
+ document.head.appendChild(styleElement);
10
+ styleElement.innerText = content;
11
+ return styleElement;
12
+ }
13
+ exports.insertStyle = insertStyle;
package/lib/throttle.d.ts CHANGED
@@ -2,4 +2,4 @@
2
2
  * 节流函数
3
3
  * 减少事件执行次数,有规律的执行
4
4
  */
5
- export declare function throttle<T extends (...args: []) => any>(fn: T, ms: number): (this: unknown, ...args: Parameters<T>) => void;
5
+ export declare function throttle<T extends (...args: any[]) => any>(fn: T, ms: number): (this: unknown, ...args: Parameters<T>) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daysnap/utils",
3
- "version": "0.0.44",
3
+ "version": "0.0.46",
4
4
  "description": "通用的工具库",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -22,7 +22,7 @@
22
22
  "clean": "npm run clean:es && npm run clean:lib",
23
23
  "build:es": "npm run clean:es && tsc -p ./tsconfig-esm.json",
24
24
  "build:lib": "npm run clean:lib && tsc -p ./tsconfig.json",
25
- "build": "npm run build:es && npm run build:lib",
25
+ "build": "npm run entry && npm run build:es && npm run build:lib",
26
26
  "test": "jest",
27
27
  "coverage": "jest --coverage",
28
28
  "prerelease": "npm run entry && npm run test && npm run docs",
@@ -1,7 +0,0 @@
1
- /**
2
- * 加载外部js、css、写入style样式
3
- * @param res 资源url或内容
4
- * @param type 类型
5
- * @param callback 回调函数
6
- */
7
- export declare function loadResource(res: string, type: 'js' | 'css' | 'style', callback?: () => void): void;
@@ -1,35 +0,0 @@
1
- import { isFunction } from './isFunction';
2
- /**
3
- * 加载外部js、css、写入style样式
4
- * @param res 资源url或内容
5
- * @param type 类型
6
- * @param callback 回调函数
7
- */
8
- export function loadResource(res, type, callback) {
9
- var _a;
10
- let ref = null;
11
- if (type === 'js') {
12
- // 外部js
13
- ref = document.createElement('js');
14
- ref.setAttribute('type', 'text/javascript');
15
- ref.setAttribute('src', res);
16
- }
17
- else if (type === 'css') {
18
- // 外部css
19
- ref = document.createElement('link');
20
- ref.setAttribute('rel', 'stylesheet');
21
- ref.setAttribute('type', 'text/css');
22
- ref.setAttribute('href', res);
23
- }
24
- else if (type === 'style') {
25
- ref = document.createElement('style');
26
- ref.innerHTML = res;
27
- }
28
- if (ref) {
29
- (_a = document.querySelector('head')) === null || _a === void 0 ? void 0 : _a.appendChild(ref);
30
- ref.onload = () => {
31
- if (isFunction(callback))
32
- callback();
33
- };
34
- }
35
- }
@@ -1,7 +0,0 @@
1
- /**
2
- * 加载外部js、css、写入style样式
3
- * @param res 资源url或内容
4
- * @param type 类型
5
- * @param callback 回调函数
6
- */
7
- export declare function loadResource(res: string, type: 'js' | 'css' | 'style', callback?: () => void): void;
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadResource = void 0;
4
- const isFunction_1 = require("./isFunction");
5
- /**
6
- * 加载外部js、css、写入style样式
7
- * @param res 资源url或内容
8
- * @param type 类型
9
- * @param callback 回调函数
10
- */
11
- function loadResource(res, type, callback) {
12
- var _a;
13
- let ref = null;
14
- if (type === 'js') {
15
- // 外部js
16
- ref = document.createElement('js');
17
- ref.setAttribute('type', 'text/javascript');
18
- ref.setAttribute('src', res);
19
- }
20
- else if (type === 'css') {
21
- // 外部css
22
- ref = document.createElement('link');
23
- ref.setAttribute('rel', 'stylesheet');
24
- ref.setAttribute('type', 'text/css');
25
- ref.setAttribute('href', res);
26
- }
27
- else if (type === 'style') {
28
- ref = document.createElement('style');
29
- ref.innerHTML = res;
30
- }
31
- if (ref) {
32
- (_a = document.querySelector('head')) === null || _a === void 0 ? void 0 : _a.appendChild(ref);
33
- ref.onload = () => {
34
- if ((0, isFunction_1.isFunction)(callback))
35
- callback();
36
- };
37
- }
38
- }
39
- exports.loadResource = loadResource;