diff options
Diffstat (limited to 'lib/leftpadesque')
-rw-r--r-- | lib/leftpadesque/debug.ts | 8 | ||||
-rw-r--r-- | lib/leftpadesque/index.ts | 3 | ||||
-rw-r--r-- | lib/leftpadesque/memoize.ts | 14 | ||||
-rw-r--r-- | lib/leftpadesque/prepend.ts | 5 |
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)]!)); |