Skip to content

Commit

Permalink
feat: export token types
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Apr 16, 2024
1 parent 978df1e commit 64247f9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const parsed = typeFlag(
if (stopParsing) {
return true
}
const isArgument = type === 'argument'
const isArgument = type === TokenType.Argument
if (isArgument) {
stopParsing = isArgument
return stopParsing
Expand Down Expand Up @@ -447,7 +447,7 @@ Type:
type Options = {
// Callback to skip parsing on certain argv tokens
ignore?: (
type: 'known-flag' | 'unknown-flag' | 'argument',
type: TokenType,
flagOrArgv: string,
value: string | undefined,
) => boolean | void
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { typeFlag } from './type-flag';
export { getFlag } from './get-flag';
export { TokenType } from './types';
export type {
TypeFlag,
TypeFlagOptions,
Expand Down
8 changes: 3 additions & 5 deletions src/type-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import type {
TypeFlagOptions,
} from './types';
import {
KNOWN_FLAG,
UNKNOWN_FLAG,
ARGUMENT,
TokenType
} from './types';
import {
hasOwn,
Expand Down Expand Up @@ -59,7 +57,7 @@ export const typeFlag = <Schemas extends Flags>(
const isKnownFlag = hasOwn(flagRegistry, name);
if (
ignore?.(
isKnownFlag ? KNOWN_FLAG : UNKNOWN_FLAG,
isKnownFlag ? TokenType.KnownFlag : TokenType.UnknownFlag,
name,
explicitValue,
)
Expand Down Expand Up @@ -103,7 +101,7 @@ export const typeFlag = <Schemas extends Flags>(
},

onArgument: (args, index, isEoF) => {
if (ignore?.(ARGUMENT, argv[index[0]])) {
if (ignore?.(TokenType.Argument, argv[index[0]])) {
return;
}

Expand Down
12 changes: 7 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,20 @@ export type TypeFlag<Schemas extends Flags> = ParsedFlags<{
[flag in keyof Schemas]: InferFlagType<Schemas[flag]>;
}>;

export const KNOWN_FLAG = 'known-flag';
export const UNKNOWN_FLAG = 'unknown-flag';
export const ARGUMENT = 'argument';
export enum TokenType {
KnownFlag,
UnknownFlag,
Argument,
};

type IgnoreFunction = {
(
type: typeof ARGUMENT,
type: TokenType.Argument,
argvElement: string,
): boolean | void;

(
type: typeof KNOWN_FLAG | typeof UNKNOWN_FLAG,
type: TokenType.KnownFlag | TokenType.UnknownFlag,
flagName: string,
flagValue: string | undefined,
): boolean | void;
Expand Down
10 changes: 5 additions & 5 deletions tests/specs/type-flag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { testSuite, expect } from 'manten';
import { typeFlag, type Flags } from '#type-flag';
import { typeFlag, TokenType, type Flags } from '#type-flag';

export default testSuite(({ describe }) => {
describe('type-flag', ({ describe }) => {
Expand Down Expand Up @@ -467,7 +467,7 @@ export default testSuite(({ describe }) => {
argv,
{
ignore: (type, flagName) => (
type === 'known-flag'
type === TokenType.KnownFlag
&& flagName === 'string'
),
},
Expand Down Expand Up @@ -505,7 +505,7 @@ export default testSuite(({ describe }) => {
},
argv,
{
ignore: type => type === 'unknown-flag',
ignore: type => type === TokenType.UnknownFlag,
},
);

Expand Down Expand Up @@ -538,7 +538,7 @@ export default testSuite(({ describe }) => {
return true;
}

const isArgument = type === 'argument';
const isArgument = type === TokenType.Argument;
if (isArgument) {
ignore = isArgument;
return isArgument;
Expand Down Expand Up @@ -575,7 +575,7 @@ export default testSuite(({ describe }) => {
},
argv,
{
ignore: (type, value) => (type === 'argument' && value === '--'),
ignore: (type, value) => (type === TokenType.Argument && value === '--'),
},
);

Expand Down

0 comments on commit 64247f9

Please sign in to comment.