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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ez-funcs

v3.1.0

Published

A list of utility functions that can be used in a variety of typescript and javascript projects.

Readme

Easy Functions

This is a collection easy utility functions I found myself copying and pasting over various personal and work projects. I am not going to gate-keep the code, that will be provided in this README as well if you just want to have them in your code base without installing the entire project. Having this project makes things easier for me, but I am not going to pretend like every project on the planet is going to need a "capitalizeFirstLetters" function.

Table of Contents

Features

  • Several utility functions that are useful in many projects.
  • Source code to all of those functions is available in this README.
  • No dependencies, just copy and paste the code you need.

Installing

Using npm:

npm install ez-funcs

Using yarn:

yarn add ez-funcs

Using bun:

bun install ez-funcs

Using pnpm:

pnpm add ez-funcs

Using bower:

bower install ez-funcs

Once the package is installed, you can import the library using import approach:

import { tryCatch } from 'ez-funcs';

Capitalize First Letters

This functions takes a string and returns the same string with the first letters of each word capitalized.

capitalizeFirstLetters Example

import { capitalizeFirstLetters } from 'ez-funcs';

const text = 'hello world';

const newText = capitalizeFirstLetters(text);

console.log(newText); // Output: Hello World

capitalizeFirstLetters Source Code

export function capitalizeFirstLetters(str: string | null) {
  if (!str) return '';
  return str
    .split(' ')
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
}

Create Dollar Amount

This function takes a number or a string representing a number and returns formatted dollar amount as a string. It supports different currencies and handles invalid inputs gracefully.

createDollaraAmount Example

import { createDollarAmount } from 'ez-funcs';

const amountString = '200';

const newAmountOne = createDollarAmount(amountString);

console.log(newAmountOne); // Output: $200.00

const amountNumber = 200;

const newAmountTwo = createDollarAmount(amountNumber);

console.log(newAmountTwo); // Output: $200.00

createDollarAmount Source Code

export function createDollarAmount(
  amountInput: number | string,
  currency: SupportedCurrency = 'USD'
): string {
  if (amountInput === null || amountInput === undefined || amountInput === '') {
    const defaultAmount = 0; // Default amount if input is null, undefined, or empty string
    return defaultAmount.toLocaleString(CURRENCY_LOCALE_MAP[currency], {
      style: 'currency',
      currency: currency
    });
  }
  const numericAmount = Number(amountInput);

  // 1. Handle invalid numeric input
  if (isNaN(numericAmount)) {
    // You can decide how to handle this:
    // - Throw an error (often preferred for library functions)
    // - Return a specific string like "Invalid Amount" or an empty string
    // - Return a default formatted value like $0.00
    // For this example, let's throw an error.
    throw new TypeError(
      "Invalid 'amountInput': failed to convert to a number."
    );
  }

  // 2. Get the locale from the map
  // This replaces the large switch statement.
  const locale = CURRENCY_LOCALE_MAP[currency];

  // 3. Use a try-catch block for the toLocaleString call
  // This can catch errors if the locale or options are somehow invalid,
  // though with the current setup, it's less likely for supported currencies.
  try {
    return numericAmount.toLocaleString(locale, {
      style: 'currency',
      currency: currency // The 'currency' option takes the ISO currency code
    });
  } catch (error) {
    console.error(
      `Error formatting currency ${currency} for locale ${locale}:`,
      error
    );
    // Fallback behavior: re-throw, or return a specific error string,
    // or even a basic formatting if possible.
    // For now, let's return an error message string.
    return `Error formatting ${currency}`; // Or throw the error
  }
}

Extract Number

This function extracts a number from a string, removing all non-numeric characters except for commas and decimal points. It returns the number as a float or null if the number cannot be parsed.

extractNumber Example

import { extractNumber } from 'ez-funcs';

const inputString = '2,812.30 refund minus 25% CANCELLATION FEE.';

const result = extractNumber(inputString);

console.log(result); // Output: 2812.3

extractNumber Source Code

