Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use argv-iterator #16

Merged
merged 13 commits into from
Oct 31, 2022
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
privatenumber committed Oct 31, 2022
commit fcb74902db9d0b93f6d7a2aa1c977acbee9c9bba
7 changes: 2 additions & 5 deletions src/type-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
normalizeBoolean,
applyParser,
hasOwn,
getOwn,
} from './utils';
import { argvIterator } from './argv-iterator';

Expand Down Expand Up @@ -53,10 +52,8 @@ export const typeFlag = <Schemas extends Flags>(

argvIterator(argv, {
onFlag(name, explicitValue, index) {
const knownFlag = getOwn(flagRegistry, name);

if (knownFlag) {
const [parser, values] = knownFlag;
if (hasOwn(flagRegistry, name)) {
const [parser, values] = flagRegistry[name];
const flagValue = normalizeBoolean(parser, explicitValue);
const getFollowingValue = (value?: string | boolean) => {
const parsedValue = applyParser(parser, value || '');
Expand Down
13 changes: 2 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ const camelToKebab = (string: string) => string.replace(camelCasePattern, '-$1')
const { hasOwnProperty } = Object.prototype;
export const hasOwn = (object: any, property: PropertyKey) => hasOwnProperty.call(object, property);

export const getOwn = <ObjectType>(
object: ObjectType,
property: keyof ObjectType,
) => (hasOwn(object, property) ? object[property] : undefined);

/**
* Default Array.isArray doesn't support type-narrowing
* on readonly arrays.
Expand Down Expand Up @@ -113,17 +108,13 @@ export const createRegistry = (
registry[name] = value;
};

// eslint-disable-next-line guard-for-in
for (const flagName in schemas) {
const schema = getOwn(schemas, flagName);

// has-own check
if (!schema) {
if (!hasOwn(schemas, flagName)) {
continue;
}

validateFlagName(flagName);

const schema = schemas[flagName];
const [parser, isArray] = parseFlagType(schema);
const values: unknown[] = [];
const asdf = [parser, values];
Expand Down