npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@skipper116/string-utilities

v1.0.2

Published

A collection of string manipulation and validation utilities for JS/TS projects.

Downloads

3

Readme

@skipper116/string-utilities

@skipper116/string-utilities is a lightweight collection of string manipulation and validation utilities built for JavaScript and TypeScript projects. This package provides functions to trim, convert, validate, and generate string values efficiently.

Installation

You can install the package via npm:

npm install @skipper116/string-utilities

Usage

To use any of the functions in your JavaScript or TypeScript project, import them from the package as shown in the examples below.

Example 1: Basic String Manipulation

import {
  trimAndLowercase,
  titleCase,
  kebabCase,
  reverseString,
} from "@skipper116/string-utilities";

// Trim and convert to lowercase
const lower = trimAndLowercase("  Hello World  ");
console.log(lower); // Output: 'hello world'

// Convert a string to Title Case
const title = titleCase("hello world from string utilities");
console.log(title); // Output: 'Hello World From String Utilities'

// Convert a string to Kebab Case
const kebab = kebabCase("Hello World From String Utilities");
console.log(kebab); // Output: 'hello-world-from-@skipper116/string-utilities'

// Reverse a string
const reversed = reverseString("Hello");
console.log(reversed); // Output: 'olleH'

Example 2: String Validations

import {
  emailValidation,
  urlValidation,
  phoneNumberValidation,
  passwordStrengthValidation,
  malawiPhoneNumberValidation,
  customPhoneNumberValidation,
  ipV4Validation,
  ipV6Validation,
} from "@skipper116/string-utilities";

// Validate an email
const isEmailValid = emailValidation("[email protected]");
console.log(isEmailValid); // Output: true

// Validate a URL
const isUrlValid = urlValidation("https://www.example.com");
console.log(isUrlValid); // Output: true

const isIpV4Valid = ipV4Validation("192.168.0.1");
console.log(isIpV4Valid); // Output: true

