@etsoo/shared 1.2.8 → 1.2.10

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 CHANGED
@@ -136,13 +136,16 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
136
136
  |Func<R>|Function type, R is return type|
137
137
  |HAlign|Horizontal align|
138
138
  |HAlignEnum|Horizontal align enum|
139
+ |IdDefaultType|Id default type|
139
140
  |IdType|Number and string combination id type|
140
141
  |IdItem|Item with id or id generator|
141
142
  |IdLabelItem|Item with id and label|
142
143
  |IdLabelType|Item with id and label dynamic type|
143
144
  |IdNameItem|Item with id and name|
145
+ |IdTitleItem|Item with id and title|
144
146
  |KeyCollection|Key collection, like { key1: {}, key2: {} }|
145
147
  |Keys|Get specific type keys|
148
+ |LabelDefaultType|Label default type|
146
149
  |MConstructor|Mixins constructor|
147
150
  |ObjType|Generic object type|
148
151
  |Optional|Make properties optional|
@@ -155,6 +158,7 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
155
158
  |SimpleObject|Simple object, string key, simple type and null value Record|
156
159
  |StringDictionary|String key, string value Record|
157
160
  |StringRecord|String key, unknown value Record|
161
+ |TitleDefaultType|Title default type|
158
162
  |VAlign|Vertical align|
159
163
  |VAlignEnum|Vertical align enum|
160
164
  |Methods||
@@ -287,6 +291,7 @@ String and other related utilities
287
291
  |objectEqual|Test two objects are equal or not|
288
292
  |objectKeys|Get two object's unqiue properties|
289
293
  |objectUpdated|Get the new object's updated fields contrast to the previous object|
294
+ |parsePath|Parse path similar with node.js path.parse|
290
295
  |parseString|Parse string (JSON) to specific type|
291
296
  |removeNonLetters|Remove non letters (0-9, a-z, A-Z)|
292
297
  |replaceNullOrEmpty|Replace null or empty with default value|
