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

fix: make BooleanArgument/resolveBoolean's contexts immutable #338

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/arguments/CoreBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class CoreArgument extends Argument<boolean> {
super(context, { name: 'boolean' });
}

public run(parameter: string, context: { truths?: string[]; falses?: string[] } & Argument.Context): Argument.Result<boolean> {
public run(parameter: string, context: { readonly truths?: string[]; falses?: readonly string[] } & Argument.Context): Argument.Result<boolean> {
const resolved = resolveBoolean(parameter, { truths: context.truths, falses: context.falses });
if (resolved.success) return this.ok(resolved.value);
return this.error({
Expand Down
6 changes: 3 additions & 3 deletions src/lib/resolvers/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Identifiers } from '../errors/Identifiers';
import { err, ok, Result } from '../parsers/Result';

const baseTruths = ['1', 'true', '+', 't', 'yes', 'y'];
const baseFalses = ['0', 'false', '-', 'f', 'no', 'n'];
const baseTruths = ['1', 'true', '+', 't', 'yes', 'y'] as const;
const baseFalses = ['0', 'false', '-', 'f', 'no', 'n'] as const;

export function resolveBoolean(
parameter: string,
customs?: { truths?: string[]; falses?: string[] }
customs?: { truths?: readonly string[]; falses?: readonly string[] }
): Result<boolean, Identifiers.ArgumentBooleanError> {
const boolean = parameter.toLowerCase();
if ([...baseTruths, ...(customs?.truths ?? [])].includes(boolean)) return ok(true);
Expand Down