Skip to content

Commit

Permalink
refactor: quote properties used for meta-programming
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
jkrems and bcoe committed Jun 20, 2021
1 parent 2617303 commit 2cfab05
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
17 changes: 11 additions & 6 deletions lib/yargs-parser-types.ts
Expand Up @@ -186,11 +186,16 @@ export type FlagsKey = KeyOf<Omit<Flags, 'keys'>>;

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

export interface DefaultValuesForType {
boolean: boolean;
string: string;
number: undefined;
array: any[];
export enum DefaultValuesForTypeKey {
BOOLEAN = 'boolean',
STRING = 'string',
NUMBER = 'number',
ARRAY = 'array',
}

export type DefaultValuesForTypeKey = KeyOf<DefaultValuesForType>;
export interface DefaultValuesForType {
[DefaultValuesForTypeKey.BOOLEAN]: boolean;
[DefaultValuesForTypeKey.STRING]: string;
[DefaultValuesForTypeKey.NUMBER]: undefined;
[DefaultValuesForTypeKey.ARRAY]: any[];
}
20 changes: 10 additions & 10 deletions lib/yargs-parser.ts
Expand Up @@ -13,7 +13,6 @@ import type {
CoerceCallback,
Configuration,
DefaultValuesForType,
DefaultValuesForTypeKey,
DetailedArguments,
Dictionary,
Flag,
Expand All @@ -29,6 +28,7 @@ import type {
ValueOf,
YargsParserMixin
} from './yargs-parser-types.js'
import { DefaultValuesForTypeKey } from './yargs-parser-types.js'
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js'

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

return def[type]
}

// given a flag, enforce a default type.
function guessType (key: string): DefaultValuesForTypeKey {
let type: DefaultValuesForTypeKey = 'boolean'
if (checkAllAliases(key, flags.strings)) type = 'string'
else if (checkAllAliases(key, flags.numbers)) type = 'number'
else if (checkAllAliases(key, flags.bools)) type = 'boolean'
else if (checkAllAliases(key, flags.arrays)) type = 'array'
let type: DefaultValuesForTypeKey = DefaultValuesForTypeKey.BOOLEAN
if (checkAllAliases(key, flags.strings)) type = DefaultValuesForTypeKey.STRING
else if (checkAllAliases(key, flags.numbers)) type = DefaultValuesForTypeKey.NUMBER
else if (checkAllAliases(key, flags.bools)) type = DefaultValuesForTypeKey.BOOLEAN
else if (checkAllAliases(key, flags.arrays)) type = DefaultValuesForTypeKey.ARRAY
return type
}

Expand Down

0 comments on commit 2cfab05

Please sign in to comment.