0.1.16 • Published 2 years ago
@holdenmatt/ts-utils v0.1.16
ts-utils
A few simple zero-dependency Typescript utilities I commonly use across projects.
Environment checks
getEnvironmentisDevelopmentisProductionisDebugisBrowserisServer
Basic type guards
isStringisArrayisRecordisNotNull
JSON types
JSONPrimitiveJSONArrayJSONObjectJSONValue
Simple perf timing
logElapsedTime
Usage:
const start = performance.now();
// Do stuff...
logElapsedTime(`We did some stuff`, start);Errors
HttpErrorAssertionErrorassertError(message: string)assertTrue(condition: boolean, message: string)
URLs
isValidUrl
Keyboard
modKey(shortcut: string): Returns a string like "⌘C" (on macOS) or "Ctrl+C" (on Windows/Linux), or undefined (on other platforms).isModKey(e: KeyboardEvent): boolean: Is the mod key (Ctrl on Windows/Linux, Command on macOS) pressed?
Logging
Add a logger:
addLogger((error: Error, cause?: unknown) => { ...do something })Log an error (with all added loggers):
logError(new Error("Uh oh), {more: "info"})By default, we console log to the browser only if isDebug is true.