/
StringTypeConverter.ts
160 lines (136 loc) · 4.58 KB
/
StringTypeConverter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module TypeConverters
*/
import type { Primitives } from "@itwin/appui-abstract";
import { TypeConverter } from "./TypeConverter";
/** Operators for string types
* @public
*/
export interface StringOperatorProcessor {
/** Determines if one string starts with another string */
startsWith(a: string, b: string, caseSensitive: boolean): boolean;
/** Determines if one string ends with another string */
endsWith(a: string, b: string, caseSensitive: boolean): boolean;
/** Determines if one string contains another string */
contains(a: string, b: string, caseSensitive: boolean): boolean;
/** Determines if one string does not contain another string */
doesNotContain(a: string, b: string, caseSensitive: boolean): boolean;
/** Determines if one string is contained within another string */
isContainedIn(a: string, b: string, caseSensitive: boolean): boolean;
/** Determines if one string is not contained within another string */
isNotContainedIn(a: string, b: string, caseSensitive: boolean): boolean;
/** Determines if a string is empty */
isEmpty(a: string): boolean;
/** Determines if a string is not empty */
isNotEmpty(a: string): boolean;
}
/**
* String Type Converter.
* @public
*/
export class StringTypeConverter
extends TypeConverter
implements StringOperatorProcessor
{
public override convertToString(value?: Primitives.String) {
return value ? value.toString() : "";
}
public override convertFromString(value: string) {
return value;
}
public sortCompare(
valueA: Primitives.String,
valueB: Primitives.String,
ignoreCase?: boolean
): number {
if (!this.checkArgTypes(valueA, valueB)) return 0;
if (ignoreCase)
return valueA
.toLocaleLowerCase()
.localeCompare(valueB.toLocaleLowerCase());
else return valueA.localeCompare(valueB);
}
public override get isStringType(): boolean {
return true;
}
public startsWith(
valueA: string,
valueB: string,
caseSensitive: boolean
): boolean {
if (!valueA || !valueB || !this.checkArgTypes(valueA, valueB)) return false;
if (caseSensitive) return valueA.substring(0, valueB.length) === valueB;
return (
valueA.toLocaleUpperCase().substring(0, valueB.length) ===
valueB.toLocaleUpperCase()
);
}
public endsWith(
valueA: string,
valueB: string,
caseSensitive: boolean
): boolean {
if (!valueA || !valueB || !this.checkArgTypes(valueA, valueB)) return false;
const position = valueA.length - valueB.length;
if (position < 0) return false;
let lastIndex: number;
if (caseSensitive) lastIndex = valueA.indexOf(valueB, position);
else
lastIndex = valueA
.toLocaleUpperCase()
.indexOf(valueB.toLocaleUpperCase(), position);
return lastIndex !== -1 && lastIndex === position;
}
public contains(
valueA: string,
valueB: string,
caseSensitive: boolean
): boolean {
if (!valueA || !valueB || !this.checkArgTypes(valueA, valueB)) return false;
if (valueB.length > valueA.length) return false;
if (caseSensitive) return valueA.indexOf(valueB, 0) !== -1;
return (
valueA.toLocaleUpperCase().indexOf(valueB.toLocaleUpperCase(), 0) !== -1
);
}
public doesNotContain(
valueA: string,
valueB: string,
caseSensitive: boolean
): boolean {
return !this.contains(valueA, valueB, caseSensitive);
}
public isContainedIn(
valueA: string,
valueB: string,
caseSensitive: boolean
): boolean {
return this.contains(valueB, valueA, caseSensitive);
}
public isNotContainedIn(
valueA: string,
valueB: string,
caseSensitive: boolean
): boolean {
return this.doesNotContain(valueB, valueA, caseSensitive);
}
public isEmpty(valueA: string): boolean {
if (!this.checkArgTypes(valueA)) return true;
return valueA.length === 0;
}
public isNotEmpty(valueA: string): boolean {
return !this.isEmpty(valueA);
}
private checkArgTypes(valueA: string, valueB?: string): boolean {
if (typeof valueA !== "string") return false;
if (valueB && typeof valueB !== "string") return false;
return true;
}
}
/*
NOTE: As this is the default type converter, it's registrations are directly in TypeConverterManager file.
*/