export function extractNumber(input: number | string) {
  input = input.toString();

  // Remove all characters except digits, commas, and decimal points
  const cleaned = input.replace(/[^0-9.,]/g, '');
  // Remove commas
  const normalized = cleaned.replace(/,/g, '');
  // Parse as float
  const number = parseFloat(normalized);
  return isNaN(number) ? null : number;
}

Format Phone

This function formats a phone number string into a standard format of (123) 456-7890. It supports both 10-digit and 11-digit numbers, where the latter starts with a '1'.

formatPhone Example

import { formatPhone } from 'ez-funcs';

const phoneNumber = '1234567890';

const formattedPhone = formatPhone(phoneNumber);

console.log(formattedPhone); // Output: (123) 456-7890

formatPhone Source Code

export function formatPhone(phoneNumber: string) {
  const cleaned = ('' + phoneNumber).replace(/\D/g, '');

  // Check for 10-digit number
  let match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);

  // If no match, check for 11-digit number starting with 1
  if (!match && cleaned.length === 11 && cleaned.charAt(0) === '1') {
    match = cleaned.match(/^1(\d{3})(\d{3})(\d{4})$/);
  }

  if (match) {
    return `(${match[1]}) ${match[2]}-${match[3]}`;
  }

  return null;
}

Trim String Properties

This function takes an object and returns a new object with all string properties trimmed of leading and trailing whitespace. Non-string properties are left unchanged.

trimStringProperties Example

import { trimStringProperties } from 'ez-funcs';

const userInput = {
  name: '  John Doe  ',
  email: '  [email protected]  ',
  age: 30,
  isActive: true
};

const cleanedInput = trimStringProperties(userInput);

console.log(cleanedInput);
// Output: { name: 'John Doe', email: '[email protected]', age: 30, isActive: true }

trimStringProperties Source Code

export function trimStringProperties<T extends Record<string, unknown>>(obj: T): T {
  const result = {} as T;

  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      const value = obj[key];
      if (typeof value === 'string') {
        result[key as keyof T] = value.trim() as T[keyof T];
      } else {
        result[key as keyof T] = value as T[keyof T];
      }
    }
  }

  return result;
}

Truncate Text

This function truncates a string to a specified length and appends an ellipsis (...) if the string exceeds that length. It also ensures that the string does not end with a space before the ellipsis.

truncateText Example

import { truncateText } from 'ez-funcs';

const text 'Imagine this is super long text that needs to be truncated';

const truncatedText = truncateText(text, 20);

console.log(truncatedText); // Output: Imagine this is super long...

truncateText Source Code

export function truncateText(text: string, length: number) {
  if (text.length <= length) {
    return text;
  }
  return text.slice(0, length) + '...';
}

Try Catch

This function executes a given function and catches any errors that occur in a much more elegant way than the built in try-catch functionality. It returns an object with the result or error. This functions exists in every project I create and I highly recommend using it. However, if you have came up with a better solution please send it my way! Also, I am not going to try to take credit for this one this comes from Theo, if you don't watch his YouTube videos maybe give them a shot! https://www.youtube.com/@t3dotgg.

tryCatch Example

import { tryCatch } from 'ez-funcs';

async function something() {
  const { data, error } = await tryCatch(async () => {
    const response = await fetch('/api/some-endpoint');
    return response.json();
  });

  if (error) {
    console.error('Error fetching data:', error);
  } else {
    console.log('Fetched data:', data);
  }
}

tryCatch Source Code

// Types for the result object with discriminated union
type Success<T> = {
  data: T;
  error: null;
};

type Failure<E> = {
  data: null;
  error: E;
};

type Result<T, E = Error> = Success<T> | Failure<E>;

// Main wrapper function
export async function tryCatch<T, E = Error>(
  promise: Promise<T>
): Promise<Result<T, E>> {
  try {
    const data = await promise;
    return { data, error: null };
  } catch (error) {
    return { data: null, error: error as E };
  }
}

If you have any suggestions visit the contact me page on my website! -> Contact Me