@etsoo/shared 1.2.8 → 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 +1 -0
- package/__tests__/Utils.ts +36 -0
- package/lib/cjs/Utils.d.ts +6 -0
- package/lib/cjs/Utils.js +38 -0
- package/lib/cjs/types/ParsedPath.d.ts +10 -0
- package/lib/cjs/types/ParsedPath.js +2 -0
- package/lib/mjs/Utils.d.ts +6 -0
- package/lib/mjs/Utils.js +38 -0
- package/lib/mjs/types/ParsedPath.d.ts +10 -0
- package/lib/mjs/types/ParsedPath.js +1 -0
- package/package.json +5 -5
- package/src/Utils.ts +49 -0
- package/src/types/ParsedPath.ts +10 -0
package/README.md
CHANGED
|
@@ -287,6 +287,7 @@ String and other related utilities
|
|
|
287
287
|
|objectEqual|Test two objects are equal or not|
|
|
288
288
|
|objectKeys|Get two object's unqiue properties|
|
|
289
289
|
|objectUpdated|Get the new object's updated fields contrast to the previous object|
|
|
290
|
+
|parsePath|Parse path similar with node.js path.parse|
|
|
290
291
|
|parseString|Parse string (JSON) to specific type|
|
|
291
292
|
|removeNonLetters|Remove non letters (0-9, a-z, A-Z)|
|
|
292
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/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/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
|
|
@@ -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.6.
|
|
60
|
-
"jest-environment-jsdom": "^29.6.
|
|
59
|
+
"jest": "^29.6.4",
|
|
60
|
+
"jest-environment-jsdom": "^29.6.4",
|
|
61
61
|
"ts-jest": "^29.1.1",
|
|
62
|
-
"typescript": "^5.
|
|
62
|
+
"typescript": "^5.2.2"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"lodash.isequal": "^4.5.0"
|
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
|