@@ -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([
@@ -224,6 +224,15 @@ export declare namespace DataTypes {
224
224
  */
225
225
  name: string;
226
226
  };
227
+ /**
228
+ * Item with id and title property
229
+ */
230
+ type IdTitleItem<T extends IdType = number> = IdItem<T> & {
231
+ /**
232
+ * title field
233
+ */
234
+ title: string;
235
+ };
227
236
  /**
228
237
  * Item with id and label dynamic type
229
238
  */
@@ -396,7 +405,7 @@ export type ListType = DataTypes.IdLabelItem<number>;
396
405
  */
397
406
  export type ListType1 = DataTypes.IdLabelItem<string>;
398
407
  /**
399
- * List item with compatible id and name / label
408
+ * List item with compatible id and name / label / title
400
409
  */
401
410
  export type ListType2 = {
402
411
  id: DataTypes.IdType;
@@ -404,6 +413,8 @@ export type ListType2 = {
404
413
  label: string;
405
414
  } | {
406
415
  name: string;
416
+ } | {
417
+ title: string;
407
418
  });
408
419
  /**
409
420
  * Id default type
@@ -417,3 +428,9 @@ export type IdDefaultType<T extends object> = T extends {
417
428
  export type LabelDefaultType<T extends object> = T extends {
418
429
  label: string;
419
430
  } ? DataTypes.Keys<T, string> & 'label' : DataTypes.Keys<T, string>;
431
+ /**
432
+ * Title default type
433
+ */
434
+ export type TitleDefaultType<T extends object> = T extends {
435
+ title: string;
436
+ } ? DataTypes.Keys<T, string> & 'title' : DataTypes.Keys<T, string>;
@@ -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
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Similar result with node.js path.parse(path)
3
+ */
4
+ export type ParsedPath = {
5
+ root: string;
6
+ dir: string;
7
+ base: string;
8
+ ext: string;
9
+ name: string;
10
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -224,6 +224,15 @@ export declare namespace DataTypes {
224
224
  */
225
225
  name: string;
226
226
  };
227
+ /**
228
+ * Item with id and title property
229
+ */
230
+ type IdTitleItem<T extends IdType = number> = IdItem<T> & {
231
+ /**
232
+ * title field
233
+ */
234
+ title: string;
235
+ };
227
236
  /**
228
237
  * Item with id and label dynamic type
229
238
  */
@@ -396,7 +405,7 @@ export type ListType = DataTypes.IdLabelItem<number>;
396
405
  */
397
406
  export type ListType1 = DataTypes.IdLabelItem<string>;
398
407
  /**
399
- * List item with compatible id and name / label
408
+ * List item with compatible id and name / label / title
400
409
  */
401
410
  export type ListType2 = {
402
411
  id: DataTypes.IdType;
@@ -404,6 +413,8 @@ export type ListType2 = {
404
413
  label: string;
405
414
  } | {
406
415
  name: string;
416
+ } | {
417
+ title: string;
407
418
  });
408
419
  /**
409
420
  * Id default type
@@ -417,3 +428,9 @@ export type IdDefaultType<T extends object> = T extends {
417
428
  export type LabelDefaultType<T extends object> = T extends {
418
429
  label: string;
419
430
  } ? DataTypes.Keys<T, string> & 'label' : DataTypes.Keys<T, string>;
431
+ /**
432
+ * Title default type
433
+ */
434
+ export type TitleDefaultType<T extends object> = T extends {
435
+ title: string;
436
+ } ? DataTypes.Keys<T, string> & 'title' : DataTypes.Keys<T, string>;
@@ -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,10 @@
1
+ /**
2
+ * Similar result with node.js path.parse(path)
3
+ */
4
+ export type ParsedPath = {
5
+ root: string;
6
+ dir: string;
7
+ base: string;
8
+ ext: string;
9
+ name: string;
10
+ };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.8",
3
+ "version": "1.2.10",
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.3",
57
+ "@types/jest": "^29.5.4",
58
58
  "@types/lodash.isequal": "^4.5.6",
59
- "jest": "^29.6.1",
60
- "jest-environment-jsdom": "^29.6.1",
59
+ "jest": "^29.6.4",
60
+ "jest-environment-jsdom": "^29.6.4",
61
61
  "ts-jest": "^29.1.1",
62
- "typescript": "^5.1.6"
62
+ "typescript": "^5.2.2"
63
63
  },
64
64
  "dependencies": {
65
65
  "lodash.isequal": "^4.5.0"
package/src/DataTypes.ts CHANGED
@@ -278,6 +278,16 @@ export namespace DataTypes {
278
278
  name: string;
279
279
  };
280
280
 
281
+ /**
282
+ * Item with id and title property
283
+ */
284
+ export type IdTitleItem<T extends IdType = number> = IdItem<T> & {
285
+ /**
286
+ * title field
287
+ */
288
+ title: string;
289
+ };
290
+
281
291
  /**
282
292
  * Item with id and label dynamic type
283
293
  */
@@ -751,11 +761,11 @@ export type ListType = DataTypes.IdLabelItem<number>;
751
761
  export type ListType1 = DataTypes.IdLabelItem<string>;
752
762
 
753
763
  /**
754
- * List item with compatible id and name / label
764
+ * List item with compatible id and name / label / title
755
765
  */
756
766
  export type ListType2 = {
757
767
  id: DataTypes.IdType;
758
- } & ({ label: string } | { name: string });
768
+ } & ({ label: string } | { name: string } | { title: string });
759
769
 
760
770
  /**
761
771
  * Id default type
@@ -770,3 +780,10 @@ export type IdDefaultType<T extends object> = T extends { id: number | string }
770
780
  export type LabelDefaultType<T extends object> = T extends { label: string }
771
781
  ? DataTypes.Keys<T, string> & 'label'
772
782
  : DataTypes.Keys<T, string>;
783
+
784
+ /**
785
+ * Title default type
786
+ */
787
+ export type TitleDefaultType<T extends object> = T extends { title: string }
788
+ ? DataTypes.Keys<T, string> & 'title'
789
+ : DataTypes.Keys<T, string>;
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
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Similar result with node.js path.parse(path)
3
+ */
4
+ export type ParsedPath = {
5
+ root: string;
6
+ dir: string;
7
+ base: string;
8
+ ext: string;
9
+ name: string;
10
+ };