/
format_options.ts
44 lines (41 loc) 路 1.66 KB
/
format_options.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export type FormatOptions = Record<string, string>;
export type ValidOption = [key: string, values: string[]];
export type ValidOptions = ValidOption[];
/**
* Check that the given `options` are allowed based on the given `validOptions`.
* @param name The name of the serializer that is receiving the options.
* @param validOptions An array of valid options and their allowed values.
* @param options The options to be validated.
*/
export function validateOptions(name: string, validOptions: ValidOptions, options: FormatOptions) {
const validOptionsMap = new Map<ValidOption[0], ValidOption[1]>(validOptions);
for (const option in options) {
if (!validOptionsMap.has(option)) {
throw new Error(
`Invalid format option for ${name}: "${option}".\n` +
`Allowed options are ${JSON.stringify(Array.from(validOptionsMap.keys()))}.`);
}
const validOptionValues = validOptionsMap.get(option)!;
const optionValue = options[option];
if (!validOptionValues.includes(optionValue)) {
throw new Error(
`Invalid format option value for ${name}: "${option}".\n` +
`Allowed option values are ${JSON.stringify(validOptionValues)} but received "${
optionValue}".`);
}
}
}
/**
* Parse the given `optionString` into a collection of `FormatOptions`.
* @param optionString The string to parse.
*/
export function parseFormatOptions(optionString: string = '{}'): FormatOptions {
return JSON.parse(optionString);
}