@fgv/ts-utils 1.2.0-alpha.2 → 1.3.0

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.
Files changed (77) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +4 -0
  3. package/brand.d.ts +2 -0
  4. package/brand.d.ts.map +1 -0
  5. package/brand.js +1 -1
  6. package/converter.d.ts +185 -73
  7. package/converter.d.ts.map +1 -0
  8. package/converter.js +104 -51
  9. package/converters.d.ts +440 -182
  10. package/converters.d.ts.map +1 -0
  11. package/converters.js +358 -209
  12. package/csvHelpers.d.ts +7 -0
  13. package/csvHelpers.d.ts.map +1 -0
  14. package/csvHelpers.js +14 -4
  15. package/extendedArray.d.ts +46 -0
  16. package/extendedArray.d.ts.map +1 -0
  17. package/extendedArray.js +53 -8
  18. package/formatter.d.ts +53 -0
  19. package/formatter.d.ts.map +1 -0
  20. package/formatter.js +27 -4
  21. package/hash.d.ts +51 -0
  22. package/hash.d.ts.map +1 -0
  23. package/hash.js +168 -0
  24. package/index.d.ts +6 -2
  25. package/index.d.ts.map +1 -0
  26. package/index.js +15 -5
  27. package/logger.d.ts +3 -2
  28. package/logger.d.ts.map +1 -0
  29. package/logger.js +12 -12
  30. package/package.json +24 -20
  31. package/rangeOf.d.ts +96 -0
  32. package/rangeOf.d.ts.map +1 -0
  33. package/rangeOf.js +81 -2
  34. package/result.d.ts +391 -39
  35. package/result.d.ts.map +1 -0
  36. package/result.js +257 -49
  37. package/ts-utils.d.ts +2451 -0
  38. package/tsdoc-metadata.json +11 -0
  39. package/utils.d.ts +49 -27
  40. package/utils.d.ts.map +1 -0
  41. package/utils.js +53 -36
  42. package/validation/boolean.d.ts +27 -0
  43. package/validation/boolean.d.ts.map +1 -0
  44. package/validation/boolean.js +59 -0
  45. package/validation/classes.d.ts +5 -1
  46. package/validation/classes.d.ts.map +1 -0
  47. package/validation/classes.js +9 -2
  48. package/validation/field.d.ts +30 -0
  49. package/validation/field.d.ts.map +1 -0
  50. package/validation/field.js +23 -5
  51. package/validation/genericValidator.d.ts +41 -22
  52. package/validation/genericValidator.d.ts.map +1 -0
  53. package/validation/genericValidator.js +25 -26
  54. package/validation/index.d.ts +5 -3
  55. package/validation/index.d.ts.map +1 -0
  56. package/validation/index.js +15 -8
  57. package/validation/number.d.ts +20 -0
  58. package/validation/number.d.ts.map +1 -0
  59. package/validation/number.js +17 -2
  60. package/validation/object.d.ts +76 -11
  61. package/validation/object.d.ts.map +1 -0
  62. package/validation/object.js +52 -14
  63. package/validation/string.d.ts +20 -0
  64. package/validation/string.d.ts.map +1 -0
  65. package/validation/string.js +17 -2
  66. package/validation/traits.d.ts +50 -1
  67. package/validation/traits.d.ts.map +1 -0
  68. package/validation/traits.js +15 -2
  69. package/validation/validator.d.ts +44 -23
  70. package/validation/validator.d.ts.map +1 -0
  71. package/validation/validator.js +1 -1
  72. package/validation/validatorBase.d.ts +16 -1
  73. package/validation/validatorBase.d.ts.map +1 -0
  74. package/validation/validatorBase.js +7 -1
  75. package/validation/validators.d.ts +26 -2
  76. package/validation/validators.d.ts.map +1 -0
  77. package/validation/validators.js +26 -8
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
@@ -21,6 +21,7 @@ Also includes a few other much less-developed odds-and-ends borrowed from one pr
21
21
 
