@d-matrix/utils 1.2.6 → 1.3.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 ADDED
@@ -0,0 +1,23 @@
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
+ export {};
package/dist/file.js ADDED
@@ -0,0 +1,89 @@
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.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;
package/dist/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * as dom from './dom';
4
4
  export * as date from './date';
5
5
  export * as types from './types';
6
6
  export * as algorithm from './algorithm';
7
+ export * as file from './file';
package/dist/index.js CHANGED
@@ -19,10 +19,11 @@ 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.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"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
- "version": "1.2.6",
3
+ "version": "1.3.0",
4
4
  "description": "A dozen of utils for Front-End Development",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/readme.md CHANGED
@@ -91,10 +91,30 @@ type T0 = WithOptional<A, 'b' | 'c'>; // { a: number; b?: number; c?: number }
91
91
 
92
92
  ### algorithm
93
93
 
94
- - `moveMulti = <T extends unknown>(arr: T[], indexes: number[], start: number): T[]`
94
+ - `moveMulti<T extends unknown>(arr: T[], indexes: number[], start: number): T[]`
95
95
 
96
96
  移动多个元素到数组中指定的位置,用法,见[测试用例](tests/algorithm.cy.ts)
97
97
 
98
+ ### file
99
+
100
+ - `toImage(file: BlobPart | FileURL, options?: BlobPropertyBag): Promise<HTMLImageElement>`
101
+
102
+ 转换BlobPart或者文件地址为图片对象
103
+
104
+ - `validateImageSize(file: BlobPart | FileURL, limitSize: { width: number; height: number }, options?: BlobPropertyBag): Promise<ImageSizeValidationResult>`
105
+
106
+ 返回值:
107
+
108
+ ```ts
109
+ interface ImageSizeValidationResult {
110
+ isOk: boolean;
111
+ width: number;
112
+ height: number;
113
+ }
114
+ ```
115
+
116
+ 图片宽,高校验
117
+
98
118
  ## 测试
99
119
 
100
120
  运行全部组件测试
@@ -109,6 +129,20 @@ npm run cy:run -- --component
109
129
  npm run cy:run -- --component --spec tests/date.cy.ts
110
130
  ```
111
131
 
132
+ ## 发布
133
+
134
+ 更新package version:
135
+
136
+ ```bash
137
+ npm version <minor> or <major>...
138
+ ```
139
+
140
+ 构建:
141
+
142
+ ```bash
143
+ npm build
144
+ ```
145
+
112
146
  ## 注意事项
113
147
 
114
148
  - [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)