@etsoo/shared 1.2.7 → 1.2.9
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 +4 -0
- package/__tests__/Utils.ts +36 -0
- package/lib/cjs/NumberUtils.js +1 -1
- package/lib/cjs/Utils.d.ts +6 -0
- package/lib/cjs/Utils.js +38 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/types/DataError.d.ts +17 -0
- package/lib/cjs/types/DataError.js +22 -0
- package/lib/cjs/types/ParsedPath.d.ts +10 -0
- package/lib/cjs/types/ParsedPath.js +2 -0
- package/lib/mjs/NumberUtils.js +1 -1
- package/lib/mjs/Utils.d.ts +6 -0
- package/lib/mjs/Utils.js +38 -0
- package/lib/mjs/index.d.ts +1 -0
- package/lib/mjs/index.js +1 -0
- package/lib/mjs/types/DataError.d.ts +17 -0
- package/lib/mjs/types/DataError.js +18 -0
- package/lib/mjs/types/ParsedPath.d.ts +10 -0
- package/lib/mjs/types/ParsedPath.js +1 -0
- package/package.json +6 -6
- package/src/NumberUtils.ts +1 -1
- package/src/Utils.ts +49 -0
- package/src/index.ts +1 -0
- package/src/types/DataError.ts +25 -0
- package/src/types/ParsedPath.ts +10 -0
package/README.md
CHANGED
|
@@ -41,6 +41,9 @@ Content disposition of HTTP
|
|
|
41
41
|
|Methods||
|
|
42
42
|
|format|Format to standard output|
|
|
43
43
|
|
|
44
|
+
## DataError
|
|
45
|
+
Error with custom data
|
|
46
|
+
|
|
44
47
|
## EColor
|
|
45
48
|
Etsoo implmented Color
|
|
46
49
|
|
|
@@ -284,6 +287,7 @@ String and other related utilities
|
|
|
284
287
|
|objectEqual|Test two objects are equal or not|
|
|
285
288
|
|objectKeys|Get two object's unqiue properties|
|
|
286
289
|
|objectUpdated|Get the new object's updated fields contrast to the previous object|
|
|
290
|
+
|parsePath|Parse path similar with node.js path.parse|
|
|
287
291
|
|parseString|Parse string (JSON) to specific type|
|
|
288
292
|
|removeNonLetters|Remove non letters (0-9, a-z, A-Z)|
|
|
289
293
|
|replaceNullOrEmpty|Replace null or empty with default value|
|
package/__tests__/Utils.ts
CHANGED
|
@@ -245,6 +245,42 @@ test('Tests for getResult', () => {
|
|
|
245
245
|
expect(valueResult).toBe(5);
|
|
246
246
|
});
|
|
247
247
|
|
|
248
|
+
test('Tests for parsePath, file only', () => {
|
|
249
|
+
const result = Utils.parsePath('a.jpg');
|
|
250
|
+
expect(result.root).toBe('');
|
|
251
|
+
expect(result.dir).toBe('');
|
|
252
|
+
expect(result.base).toBe('a.jpg');
|
|
253
|
+
expect(result.ext).toBe('.jpg');
|
|
254
|
+
expect(result.name).toBe('a');
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
test('Tests for parsePath, root file only', () => {
|
|
258
|
+
const result = Utils.parsePath('/a.JPG');
|
|
259
|
+
expect(result.root).toBe('/');
|
|
260
|
+
expect(result.dir).toBe('/');
|
|
261
|
+
expect(result.base).toBe('a.JPG');
|
|
262
|
+
expect(result.ext).toBe('.JPG');
|
|
263
|
+
expect(result.name).toBe('a');
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test('Tests for parsePath, Linux path', () => {
|
|
267
|
+
const result = Utils.parsePath('/home/user/dir/file.txt');
|
|
268
|
+
expect(result.root).toBe('/');
|
|
269
|
+
expect(result.dir).toBe('/home/user/dir');
|
|
270
|
+
expect(result.base).toBe('file.txt');
|
|
271
|
+
expect(result.ext).toBe('.txt');
|
|
272
|
+
expect(result.name).toBe('file');
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test('Tests for parsePath, Windows path', () => {
|
|
276
|
+
const result = Utils.parsePath('C:\\path\\dir\\file.txt');
|
|
277
|
+
expect(result.root).toBe('C:\\');
|
|
278
|
+
expect(result.dir).toBe('C:\\path\\dir');
|
|
279
|
+
expect(result.base).toBe('file.txt');
|
|
280
|
+
expect(result.ext).toBe('.txt');
|
|
281
|
+
expect(result.name).toBe('file');
|
|
282
|
+
});
|
|
283
|
+
|
|
248
284
|
test('Tests for sortByFavor', () => {
|
|
249
285
|
const items = [1, 2, 3, 4, 5, 6, 7];
|
|
250
286
|
expect(Utils.sortByFavor(items, [5, 1, 3])).toStrictEqual([
|
package/lib/cjs/NumberUtils.js
CHANGED
|
@@ -60,7 +60,7 @@ var NumberUtils;
|
|
|
60
60
|
const i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
|
|
61
61
|
return ((size / Math.pow(1024, i)).toFixed(fractionDigits) +
|
|
62
62
|
' ' +
|
|
63
|
-
['B', 'KB', 'MB', 'GB', 'TB'][i]);
|
|
63
|
+
['B', 'KB', 'MB', 'GB', 'TB', 'PB'][i]);
|
|
64
64
|
}
|
|
65
65
|
NumberUtils.formatFileSize = formatFileSize;
|
|
66
66
|
/**
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
|
+
import { ParsedPath } from './types/ParsedPath';
|
|
2
3
|
declare global {
|
|
3
4
|
interface String {
|
|
4
5
|
/**
|
|
@@ -247,6 +248,11 @@ export declare namespace Utils {
|
|
|
247
248
|
* @param firstOnly Only convert the first word to upper case
|
|
248
249
|
*/
|
|
249
250
|
const snakeNameToWord: (name: string, firstOnly?: boolean) => string;
|
|
251
|
+
/**
|
|
252
|
+
* Parse path similar with node.js path.parse
|
|
253
|
+
* @param path Input path
|
|
254
|
+
*/
|
|
255
|
+
const parsePath: (path: string) => ParsedPath;
|
|
250
256
|
/**
|
|
251
257
|
* Sort array by favored values
|
|
252
258
|
* @param items Items
|
package/lib/cjs/Utils.js
CHANGED
|
@@ -484,6 +484,44 @@ var Utils;
|
|
|
484
484
|
return -1;
|
|
485
485
|
return n1 - n2;
|
|
486
486
|
}
|
|
487
|
+
/**
|
|
488
|
+
* Parse path similar with node.js path.parse
|
|
489
|
+
* @param path Input path
|
|
490
|
+
*/
|
|
491
|
+
Utils.parsePath = (path) => {
|
|
492
|
+
// Two formats or mixed
|
|
493
|
+
// /home/user/dir/file.txt
|
|
494
|
+
// C:\\path\\dir\\file.txt
|
|
495
|
+
const lastIndex = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
|
|
496
|
+
let root = '', dir = '', base, ext, name;
|
|
497
|
+
if (lastIndex === -1) {
|
|
498
|
+
base = path;
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
base = path.substring(lastIndex + 1);
|
|
502
|
+
const index1 = path.indexOf('/');
|
|
503
|
+
const index2 = path.indexOf('\\');
|
|
504
|
+
const index = index1 === -1
|
|
505
|
+
? index2
|
|
506
|
+
: index2 === -1
|
|
507
|
+
? index1
|
|
508
|
+
: Math.min(index1, index2);
|
|
509
|
+
root = path.substring(0, index + 1);
|
|
510
|
+
dir = path.substring(0, lastIndex);
|
|
511
|
+
if (dir === '')
|
|
512
|
+
dir = root;
|
|
513
|
+
}
|
|
514
|
+
const extIndex = base.lastIndexOf('.');
|
|
515
|
+
if (extIndex === -1) {
|
|
516
|
+
name = base;
|
|
517
|
+
ext = '';
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
name = base.substring(0, extIndex);
|
|
521
|
+
ext = base.substring(extIndex);
|
|
522
|
+
}
|
|
523
|
+
return { root, dir, base, ext, name };
|
|
524
|
+
};
|
|
487
525
|
/**
|
|
488
526
|
* Sort array by favored values
|
|
489
527
|
* @param items Items
|
package/lib/cjs/index.d.ts
CHANGED
package/lib/cjs/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./types/ContentDisposition"), exports);
|
|
18
|
+
__exportStar(require("./types/DataError"), exports);
|
|
18
19
|
__exportStar(require("./types/DelayedExecutorType"), exports);
|
|
19
20
|
__exportStar(require("./types/EColor"), exports);
|
|
20
21
|
__exportStar(require("./types/EHistory"), exports);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error with custom data
|
|
3
|
+
* Other information can be hold by 'name', 'cause', and 'stack' property
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class DataError<T = Record<string, unknown>> extends Error {
|
|
7
|
+
/**
|
|
8
|
+
* Custom data
|
|
9
|
+
*/
|
|
10
|
+
readonly data: T;
|
|
11
|
+
/**
|
|
12
|
+
* Constructor
|
|
13
|
+
* @param message Error message
|
|
14
|
+
* @param data Custom data
|
|
15
|
+
*/
|
|
16
|
+
constructor(message: string, data: T);
|
|
17
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Error with custom data
|
|
6
|
+
* Other information can be hold by 'name', 'cause', and 'stack' property
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
class DataError extends Error {
|
|
10
|
+
/**
|
|
11
|
+
* Constructor
|
|
12
|
+
* @param message Error message
|
|
13
|
+
* @param data Custom data
|
|
14
|
+
*/
|
|
15
|
+
constructor(message, data) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.data = data;
|
|
18
|
+
// Set the prototype explicitly to ensure instanceof works correctly
|
|
19
|
+
Object.setPrototypeOf(this, DataError.prototype);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.DataError = DataError;
|
package/lib/mjs/NumberUtils.js
CHANGED
|
@@ -57,7 +57,7 @@ export var NumberUtils;
|
|
|
57
57
|
const i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
|
|
58
58
|
return ((size / Math.pow(1024, i)).toFixed(fractionDigits) +
|
|
59
59
|
' ' +
|
|
60
|
-
['B', 'KB', 'MB', 'GB', 'TB'][i]);
|
|
60
|
+
['B', 'KB', 'MB', 'GB', 'TB', 'PB'][i]);
|
|
61
61
|
}
|
|
62
62
|
NumberUtils.formatFileSize = formatFileSize;
|
|
63
63
|
/**
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
|
+
import { ParsedPath } from './types/ParsedPath';
|
|
2
3
|
declare global {
|
|
3
4
|
interface String {
|
|
4
5
|
/**
|
|
@@ -247,6 +248,11 @@ export declare namespace Utils {
|
|
|
247
248
|
* @param firstOnly Only convert the first word to upper case
|
|
248
249
|
*/
|
|
249
250
|
const snakeNameToWord: (name: string, firstOnly?: boolean) => string;
|
|
251
|
+
/**
|
|
252
|
+
* Parse path similar with node.js path.parse
|
|
253
|
+
* @param path Input path
|
|
254
|
+
*/
|
|
255
|
+
const parsePath: (path: string) => ParsedPath;
|
|
250
256
|
/**
|
|
251
257
|
* Sort array by favored values
|
|
252
258
|
* @param items Items
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -478,6 +478,44 @@ export var Utils;
|
|
|
478
478
|
return -1;
|
|
479
479
|
return n1 - n2;
|
|
480
480
|
}
|
|
481
|
+
/**
|
|
482
|
+
* Parse path similar with node.js path.parse
|
|
483
|
+
* @param path Input path
|
|
484
|
+
*/
|
|
485
|
+
Utils.parsePath = (path) => {
|
|
486
|
+
// Two formats or mixed
|
|
487
|
+
// /home/user/dir/file.txt
|
|
488
|
+
// C:\\path\\dir\\file.txt
|
|
489
|
+
const lastIndex = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
|
|
490
|
+
let root = '', dir = '', base, ext, name;
|
|
491
|
+
if (lastIndex === -1) {
|
|
492
|
+
base = path;
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
base = path.substring(lastIndex + 1);
|
|
496
|
+
const index1 = path.indexOf('/');
|
|
497
|
+
const index2 = path.indexOf('\\');
|
|
498
|
+
const index = index1 === -1
|
|
499
|
+
? index2
|
|
500
|
+
: index2 === -1
|
|
501
|
+
? index1
|
|
502
|
+
: Math.min(index1, index2);
|
|
503
|
+
root = path.substring(0, index + 1);
|
|
504
|
+
dir = path.substring(0, lastIndex);
|
|
505
|
+
if (dir === '')
|
|
506
|
+
dir = root;
|
|
507
|
+
}
|
|
508
|
+
const extIndex = base.lastIndexOf('.');
|
|
509
|
+
if (extIndex === -1) {
|
|
510
|
+
name = base;
|
|
511
|
+
ext = '';
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
name = base.substring(0, extIndex);
|
|
515
|
+
ext = base.substring(extIndex);
|
|
516
|
+
}
|
|
517
|
+
return { root, dir, base, ext, name };
|
|
518
|
+
};
|
|
481
519
|
/**
|
|
482
520
|
* Sort array by favored values
|
|
483
521
|
* @param items Items
|
package/lib/mjs/index.d.ts
CHANGED
package/lib/mjs/index.js
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error with custom data
|
|
3
|
+
* Other information can be hold by 'name', 'cause', and 'stack' property
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class DataError<T = Record<string, unknown>> extends Error {
|
|
7
|
+
/**
|
|
8
|
+
* Custom data
|
|
9
|
+
*/
|
|
10
|
+
readonly data: T;
|
|
11
|
+
/**
|
|
12
|
+
* Constructor
|
|
13
|
+
* @param message Error message
|
|
14
|
+
* @param data Custom data
|
|
15
|
+
*/
|
|
16
|
+
constructor(message: string, data: T);
|
|
17
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error with custom data
|
|
3
|
+
* Other information can be hold by 'name', 'cause', and 'stack' property
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export class DataError extends Error {
|
|
7
|
+
/**
|
|
8
|
+
* Constructor
|
|
9
|
+
* @param message Error message
|
|
10
|
+
* @param data Custom data
|
|
11
|
+
*/
|
|
12
|
+
constructor(message, data) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.data = data;
|
|
15
|
+
// Set the prototype explicitly to ensure instanceof works correctly
|
|
16
|
+
Object.setPrototypeOf(this, DataError.prototype);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.9",
|
|
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.5.
|
|
57
|
+
"@types/jest": "^29.5.4",
|
|
58
58
|
"@types/lodash.isequal": "^4.5.6",
|
|
59
|
-
"jest": "^29.
|
|
60
|
-
"jest-environment-jsdom": "^29.
|
|
61
|
-
"ts-jest": "^29.1.
|
|
62
|
-
"typescript": "^5.
|
|
59
|
+
"jest": "^29.6.4",
|
|
60
|
+
"jest-environment-jsdom": "^29.6.4",
|
|
61
|
+
"ts-jest": "^29.1.1",
|
|
62
|
+
"typescript": "^5.2.2"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"lodash.isequal": "^4.5.0"
|
package/src/NumberUtils.ts
CHANGED
package/src/Utils.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
2
|
import isEqual from 'lodash.isequal';
|
|
3
3
|
import { DateUtils } from './DateUtils';
|
|
4
|
+
import { ParsedPath } from './types/ParsedPath';
|
|
4
5
|
|
|
5
6
|
declare global {
|
|
6
7
|
interface String {
|
|
@@ -672,6 +673,54 @@ export namespace Utils {
|
|
|
672
673
|
return n1 - n2;
|
|
673
674
|
}
|
|
674
675
|
|
|
676
|
+
/**
|
|
677
|
+
* Parse path similar with node.js path.parse
|
|
678
|
+
* @param path Input path
|
|
679
|
+
*/
|
|
680
|
+
export const parsePath = (path: string): ParsedPath => {
|
|
681
|
+
// Two formats or mixed
|
|
682
|
+
// /home/user/dir/file.txt
|
|
683
|
+
// C:\\path\\dir\\file.txt
|
|
684
|
+
const lastIndex = Math.max(
|
|
685
|
+
path.lastIndexOf('/'),
|
|
686
|
+
path.lastIndexOf('\\')
|
|
687
|
+
);
|
|
688
|
+
|
|
689
|
+
let root = '',
|
|
690
|
+
dir = '',
|
|
691
|
+
base: string,
|
|
692
|
+
ext: string,
|
|
693
|
+
name: string;
|
|
694
|
+
|
|
695
|
+
if (lastIndex === -1) {
|
|
696
|
+
base = path;
|
|
697
|
+
} else {
|
|
698
|
+
base = path.substring(lastIndex + 1);
|
|
699
|
+
const index1 = path.indexOf('/');
|
|
700
|
+
const index2 = path.indexOf('\\');
|
|
701
|
+
const index =
|
|
702
|
+
index1 === -1
|
|
703
|
+
? index2
|
|
704
|
+
: index2 === -1
|
|
705
|
+
? index1
|
|
706
|
+
: Math.min(index1, index2);
|
|
707
|
+
root = path.substring(0, index + 1);
|
|
708
|
+
dir = path.substring(0, lastIndex);
|
|
709
|
+
if (dir === '') dir = root;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
const extIndex = base.lastIndexOf('.');
|
|
713
|
+
if (extIndex === -1) {
|
|
714
|
+
name = base;
|
|
715
|
+
ext = '';
|
|
716
|
+
} else {
|
|
717
|
+
name = base.substring(0, extIndex);
|
|
718
|
+
ext = base.substring(extIndex);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
return { root, dir, base, ext, name };
|
|
722
|
+
};
|
|
723
|
+
|
|
675
724
|
/**
|
|
676
725
|
* Sort array by favored values
|
|
677
726
|
* @param items Items
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error with custom data
|
|
3
|
+
* Other information can be hold by 'name', 'cause', and 'stack' property
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export class DataError<T = Record<string, unknown>> extends Error {
|
|
7
|
+
/**
|
|
8
|
+
* Custom data
|
|
9
|
+
*/
|
|
10
|
+
public readonly data: T;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Constructor
|
|
14
|
+
* @param message Error message
|
|
15
|
+
* @param data Custom data
|
|
16
|
+
*/
|
|
17
|
+
constructor(message: string, data: T) {
|
|
18
|
+
super(message);
|
|
19
|
+
|
|
20
|
+
this.data = data;
|
|
21
|
+
|
|
22
|
+
// Set the prototype explicitly to ensure instanceof works correctly
|
|
23
|
+
Object.setPrototypeOf(this, DataError.prototype);
|
|
24
|
+
}
|
|
25
|
+
}
|