diff options
author | Elizabeth <me@liz.coffee> | 2025-06-02 11:14:52 -0700 |
---|---|---|
committer | Elizabeth <me@liz.coffee> | 2025-06-02 11:14:52 -0700 |
commit | 373d9ec700c0097a22cf665a8e33cf48998d1dc2 (patch) | |
tree | 71297ac69177037929e1bfb00b8c71038058acd5 /u | |
parent | 646c5eb11d3b9240f8434163d103a117d30c88c7 (diff) | |
download | ci-373d9ec700c0097a22cf665a8e33cf48998d1dc2.tar.gz ci-373d9ec700c0097a22cf665a8e33cf48998d1dc2.zip |
Minor things
Diffstat (limited to 'u')
-rw-r--r-- | u/deno.json | 3 | ||||
-rw-r--r-- | u/fn/either.ts | 2 | ||||
-rw-r--r-- | u/process/argv.ts | 65 | ||||
-rw-r--r-- | u/process/mod.ts | 1 | ||||
-rw-r--r-- | u/server/activity/fourohfour.ts | 12 | ||||
-rw-r--r-- | u/server/request.ts | 9 |
6 files changed, 76 insertions, 16 deletions
diff --git a/u/deno.json b/u/deno.json index 26b08bf..b277873 100644 --- a/u/deno.json +++ b/u/deno.json @@ -1,6 +1,5 @@ { "name": "@emprespresso/pengueno", "version": "0.1.0", - "exports": "./mod.ts", - "workspace": ["./*"] + "exports": "./mod.ts" } diff --git a/u/fn/either.ts b/u/fn/either.ts index 8b233bf..b228af2 100644 --- a/u/fn/either.ts +++ b/u/fn/either.ts @@ -10,7 +10,7 @@ export interface IEither<E, T> { errBranch: Mapper<E, Ee>, okBranch: Mapper<T, Tt>, ) => IEither<Ee, Tt>; - fold: <Tt>(folder: BiMapper<E | undefined, T | undefined, Tt>) => Tt; + fold: <Tt>(folder: (err: E | undefined, val: T | undefined) => Tt) => Tt; //BiMapper<E | undefined, T | undefined, Tt>) => Tt;; moveRight: <Tt>(t: Tt) => IEither<E, Tt>; mapRight: <Tt>(mapper: Mapper<T, Tt>) => IEither<E, Tt>; mapLeft: <Ee>(mapper: Mapper<E, Ee>) => IEither<Ee, T>; diff --git a/u/process/argv.ts b/u/process/argv.ts new file mode 100644 index 0000000..657c9a7 --- /dev/null +++ b/u/process/argv.ts @@ -0,0 +1,65 @@ +import { Either, type IEither } from "@emprespresso/pengueno"; + +export const isArgKey = <K extends string>(k: string): k is K => + k.startsWith("--"); + +export const getArg = <K extends string, V extends string>( + arg: K, + args: Array<K>, +): IEither<Error, V> => { + const result = args.findIndex((_arg) => _arg.startsWith(arg)); + if (result < 0) return Either.left(new Error("no argument found for " + arg)); + const [resultArg, next]: Array<string | undefined> = [ + args[result], + args.at(result + 1), + ]; + if (resultArg && resultArg.includes("=")) { + return Either.right(resultArg.split("=")[1] as V); + } + if (typeof next === "undefined") + return Either.left(new Error("no value specified for " + arg)); + if (isArgKey(next)) + return Either.left( + new Error("next value for arg " + arg + " is another arg " + next), + ); + return Either.right(next as V); +}; + +type ObjectFromList<T extends ReadonlyArray<string>, V = string> = { + [K in T extends ReadonlyArray<infer U> ? U : never]: V; +}; +export const argv = <K extends string, V extends string>( + args: ReadonlyArray<K>, + defaultArgs?: Partial<Record<K, V>>, + argv = Deno.args, +) => { + return args + .map((arg) => [arg, getArg(arg, argv)] as [K, IEither<Error, V>]) + .map(([arg, specified]): [K, IEither<Error, V>] => [ + arg, + specified.fold((e, val) => { + const hasDefaultVal = e && defaultArgs && arg in defaultArgs; + if (hasDefaultVal) { + return Either.right<Error, V>(defaultArgs[arg]!); + } else if (!val || e) { + return Either.left<Error, V>(e ?? new Error("unknown")); + } + return Either.right<Error, V>(val); + }), + ]) + .reduce( + ( + acc: IEither<Error, ObjectFromList<typeof args, V>>, + x: [K, IEither<Error, V>], + ): IEither<Error, ObjectFromList<typeof args, V>> => { + const [arg, eitherVal] = x; + return acc.flatMap((args) => { + return eitherVal.mapRight((envValue) => ({ + ...args, + [arg]: envValue, + })); + }); + }, + Either.right({} as ObjectFromList<typeof args, V>), + ); +}; diff --git a/u/process/mod.ts b/u/process/mod.ts index 3f02d46..211e9a7 100644 --- a/u/process/mod.ts +++ b/u/process/mod.ts @@ -1,3 +1,4 @@ export * from "./env.ts"; export * from "./run.ts"; export * from "./validate_identifier.ts"; +export * from "./argv.ts"; diff --git a/u/server/activity/fourohfour.ts b/u/server/activity/fourohfour.ts index c09aef6..ed8c7eb 100644 --- a/u/server/activity/fourohfour.ts +++ b/u/server/activity/fourohfour.ts @@ -7,14 +7,10 @@ import { } from "@emprespresso/pengueno"; const messages = [ - "(≧ω≦)ゞ Oopsie! This endpoint has gone a-404-dable!", - "。゚(。ノωヽ。)゚。 Meow-t found! Your API call ran away!", - "404-bidden! But like...in a cute way (・`ω´・) !", - "(=①ω①=) This endpoint is hiss-terically missing!", - "┐(´∀`)┌ Whoopsie fluff! No API here!", - "(つ≧▽≦)つ Your data went on a paw-sible vacation!", - "(ꈍᴗꈍ) Uwu~ not found, but found our hearts instead!", - "ヽ(;▽;)ノ Eep! This route has ghosted you~", + "D: Meow-t found! Your API call ran away!", + "404-bidden! But like...in a cute way >:3 !", + ":o Your data went on a paw-sible vacation!", + "uwu~ not found, but found our hearts instead!", ]; const randomFourOhFour = () => messages[Math.random() * messages.length]; diff --git a/u/server/request.ts b/u/server/request.ts index 480ee69..c857f88 100644 --- a/u/server/request.ts +++ b/u/server/request.ts @@ -2,11 +2,10 @@ import { LogMetricTraceable } from "@emprespresso/pengueno"; const greetings = [ "hewwo :D", - "hiya cutie (✿◠‿◠)", - "boop! ૮・ᴥ・ა", - "sending virtual hugs! (づ。◕‿‿◕。)づ", - "stay pawsitive ₍^..^₎⟆", - "⋆。‧˚❆🐧❆˚‧。⋆", + "hiya cutie", + "boop!", + "sending virtual hugs!", + "stay pawsitive", ]; const penguenoGreeting = () => greetings[Math.floor(Math.random() * greetings.length)]; |