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

formatting-utils

v1.0.1

Published

A utility package to format various data types such as dates, numbers, strings, phone numbers, and URLs.

Downloads

1

Readme

Formatting Utils

A utility package to format various data types such as dates, numbers, and strings.

Installation

npm install formatting-utils

Usage

const { formatDate, customFormatDate, formatTime, formatNumber, capitalize, decapitalize, formatPhoneNumber, dateDifference, trimString, truncateString, formatURL, pluralize } = require('formatting-utils');

// Test formatDate
const date = new Date();
console.log('Short date format:', formatDate(date, 'short')); // Example: Jan 1, 2024
console.log('Long date format:', formatDate(date, 'long'));  // Example: Monday, January 1, 2024
console.log('Custom date format (DD-YYYY-MM):', customFormatDate(date, 'DD-YYYY-MM')); // Example: 01-2024-01
console.log('Custom date format (MM-YYYY-DD):', customFormatDate(date, 'MM-YYYY-DD')); // Example: 01-2024-01

// Test formatTime
console.log('Short time format (12-hour):', formatTime(date, 'short', true)); // Example: 12:34 PM
console.log('Short time format (24-hour):', formatTime(date, 'short', false)); // Example: 12:34
console.log('Medium time format (12-hour):', formatTime(date, 'medium', true)); // Example: 12:34:56 PM
console.log('Medium time format (24-hour):', formatTime(date, 'medium', false)); // Example: 12:34:56
console.log('Long time format (12-hour):', formatTime(date, 'long', true)); // Example: 12:34:56 PM GMT
console.log('Long time format (24-hour):', formatTime(date, 'long', false)); // Example: 12:34:56 GMT

// Test formatNumber
console.log('Decimal number format:', formatNumber(1234567.89)); // Example: 1,234,567.89
console.log('Currency number format:', formatNumber(1234567.89, 'currency')); // Default currency: $1,234,567.89
console.log('Currency number format with EUR:', formatNumber(1234567.89, 'currency', 'EUR')); // Example: €1,234,567.89

// Test capitalize
console.log('Capitalized string:', capitalize('hello world')); // Example: Hello World

// Test decapitalize
console.log('Decapitalized string:', decapitalize('Hello World')); // Example: hello world

// Test formatPhoneNumber
console.log('Formatted phone number (US):', formatPhoneNumber('1234567890', 'US')); // Example: (123) 456-7890

// Test dateDifference
const date1 = new Date('2024-01-01');
const date2 = new Date('2024-12-31');
console.log('Date difference in days:', dateDifference(date1, date2, 'days')); // Example: 365
console.log('Date difference in months:', dateDifference(date1, date2, 'months')); // Example: 12
console.log('Date difference in years:', dateDifference(date1, date2, 'years')); // Example: 1

// Test trimString
console.log('Trimmed string:', trimString('   Hello World   ')); // Example: Hello World

// Test truncateString
console.log('Truncated string:', truncateString('Hello World', 5)); // Example: Hello...

// Test formatURL
console.log('Formatted URL (default https):', formatURL('example.com')); // Example: https://example.com
console.log('Formatted URL (http):', formatURL('example.com', 'http')); // Example: http://example.com

// Test pluralize
console.log('Pluralized word (1 apple):', pluralize('apple', 1)); // Example: apple
console.log('Pluralized word (2 apples):', pluralize('apple', 2)); // Example: apples

Functions

  • formatDate(date, format): Formats a date according to the specified format ('short', 'long'). For custom formats, use customFormatDate.
  • customFormatDate(date, format): Custom date formatting using 'DD', 'MM', 'YYYY' placeholders.
  • formatTime(date, format, hour12): Formats a time according to the specified format ('short', 'medium', 'long') and whether to use 12-hour clock (default false for 24-hour).
  • formatNumber(number, style, currency, locale): Formats a number according to the specified style ('decimal', 'currency', 'percent') and locale (default 'en-US').
  • capitalize(str): Capitalizes the first letter of each word in a string.
  • decapitalize(str): Decapitalizes the first letter of each word in a string.
  • formatPhoneNumber(number, country): Formats a phone number based on the specified country (default 'US'). (RIGHT NOW ONLY THERE IS US)
  • dateDifference(date1, date2, unit): Calculates the difference between two dates in the specified unit ('days', 'months', 'years').
  • trimString(str): Trims whitespace from both ends of a string.
  • truncateString(str, length): Truncates a string to the specified length, appending '...' if truncated.
  • formatURL(url, protocol): Ensures a URL starts with the specified protocol (default 'https').
  • pluralize(word, count): Returns the plural form of a word based on the count.
  • formatNumber(number, style): Formats a number according to the specified style ('decimal', 'currency', 'percent').