1.0.15 • Published 11 months ago

@nerdware/ts-type-safety-utils v1.0.15

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

TypeScript type-guards and other type-safety utils for any environment 🎉ESM ✅ CommonJS ✅ NodeJS ✅ browsers ✅

npm package Test Workflow CodeCov pre-commit semantic-release License: MIT

🚀 Getting Started

This package provides a lightweight set of TypeScript type-guards and other type-safety utils to suit common use cases.

📦 Installation

Install the package using your package manager of choice:

npm:

npm install @nerdware/ts-type-safety-utils

yarn:

yarn add @nerdware/ts-type-safety-utils

🛠️ Usage

import { isPlainObject } from "@nerdware/ts-type-safety-utils";

// or const { isPlainObject } = require("@nerdware/ts-type-safety-utils");

const fooFn = (input: unknown) => {
  if (isPlainObject(input)) {
    // TS knows `input` is a plain object
  } else {
    // TS knows `input` is not a plain object
  }
};

⚙️ API

Is-x type-guards

FunctionDescription
isArrayChecks if a value is an array or readonly array
isBigIntChecks if a value is a BigInt
isBooleanChecks if a value is a boolean
isBufferChecks if a value is a NodeJS Buffer (this is a no-op in browser envs)
isDateChecks if a value is a Date
isErrorChecks if a value is an Error, an Error subclass, or DOMException
isFunctionChecks if a value is a function
isNullChecks if a value is null
isObjectLikeChecks if a value is "object-like" (typeof "object" and not null)
isPlainObjectChecks if a value is a plain object
isSafeIntegerChecks if a value is a safe integer
isStringChecks if a value is a string
isSymbolChecks if a value is a symbol
isUndefinedChecks if a value is undefined
isUnsafeNumberChecks if a value is typeof "number" (🚨 allows NaN and other "number" values)

Other type-guards

FunctionDescription
getTypeSafeErrorConverts any argument into an Error object
hasKeyChecks if an object contains the provided key
hasKeysChecks if an object contains all provided keys
safeJsonStringifyType-safe JSON.stringify which will not throw if the input contains circular references or BigInts

🤝 Contributing

Pull requests are welcome! Before you begin, please check existing GitHub Issues and Pull Requests to see if your idea is already in the pipeline. If not, here's a guide on how to contribute to this project. Thank you!

📝 License

All files, scripts, and source code contained herein are open-source software licensed under an MIT License.

See LICENSE for more information.

💬 Contact

Trevor Anderson — Trevor@Nerdware.cloud@TeeRevTweets

Dare Mighty Things.

1.0.15-next.1

11 months ago

1.0.15

11 months ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.2

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.3

1 year ago

1.0.0

1 year ago