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

commafy-anything

v4.0.0

Published

Adds comma's to a number. A simple and small integration

Downloads

3,731

Readme

Commafy anything 🍡

npm i commafy-anything

Return a number as string with , or K. A simple and small integration.

Motivation

U built this package because I needed to add comma's and K to numbers! And I wanted to build it myself. 😄

Meet the family (more tiny utils with TS support)

Usage

import { commafy } from 'commafy-anything'

commafy(1000) === '1,000'
commafy(10000) === '10,000'
commafy(100000) === '100,000'
commafy(1000000) === '1,000,000'
// etc.

K

You can show numbers as 1K.

const options = { K: true }
// when smaller than 1000 will be shown as is, without K
commafy(123, options) === '123'

// when larger than 1000 it will round up/down behind the K
commafy(1234, options) === '1K'
commafy(10234, options) === '10K'
commafy(100234, options) === '100K'
commafy(1000234, options) === '1,000K'

commafy(1955, options) === '2K'
commafy(10955, options) === '11K'
commafy(100955, options) === '101K'
commafy(1000955, options) === '1,001K'

Thousands

You can disable a comma to be added when the number is between 1000 ~ 9999.

// default:
commafy(1000) === '1,000'

const options = { thousandsComma: false }
commafy(1000, options) === '1000'
commafy(9999, options) === '9999'

// beyond 9999 it will always have a comma
commafy(10000, options) === '10,000'

Spaced decimals

You can add spaces to decimals.

// default:
commafy(1.0001) === '1.0001'
commafy(1.00001) === '1.00001'
commafy(1.000001) === '1.000001'
commafy(1.0000001) === '1.0000001'

// spaced decimals:
const options = { spacedDecimals: true }
commafy(1.0001, options) === '1.0001'
commafy(1.00001, options) === '1.000 01'
commafy(1.000001, options) === '1.000 001'
commafy(1.0000001, options) === '1.000 0001'

Strip decimals

You can add strip away decimals.

// default:
commafy(1.0001) === '1.0001'

// strip decimals:
const options = { stripDecimals: true }
commafy(1.001, options) === '1'
commafy(1.999, options) === '1'

Source code

I'm using simple regular expressions. The source code is in TypeScript, but the essense of my source code looks something like this:

function commafy(num, { stripDecimals, spacedDecimals } = {}) {
  const str = num.toString().split('.')
  if (str[0].length >= 4) {
    str[0] = str[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,')
  }
  if (stripDecimals) return str[0]
  if (spacedDecimals && str[1] && str[1].length >= 5) {
    str[1] = str[1].replace(/(\d{3})/g, '$1 ').trim()
  }
  return str.join('.')
}