@etsoo/shared 1.1.87 → 1.1.89
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/README.md +2 -0
- package/__tests__/Utils.ts +8 -0
- package/lib/cjs/DomUtils.d.ts +9 -1
- package/lib/cjs/DomUtils.js +30 -0
- package/lib/cjs/Utils.d.ts +19 -1
- package/lib/cjs/Utils.js +12 -0
- package/lib/mjs/DomUtils.d.ts +9 -1
- package/lib/mjs/DomUtils.js +30 -0
- package/lib/mjs/Utils.d.ts +19 -1
- package/lib/mjs/Utils.js +12 -0
- package/package.json +6 -6
- package/src/DomUtils.ts +37 -0
- package/src/Utils.ts +39 -1
package/README.md
CHANGED
|
@@ -204,6 +204,7 @@ DOM/window related utilities
|
|
|
204
204
|
|mergeFormData|Merge form data to primary one|
|
|
205
205
|
|mergeURLSearchParams|Merge URL search parameters|
|
|
206
206
|
|setFocus|Set HTML element focus by name|
|
|
207
|
+
|verifyPermission|Verify file system permission|
|
|
207
208
|
|
|
208
209
|
## ExtendUtils
|
|
209
210
|
Extend current class/object functioning
|
|
@@ -245,6 +246,7 @@ String and other related utilities
|
|
|
245
246
|
|addBlankItem|Add blank item to collection|
|
|
246
247
|
|arrayDifferences|Array 1 items do not exist in Array 2 or reverse match|
|
|
247
248
|
|charsToNumber|Base64 chars to number|
|
|
249
|
+
|containChinese|Check the input string contains Chinese character or not|
|
|
248
250
|
|correctTypes|Correct object's property value type|
|
|
249
251
|
|equals|Two values equal|
|
|
250
252
|
|exclude|Exclude specific items|
|
package/__tests__/Utils.ts
CHANGED
|
@@ -13,6 +13,14 @@ test('Tests for addBlankItem', () => {
|
|
|
13
13
|
expect(options.length).toBe(3);
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
+
test('Tests for containChinese', () => {
|
|
17
|
+
expect('123 abC'.containChinese()).toBeFalsy();
|
|
18
|
+
expect('亿速思维'.containChinese()).toBeTruthy();
|
|
19
|
+
expect('亿速Etsoo'.containChinese()).toBeTruthy();
|
|
20
|
+
expect('김 민수'.containKorean()).toBeTruthy();
|
|
21
|
+
expect('ぁ-ん'.containJapanese()).toBeTruthy();
|
|
22
|
+
});
|
|
23
|
+
|
|
16
24
|
test('Tests for arrayDifferences', () => {
|
|
17
25
|
const a1 = ['a', 'b', 'c', 'e'];
|
|
18
26
|
const a2 = ['a', 'c', 'd'];
|
package/lib/cjs/DomUtils.d.ts
CHANGED
|
@@ -57,7 +57,7 @@ export declare namespace DomUtils {
|
|
|
57
57
|
* @param suggestedName Suggested file name
|
|
58
58
|
* @param autoDetect Auto detect, false will use link click way
|
|
59
59
|
*/
|
|
60
|
-
function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<boolean>;
|
|
60
|
+
function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<boolean | undefined>;
|
|
61
61
|
/**
|
|
62
62
|
* File to data URL
|
|
63
63
|
* @param file File
|
|
@@ -141,4 +141,12 @@ export declare namespace DomUtils {
|
|
|
141
141
|
* @param container Container, limits the element range
|
|
142
142
|
*/
|
|
143
143
|
function setFocus(name: string | object, container?: HTMLElement): void;
|
|
144
|
+
/**
|
|
145
|
+
* Verify file system permission
|
|
146
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
|
|
147
|
+
* @param fileHandle FileSystemHandle
|
|
148
|
+
* @param withWrite With write permission
|
|
149
|
+
* @returns Result
|
|
150
|
+
*/
|
|
151
|
+
function verifyPermission(fileHandle: any, withWrite?: boolean): Promise<boolean>;
|
|
144
152
|
}
|
package/lib/cjs/DomUtils.js
CHANGED
|
@@ -242,9 +242,12 @@ var DomUtils;
|
|
|
242
242
|
return __awaiter(this, void 0, void 0, function* () {
|
|
243
243
|
try {
|
|
244
244
|
if (autoDetect && 'showSaveFilePicker' in globalThis) {
|
|
245
|
+
// AbortError - Use dismisses the window
|
|
245
246
|
const handle = yield globalThis.showSaveFilePicker({
|
|
246
247
|
suggestedName
|
|
247
248
|
});
|
|
249
|
+
if (!(yield verifyPermission(handle, true)))
|
|
250
|
+
return undefined;
|
|
248
251
|
const stream = yield handle.createWritable();
|
|
249
252
|
if (data instanceof Blob) {
|
|
250
253
|
data.stream().pipeTo(stream);
|
|
@@ -474,4 +477,31 @@ var DomUtils;
|
|
|
474
477
|
element.focus();
|
|
475
478
|
}
|
|
476
479
|
DomUtils.setFocus = setFocus;
|
|
480
|
+
/**
|
|
481
|
+
* Verify file system permission
|
|
482
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
|
|
483
|
+
* @param fileHandle FileSystemHandle
|
|
484
|
+
* @param withWrite With write permission
|
|
485
|
+
* @returns Result
|
|
486
|
+
*/
|
|
487
|
+
function verifyPermission(fileHandle, withWrite = false) {
|
|
488
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
489
|
+
if (!('queryPermission' in fileHandle) ||
|
|
490
|
+
!('requestPermission' in fileHandle))
|
|
491
|
+
return false;
|
|
492
|
+
// FileSystemHandlePermissionDescriptor
|
|
493
|
+
const opts = { mode: withWrite ? 'readwrite' : 'read' };
|
|
494
|
+
// Check if we already have permission, if so, return true.
|
|
495
|
+
if ((yield fileHandle.queryPermission(opts)) === 'granted') {
|
|
496
|
+
return true;
|
|
497
|
+
}
|
|
498
|
+
// Request permission to the file, if the user grants permission, return true.
|
|
499
|
+
if ((yield fileHandle.requestPermission(opts)) === 'granted') {
|
|
500
|
+
return true;
|
|
501
|
+
}
|
|
502
|
+
// The user did not grant permission, return false.
|
|
503
|
+
return false;
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
DomUtils.verifyPermission = verifyPermission;
|
|
477
507
|
})(DomUtils = exports.DomUtils || (exports.DomUtils = {}));
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
2
|
declare global {
|
|
3
3
|
interface String {
|
|
4
|
+
/**
|
|
5
|
+
* Check the input string contains Chinese character or not
|
|
6
|
+
* @param this Input
|
|
7
|
+
* @param test Test string
|
|
8
|
+
*/
|
|
9
|
+
containChinese(this: string): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Check the input string contains Korean character or not
|
|
12
|
+
* @param this Input
|
|
13
|
+
* @param test Test string
|
|
14
|
+
*/
|
|
15
|
+
containKorean(this: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Check the input string contains Japanese character or not
|
|
18
|
+
* @param this Input
|
|
19
|
+
* @param test Test string
|
|
20
|
+
*/
|
|
21
|
+
containJapanese(this: string): boolean;
|
|
4
22
|
/**
|
|
5
23
|
* Format string with parameters
|
|
6
|
-
* @param this
|
|
24
|
+
* @param this Input template
|
|
7
25
|
* @param parameters Parameters to fill the template
|
|
8
26
|
*/
|
|
9
27
|
format(this: string, ...parameters: string[]): string;
|
package/lib/cjs/Utils.js
CHANGED
|
@@ -15,6 +15,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.Utils = void 0;
|
|
16
16
|
const DataTypes_1 = require("./DataTypes");
|
|
17
17
|
const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
|
|
18
|
+
String.prototype.containChinese = function () {
|
|
19
|
+
const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
|
|
20
|
+
return regExp.test(this);
|
|
21
|
+
};
|
|
22
|
+
String.prototype.containKorean = function () {
|
|
23
|
+
const regExp = /[\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff\u3400-\u4dbf]/g;
|
|
24
|
+
return regExp.test(this);
|
|
25
|
+
};
|
|
26
|
+
String.prototype.containJapanese = function () {
|
|
27
|
+
const regExp = /[\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf]/g;
|
|
28
|
+
return regExp.test(this);
|
|
29
|
+
};
|
|
18
30
|
String.prototype.format = function (...parameters) {
|
|
19
31
|
let template = this;
|
|
20
32
|
parameters.forEach((value, index) => {
|
package/lib/mjs/DomUtils.d.ts
CHANGED
|
@@ -57,7 +57,7 @@ export declare namespace DomUtils {
|
|
|
57
57
|
* @param suggestedName Suggested file name
|
|
58
58
|
* @param autoDetect Auto detect, false will use link click way
|
|
59
59
|
*/
|
|
60
|
-
function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<boolean>;
|
|
60
|
+
function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<boolean | undefined>;
|
|
61
61
|
/**
|
|
62
62
|
* File to data URL
|
|
63
63
|
* @param file File
|
|
@@ -141,4 +141,12 @@ export declare namespace DomUtils {
|
|
|
141
141
|
* @param container Container, limits the element range
|
|
142
142
|
*/
|
|
143
143
|
function setFocus(name: string | object, container?: HTMLElement): void;
|
|
144
|
+
/**
|
|
145
|
+
* Verify file system permission
|
|
146
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
|
|
147
|
+
* @param fileHandle FileSystemHandle
|
|
148
|
+
* @param withWrite With write permission
|
|
149
|
+
* @returns Result
|
|
150
|
+
*/
|
|
151
|
+
function verifyPermission(fileHandle: any, withWrite?: boolean): Promise<boolean>;
|
|
144
152
|
}
|
package/lib/mjs/DomUtils.js
CHANGED
|
@@ -239,9 +239,12 @@ export var DomUtils;
|
|
|
239
239
|
return __awaiter(this, void 0, void 0, function* () {
|
|
240
240
|
try {
|
|
241
241
|
if (autoDetect && 'showSaveFilePicker' in globalThis) {
|
|
242
|
+
// AbortError - Use dismisses the window
|
|
242
243
|
const handle = yield globalThis.showSaveFilePicker({
|
|
243
244
|
suggestedName
|
|
244
245
|
});
|
|
246
|
+
if (!(yield verifyPermission(handle, true)))
|
|
247
|
+
return undefined;
|
|
245
248
|
const stream = yield handle.createWritable();
|
|
246
249
|
if (data instanceof Blob) {
|
|
247
250
|
data.stream().pipeTo(stream);
|
|
@@ -471,4 +474,31 @@ export var DomUtils;
|
|
|
471
474
|
element.focus();
|
|
472
475
|
}
|
|
473
476
|
DomUtils.setFocus = setFocus;
|
|
477
|
+
/**
|
|
478
|
+
* Verify file system permission
|
|
479
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
|
|
480
|
+
* @param fileHandle FileSystemHandle
|
|
481
|
+
* @param withWrite With write permission
|
|
482
|
+
* @returns Result
|
|
483
|
+
*/
|
|
484
|
+
function verifyPermission(fileHandle, withWrite = false) {
|
|
485
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
486
|
+
if (!('queryPermission' in fileHandle) ||
|
|
487
|
+
!('requestPermission' in fileHandle))
|
|
488
|
+
return false;
|
|
489
|
+
// FileSystemHandlePermissionDescriptor
|
|
490
|
+
const opts = { mode: withWrite ? 'readwrite' : 'read' };
|
|
491
|
+
// Check if we already have permission, if so, return true.
|
|
492
|
+
if ((yield fileHandle.queryPermission(opts)) === 'granted') {
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
// Request permission to the file, if the user grants permission, return true.
|
|
496
|
+
if ((yield fileHandle.requestPermission(opts)) === 'granted') {
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
// The user did not grant permission, return false.
|
|
500
|
+
return false;
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
DomUtils.verifyPermission = verifyPermission;
|
|
474
504
|
})(DomUtils || (DomUtils = {}));
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
2
|
declare global {
|
|
3
3
|
interface String {
|
|
4
|
+
/**
|
|
5
|
+
* Check the input string contains Chinese character or not
|
|
6
|
+
* @param this Input
|
|
7
|
+
* @param test Test string
|
|
8
|
+
*/
|
|
9
|
+
containChinese(this: string): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Check the input string contains Korean character or not
|
|
12
|
+
* @param this Input
|
|
13
|
+
* @param test Test string
|
|
14
|
+
*/
|
|
15
|
+
containKorean(this: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Check the input string contains Japanese character or not
|
|
18
|
+
* @param this Input
|
|
19
|
+
* @param test Test string
|
|
20
|
+
*/
|
|
21
|
+
containJapanese(this: string): boolean;
|
|
4
22
|
/**
|
|
5
23
|
* Format string with parameters
|
|
6
|
-
* @param this
|
|
24
|
+
* @param this Input template
|
|
7
25
|
* @param parameters Parameters to fill the template
|
|
8
26
|
*/
|
|
9
27
|
format(this: string, ...parameters: string[]): string;
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -9,6 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { DataTypes } from './DataTypes';
|
|
11
11
|
import isEqual from 'lodash.isequal';
|
|
12
|
+
String.prototype.containChinese = function () {
|
|
13
|
+
const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
|
|
14
|
+
return regExp.test(this);
|
|
15
|
+
};
|
|
16
|
+
String.prototype.containKorean = function () {
|
|
17
|
+
const regExp = /[\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff\u3400-\u4dbf]/g;
|
|
18
|
+
return regExp.test(this);
|
|
19
|
+
};
|
|
20
|
+
String.prototype.containJapanese = function () {
|
|
21
|
+
const regExp = /[\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf]/g;
|
|
22
|
+
return regExp.test(this);
|
|
23
|
+
};
|
|
12
24
|
String.prototype.format = function (...parameters) {
|
|
13
25
|
let template = this;
|
|
14
26
|
parameters.forEach((value, index) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.89",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://github.com/ETSOO/Shared#readme",
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@types/jest": "^29.
|
|
57
|
+
"@types/jest": "^29.4.0",
|
|
58
58
|
"@types/lodash.isequal": "^4.5.6",
|
|
59
|
-
"jest": "^29.3
|
|
60
|
-
"jest-environment-jsdom": "^29.3
|
|
61
|
-
"ts-jest": "^29.0.
|
|
62
|
-
"typescript": "^4.9.
|
|
59
|
+
"jest": "^29.4.3",
|
|
60
|
+
"jest-environment-jsdom": "^29.4.3",
|
|
61
|
+
"ts-jest": "^29.0.5",
|
|
62
|
+
"typescript": "^4.9.5"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"lodash.isequal": "^4.5.0"
|
package/src/DomUtils.ts
CHANGED
|
@@ -284,10 +284,13 @@ export namespace DomUtils {
|
|
|
284
284
|
) {
|
|
285
285
|
try {
|
|
286
286
|
if (autoDetect && 'showSaveFilePicker' in globalThis) {
|
|
287
|
+
// AbortError - Use dismisses the window
|
|
287
288
|
const handle = await (globalThis as any).showSaveFilePicker({
|
|
288
289
|
suggestedName
|
|
289
290
|
});
|
|
290
291
|
|
|
292
|
+
if (!(await verifyPermission(handle, true))) return undefined;
|
|
293
|
+
|
|
291
294
|
const stream = await handle.createWritable();
|
|
292
295
|
|
|
293
296
|
if (data instanceof Blob) {
|
|
@@ -547,4 +550,38 @@ export namespace DomUtils {
|
|
|
547
550
|
|
|
548
551
|
if (element != null) element.focus();
|
|
549
552
|
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Verify file system permission
|
|
556
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
|
|
557
|
+
* @param fileHandle FileSystemHandle
|
|
558
|
+
* @param withWrite With write permission
|
|
559
|
+
* @returns Result
|
|
560
|
+
*/
|
|
561
|
+
export async function verifyPermission(
|
|
562
|
+
fileHandle: any,
|
|
563
|
+
withWrite: boolean = false
|
|
564
|
+
) {
|
|
565
|
+
if (
|
|
566
|
+
!('queryPermission' in fileHandle) ||
|
|
567
|
+
!('requestPermission' in fileHandle)
|
|
568
|
+
)
|
|
569
|
+
return false;
|
|
570
|
+
|
|
571
|
+
// FileSystemHandlePermissionDescriptor
|
|
572
|
+
const opts = { mode: withWrite ? 'readwrite' : 'read' };
|
|
573
|
+
|
|
574
|
+
// Check if we already have permission, if so, return true.
|
|
575
|
+
if ((await fileHandle.queryPermission(opts)) === 'granted') {
|
|
576
|
+
return true;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// Request permission to the file, if the user grants permission, return true.
|
|
580
|
+
if ((await fileHandle.requestPermission(opts)) === 'granted') {
|
|
581
|
+
return true;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// The user did not grant permission, return false.
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
550
587
|
}
|
package/src/Utils.ts
CHANGED
|
@@ -3,9 +3,30 @@ import isEqual from 'lodash.isequal';
|
|
|
3
3
|
|
|
4
4
|
declare global {
|
|
5
5
|
interface String {
|
|
6
|
+
/**
|
|
7
|
+
* Check the input string contains Chinese character or not
|
|
8
|
+
* @param this Input
|
|
9
|
+
* @param test Test string
|
|
10
|
+
*/
|
|
11
|
+
containChinese(this: string): boolean;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Check the input string contains Korean character or not
|
|
15
|
+
* @param this Input
|
|
16
|
+
* @param test Test string
|
|
17
|
+
*/
|
|
18
|
+
containKorean(this: string): boolean;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Check the input string contains Japanese character or not
|
|
22
|
+
* @param this Input
|
|
23
|
+
* @param test Test string
|
|
24
|
+
*/
|
|
25
|
+
containJapanese(this: string): boolean;
|
|
26
|
+
|
|
6
27
|
/**
|
|
7
28
|
* Format string with parameters
|
|
8
|
-
* @param this
|
|
29
|
+
* @param this Input template
|
|
9
30
|
* @param parameters Parameters to fill the template
|
|
10
31
|
*/
|
|
11
32
|
format(this: string, ...parameters: string[]): string;
|
|
@@ -51,6 +72,23 @@ declare global {
|
|
|
51
72
|
}
|
|
52
73
|
}
|
|
53
74
|
|
|
75
|
+
String.prototype.containChinese = function (this: string): boolean {
|
|
76
|
+
const regExp =
|
|
77
|
+
/[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
|
|
78
|
+
return regExp.test(this);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
String.prototype.containKorean = function (this: string): boolean {
|
|
82
|
+
const regExp =
|
|
83
|
+
/[\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff\u3400-\u4dbf]/g;
|
|
84
|
+
return regExp.test(this);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
String.prototype.containJapanese = function (this: string): boolean {
|
|
88
|
+
const regExp = /[\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf]/g;
|
|
89
|
+
return regExp.test(this);
|
|
90
|
+
};
|
|
91
|
+
|
|
54
92
|
String.prototype.format = function (
|
|
55
93
|
this: string,
|
|
56
94
|
...parameters: string[]
|