@fgv/ts-json-base 5.0.1-7 → 5.0.1-8

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.
@@ -3,6 +3,7 @@ import { Converter } from '@fgv/ts-utils';
3
3
  import { Converters as Converters_3 } from '@fgv/ts-utils';
4
4
  import { ObjectConverter as ObjectConverter_2 } from '@fgv/ts-utils';
5
5
  import { Result } from '@fgv/ts-utils';
6
+ import { StringConverter } from '@fgv/ts-utils';
6
7
  import { Validation } from '@fgv/ts-utils';
7
8
  import { Validator } from '@fgv/ts-utils';
8
9
  import { Validators as Validators_3 } from '@fgv/ts-utils';
@@ -38,6 +39,14 @@ declare function arrayOf_2<T, TC = unknown>(validateElement: JsonCompatible_2.Va
38
39
  */
39
40
  declare type ArrayValidator<T, TC = unknown> = Validation.Classes.ArrayValidator<JsonCompatibleType<T>, TC>;
40
41
 
42
+ /**
43
+ * A {@link Converter | Converter} which converts `unknown` to a `boolean`.
44
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
45
+ * Mirrors the behavior of `@fgv/ts-utils`.
46
+ * @public
47
+ */
48
+ declare const boolean: Converter<boolean, IJsonConverterContext>;
49
+
41
50
  /**
42
51
  * Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
43
52
  * {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value
@@ -62,11 +71,16 @@ declare type Converter_2<T, TC = unknown> = Converter<JsonCompatibleType<T>, TC>
62
71
 
63
72
  declare namespace Converters {
64
73
  export {
74
+ literal,
75
+ enumeratedValue,
65
76
  IJsonConverterContext,
66
77
  jsonPrimitive,
67
78
  jsonObject,
68
79
  jsonArray,
69
- jsonValue
80
+ jsonValue,
81
+ string,
82
+ number,
83
+ boolean
70
84
  }
71
85
  }
72
86
  export { Converters }
@@ -187,6 +201,22 @@ declare class DirectoryItem<TCT extends string = string> implements IFileTreeDir
187
201
  */
188
202
  declare function discriminatedObject<T, TD extends string = string, TC = unknown>(discriminatorProp: string, converters: Converters_3.DiscriminatedObjectConverters<JsonCompatibleType<T>, TD, TC>): JsonCompatible_2.Converter<T, TC>;
189
203
 
204
+ /**
205
+ * Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of
206
+ * supplied enumerated values. Anything else fails.
207
+ *
208
+ * @remarks
209
+ * This JSON variant accepts an {@link Converters.IJsonConverterContext | IJsonConverterContext} OR
210
+ * a `ReadonlyArray<T>` as its conversion context. If the context is an array, it is used to override the
211
+ * allowed values for that conversion; otherwise, the original `values` supplied at creation time are used.
212
+ *
213
+ * @param values - Array of allowed values.
214
+ * @param message - Optional custom failure message.
215
+ * @returns A new {@link Converter | Converter} returning `<T>`.
216
+ * @public
217
+ */
218
+ declare function enumeratedValue<T>(values: ReadonlyArray<T>, message?: string): Converter<T, IJsonConverterContext | ReadonlyArray<T>>;
219
+
190
220
  /**
191
221
  * Class representing a file in a file tree.
192
222
  * @public
@@ -1146,6 +1176,22 @@ declare const jsonValue_2: Validator<JsonValue, IJsonValidatorContext>;
1146
1176
  */
1147
1177
  export declare type JsonValueType = 'primitive' | 'object' | 'array';
1148
1178
 
1179
+ /**
1180
+ * Helper to create a converter for a literal value.
1181
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
1182
+ * Mirrors the behavior of `@fgv/ts-utils`.
1183
+ * @public
1184
+ */
1185
+ declare function literal<T>(value: T): Converter<T, IJsonConverterContext>;
1186
+
1187
+ /**
1188
+ * A {@link Converter | Converter} which converts `unknown` to a `number`.
1189
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
1190
+ * Mirrors the behavior of `@fgv/ts-utils`.
1191
+ * @public
1192
+ */
1193
+ declare const number: Converter<number, IJsonConverterContext>;
1194
+
1149
1195
  /**
1150
1196
  * A helper function to create a {@link JsonCompatible.ObjectConverter | JSON-compatible ObjectConverter<T, TC>} which converts a
1151
1197
  * supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
@@ -1278,6 +1324,13 @@ export declare function sanitizeJsonObject<T>(from: T): Result<T>;
1278
1324
  */
1279
1325
  declare function strictObject<T, TC = unknown>(properties: Conversion.FieldConverters<JsonCompatibleType<T>, TC>, options?: Converters_3.StrictObjectConverterOptions<JsonCompatibleType<T>>): JsonCompatible_2.ObjectConverter<T, TC>;
1280
1326
 
1327
+ /**
1328
+ * A {@link Converter | Converter} which converts `unknown` to a `string`.
1329
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
1330
+ * @public
1331
+ */
1332
+ declare const string: StringConverter<string, IJsonConverterContext>;
1333
+
1281
1334
  /**
1282
1335
  * A validator which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
1283
1336
  * @public
@@ -1,4 +1,4 @@
1
- import { Converter } from '@fgv/ts-utils';
1
+ import { Converter, StringConverter } from '@fgv/ts-utils';
2
2
  import { JsonArray, JsonObject, JsonPrimitive, JsonValue } from '../json';
3
3
  /**
4
4
  * Conversion context for JSON converters.
@@ -40,4 +40,46 @@ export declare const jsonArray: Converter<JsonArray, IJsonConverterContext>;
40
40
  * @public
41
41
  */
42
42
  export declare const jsonValue: Converter<JsonValue, IJsonConverterContext>;
43
+ /**
44
+ * A {@link Converter | Converter} which converts `unknown` to a `string`.
45
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
46
+ * @public
47
+ */
48
+ export declare const string: StringConverter<string, IJsonConverterContext>;
49
+ /**
50
+ * A {@link Converter | Converter} which converts `unknown` to a `number`.
51
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
52
+ * Mirrors the behavior of `@fgv/ts-utils`.
53
+ * @public
54
+ */
55
+ export declare const number: Converter<number, IJsonConverterContext>;
56
+ /**
57
+ * A {@link Converter | Converter} which converts `unknown` to a `boolean`.
58
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
59
+ * Mirrors the behavior of `@fgv/ts-utils`.
60
+ * @public
61
+ */
62
+ export declare const boolean: Converter<boolean, IJsonConverterContext>;
63
+ /**
64
+ * Helper to create a converter for a literal value.
65
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
66
+ * Mirrors the behavior of `@fgv/ts-utils`.
67
+ * @public
68
+ */
69
+ export declare function literal<T>(value: T): Converter<T, IJsonConverterContext>;
70
+ /**
71
+ * Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of
72
+ * supplied enumerated values. Anything else fails.
73
+ *
74
+ * @remarks
75
+ * This JSON variant accepts an {@link Converters.IJsonConverterContext | IJsonConverterContext} OR
76
+ * a `ReadonlyArray<T>` as its conversion context. If the context is an array, it is used to override the
77
+ * allowed values for that conversion; otherwise, the original `values` supplied at creation time are used.
78
+ *
79
+ * @param values - Array of allowed values.
80
+ * @param message - Optional custom failure message.
81
+ * @returns A new {@link Converter | Converter} returning `<T>`.
82
+ * @public
83
+ */
84
+ export declare function enumeratedValue<T>(values: ReadonlyArray<T>, message?: string): Converter<T, IJsonConverterContext | ReadonlyArray<T>>;
43
85
  //# sourceMappingURL=converters.d.ts.map
@@ -21,7 +21,9 @@
21
21
  * SOFTWARE.
22
22
  */
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.jsonValue = exports.jsonArray = exports.jsonObject = exports.jsonPrimitive = void 0;
24
+ exports.boolean = exports.number = exports.string = exports.jsonValue = exports.jsonArray = exports.jsonObject = exports.jsonPrimitive = void 0;
25
+ exports.literal = literal;
26
+ exports.enumeratedValue = enumeratedValue;
25
27
  const ts_utils_1 = require("@fgv/ts-utils");
26
28
  const json_1 = require("../json");
27
29
  /**
@@ -134,4 +136,57 @@ exports.jsonValue = new ts_utils_1.Conversion.BaseConverter((from, __self, ctx)
134
136
  }
135
137
  return exports.jsonPrimitive.convert(from, ctx);
136
138
  });
139
+ /**
140
+ * A {@link Converter | Converter} which converts `unknown` to a `string`.
141
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
142
+ * @public
143
+ */
144
+ exports.string = new ts_utils_1.StringConverter();
145
+ /**
146
+ * A {@link Converter | Converter} which converts `unknown` to a `number`.
147
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
148
+ * Mirrors the behavior of `@fgv/ts-utils`.
149
+ * @public
150
+ */
151
+ exports.number = new ts_utils_1.Conversion.BaseConverter((from) => ts_utils_1.Converters.number.convert(from));
152
+ /**
153
+ * A {@link Converter | Converter} which converts `unknown` to a `boolean`.
154
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
155
+ * Mirrors the behavior of `@fgv/ts-utils`.
156
+ * @public
157
+ */
158
+ exports.boolean = new ts_utils_1.Conversion.BaseConverter((from) => ts_utils_1.Converters.boolean.convert(from));
159
+ /**
160
+ * Helper to create a converter for a literal value.
161
+ * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
162
+ * Mirrors the behavior of `@fgv/ts-utils`.
163
+ * @public
164
+ */
165
+ function literal(value) {
166
+ return ts_utils_1.Converters.literal(value);
167
+ }
168
+ /**
169
+ * Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of
170
+ * supplied enumerated values. Anything else fails.
171
+ *
172
+ * @remarks
173
+ * This JSON variant accepts an {@link Converters.IJsonConverterContext | IJsonConverterContext} OR
174
+ * a `ReadonlyArray<T>` as its conversion context. If the context is an array, it is used to override the
175
+ * allowed values for that conversion; otherwise, the original `values` supplied at creation time are used.
176
+ *
177
+ * @param values - Array of allowed values.
178
+ * @param message - Optional custom failure message.
179
+ * @returns A new {@link Converter | Converter} returning `<T>`.
180
+ * @public
181
+ */
182
+ function enumeratedValue(values, message) {
183
+ return new ts_utils_1.Conversion.BaseConverter((from, __self, context) => {
184
+ const effectiveValues = Array.isArray(context) ? context : values;
185
+ const index = effectiveValues.indexOf(from);
186
+ if (index >= 0) {
187
+ return (0, ts_utils_1.succeed)(effectiveValues[index]);
188
+ }
189
+ return (0, ts_utils_1.fail)(message !== null && message !== void 0 ? message : `Invalid enumerated value ${JSON.stringify(from)}`);
190
+ });
191
+ }
137
192
  //# sourceMappingURL=converters.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-json-base",
3
- "version": "5.0.1-7",
3
+ "version": "5.0.1-8",
4
4
  "description": "Typescript types and basic functions for working with json",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-json-base.d.ts",
@@ -53,11 +53,11 @@
53
53
  "@rushstack/eslint-config": "4.5.3",
54
54
  "eslint-plugin-tsdoc": "~0.4.0",
55
55
  "@types/luxon": "^3.7.1",
56
- "@fgv/ts-utils": "5.0.1-7",
57
- "@fgv/ts-utils-jest": "5.0.1-7"
56
+ "@fgv/ts-utils": "5.0.1-8",
57
+ "@fgv/ts-utils-jest": "5.0.1-8"
58
58
  },
59
59
  "peerDependencies": {
60
- "@fgv/ts-utils": "5.0.1-7"
60
+ "@fgv/ts-utils": "5.0.1-8"
61
61
  },
62
62
  "dependencies": {
63
63
  "luxon": "^3.7.2"