summaryrefslogtreecommitdiff
path: root/lib/leftpadesque
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2025-07-27 17:03:10 -0700
committerElizabeth Hunt <me@liz.coffee>2025-07-27 18:30:30 -0700
commit9970036d203ba2d0a46b35ba6fad21d49441cdd4 (patch)
treea585d13933bf4149dcb07e28526063d071453105 /lib/leftpadesque
downloadpengueno-9970036d203ba2d0a46b35ba6fad21d49441cdd4.tar.gz
pengueno-9970036d203ba2d0a46b35ba6fad21d49441cdd4.zip
hai
Diffstat (limited to 'lib/leftpadesque')
-rw-r--r--lib/leftpadesque/debug.ts8
-rw-r--r--lib/leftpadesque/index.ts3
-rw-r--r--lib/leftpadesque/memoize.ts14
-rw-r--r--lib/leftpadesque/prepend.ts5
4 files changed, 30 insertions, 0 deletions
diff --git a/lib/leftpadesque/debug.ts b/lib/leftpadesque/debug.ts
new file mode 100644
index 0000000..074e567
--- /dev/null
+++ b/lib/leftpadesque/debug.ts
@@ -0,0 +1,8 @@
+const _hasEnv = true; // Node.js always has access to environment variables
+
+const _env: 'development' | 'production' =
+ _hasEnv && (process.env.ENVIRONMENT ?? '').toLowerCase().includes('prod') ? 'production' : 'development';
+export const isProd = () => _env === 'production';
+
+const _debug = !isProd() || (_hasEnv && ['y', 't'].some((process.env.DEBUG ?? '').toLowerCase().startsWith));
+export const isDebug = () => _debug;
diff --git a/lib/leftpadesque/index.ts b/lib/leftpadesque/index.ts
new file mode 100644
index 0000000..1a644cb
--- /dev/null
+++ b/lib/leftpadesque/index.ts
@@ -0,0 +1,3 @@
+export * from './prepend';
+export * from './debug';
+export * from './memoize';
diff --git a/lib/leftpadesque/memoize.ts b/lib/leftpadesque/memoize.ts
new file mode 100644
index 0000000..2f0e87a
--- /dev/null
+++ b/lib/leftpadesque/memoize.ts
@@ -0,0 +1,14 @@
+import type { Callable } from '@emprespresso/pengueno';
+
+export const memoize = <R, F extends Callable<R>>(fn: F): F => {
+ const cache = new Map<string, R>();
+ return ((...args: unknown[]): R => {
+ const key = JSON.stringify(args);
+ if (cache.has(key)) {
+ return cache.get(key)!;
+ }
+ const res = fn(...args);
+ cache.set(key, res);
+ return res;
+ }) as F;
+};
diff --git a/lib/leftpadesque/prepend.ts b/lib/leftpadesque/prepend.ts
new file mode 100644
index 0000000..1819536
--- /dev/null
+++ b/lib/leftpadesque/prepend.ts
@@ -0,0 +1,5 @@
+export const prependWith = (arr: string[], prep: string) =>
+ Array(arr.length * 2)
+ .fill(0)
+ .map((_, i) => i % 2 === 0)
+ .map((isPrep, i) => (isPrep ? prep : arr[Math.floor(i / 2)]!));