@holdenmatt/ts-utils
TypeScript icon, indicating that this package has built-in type declarations

0.1.16 • Public • Published

ts-utils

A few simple zero-dependency Typescript utilities I commonly use across projects.

Environment checks

  • getEnvironment
  • isDevelopment
  • isProduction
  • isDebug
  • isBrowser
  • isServer

Basic type guards

  • isString
  • isArray
  • isRecord
  • isNotNull

JSON types

  • JSONPrimitive
  • JSONArray
  • JSONObject
  • JSONValue

Simple perf timing

  • logElapsedTime

Usage:

const start = performance.now();
// Do stuff...
logElapsedTime(`We did some stuff`, start);

Errors

  • HttpError
  • AssertionError
  • assertError(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.

Package Sidebar

Install

npm i @holdenmatt/ts-utils

Weekly Downloads

144

Version

0.1.16

License

MIT

Unpacked Size

22.8 kB

Total Files

6

Last publish

Collaborators

  • holdenmatt