From d4791f3d357634daf506fb8f91cc5332a794c421 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Fri, 20 Jun 2025 14:53:38 -0700 Subject: Move to nodejs --- u/process/argv.ts | 77 +++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 39 deletions(-) (limited to 'u/process/argv.ts') diff --git a/u/process/argv.ts b/u/process/argv.ts index 45f8ff0..dcdba85 100644 --- a/u/process/argv.ts +++ b/u/process/argv.ts @@ -1,7 +1,6 @@ -import { Either, type Mapper, type IEither } from "@emprespresso/pengueno"; +import { Either, type Mapper, type IEither } from '@emprespresso/pengueno'; -export const isArgKey = (k: string): k is K => - k.startsWith("--"); +export const isArgKey = (k: string): k is K => k.startsWith('--'); interface ArgHandler { absent?: V; @@ -10,42 +9,42 @@ interface ArgHandler { } export const getArg = ( - arg: K, - argv: Array, - whenValue: ArgHandler, + arg: K, + argv: Array, + whenValue: ArgHandler, ): IEither => { - const value = argv.filter((_argv) => isArgKey(_argv) && _argv.split("=")[0] === arg).map((_argv, i) => { - const next = _argv.includes("=") ? _argv.split("=")[1] : argv.at(i + 1); - if (next) { - if (isArgKey(next)) return whenValue.unspecified; - return whenValue.present(next); - } - return whenValue.unspecified; - }).find(x => x) ?? whenValue.absent; + const value = + argv + .filter((_argv) => isArgKey(_argv) && _argv.split('=')[0] === arg) + .map((_argv, i) => { + const next = _argv.includes('=') ? _argv.split('=')[1] : argv.at(i + 1); + if (next) { + if (isArgKey(next)) return whenValue.unspecified; + return whenValue.present(next); + } + return whenValue.unspecified; + }) + .find((x) => x) ?? whenValue.absent; if (value === undefined) { - return Either.left(new Error("no value specified for " + arg)); + return Either.left(new Error('no value specified for ' + arg)); } return Either.right(value); }; type MappedArgs< - Args extends ReadonlyArray, - Handlers extends Partial>> + Args extends ReadonlyArray, + Handlers extends Partial>>, > = { - [K in Args[number]]: K extends keyof Handlers - ? Handlers[K] extends ArgHandler - ? T - : string - : string; + [K in Args[number]]: K extends keyof Handlers ? (Handlers[K] extends ArgHandler ? T : string) : string; }; export const argv = < - const Args extends ReadonlyArray, - const Handlers extends Partial>> + const Args extends ReadonlyArray, + const Handlers extends Partial>>, >( args: Args, handlers?: Handlers, - argv = Deno.args, + argv = process.argv.slice(2), ): IEither> => { type Result = MappedArgs; @@ -53,20 +52,20 @@ export const argv = < const processArg = (arg: Args[number]): IEither => { const handler = handlers?.[arg] ?? defaultHandler; - return getArg(arg, argv, handler).mapRight(value => [arg, value] as const); + return getArg(arg, argv, handler).mapRight((value) => [arg, value] as const); }; - const argResults = args.map(processArg); - - return argResults.reduce( - (acc: IEither>, current: IEither) => { - return acc.flatMap(accValue => - current.mapRight(([key, value]) => ({ - ...accValue, - [key]: value - })) - ); - }, - Either.right({} as Partial) - ).mapRight(result => result as Result); + return args + .map(processArg) + .reduce( + (acc: IEither>, current: IEither) => + acc.flatMap((accValue) => + current.mapRight(([key, value]) => ({ + ...accValue, + [key]: value, + })), + ), + Either.right(>{}), + ) + .mapRight((result) => result); }; -- cgit v1.2.3-70-g09d2