@d-matrix/utils 1.2.6 → 1.4.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/dist/file.d.ts +30 -0
- package/dist/file.js +110 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/support.d.ts +12 -0
- package/dist/support.js +18 -0
- package/dist/types.d.ts +6 -0
- package/package.json +9 -4
- package/readme.md +88 -2
package/dist/file.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
declare type FileURL = string;
|
|
2
|
+
/**
|
|
3
|
+
* 转换BlobPart或者文件地址为图片对象
|
|
4
|
+
* @param file 传入文件 或者 url
|
|
5
|
+
* @returns 返回 一个图片详情对象
|
|
6
|
+
*/
|
|
7
|
+
export declare const toImage: (file: BlobPart | FileURL, options?: BlobPropertyBag | undefined) => Promise<HTMLImageElement>;
|
|
8
|
+
export interface ImageSizeValidationResult {
|
|
9
|
+
isOk: boolean;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 图片宽,高校验
|
|
15
|
+
* @param file 传入文件 或者 url
|
|
16
|
+
* @param limitSize 限制宽度, 高度
|
|
17
|
+
* @returns isOK: 是否超出宽高; width, height: 图片的宽高
|
|
18
|
+
*/
|
|
19
|
+
export declare const validateImageSize: (file: BlobPart | FileURL, limitSize: {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
}, options?: BlobPropertyBag | undefined) => Promise<ImageSizeValidationResult>;
|
|
23
|
+
/**
|
|
24
|
+
* 检测图片地址是否可用
|
|
25
|
+
* @param src 图片地址
|
|
26
|
+
* @param img HTMLImageElement
|
|
27
|
+
* @returns Promise<boolean>
|
|
28
|
+
*/
|
|
29
|
+
export declare function isImageExists(src: string, img?: HTMLImageElement): Promise<boolean>;
|
|
30
|
+
export {};
|
package/dist/file.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.isImageExists = exports.validateImageSize = exports.toImage = void 0;
|
|
40
|
+
/**
|
|
41
|
+
* 转换BlobPart或者文件地址为图片对象
|
|
42
|
+
* @param file 传入文件 或者 url
|
|
43
|
+
* @returns 返回 一个图片详情对象
|
|
44
|
+
*/
|
|
45
|
+
var toImage = function (file, options) {
|
|
46
|
+
return new Promise(function (resolve, reject) {
|
|
47
|
+
var image = new Image();
|
|
48
|
+
if (typeof file === 'string') {
|
|
49
|
+
image.src = file;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
var blob = URL.createObjectURL(new Blob([file], options));
|
|
53
|
+
image.src = blob;
|
|
54
|
+
}
|
|
55
|
+
// 如果有缓存,读缓存
|
|
56
|
+
if (image.complete) {
|
|
57
|
+
resolve(image);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
//否则加载图片
|
|
61
|
+
image.onload = function () {
|
|
62
|
+
resolve(image);
|
|
63
|
+
image.onload = null; // 避免重复加载
|
|
64
|
+
};
|
|
65
|
+
image.onerror = function () {
|
|
66
|
+
reject('图片加载失败');
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
exports.toImage = toImage;
|
|
72
|
+
/**
|
|
73
|
+
* 图片宽,高校验
|
|
74
|
+
* @param file 传入文件 或者 url
|
|
75
|
+
* @param limitSize 限制宽度, 高度
|
|
76
|
+
* @returns isOK: 是否超出宽高; width, height: 图片的宽高
|
|
77
|
+
*/
|
|
78
|
+
var validateImageSize = function (file, limitSize, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
79
|
+
var image;
|
|
80
|
+
return __generator(this, function (_a) {
|
|
81
|
+
switch (_a.label) {
|
|
82
|
+
case 0: return [4 /*yield*/, exports.toImage(file, options)];
|
|
83
|
+
case 1:
|
|
84
|
+
image = _a.sent();
|
|
85
|
+
return [2 /*return*/, { isOk: image.width <= limitSize.width && image.height <= limitSize.height, width: image.width, height: image.height }];
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}); };
|
|
89
|
+
exports.validateImageSize = validateImageSize;
|
|
90
|
+
/**
|
|
91
|
+
* 检测图片地址是否可用
|
|
92
|
+
* @param src 图片地址
|
|
93
|
+
* @param img HTMLImageElement
|
|
94
|
+
* @returns Promise<boolean>
|
|
95
|
+
*/
|
|
96
|
+
function isImageExists(src, img) {
|
|
97
|
+
if (img === void 0) { img = new Image(); }
|
|
98
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
99
|
+
return __generator(this, function (_a) {
|
|
100
|
+
if (!src)
|
|
101
|
+
return [2 /*return*/, false];
|
|
102
|
+
return [2 /*return*/, new Promise(function (resolve) {
|
|
103
|
+
img.src = src;
|
|
104
|
+
img.onload = function () { return resolve(true); };
|
|
105
|
+
img.onerror = function () { return resolve(false); };
|
|
106
|
+
})];
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
exports.isImageExists = isImageExists;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -19,10 +19,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.algorithm = exports.types = exports.date = exports.dom = exports.react = exports.clipboard = void 0;
|
|
22
|
+
exports.support = exports.file = exports.algorithm = exports.types = exports.date = exports.dom = exports.react = exports.clipboard = void 0;
|
|
23
23
|
exports.clipboard = __importStar(require("./clipboard"));
|
|
24
24
|
exports.react = __importStar(require("./react"));
|
|
25
25
|
exports.dom = __importStar(require("./dom"));
|
|
26
26
|
exports.date = __importStar(require("./date"));
|
|
27
27
|
exports.types = __importStar(require("./types"));
|
|
28
28
|
exports.algorithm = __importStar(require("./algorithm"));
|
|
29
|
+
exports.file = __importStar(require("./file"));
|
|
30
|
+
exports.support = __importStar(require("./support"));
|
package/dist/support.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isSharedWorker = exports.isWebSocket = exports.isBrowserEnv = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* 是否是浏览器环境
|
|
6
|
+
*/
|
|
7
|
+
var isBrowserEnv = function () { return typeof window !== 'undefined'; };
|
|
8
|
+
exports.isBrowserEnv = isBrowserEnv;
|
|
9
|
+
/**
|
|
10
|
+
* 是否支持WebSocket
|
|
11
|
+
*/
|
|
12
|
+
var isWebSocket = function () { return typeof WebSocket !== 'undefined'; };
|
|
13
|
+
exports.isWebSocket = isWebSocket;
|
|
14
|
+
/**
|
|
15
|
+
* 是否支持SharedWorker
|
|
16
|
+
*/
|
|
17
|
+
var isSharedWorker = function () { return typeof SharedWorker !== 'undefined'; };
|
|
18
|
+
exports.isSharedWorker = isSharedWorker;
|
package/dist/types.d.ts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
+
/** 指定的属性变为可选 */
|
|
1
2
|
export declare type WithOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
3
|
+
/** 除了 K 的其他属性变成可选 */
|
|
4
|
+
export declare type ExcludePickPartial<T, K extends keyof T> = Partial<Omit<T, K>> & Pick<T, K>;
|
|
5
|
+
export declare type Undefinable<T> = {
|
|
6
|
+
[K in keyof T]: T[K] | undefined;
|
|
7
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-matrix/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "A dozen of utils for Front-End Development",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"prebuild": "npm run clean",
|
|
9
9
|
"clean": "rimraf dist",
|
|
10
10
|
"cy:open": "cypress open",
|
|
11
|
-
"cy:run": "cypress run"
|
|
11
|
+
"cy:run": "cypress run",
|
|
12
|
+
"test:tsd": "tsd"
|
|
12
13
|
},
|
|
13
14
|
"engines": {
|
|
14
15
|
"node": ">=16.20.0"
|
|
@@ -18,9 +19,12 @@
|
|
|
18
19
|
"url": "git+https://github.com/mrdulin/dm-utils.git"
|
|
19
20
|
},
|
|
20
21
|
"files": [
|
|
21
|
-
"dist"
|
|
22
|
+
"dist",
|
|
23
|
+
"readme.md"
|
|
24
|
+
],
|
|
25
|
+
"keywords": [
|
|
26
|
+
"utils"
|
|
22
27
|
],
|
|
23
|
-
"keywords": [],
|
|
24
28
|
"author": "Lin Du",
|
|
25
29
|
"license": "MIT",
|
|
26
30
|
"bugs": {
|
|
@@ -37,6 +41,7 @@
|
|
|
37
41
|
"devDependencies": {
|
|
38
42
|
"@vitejs/plugin-react": "^4.2.1",
|
|
39
43
|
"cypress": "^13.6.6",
|
|
44
|
+
"expect-type": "^0.19.0",
|
|
40
45
|
"rimraf": "^5.0.5",
|
|
41
46
|
"vite": "^4.5.2"
|
|
42
47
|
}
|
package/readme.md
CHANGED
|
@@ -12,7 +12,9 @@ A dozen of utils for Front-End Development
|
|
|
12
12
|
- [dom](#dom)
|
|
13
13
|
- [date](#date)
|
|
14
14
|
- [types](#types)
|
|
15
|
-
- [algorithm](
|
|
15
|
+
- [algorithm](#algorithm)
|
|
16
|
+
- [file](#file)
|
|
17
|
+
- [support](#support)
|
|
16
18
|
|
|
17
19
|
### clipboard
|
|
18
20
|
|
|
@@ -91,10 +93,64 @@ type T0 = WithOptional<A, 'b' | 'c'>; // { a: number; b?: number; c?: number }
|
|
|
91
93
|
|
|
92
94
|
### algorithm
|
|
93
95
|
|
|
94
|
-
- `moveMulti
|
|
96
|
+
- `moveMulti<T extends unknown>(arr: T[], indexes: number[], start: number): T[]`
|
|
95
97
|
|
|
96
98
|
移动多个元素到数组中指定的位置,用法,见[测试用例](tests/algorithm.cy.ts)
|
|
97
99
|
|
|
100
|
+
### file
|
|
101
|
+
|
|
102
|
+
- `toImage(file: BlobPart | FileURL, options?: BlobPropertyBag): Promise<HTMLImageElement>`
|
|
103
|
+
|
|
104
|
+
转换BlobPart或者文件地址为图片对象
|
|
105
|
+
|
|
106
|
+
- `validateImageSize(file: BlobPart | FileURL, limitSize: { width: number; height: number }, options?: BlobPropertyBag): Promise<ImageSizeValidationResult>`
|
|
107
|
+
|
|
108
|
+
返回值:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
interface ImageSizeValidationResult {
|
|
112
|
+
isOk: boolean;
|
|
113
|
+
width: number;
|
|
114
|
+
height: number;
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
图片宽,高校验
|
|
119
|
+
|
|
120
|
+
- `isImageExists(src: string, img: HTMLImageElement = new Image()): Promise<boolean>`
|
|
121
|
+
|
|
122
|
+
检测图片地址是否可用
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import { file } from '@d-matrix/utils';
|
|
126
|
+
|
|
127
|
+
const url = 'https://picsum.photos/200/300';
|
|
128
|
+
const res = await file.isImageExists(url);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
传入HTML中已经存在的`img`元素
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
import { file } from '@d-matrix/utils';
|
|
135
|
+
|
|
136
|
+
const $img = document.getElementById('img');
|
|
137
|
+
const res = await file.isImageExists(url, $img);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## support
|
|
141
|
+
|
|
142
|
+
- `isBrowserEnv(): boolean`
|
|
143
|
+
|
|
144
|
+
是否是浏览器环境
|
|
145
|
+
|
|
146
|
+
- `isWebSocket(): boolean`
|
|
147
|
+
|
|
148
|
+
是否支持WebSocket
|
|
149
|
+
|
|
150
|
+
- `isSharedWorker(): boolean`
|
|
151
|
+
|
|
152
|
+
是否支持SharedWorker
|
|
153
|
+
|
|
98
154
|
## 测试
|
|
99
155
|
|
|
100
156
|
运行全部组件测试
|
|
@@ -109,6 +165,36 @@ npm run cy:run -- --component
|
|
|
109
165
|
npm run cy:run -- --component --spec tests/date.cy.ts
|
|
110
166
|
```
|
|
111
167
|
|
|
168
|
+
## 发布
|
|
169
|
+
|
|
170
|
+
更新package version:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
npm version <minor> or <major>...
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
构建:
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
npm build
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
发布:
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
npm publish --access public
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
镜像站查询版本与手动同步:
|
|
189
|
+
|
|
190
|
+
[npm镜像站](https://npmmirror.com/package/@d-matrix/utils)
|
|
191
|
+
|
|
192
|
+
通过`git log`命令获取changelogs,用于填写GitHub Release内容:
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
git log --oneline --decorate
|
|
196
|
+
```
|
|
197
|
+
|
|
112
198
|
## 注意事项
|
|
113
199
|
|
|
114
200
|
- [Before Publishing: Make Sure Your Package Installs and Works](https://docs.npmjs.com/cli/v10/using-npm/developers/#before-publishing-make-sure-your-package-installs-and-works)
|