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 authored Jun 20, 2021
1 parent 2617303 commit 2cfab05
Showing 2 changed files with 21 additions and 16 deletions.
17 changes: 11 additions & 6 deletions lib/yargs-parser-types.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@ import type {
CoerceCallback,
Configuration,
DefaultValuesForType,
DefaultValuesForTypeKey,
DetailedArguments,
Dictionary,
Flag,
@@ -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
@@ -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
}

0 comments on commit 2cfab05

Please sign in to comment.