Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
use predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Dec 2, 2023
1 parent 4ea0680 commit 378f94f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.ts
17 changes: 13 additions & 4 deletions src/internal/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ export interface WithFallbackConfig extends
export const isArgs = (u: unknown): u is Args.Args<unknown> =>
typeof u === "object" && u != null && ArgsTypeId in u

/** @internal */
export const isInstruction = <_>(self: Args.Args<_>): self is Instruction => self as any

/** @internal */
export const isEmpty = (self: Instruction): self is Empty => self._tag === "Empty"

Expand All @@ -131,6 +134,13 @@ export const isMap = (self: Instruction): self is Map => self._tag === "Map"
/** @internal */
export const isVariadic = (self: Instruction): self is Variadic => self._tag === "Variadic"

/** @internal */
export const isWithDefault = (self: Instruction): self is WithDefault => self._tag === "WithDefault"

/** @internal */
export const isWithFallbackConfig = (self: Instruction): self is WithFallbackConfig =>
self._tag === "WithFallbackConfig"

// =============================================================================
// Constructors
// =============================================================================
Expand Down Expand Up @@ -351,11 +361,10 @@ export const withFallbackConfig: {
<B>(config: Config.Config<B>) => <A>(self: Args.Args<A>) => Args.Args<A | B>,
<A, B>(self: Args.Args<A>, config: Config.Config<B>) => Args.Args<A | B>
>(2, (self, config) => {
if ((self as Instruction)._tag === "WithDefault") {
const withDefault = self as WithDefault
if (isInstruction(self) && isWithDefault(self)) {
return makeWithDefault(
withFallbackConfig(withDefault.args, config),
withDefault.fallback as any
withFallbackConfig(self.args, config),
self.fallback as any
)
}
return makeWithFallbackConfig(self, config)
Expand Down
14 changes: 10 additions & 4 deletions src/internal/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ export interface WithDefault extends
export const isOptions = (u: unknown): u is Options.Options<unknown> =>
typeof u === "object" && u != null && OptionsTypeId in u

/** @internal */
export const isInstruction = <_>(self: Options.Options<_>): self is Instruction => self as any

/** @internal */
export const isEmpty = (self: Instruction): self is Empty => self._tag === "Empty"

Expand All @@ -165,6 +168,10 @@ export const isOrElse = (self: Instruction): self is OrElse => self._tag === "Or
/** @internal */
export const isWithDefault = (self: Instruction): self is WithDefault => self._tag === "WithDefault"

/** @internal */
export const isWithFallbackConfig = (self: Instruction): self is WithFallbackConfig =>
self._tag === "WithFallbackConfig"

// =============================================================================
// Constructors
// =============================================================================
Expand Down Expand Up @@ -540,11 +547,10 @@ export const withFallbackConfig: {
<B>(config: Config.Config<B>) => <A>(self: Options.Options<A>) => Options.Options<A | B>,
<A, B>(self: Options.Options<A>, config: Config.Config<B>) => Options.Options<A | B>
>(2, (self, config) => {
if ((self as Instruction)._tag === "WithDefault") {
const withDefault = self as WithDefault
if (isInstruction(self) && isWithDefault(self)) {
return makeWithDefault(
withFallbackConfig(withDefault.options, config),
withDefault.fallback as any
withFallbackConfig(self.options, config),
self.fallback as any
)
}
return makeWithFallbackConfig(self, config)
Expand Down

0 comments on commit 378f94f

Please sign in to comment.