const isIpV6Valid = ipV6Validation("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
console.log(isIpV6Valid); // Output: true

// Validate a phone number (E.164 format)
const isPhoneValid = phoneNumberValidation("+1234567890");
console.log(isPhoneValid); // Output: true

// Validate password strength (minimum 8 characters, at least one letter and one number)
const isPasswordStrong = passwordStrengthValidation("Password123");
console.log(isPasswordStrong); // Output: true

// Validate a Malawi phone number
const isMalawiPhoneValid = malawiPhoneNumberValidation("0999123456");
console.log(isMalawiPhoneValid); // Output: true

// Validate a custom phone number format
const isCustomPhoneValid = customPhoneNumberValidation("+1-123-456-7890", "1");
console.log(isCustomPhoneValid); // Output: true

Example 3: Custom Find and Replace

import { findAndReplace } from "@skipper116/string-utilities";

// Find and replace occurrences of a string
const newString = findAndReplace("Hello World! Hello Everyone!", "Hello", "Hi");
console.log(newString); // Output: 'Hi World! Hi Everyone!'

Example 4: Generate Unique IDs

import { generateUniqueId } from "@skipper116/string-utilities";

// Generate a unique ID with an optional prefix
const uniqueId = generateUniqueId("user");
console.log(uniqueId); // Output: 'user_xxxxxxx' (random characters)
import { generateUUIDv7 } from "@skipper116/string-utilities";

const uuid = generateUUIDv7();
console.log(uuid); // Output: 'xxxxxxxx-xxxx-7xxx-yxxx-xxxxxxxxxxxx' (random characters)

Example 5: Alphanumeric and Length Check Validations

import { alphanumericCheck, lengthCheck } from "@skipper116/string-utilities";

// Check if a string is alphanumeric
const isAlphanumeric = alphanumericCheck("abc123");
console.log(isAlphanumeric); // Output: true

// Validate the length of a string
const isLengthValid = lengthCheck("Hello World", 5, 20);
console.log(isLengthValid); // Output: true

API Reference

String Manipulation Functions

  1. trimAndLowercase(str: string): string
    Trims the whitespace from the beginning and end of the string and converts the entire string to lowercase.

    Example:

    const result = trimAndLowercase("  HELLO world  ");
    console.log(result); // 'hello world'
  2. kebabCase(str: string): string
    Converts a string to kebab case (lowercase with hyphens).

    Example:

    const result = kebabCase("Hello World");
    console.log(result); // 'hello-world'
  3. camelCase(str: string): string
    Converts a string to camel case (lowercase with the first letter of each word capitalized).

    Example:

    const result = camelCase("hello world from string utilities");
    console.log(result); // 'helloWorldFromStringUtilities'
  4. snakeCase(str: string): string Converts a string to snake case (lowercase with underscores).

    Example:

    const result = snakeCase("Hello World");
    console.log(result); // 'hello_world'
  5. pascalCase(str: string): string Converts a string to pascal case (uppercase with the first letter of each word capitalized).

    Example:

    const result = pascalCase("hello world from string utilities");
    console.log(result); // 'HelloWorldFromStringUtilities'
  6. titleCase(str: string): string
    Converts a string to title case, where the first letter of each word is capitalized.

    Example:

    const result = titleCase("hello world from string utilities");
    console.log(result); // 'Hello World From String Utilities'
  7. reverseString(str: string): string
    Reverses the characters in a given string.

    Example:

    const result = reverseString("Hello");
    console.log(result); // 'olleH'
  8. findAndReplace(str: string, find: string, replace: string): string
    Finds all occurrences of a substring and replaces them with another string.

    Example:

    const result = findAndReplace("Hello World! Hello Everyone!", "Hello", "Hi");
    console.log(result); // 'Hi World! Hi Everyone!'
  9. generateUniqueId(prefix?: string): string
    Generates a unique ID with an optional prefix. The unique ID is based on a random string.

    Example:

    const result = generateUniqueId("user");
    console.log(result); // 'user_xxxxxxxx' (random characters)

String Validation Functions

  1. emailValidation(email: string): boolean
    Validates if the given string is a valid email format.

    Example:

    const isValid = emailValidation("[email protected]");
    console.log(isValid); // true
  2. urlValidation(url: string): boolean
    Validates if the given string is a valid URL format.

    Example:

    const isValid = urlValidation("https://www.example.com");
    console.log(isValid); // true
  3. phoneNumberValidation(phoneNumber: string): boolean
    Validates if the given string is a valid phone number format in E.164 standard.

    Example:

    const isValid = phoneNumberValidation("+1234567890");
    console.log(isValid); // true
  4. passwordStrengthValidation(password: string): boolean
    Validates if the password meets minimum strength criteria: at least 8 characters long, and contains both letters and numbers.

    Example:

    const isValid = passwordStrengthValidation("Password123");
    console.log(isValid); // true
  5. alphanumericCheck(str: string): boolean
    Checks if the string contains only alphanumeric characters (letters and numbers).

    Example:

    const isValid = alphanumericCheck("abc123");
    console.log(isValid); // true
  6. lengthCheck(str: string, min: number, max: number): boolean
    Validates if the string length is within a specified range (inclusive).

    Example:

    const isValid = lengthCheck("Hello World", 5, 20);
    console.log(isValid); // true
  7. isBlank(value: any): boolean
    Checks if the given value is a blank string, null, or undefined.

    Example:

    const isValid = isBlank("");
    console.log(isValid); // true
  8. isPresent(value: any): boolean
    Checks if the given value is not a blank string, null, or undefined.

    Example:

    const isValid = isPresent("Hello");
    console.log(isValid); // true

Typescript Support

This package is fully typed with TypeScript, so you will get type definitions and autocompletion in supported editors.

Example

import {
  generateUniqueId,
  passwordStrengthValidation,
} from "@skipper116/string-utilities";

const userId: string = generateUniqueId("user");
const isStrongPassword: boolean = passwordStrengthValidation("MyPassword123");

console.log(userId); // Output: user_xxxxxxxx
console.log(isStrongPassword); // Output: true

Contributing

We welcome contributions to improve this package. If you want to contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Submit a pull request with a detailed explanation.

More details can be found in the CONTRIBUTING.md file.


License

This project is licensed under the MIT License - see the LICENSE file for details.