hsu-utils 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  *
3
- * hsu-utils v0.0.11
3
+ * hsu-utils v0.0.12
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -27,3 +27,33 @@
27
27
  * SOFTWARE.
28
28
  *
29
29
  */
30
+
31
+ /**
32
+ * @license
33
+ * web-streams-polyfill v3.3.3
34
+ * Copyright 2024 Mattias Buelens, Diwank Singh Tomer and other contributors.
35
+ * This code is released under the MIT license.
36
+ * SPDX-License-Identifier: MIT
37
+ */
38
+
39
+ /**
40
+ * @licstart The following is the entire license notice for the
41
+ * Javascript code in this page
42
+ *
43
+ * Copyright 2022 Mozilla Foundation
44
+ *
45
+ * Licensed under the Apache License, Version 2.0 (the "License");
46
+ * you may not use this file except in compliance with the License.
47
+ * You may obtain a copy of the License at
48
+ *
49
+ * http://www.apache.org/licenses/LICENSE-2.0
50
+ *
51
+ * Unless required by applicable law or agreed to in writing, software
52
+ * distributed under the License is distributed on an "AS IS" BASIS,
53
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
54
+ * See the License for the specific language governing permissions and
55
+ * limitations under the License.
56
+ *
57
+ * @licend The above is the entire license notice for the
58
+ * Javascript code in this page
59
+ */
@@ -1 +1 @@
1
- export default function deepCopy<T = unknown>(data: T): T;
1
+ export default function deepCopy<T = any>(data: T): T;
@@ -3,7 +3,7 @@ export default function deepCopy(data) {
3
3
  if (Typeof(data) === 'date') {
4
4
  return new Date(data.toISOString());
5
5
  }
6
- else if (Typeof(data) === 'formdata') {
6
+ if (Typeof(data) === 'formdata') {
7
7
  const _data = data;
8
8
  const _formData = new FormData();
9
9
  _data.forEach((value, key) => {
@@ -11,7 +11,23 @@ export default function deepCopy(data) {
11
11
  });
12
12
  return _formData;
13
13
  }
14
- else if (Array.isArray(data)) {
14
+ if (Typeof(data) === 'map') {
15
+ const _data = data;
16
+ const map = new Map();
17
+ _data.forEach((value, key) => {
18
+ map.set(key, value);
19
+ });
20
+ return map;
21
+ }
22
+ if (Typeof(data) === 'set') {
23
+ const _data = data;
24
+ const set = new Set();
25
+ _data.forEach((value) => {
26
+ set.add(value);
27
+ });
28
+ return set;
29
+ }
30
+ if (Array.isArray(data)) {
15
31
  const newData = data.map((item) => {
16
32
  if (typeof item === 'object') {
17
33
  return deepCopy(item);
@@ -0,0 +1 @@
1
+ export default function downloadFile(file: ArrayBuffer | string, fileName: string): void;
@@ -0,0 +1,34 @@
1
+ function downloadFileByLocalPath(path, fileName) {
2
+ fetch(path)
3
+ .then((response) => response.arrayBuffer())
4
+ .then((arrayBuffer) => {
5
+ downloadFile(arrayBuffer, fileName);
6
+ });
7
+ }
8
+ function downloadFileByUrl(url, fileName) {
9
+ if (!url.startsWith('http')) {
10
+ downloadFileByLocalPath(url, fileName);
11
+ return;
12
+ }
13
+ const downloadElement = document.createElement('a');
14
+ downloadElement.href = url;
15
+ downloadElement.download = decodeURI(fileName || '');
16
+ document.body.appendChild(downloadElement);
17
+ downloadElement.click();
18
+ document.body.removeChild(downloadElement);
19
+ }
20
+ export default function downloadFile(file, fileName) {
21
+ if (typeof file === 'string') {
22
+ downloadFileByUrl(file, fileName);
23
+ return;
24
+ }
25
+ const blob = new Blob([file]);
26
+ const downloadElement = document.createElement('a');
27
+ const href = window.URL.createObjectURL(blob);
28
+ downloadElement.href = href;
29
+ downloadElement.download = decodeURI(fileName || '');
30
+ document.body.appendChild(downloadElement);
31
+ downloadElement.click();
32
+ document.body.removeChild(downloadElement);
33
+ window.URL.revokeObjectURL(href);
34
+ }
@@ -0,0 +1,8 @@
1
+ interface PDFOption {
2
+ pdfUrl: string;
3
+ containerId: string;
4
+ startPageNum?: number;
5
+ endPageNum?: number;
6
+ }
7
+ export default function renderPDF({ pdfUrl, containerId, startPageNum, endPageNum }: PDFOption): Promise<void>;
8
+ export {};
@@ -0,0 +1,60 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { GlobalWorkerOptions, getDocument } from 'pdfjs-dist/legacy/build/pdf.js';
11
+ GlobalWorkerOptions.workerSrc = 'https://cdn.bootcss.com/pdf.js/2.13.216/pdf.worker.js';
12
+ function clear(containerId) {
13
+ const container = document.getElementById(containerId);
14
+ const pages = document.querySelectorAll(`[id^="${containerId}-page-"]`);
15
+ pages === null || pages === void 0 ? void 0 : pages.forEach((item) => {
16
+ container === null || container === void 0 ? void 0 : container.removeChild(item);
17
+ });
18
+ }
19
+ function render(pdf, container, num) {
20
+ pdf.getPage(num).then((page) => {
21
+ const pageDiv = document.createElement('div');
22
+ pageDiv.setAttribute('id', `${container.id}-page-${num}`);
23
+ pageDiv.setAttribute('style', 'position: relative; ');
24
+ container.appendChild(pageDiv);
25
+ const canvas = document.createElement('canvas');
26
+ pageDiv.appendChild(canvas);
27
+ const ctx = canvas.getContext('2d');
28
+ const scale = 1;
29
+ const devicePixelRatio = window.devicePixelRatio * 2;
30
+ const viewport = page.getViewport({ scale: scale * devicePixelRatio });
31
+ canvas.style.width = '100%';
32
+ canvas.style.height = '100%';
33
+ canvas.width = viewport.width;
34
+ canvas.height = viewport.height;
35
+ const renderContext = {
36
+ canvasContext: ctx,
37
+ viewport: viewport
38
+ };
39
+ page.render(renderContext);
40
+ });
41
+ }
42
+ export default function renderPDF({ pdfUrl, containerId, startPageNum, endPageNum }) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ clear(containerId);
45
+ const loadingTask = getDocument({
46
+ url: pdfUrl,
47
+ cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
48
+ cMapPacked: true
49
+ });
50
+ const pdf = yield loadingTask.promise;
51
+ const container = document.getElementById(containerId);
52
+ if (!container)
53
+ return;
54
+ const start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
55
+ const end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
56
+ for (let i = start; i <= end; i++) {
57
+ render(pdf, container, i);
58
+ }
59
+ });
60
+ }
@@ -1,3 +1,5 @@
1
- type Type = 'number' | 'string' | 'boolean' | 'object' | 'array' | 'null' | 'undefined' | 'unknown' | 'function' | 'symbol' | 'date' | 'formdata' | string;
2
- export default function Typeof(value: any): Type;
1
+ type BaseType = 'string' | 'number' | 'boolean' | 'undefined' | 'function' | 'symbol' | 'bigint';
2
+ type ObjectType = 'object' | 'array' | 'null' | 'date' | 'formdata' | 'set' | 'map' | 'regexp' | 'arraybuffer' | 'blob';
3
+ type Type = BaseType | ObjectType;
4
+ export default function Typeof<T>(value: T, isType?: Type): boolean | Type;
3
5
  export {};
@@ -1,6 +1,14 @@
1
- export default function Typeof(value) {
1
+ export default function Typeof(value, isType) {
2
2
  const object_type = Object.prototype.toString.call(value);
3
3
  const reg = new RegExp(' (.+?)]');
4
- const type = object_type.match(reg)[1];
5
- return type.toLowerCase();
4
+ const type = object_type.match(reg)[1].toLowerCase();
5
+ if (!isType) {
6
+ return type;
7
+ }
8
+ if (typeof value !== 'object') {
9
+ return typeof value === isType;
10
+ }
11
+ else {
12
+ return type === isType;
13
+ }
6
14
  }
package/es/index.d.ts CHANGED
@@ -5,6 +5,8 @@ import Typeof from './Typeof';
5
5
  import get_string_width from './GetStrWidth';
6
6
  import ConvertNumbers from './ConvertNumbers';
7
7
  import loadImage from './LoadImage';
8
- export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage };
8
+ import renderPDF from './RenderPDF';
9
+ import downloadFile from './DownloadFile';
10
+ export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, renderPDF, downloadFile };
9
11
  import { ConsoleData } from './ConsoleTable';
10
12
  export type { ConsoleData };
package/es/index.js CHANGED
@@ -5,4 +5,6 @@ import Typeof from './Typeof';
5
5
  import get_string_width from './GetStrWidth';
6
6
  import ConvertNumbers from './ConvertNumbers';
7
7
  import loadImage from './LoadImage';
8
- export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage };
8
+ import renderPDF from './RenderPDF';
9
+ import downloadFile from './DownloadFile';
10
+ export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, renderPDF, downloadFile };
@@ -1 +1 @@
1
- export default function deepCopy<T = unknown>(data: T): T;
1
+ export default function deepCopy<T = any>(data: T): T;
@@ -5,7 +5,7 @@ function deepCopy(data) {
5
5
  if ((0, __1.Typeof)(data) === 'date') {
6
6
  return new Date(data.toISOString());
7
7
  }
8
- else if ((0, __1.Typeof)(data) === 'formdata') {
8
+ if ((0, __1.Typeof)(data) === 'formdata') {
9
9
  var _data = data;
10
10
  var _formData_1 = new FormData();
11
11
  _data.forEach(function (value, key) {
@@ -13,7 +13,23 @@ function deepCopy(data) {
13
13
  });
14
14
  return _formData_1;
15
15
  }
16
- else if (Array.isArray(data)) {
16
+ if ((0, __1.Typeof)(data) === 'map') {
17
+ var _data = data;
18
+ var map_1 = new Map();
19
+ _data.forEach(function (value, key) {
20
+ map_1.set(key, value);
21
+ });
22
+ return map_1;
23
+ }
24
+ if ((0, __1.Typeof)(data) === 'set') {
25
+ var _data = data;
26
+ var set_1 = new Set();
27
+ _data.forEach(function (value) {
28
+ set_1.add(value);
29
+ });
30
+ return set_1;
31
+ }
32
+ if (Array.isArray(data)) {
17
33
  var newData = data.map(function (item) {
18
34
  if (typeof item === 'object') {
19
35
  return deepCopy(item);
@@ -0,0 +1 @@
1
+ export default function downloadFile(file: ArrayBuffer | string, fileName: string): void;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function downloadFileByLocalPath(path, fileName) {
4
+ fetch(path)
5
+ .then(function (response) { return response.arrayBuffer(); })
6
+ .then(function (arrayBuffer) {
7
+ downloadFile(arrayBuffer, fileName);
8
+ });
9
+ }
10
+ function downloadFileByUrl(url, fileName) {
11
+ if (!url.startsWith('http')) {
12
+ downloadFileByLocalPath(url, fileName);
13
+ return;
14
+ }
15
+ var downloadElement = document.createElement('a');
16
+ downloadElement.href = url;
17
+ downloadElement.download = decodeURI(fileName || '');
18
+ document.body.appendChild(downloadElement);
19
+ downloadElement.click();
20
+ document.body.removeChild(downloadElement);
21
+ }
22
+ function downloadFile(file, fileName) {
23
+ if (typeof file === 'string') {
24
+ downloadFileByUrl(file, fileName);
25
+ return;
26
+ }
27
+ var blob = new Blob([file]);
28
+ var downloadElement = document.createElement('a');
29
+ var href = window.URL.createObjectURL(blob);
30
+ downloadElement.href = href;
31
+ downloadElement.download = decodeURI(fileName || '');
32
+ document.body.appendChild(downloadElement);
33
+ downloadElement.click();
34
+ document.body.removeChild(downloadElement);
35
+ window.URL.revokeObjectURL(href);
36
+ }
37
+ exports.default = downloadFile;
@@ -0,0 +1,8 @@
1
+ interface PDFOption {
2
+ pdfUrl: string;
3
+ containerId: string;
4
+ startPageNum?: number;
5
+ endPageNum?: number;
6
+ }
7
+ export default function renderPDF({ pdfUrl, containerId, startPageNum, endPageNum }: PDFOption): Promise<void>;
8
+ export {};
@@ -0,0 +1,100 @@
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 (g && (g = 0, op[0] && (_ = 0)), _) 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
+ var pdf_js_1 = require("pdfjs-dist/legacy/build/pdf.js");
40
+ pdf_js_1.GlobalWorkerOptions.workerSrc = 'https://cdn.bootcss.com/pdf.js/2.13.216/pdf.worker.js';
41
+ function clear(containerId) {
42
+ var container = document.getElementById(containerId);
43
+ var pages = document.querySelectorAll("[id^=\"".concat(containerId, "-page-\"]"));
44
+ pages === null || pages === void 0 ? void 0 : pages.forEach(function (item) {
45
+ container === null || container === void 0 ? void 0 : container.removeChild(item);
46
+ });
47
+ }
48
+ function render(pdf, container, num) {
49
+ pdf.getPage(num).then(function (page) {
50
+ var pageDiv = document.createElement('div');
51
+ pageDiv.setAttribute('id', "".concat(container.id, "-page-").concat(num));
52
+ pageDiv.setAttribute('style', 'position: relative; ');
53
+ container.appendChild(pageDiv);
54
+ var canvas = document.createElement('canvas');
55
+ pageDiv.appendChild(canvas);
56
+ var ctx = canvas.getContext('2d');
57
+ var scale = 1;
58
+ var devicePixelRatio = window.devicePixelRatio * 2;
59
+ var viewport = page.getViewport({ scale: scale * devicePixelRatio });
60
+ canvas.style.width = '100%';
61
+ canvas.style.height = '100%';
62
+ canvas.width = viewport.width;
63
+ canvas.height = viewport.height;
64
+ var renderContext = {
65
+ canvasContext: ctx,
66
+ viewport: viewport
67
+ };
68
+ page.render(renderContext);
69
+ });
70
+ }
71
+ function renderPDF(_a) {
72
+ var pdfUrl = _a.pdfUrl, containerId = _a.containerId, startPageNum = _a.startPageNum, endPageNum = _a.endPageNum;
73
+ return __awaiter(this, void 0, void 0, function () {
74
+ var loadingTask, pdf, container, start, end, i;
75
+ return __generator(this, function (_b) {
76
+ switch (_b.label) {
77
+ case 0:
78
+ clear(containerId);
79
+ loadingTask = (0, pdf_js_1.getDocument)({
80
+ url: pdfUrl,
81
+ cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
82
+ cMapPacked: true
83
+ });
84
+ return [4, loadingTask.promise];
85
+ case 1:
86
+ pdf = _b.sent();
87
+ container = document.getElementById(containerId);
88
+ if (!container)
89
+ return [2];
90
+ start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
91
+ end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
92
+ for (i = start; i <= end; i++) {
93
+ render(pdf, container, i);
94
+ }
95
+ return [2];
96
+ }
97
+ });
98
+ });
99
+ }
100
+ exports.default = renderPDF;
@@ -1,3 +1,5 @@
1
- type Type = 'number' | 'string' | 'boolean' | 'object' | 'array' | 'null' | 'undefined' | 'unknown' | 'function' | 'symbol' | 'date' | 'formdata' | string;
2
- export default function Typeof(value: any): Type;
1
+ type BaseType = 'string' | 'number' | 'boolean' | 'undefined' | 'function' | 'symbol' | 'bigint';
2
+ type ObjectType = 'object' | 'array' | 'null' | 'date' | 'formdata' | 'set' | 'map' | 'regexp' | 'arraybuffer' | 'blob';
3
+ type Type = BaseType | ObjectType;
4
+ export default function Typeof<T>(value: T, isType?: Type): boolean | Type;
3
5
  export {};
@@ -1,9 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- function Typeof(value) {
3
+ function Typeof(value, isType) {
4
4
  var object_type = Object.prototype.toString.call(value);
5
5
  var reg = new RegExp(' (.+?)]');
6
- var type = object_type.match(reg)[1];
7
- return type.toLowerCase();
6
+ var type = object_type.match(reg)[1].toLowerCase();
7
+ if (!isType) {
8
+ return type;
9
+ }
10
+ if (typeof value !== 'object') {
11
+ return typeof value === isType;
12
+ }
13
+ else {
14
+ return type === isType;
15
+ }
8
16
  }
9
17
  exports.default = Typeof;
package/lib/index.d.ts CHANGED
@@ -5,6 +5,8 @@ import Typeof from './Typeof';
5
5
  import get_string_width from './GetStrWidth';
6
6
  import ConvertNumbers from './ConvertNumbers';
7
7
  import loadImage from './LoadImage';
8
- export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage };
8
+ import renderPDF from './RenderPDF';
9
+ import downloadFile from './DownloadFile';
10
+ export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, renderPDF, downloadFile };
9
11
  import { ConsoleData } from './ConsoleTable';
10
12
  export type { ConsoleData };
package/lib/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.loadImage = exports.ConvertNumbers = exports.get_string_width = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
6
+ exports.downloadFile = exports.renderPDF = exports.loadImage = exports.ConvertNumbers = exports.get_string_width = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
7
7
  var ConsoleTable_1 = __importDefault(require("./ConsoleTable"));
8
8
  exports.console_table = ConsoleTable_1.default;
9
9
  var DeepCopy_1 = __importDefault(require("./DeepCopy"));
@@ -18,3 +18,7 @@ var ConvertNumbers_1 = __importDefault(require("./ConvertNumbers"));
18
18
  exports.ConvertNumbers = ConvertNumbers_1.default;
19
19
  var LoadImage_1 = __importDefault(require("./LoadImage"));
20
20
  exports.loadImage = LoadImage_1.default;
21
+ var RenderPDF_1 = __importDefault(require("./RenderPDF"));
22
+ exports.renderPDF = RenderPDF_1.default;
23
+ var DownloadFile_1 = __importDefault(require("./DownloadFile"));
24
+ exports.downloadFile = DownloadFile_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",
@@ -53,5 +53,7 @@
53
53
  "webpack": "^5.65.0",
54
54
  "webpack-cli": "^4.9.1"
55
55
  },
56
- "dependencies": {}
56
+ "dependencies": {
57
+ "pdfjs-dist": "2.13.216"
58
+ }
57
59
  }