zavadil-ts-common 1.2.68 → 1.2.70

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.
@@ -1,4 +1,5 @@
1
1
  export declare class StringUtil {
2
+ static isString(s: any): boolean;
2
3
  static toString(s: any): string;
3
4
  static isEmpty(str: string | null | undefined): str is null | undefined;
4
5
  static notEmpty(str: string | null | undefined): str is string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zavadil-ts-common",
3
- "version": "1.2.68",
3
+ "version": "1.2.70",
4
4
  "description": "Common types and components for Typescript UI apps.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -1,11 +1,17 @@
1
+ import {ObjectUtil} from "./ObjectUtil";
2
+
1
3
  export class StringUtil {
2
4
 
5
+ static isString(s: any): boolean {
6
+ return typeof s === 'string';
7
+ }
8
+
3
9
  static toString(s: any): string {
4
- return typeof s === 'string' ? s : s?.toString?.() ?? '';
10
+ return StringUtil.isString(s) ? s : s?.toString?.() ?? '';
5
11
  }
6
12
 
7
13
  static isEmpty(str: string | null | undefined): str is null | undefined {
8
- if (typeof str !== 'string') return StringUtil.isEmpty(StringUtil.toString(str));
14
+ if (ObjectUtil.isEmpty(str)) return true;
9
15
  return str.length === 0;
10
16
  }
11
17
 
@@ -40,27 +46,22 @@ export class StringUtil {
40
46
 
41
47
  static trimLeadingSlashes(str: string | null): string {
42
48
  if (this.isEmpty(str)) return '';
43
-
44
- // @ts-ignore
45
- return str.replace(/^\//g, '');
49
+ return StringUtil.toString(str).replace(/^\//g, '');
46
50
  }
47
51
 
48
52
  static trimTrailingSlashes(str: string | null): string {
49
53
  if (this.isEmpty(str)) return '';
50
-
51
- // @ts-ignore
52
- return str.replace(/\/$/g, '');
54
+ return StringUtil.toString(str).replace(/\/$/g, '');
53
55
  }
54
56
 
55
57
  static trimSlashes(str: string | null): string {
56
58
  if (this.isEmpty(str)) return '';
57
-
58
- // @ts-ignore
59
- return str.replace(/^\/|\/$/g, '');
59
+ return StringUtil.toString(str).replace(/^\/|\/$/g, '');
60
60
  }
61
61
 
62
62
  static safeTruncate(str: string | null | undefined, len: number, ellipsis: string = ''): string {
63
63
  if (StringUtil.isEmpty(str) || !str) return '';
64
+ str = StringUtil.toString(str);
64
65
  if (str.length <= len) return String(str);
65
66
  return str.substring(0, len - ellipsis.length) + ellipsis;
66
67
  }
@@ -70,18 +71,18 @@ export class StringUtil {
70
71
  }
71
72
 
72
73
  static safeTrim(str: string | null | undefined): string {
73
- if (StringUtil.isEmpty(str) || !str) return '';
74
- return str.trim();
74
+ if (StringUtil.isEmpty(str)) return '';
75
+ return StringUtil.toString(str).trim();
75
76
  }
76
77
 
77
78
  static safeLowercase(str: string | null | undefined): string {
78
79
  if (StringUtil.isEmpty(str) || !str) return '';
79
- return str.toLowerCase();
80
+ return StringUtil.toString(str).toLowerCase();
80
81
  }
81
82
 
82
83
  static safeUppercase(str: string | null | undefined): string {
83
84
  if (StringUtil.isEmpty(str) || !str) return '';
84
- return str.toUpperCase();
85
+ return StringUtil.toString(str).toUpperCase();
85
86
  }
86
87
 
87
88
  static toBigInt(str: string | null): bigint | null {