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

@salesvista/format

v1.0.1

Published

Format some numbers

Downloads

1

Readme

@salesvista/format

Format some numbers

CI Status Coverage Status JavaScript Style Guide Conventional Commits

A utility package to consistently format numbers as either currency, percentages, or decimals in a locale-specific way.

Install

$ npm i @salesvista/format
// with babel
import format from '@salesvista/format'
// without babel
const format = require('@salesvista/format')

Usage

This package contains a single module. The main export is an object containing utility functions and helper constants. When you import/require this module, you can either keep the object reference (to have all functions available at the same namespace) or decompose into only the desired functions.

// all functions under one namespace
const format = require('@salesvista/format')
// or pick the functions you want
const { formatAs } = require('@salesvista/format')

The API documentation below assumes the former.

API

The following functions are listed in alphabetical order.

format.formatAs(value, valueType, opts)

Format a given number value into a locale-specific string, interpreted as the given type. Rounding may be applied, depending on the scenario.

value should be a number or string representing the value to format.

valueType should be one of:

  • format.CURRENCY to format value as currency
  • format.PERCENTAGE to format value as a percentage
  • format.UNIT to format value as volume (decimal)

The following opts are supported:

  • locale (string, default 'en'): a BCP 47 language tag
  • currency (string, default 'usd'): an ISO 4217 currency code
  • _terse (boolean, default false): pass true to override default decimal display for currency values that are integers
  • any property supported by the Intl.NumberFormat constructor, such as style, minimumFractionDigits, maximumFractionDigits, etc

Examples:

format.formatAs(0, format.CURRENCY)
// => '$0.00'
format.formatAs(0, format.CURRENCY, format.terse())
// => '$0'
format.formatAs(12345.12345, format.CURRENCY)
// => '$12,345.12'
format.formatAs(12345.12345, format.CURRENCY, { locale: 'fr-FR', currency: 'eur' })
// => '12 345,12 €'
format.formatAs(0, format.PERCENTAGE)
// => '0%'
format.formatAs(25 / 12, format.PERCENTAGE)
// => '2.08%'

format.formatAttainmentPercentage(attainment, target, opts)

Calculate percentage attained and return as a formatted string in a locale-specific way.

attainment should be the actual value (number) attained for a given time period.

target should be the target value (number) representing the goal for the given time period.

The percentage will be calculated using format.toAttainmentPercentage(attainment, target, opts) and formatted using format.formatAs(percentage, format.PERCENTAGE, opts). See documentation for those functions.

Examples:

format.formatAttainmentPercentage(5, 9)
// => '55.56%'
format.formatAttainmentPercentage(5, 9, { locale: 'fr-FR' })
// => '55,56 %'
format.formatAttainmentPercentage(25, 12)
// => '208.33%'
format.formatAttainmentPercentage(1, 0)
// => '100%'
format.formatAttainmentPercentage(0, 1)
// => '0%'

format.isNumber(value)

Returns a boolean representing if the value represents a number.

format.isValidType(valueType)

Returns a boolean representing if the given value type is valid/known to this library.

format.round(value)

Returns the given value converted to a number via format.toNumber(value) and rounded to two decimal places.

format.terse(opts)

Convenience function to apply a _terse: true option to the given opts object.

format.toAttainmentPercentage(attainment, target, opts)

Calculate percentage attained as (attainment / target) * 100, rounded to two decimal places by default, returned as a number.

attainment should be the actual value (number) attained for a given time period.

target should be the target value (number) representing the goal for the given time period.

The following opts are supported:

  • rounded (boolean, default true): pass false to skip rounding of the returned value

format.toNumber(value)

Convert the given value to a number, if possible. If not possible, returns null.

format.trim(value)

Helper function to trim whitespace off a string, used internally by format.isNumber(value).

Releasing

After one or more PRs have been merged to master, you can cut a new release with the following commands:

# update local master branch
git checkout master && git pull origin master
# make sure tests pass
npm it
# bump version, update changelog, and create git tag
npm run release
# push release to github
git push -u --follow-tags origin master
# publish release to npm
npm publish --access public

Then you can update the version referenced by any apps/packages that use this as a dependency.