22
22
  - [Summary](#summary)
23
23
  - [Installation](#installation)
24
+ - [API Documentation](#api-documentation)
24
25
  - [Overview](#overview)
25
26
  - [The Result Pattern](#the-result-pattern)
26
27
  - [Converters](#converters)
@@ -40,6 +41,9 @@ With npm:
40
41
  npm install ts-utils
41
42
  ```
42
43
 
44
+ ## API Documentation
45
+ Extracted API documentation is [here](./docs/ts-utils.md).
46
+
43
47
  ## Overview
44
48
  ### The Result Pattern
45
49
 
package/brand.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * Helper type to brand a simple type to prevent inappropriate use
3
+ * @public
3
4
  */
4
5
  export declare type Brand<T, B> = T & {
5
6
  __brand: B;
6
7
  };
8
+ //# sourceMappingURL=brand.d.ts.map
package/brand.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"brand.d.ts","sourceRoot":"","sources":["../src/brand.ts"],"names":[],"mappings":"AAsBA;;;GAGG;AAEH,oBAAY,KAAK,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG;IAAE,OAAO,EAAE,CAAC,CAAA;CAAE,CAAC"}
package/brand.js CHANGED
@@ -21,4 +21,4 @@
21
21
  * SOFTWARE.
22
22
  */
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnJhbmQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvYnJhbmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQW9CRyIsInNvdXJjZXNDb250ZW50IjpbIi8qXG4gKiBDb3B5cmlnaHQgKGMpIDIwMjEgRXJpayBGb3J0dW5lXG4gKlxuICogUGVybWlzc2lvbiBpcyBoZXJlYnkgZ3JhbnRlZCwgZnJlZSBvZiBjaGFyZ2UsIHRvIGFueSBwZXJzb24gb2J0YWluaW5nIGEgY29weVxuICogb2YgdGhpcyBzb2Z0d2FyZSBhbmQgYXNzb2NpYXRlZCBkb2N1bWVudGF0aW9uIGZpbGVzICh0aGUgXCJTb2Z0d2FyZVwiKSwgdG8gZGVhbFxuICogaW4gdGhlIFNvZnR3YXJlIHdpdGhvdXQgcmVzdHJpY3Rpb24sIGluY2x1ZGluZyB3aXRob3V0IGxpbWl0YXRpb24gdGhlIHJpZ2h0c1xuICogdG8gdXNlLCBjb3B5LCBtb2RpZnksIG1lcmdlLCBwdWJsaXNoLCBkaXN0cmlidXRlLCBzdWJsaWNlbnNlLCBhbmQvb3Igc2VsbFxuICogY29waWVzIG9mIHRoZSBTb2Z0d2FyZSwgYW5kIHRv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BvcnQgdHlwZSBCcmFuZDxULCBCPiA9IFQgJiB7IF9fYnJhbmQ6IEIgfTtcbiJdfQ==
package/converter.d.ts CHANGED
@@ -3,156 +3,268 @@ import { Brand } from './brand';
3
3
  declare type OnError = 'failOnError' | 'ignoreErrors';
4
4
  /**
5
5
  * Converter traits.
6
+ * @public
6
7
  */
7
8
  export interface ConverterTraits {
8
9
  readonly isOptional: boolean;
9
10
  readonly brand?: string;
10
11
  }
12
+ /**
13
+ * Options for {@link Converter.withConstraint}.
14
+ * @public
15
+ */
16
+ export interface ConstraintOptions {
17
+ /**
18
+ * Optional description for error messages when constraint
19
+ * function returns false.
20
+ */
21
+ readonly description: string;
22
+ }
23
+ /**
24
+ * Generic converter to convert unknown to a templated type `<T>`, using
25
+ * intrinsic rules or as modified by an optional conversion context
26
+ * of optional templated type `<TC>` (default `undefined`).
27
+ * @public
28
+ */
11
29
  export interface Converter<T, TC = undefined> extends ConverterTraits {
12
30
  /**
13
- * Indicates whether this element is explicitly optional
31
+ * Indicates whether this element is explicitly optional.
14
32
  */
15
33
  readonly isOptional: boolean;
16
34
  /**
17
- * Returns the brand for a branded type
35
+ * Returns the brand for a branded type.
18
36
  */
19
37
  readonly brand?: string;
20
38
  /**
21
- * Converts from unknown to <T>
22
- * @param from The unknown to be converted
23
- * @param context An optional context applied to the conversion
24
- * @returns An @see Result with a value or an error message
39
+ * Converts from `unknown` to `<T>`.
40
+ * @param from - The `unknown` to be converted
41
+ * @param context - An optional conversion context of type `<TC>` to be used in
42
+ * the conversion.
43
+ * @returns A {@link Result} with a {@link Success} and a value on success or an
44
+ * {@link Failure} with a a message on failure.
25
45
  */
26
46
  convert(from: unknown, context?: TC): Result<T>;
27
47
  /**
28
- * Converts from unknown to <T> or undefined, as appropriate.
29
- * If 'onError' is 'failOnError', the converter succeeds for
30
- * 'undefined' or any convertible value, but reports an error
31
- * if it encounters a value that cannot be converted. If 'onError'
32
- * is 'ignoreErrors' (default) then values that cannot be converted
33
- * result in a successful return of 'undefined'.
34
- * @param from The unknown to be converted
35
- * @param context Optional context for use by the converter
36
- * @param onError Specifies handling of values that cannot be converted, default 'ignoreErrors'
48
+ * Converts from `unknown` to `<T>` or `undefined`, as appropriate.
49
+ *
50
+ * @remarks
51
+ * If `onError` is `failOnError`, the converter succeeds for
52
+ * `undefined` or any convertible value, but reports an error
53
+ * if it encounters a value that cannot be converted.
54
+ *
55
+ * If `onError` is `ignoreErrors` (default) then values that
56
+ * cannot be converted result in a successful return of `undefined`.
57
+ * @param from - The `unknown` to be converted
58
+ * @param context - An optional conversion context of type `<TC>` to be used in
59
+ * the conversion.
60
+ * @param onError - Specifies handling of values that cannot be converted (default `ignoreErrors`).
61
+ * @returns A {@link Result} with a {@link Success} and a value on success or an
62
+ * {@link Failure} with a a message on failure.
37
63
  */
38
64
  convertOptional(from: unknown, context?: TC, onError?: OnError): Result<T | undefined>;
39
65
  /**
40
- * Creates a converter for an optional value. If 'onError'
41
- * is 'failOnError', the converter accepts 'undefined' or a
42
- * convertible value, but reports an error if it encounters
43
- * a value that cannot be converted. If 'onError' is 'ignoreErrors'
44
- * (default) then values that cannot be converted result in a
45
- * successful return of 'undefined'.
66
+ * Creates a {@link Converter} for an optional value.
67
+ *
68
+ * @remarks
69
+ * If `onError` is `failOnError`, the resulting converter will accept `undefined`
70
+ * or a convertible value, but report an error if it encounters a value that cannot be
71
+ * converted.
46
72
  *
47
- * @param onError Specifies handling of values that cannot be converted, default 'ignoreErrors'
73
+ * If `onError` is `ignoreErrors` (default) then values that cannot be converted will
74
+ * result in a successful return of `undefined`.
75
+ *
76
+ * @param onError - Specifies handling of values that cannot be converted (default `ignoreErrors`).
77
+ * @returns A new {@link Converter} returning `<T|undefined>`.
48
78
  * */
49
79
  optional(onError?: OnError): Converter<T | undefined, TC>;
50
80
  /**
51
- * Applies a (possibly) mapping conversion to the converted value.
52
- * @param mapper A function which maps from the converted type to some other type.
81
+ * Creates a {@link Converter} which applies a (possibly) mapping conversion to
82
+ * the converted value of this {@link Converter}.
83
+ * @param mapper - A function which maps from the the result type `<T>` of this
84
+ * converter to a new result type `<T2>`.
85
+ * @returns A new {@link Converter} returning `<T2>`.
53
86
  */
54
87
  map<T2>(mapper: (from: T) => Result<T2>): Converter<T2, TC>;
55
88
  /**
56
- * Applies an additional converter to the converted value.
57
- * @param mapConverter The converter to be applied to the converted value
89
+ * Creates a {@link Converter} which applies an additional supplied
90
+ * converter to the result of this converter.
91
+ *
92
+ * @param mapConverter - The {@link Converter} to be applied to the
93
+ * converted result from this {@link Converter}.
94
+ * @returns A new {@link Converter} returning `<T2>`.
58
95
  */
59
96
  mapConvert<T2>(mapConverter: Converter<T2>): Converter<T2, TC>;
60
97
  /**
61
- * Creates a converter with an optional constraint. If the base converter
62
- * succeeds, calls a supplied constraint evaluation function with the
63
- * value and fails the conversion if the function returns either false
64
- * or Failure<T>.
98
+ * Creates a {@link Converter} which maps the individual items of a collection
99
+ * resulting from this {@link Converter} using the supplied map fuction.
65
100
  *
66
- * @param constraint Constraint evaluation function
101
+ * @remarks
102
+ * Fails if `from` is not an array.
103
+ *
104
+ * @param mapper - The map function to be applied to each element of the
105
+ * result of this {@link Converter}.
106
+ * @returns A new {@link Converter} returning `<TI[]>`.
67
107
  */
68
- withConstraint(constraint: (val: T) => boolean | Result<T>): Converter<T, TC>;
108
+ mapItems<TI>(mapper: (from: unknown) => Result<TI>): Converter<TI[], TC>;
69
109
  /**
70
- * Adds a brand to the type to prevent mismatched usage of simple types
110
+ * Creates a {@link Converter} which maps the individual items of a collection
111
+ * resulting from this {@link Converter} using the supplied {@link Converter}.
112
+ *
113
+ * @remarks
114
+ * Fails if `from` is not an array.
115
+ *
116
+ * @param mapConverter - The {@link Converter} to be applied to each element of the
117
+ * result of this {@link Converter}.
118
+ * @returns A new {@link Converter} returning `<TI[]>`.
119
+ */
120
+ mapConvertItems<TI>(mapConverter: Converter<TI, unknown>): Converter<TI[], TC>;
121
+ /**
122
+ * Creates a {@link Converter} which applies a supplied type guard to the conversion
123
+ * result.
124
+ * @param guard - The type guard function to apply.
125
+ * @param message - Optional message to be reported if the type guard fails.
126
+ * @returns A new {@link Converter} returning `<TI>`.
127
+ */
128
+ withTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI, TC>;
129
+ /**
130
+ * Creates a {@link Converter} which applies a supplied type guard to each member of
131
+ * the conversion result from this converter.
132
+ *
133
+ * @remarks
134
+ * Fails if the conversion result is not an array or if any member fails the
135
+ * type guard.
136
+ * @param guard - The type guard function to apply to each element.
137
+ * @param message - Optional message to be reported if the type guard fails.
138
+ * @returns A new {@link Converter} returning `<TI>`.
139
+ */
140
+ withItemTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI[], TC>;
141
+ /**
142
+ * Creates a {@link Converter} which applies an optional constraint to the result
143
+ * of this conversion. If this {@link Converter} (the base converter) succeeds, the new
144
+ * converter calls a supplied constraint evaluation function with the conversion, which
145
+ * fails the entire conversion if the constraint function returns either `false` or
146
+ * {@link Failure | Failure<T>}.
147
+ *
148
+ * @param constraint - Constraint evaluation function.
149
+ * @param options - {@link ConstraintOptions | Options} for constraint evaluation.
150
+ * @returns A new {@link Converter} returning `<T>`.
151
+ */
152
+ withConstraint(constraint: (val: T) => boolean | Result<T>, options?: ConstraintOptions): Converter<T, TC>;
153
+ /**
154
+ * returns a converter which adds a brand to the type to prevent mismatched usage
155
+ * of simple types.
156
+ * @param brand - The brand to be applied to the result value.
157
+ * @returns A {@link Converter} returning `Brand<T, B>`.
71
158
  */
72
159
  withBrand<B extends string>(brand: B): Converter<Brand<T, B>, TC>;
73
160
  }
161
+ /**
162
+ * internal
163
+ */
74
164
  declare type InnerInferredType<TCONV> = TCONV extends Converter<infer TTO> ? (TTO extends Array<infer TTOELEM> ? InnerInferredType<TTOELEM>[] : TTO) : (TCONV extends Array<infer TELEM> ? InnerInferredType<TELEM>[] : TCONV);
75
165
  /**
76
166
  * Infers the type that will be returned by an intstantiated converter. Works
77
167
  * for complex as well as simple types.
78
- * @example Infer<typeof Converters.mapOf(Converters.stringArray)> is Map<string, string[]>
168
+ * @example `Infer<typeof Converters.mapOf(Converters.stringArray)>` is `Map<string, string[]>`
169
+ * @beta
79
170
  */
80
171
  export declare type Infer<TCONV> = TCONV extends Converter<infer TTO> ? InnerInferredType<TTO> : never;
81
172
  /**
82
173
  * Deprecated name for Infer<T> retained for compatibility
83
174
  * @deprecated use @see Infer instead
175
+ * @internal
84
176
  */
85
177
  export declare type ConvertedToType<TCONV> = Infer<TCONV>;
86
178
  /**
87
- * Simple templated converter wrapper to simplify typed conversion from unknown.
179
+ * Base templated wrapper to simplify creation of new {@link Converter}s.
180
+ * @public
88
181
  */
89
182
  export declare class BaseConverter<T, TC = undefined> implements Converter<T, TC> {
183
+ /**
184
+ * @internal
185
+ */
90
186
  protected readonly _defaultContext?: TC;
187
+ /**
188
+ * @internal
189
+ */
91
190
  protected _isOptional: boolean;
191
+ /**
192
+ * @internal
193
+ */
92
194
  protected _brand?: string;
93
195
  private readonly _converter;
196
+ /**
197
+ * Constructs a new {@link Converter} which uses the supplied function to perform the conversion.
198
+ * @param converter - The conversion function to be applied.
199
+ * @param defaultContext - Optional conversion context to be used by default.
200
+ * @param traits - Optional {@link ConverterTraits | traits} to be assigned to the resulting
201
+ * converter.
202
+ */
94
203
  constructor(converter: (from: unknown, self: Converter<T, TC>, context?: TC) => Result<T>, defaultContext?: TC, traits?: ConverterTraits);
95
204
  /**
96
- * Converts from unknown to <T>
97
- * @param from The unknown to be converted
98
- * @returns An @see Result with a value or an error message
205
+ * {@inheritdoc Converter.isOptional}
206
+ */
207
+ get isOptional(): boolean;
208
+ /**
209
+ * {@inheritdoc Converter.brand}
210
+ */
211
+ get brand(): string | undefined;
212
+ /**
213
+ * {@inheritdoc Converter.convert}
99
214
  */
100
215
  convert(from: unknown, context?: TC): Result<T>;
101
216
  /**
102
- * Converts from unknown to <T> or undefined, as appropriate.
103
- * If 'onError' is 'failOnError', the converter succeeds for
104
- * 'undefined' or any convertible value, but reports an error
105
- * if it encounters a value that cannot be converted. If 'onError'
106
- * is 'ignoreErrors' (default) then values that cannot be converted
107
- * result in a successful return of 'undefined'.
108
- * @param from The unknown to be converted
109
- * @param onError Specifies handling of values that cannot be converted, default 'ignoreErrors'
217
+ * {@inheritdoc Converter.convertOptional}
110
218
  */
111
219
  convertOptional(from: unknown, context?: TC, onError?: OnError): Result<T | undefined>;
112
220
  /**
113
- * Creates a converter for an optional value. If 'onError'
114
- * is 'failOnError', the converter accepts 'undefined' or a
115
- * convertible value, but reports an error if it encounters
116
- * a value that cannot be converted. If 'onError' is 'ignoreErrors'
117
- * then values that cannot be converted result in a
118
- * successful return of 'undefined'.
119
- *
120
- * @param onError Specifies handling of values that cannot be converted, default 'ignoreErrors'
121
- * */
221
+ * {@inheritdoc Converter.optional}
222
+ */
122
223
  optional(onError?: OnError): Converter<T | undefined, TC>;
123
224
  /**
124
- * Reports whether this value is explicitly optional
225
+ * {@inheritdoc Converter.map}
125
226
  */
126
- get isOptional(): boolean;
227
+ map<T2>(mapper: (from: T) => Result<T2>): Converter<T2, TC>;
127
228
  /**
128
- * Reports the brand of a branded type.
229
+ * {@inheritdoc Converter.mapConvert}
129
230
  */
130
- get brand(): string | undefined;
231
+ mapConvert<T2>(mapConverter: Converter<T2>): Converter<T2, TC>;
131
232
  /**
132
- * Applies a (possibly) mapping conversion to the converted value.
133
- * @param mapper A function which maps from the converted type to some other type.
233
+ * {@inheritdoc Converter.mapItems}
134
234
  */
135
- map<T2>(mapper: (from: T) => Result<T2>): Converter<T2, TC>;
235
+ mapItems<TI>(mapper: (from: unknown) => Result<TI>): Converter<TI[], TC>;
136
236
  /**
137
- * Applies an additional converter to the converted value.
138
- * @param mapConverter The converter to be applied to the converted value
237
+ * {@inheritdoc Converter.mapConvertItems}
139
238
  */
140
- mapConvert<T2>(mapConverter: Converter<T2>): Converter<T2, TC>;
239
+ mapConvertItems<TI>(mapConverter: Converter<TI, unknown>): Converter<TI[], TC>;
141
240
  /**
142
- * Creates a converter with an optional constraint. If the base converter
143
- * succeeds, calls a supplied constraint evaluation function with the
144
- * value and fails the conversion if the function returns either false
145
- * or Failure<T>.
146
- *
147
- * @param constraint Constraint evaluation function
241
+ * {@inheritdoc Converter.withTypeGuard}
242
+ */
243
+ withTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI, TC>;
244
+ /**
245
+ * {@inheritdoc Converter.withItemTypeGuard}
148
246
  */
149
- withConstraint(constraint: (val: T) => boolean | Result<T>): Converter<T, TC>;
247
+ withItemTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI[], TC>;
150
248
  /**
151
- * Adds a brand to the type to prevent mismatched usage of simple types
249
+ * {@inheritdoc Converter.withConstraint}
250
+ */
251
+ withConstraint(constraint: (val: T) => boolean | Result<T>, options?: ConstraintOptions): Converter<T, TC>;
252
+ /**
253
+ * {@inheritdoc Converter.withBrand}
152
254
  */
153
255
  withBrand<B extends string>(brand: B): Converter<Brand<T, B>, TC>;
256
+ /**
257
+ * @internal
258
+ */
154
259
  protected _context(supplied?: TC): TC | undefined;
260
+ /**
261
+ * @internal
262
+ */
155
263
  protected _traits(traits?: Partial<ConverterTraits>): ConverterTraits;
264
+ /**
265
+ * @internal
266
+ */
156
267
  protected _with(traits: Partial<ConverterTraits>): this;
157
268
  }
158
269
  export {};
270
+ //# sourceMappingURL=converter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"converter.d.ts","sourceRoot":"","sources":["../src/converter.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,MAAM,EAA6B,MAAM,UAAU,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,aAAK,OAAO,GAAG,aAAa,GAAG,cAAc,CAAC;AAE9C;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC5B,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;C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