-
Notifications
You must be signed in to change notification settings - Fork 2
/
TypeConverter.ts
191 lines (173 loc) · 5.92 KB
/
TypeConverter.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*---------------------------------------------------------------------------------------------
* 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
*/
// cSpell:ignore valuetypes shortdate
import type {
Primitives,
PrimitiveValue,
PropertyDescription,
PropertyRecord,
PropertyValue,
} from "@itwin/appui-abstract";
import { PropertyValueFormat } from "@itwin/appui-abstract";
import type { ConvertedPrimitives } from "./valuetypes/ConvertedTypes";
/** Sort compare method for types that support sorting
* @public
*/
export interface SortComparer {
/** Sort function for two primitive values */
sortCompare(
valueA: Primitives.Value,
valueB: Primitives.Value,
ignoreCase?: boolean
): number;
}
/** Operators for all filterable types
* @public
*/
export interface OperatorProcessor {
/** Determines if two primitive values are equal */
isEqualTo(a: Primitives.Value, b: Primitives.Value): boolean;
/** Determines if two primitive values are not equal */
isNotEqualTo(a: Primitives.Value, b: Primitives.Value): boolean;
}
/** Operators for Numeric types, DateTime, TimeSpan, or any type that supports these comparisons
* @public
*/
export interface LessGreaterOperatorProcessor {
/** Determines if a primitive values is less than another primitive value */
isLessThan(a: Primitives.Value, b: Primitives.Value): boolean;
/** Determines if a primitive values is less than or equal to another primitive value */
isLessThanOrEqualTo(a: Primitives.Value, b: Primitives.Value): boolean;
/** Determines if a primitive values is greater than another primitive value */
isGreaterThan(a: Primitives.Value, b: Primitives.Value): boolean;
/** Determines if a primitive values is greater than or equal to another primitive value */
isGreaterThanOrEqualTo(a: Primitives.Value, b: Primitives.Value): boolean;
}
/** Operators for all filterable null-able types
* @public
*/
export interface NullableOperatorProcessor {
/** Determines if a primitive value is null or undefined */
isNull(value: Primitives.Value): boolean;
/** Determines if a primitive value is not null or not undefined */
isNotNull(value: Primitives.Value): boolean;
}
/**
* Type Converter base class.
* @public
*/
export abstract class TypeConverter
implements SortComparer, OperatorProcessor, NullableOperatorProcessor
{
/** Converts a primitive value to a string */
public convertToString(value?: Primitives.Value): string | Promise<string> {
if (value === undefined) return "";
// eslint-disable-next-line @typescript-eslint/no-base-to-string
return value.toString();
}
/** Default implementation just calls convertToString with no options */
public convertToStringWithOptions(
value?: Primitives.Value,
_options?: { [key: string]: any }
): string | Promise<string> {
return this.convertToString(value);
}
/** Converts a string to a primitive value */
public convertFromString(
_value: string
):
| ConvertedPrimitives.Value
| undefined
| Promise<ConvertedPrimitives.Value | undefined> {
return undefined;
}
/** Default implementation just calls convertFromString with no options */
public convertFromStringWithOptions(
value: string,
_options?: { [key: string]: any }
):
| ConvertedPrimitives.Value
| undefined
| Promise<ConvertedPrimitives.Value | undefined> {
return this.convertFromString(value);
}
/** Converts a value associated with a property description to a string */
public convertPropertyToString(
propertyDescription: PropertyDescription,
value?: Primitives.Value
): string | Promise<string> {
return this.convertToStringWithOptions(
value,
propertyDescription.converter?.options
);
}
/** Converts a string with a property record to a property value */
public async convertFromStringToPropertyValue(
value: string,
propertyRecord?: PropertyRecord
): Promise<PropertyValue> {
const converterOptions =
propertyRecord && propertyRecord.property.converter
? propertyRecord.property.converter.options
: undefined;
const stringValue = await this.convertFromStringWithOptions(
value,
converterOptions
);
const propertyValue: PrimitiveValue = {
valueFormat: PropertyValueFormat.Primitive,
value: stringValue,
displayValue: "",
};
return propertyValue;
}
/** Sort function for two primitive values */
public abstract sortCompare(
valueA: Primitives.Value,
valueB: Primitives.Value,
_ignoreCase?: boolean
): number;
/** Determines if two primitive values are equal */
public isEqualTo(
valueA: Primitives.Value,
valueB: Primitives.Value
): boolean {
return valueA === valueB;
}
/** Determines if two primitive values are not equal */
public isNotEqualTo(
valueA: Primitives.Value,
valueB: Primitives.Value
): boolean {
return valueA !== valueB;
}
/** Determines if a primitive value is null or undefined */
public isNull(value: Primitives.Value): boolean {
return value === null || value === undefined;
}
/** Determines if a primitive value is not null or not undefined */
public isNotNull(value: Primitives.Value): boolean {
return value !== null && value !== undefined;
}
/** Determines if the converter is for a string type */
public get isStringType(): boolean {
return false;
}
/** Determines if the converter is for a numeric type */
public get isLessGreaterType(): boolean {
return false;
}
/** Determines if the converter is for a boolean type */
public get isBooleanType(): boolean {
return false;
}
/** Determines if the converter is for a nullable type */
public get isNullableType(): boolean {
return true;
}
}