Skip to content

Commit 2cfab05

Browse files
jkremsbcoe
andauthored
refactor: quote properties used for meta-programming
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
1 parent 2617303 commit 2cfab05

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

lib/yargs-parser-types.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,16 @@ export type FlagsKey = KeyOf<Omit<Flags, 'keys'>>;
186186

187187
export type ArrayFlagsKey = Extract<FlagsKey, 'bools' | 'strings' | 'numbers'>;
188188

189-
export interface DefaultValuesForType {
190-
boolean: boolean;
191-
string: string;
192-
number: undefined;
193-
array: any[];
189+
export enum DefaultValuesForTypeKey {
190+
BOOLEAN = 'boolean',
191+
STRING = 'string',
192+
NUMBER = 'number',
193+
ARRAY = 'array',
194194
}
195195

196-
export type DefaultValuesForTypeKey = KeyOf<DefaultValuesForType>;
196+
export interface DefaultValuesForType {
197+
[DefaultValuesForTypeKey.BOOLEAN]: boolean;
198+
[DefaultValuesForTypeKey.STRING]: string;
199+
[DefaultValuesForTypeKey.NUMBER]: undefined;
200+
[DefaultValuesForTypeKey.ARRAY]: any[];
201+
}

lib/yargs-parser.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import type {
1313
CoerceCallback,
1414
Configuration,
1515
DefaultValuesForType,
16-
DefaultValuesForTypeKey,
1716
DetailedArguments,
1817
Dictionary,
1918
Flag,
@@ -29,6 +28,7 @@ import type {
2928
ValueOf,
3029
YargsParserMixin
3130
} from './yargs-parser-types.js'
31+
import { DefaultValuesForTypeKey } from './yargs-parser-types.js'
3232
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js'
3333

3434
let mixin: YargsParserMixin
@@ -1002,22 +1002,22 @@ export class YargsParser {
10021002
// return a default value, given the type of a flag.,
10031003
function defaultForType<K extends DefaultValuesForTypeKey> (type: K): DefaultValuesForType[K] {
10041004
const def: DefaultValuesForType = {
1005-
boolean: true,
1006-
string: '',
1007-
number: undefined,
1008-
array: []
1005+
[DefaultValuesForTypeKey.BOOLEAN]: true,
1006+
[DefaultValuesForTypeKey.STRING]: '',
1007+
[DefaultValuesForTypeKey.NUMBER]: undefined,
1008+
[DefaultValuesForTypeKey.ARRAY]: []
10091009
}
10101010

10111011
return def[type]
10121012
}
10131013

10141014
// given a flag, enforce a default type.
10151015
function guessType (key: string): DefaultValuesForTypeKey {
1016-
let type: DefaultValuesForTypeKey = 'boolean'
1017-
if (checkAllAliases(key, flags.strings)) type = 'string'
1018-
else if (checkAllAliases(key, flags.numbers)) type = 'number'
1019-
else if (checkAllAliases(key, flags.bools)) type = 'boolean'
1020-
else if (checkAllAliases(key, flags.arrays)) type = 'array'
1016+
let type: DefaultValuesForTypeKey = DefaultValuesForTypeKey.BOOLEAN
1017+
if (checkAllAliases(key, flags.strings)) type = DefaultValuesForTypeKey.STRING
1018+
else if (checkAllAliases(key, flags.numbers)) type = DefaultValuesForTypeKey.NUMBER
1019+
else if (checkAllAliases(key, flags.bools)) type = DefaultValuesForTypeKey.BOOLEAN
1020+
else if (checkAllAliases(key, flags.arrays)) type = DefaultValuesForTypeKey.ARRAY
10211021
return type
10221022
}
10231023

0 commit comments

Comments
 (0)