1.4.1 • Published 1 month ago

@nerdware/ts-string-helpers v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

TypeScript utils to sanitize and validate strings in 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 utils to sanitize and validate strings in any environment.

The sanitize functions use reverse-regex patterns to strip unwanted characters from strings — even pesky zero-width control characters — leaving only the characters you want. This is useful for sanitizing user input and other untrusted data.

For each sanitize function, there's a corresponding validate function to ensure strings match a specific format.

📦 Installation

Install the package using your package manager of choice:

npm:

npm install @nerdware/ts-string-helpers

yarn:

yarn add @nerdware/ts-string-helpers

🛠️ Usage

Here's a simple example of how to use the sanitizeEmail and isValidEmail functions to sanitize and validate an email address before using it in a NodeJS Express route:

import { sanitizeEmail, isValidEmail } from "@nerdware/ts-string-helpers";
import express from "express";
import { UserModel } from "./models/my-user-model";

// or const { sanitizeEmail } = require("@nerdware/ts-string-helpers");

const app = express();

app.use(express.json());

app.post("/register", (req, res, next) => {
  // Sanitize the unknown `email` input before using it!
  const userEmail = sanitizeEmail(req.body.email);

  // Validate the sanitized email
  if (!isValidEmail(userEmail)) {
    return res.status(400).send("Invalid email address");
  }

  // Now you can safely use the sanitized value throughout the rest of your stack!🎉
  const newUser = UserModel.create({ email: userEmail });

  res.status(201).json(newUser);
});

⚙️ API

!TIP In the tables below, click on a function to view the exact regex pattern it uses. The more complex patterns are accompanied by in-source documentation/explanations.

Sanitizers

FunctionDescription
sanitizeAlphabeticRemoves non-alphabetic characters
sanitizeAlphabeticWithSpacesRemoves non-alphabetic/space characters
sanitizeAlphanumericRemoves non-alphanumeric characters
sanitizeAlphanumericWithSpacesRemoves non-alphanumeric/space characters
sanitizeBase64Removes invalid base64 characters
sanitizeBase64URLRemoves invalid base64URL characters
sanitizeEmailRemoves invalid email characters (see RFC 5322)
sanitizeHandleRemoves invalid social-handle characters
sanitizeHexRemoves non-hexadecimal characters
sanitizeIDRemoves non-alphanumeric characters which are not _, -, or #
sanitizeJsonStringRemoves characters which are not valid in stringified JSON
sanitizeJWTRemoves characters which are not valid in a JSON Web Token
sanitizeNumericRemoves non-numeric characters
sanitizePasswordRemoves non-alphanumeric characters which are not !, @, #, $, %, ^, &, or *
sanitizePhoneAlias of sanitizeNumeric
sanitizeURLRemoves invalid URL characters

Validators

FunctionDescription
isValidAlphabeticReturns true if value only contains alphabetic characters
isValidAlphabeticWithSpacesReturns true if value only contains alphabetic characters and/or spaces
isValidAlphanumericReturns true if value only contains alphanumeric characters
isValidAlphanumericWithSpacesReturns true if value only contains alphanumeric characters and/or spaces
isValidBase64Returns true if value is a valid base64 string
isValidBase64URLReturns true if value is a valid base64URL string
isValidCurrencyReturns true if value is a valid USD currency-formatted string
isValidEmailReturns true if value is a valid email address (see RFC 5322)
isValidHandleReturns true if value is a valid social account handle (e.g., @foo_user)
isValidHexReturns true if value only contains hexadecimal characters
isValidIDReturns true if value only contains alphanumeric characters, _, -, or #
isValidJsonStringReturns true is value only contains valid JSON characters
isValidJWTReturns true if value only contains valid JSON Web Token characters
isValidNumericReturns true if value only contains numeric characters
isValidPasswordReturns true if value is a valid password (see jsdoc for details)
isValidPhoneReturns true if value is a valid string of US phone number DIGITS
isValidURLReturns true if value is a valid absolute HTTP/S URL

🤝 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.4.1

1 month ago

1.3.0-next.1

2 months ago

1.4.0

2 months ago

1.3.0

2 months ago

1.2.5

2 months ago

1.2.4

2 months ago

1.2.3

2 months ago

1.2.2

2 months ago

1.2.0

3 months ago

1.2.1

3 months ago

1.1.2

4 months ago

1.1.1

4 months ago

1.1.0

4 months ago

1.0.4

4 months ago

1.0.3

4 months ago

1.0.2

4 months ago

1.0.0

4 months ago