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

format-quantity-with-sixteenths

v1.0.0

Published

Number formatter for imperial measurements with support for vulgar fractions

Downloads

3

Readme

format-quantity

npm version workflow status codecov.io downloads MIT License

Formats a number (or string that appears to be a number) as one would see it written in imperial measurements, e.g. "1 1/2" instead of "1.5". To use vulgar fraction characters like "⅞", pass true as the second argument.

For the inverse operation, converting a string (which may include mixed numbers or vulgar fractions) to a number, check out numeric-quantity or, if you're interested in parsing recipe ingredient strings, try parse-ingredient.

Installation

npm

# npm
npm i format-quantity

# yarn
yarn add format-quantity

Browser

In the browser, all exports including the formatQuantity function are available on the global object FormatQuantity.

<script src="https://unpkg.com/format-quantity"></script>
<script>
  console.log(FormatQuantity.formatQuantity(10.5)); // "10 1/2"
</script>

Usage

import { formatQuantity } from 'format-quantity';

formatQuantity(1.5); // "1 1/2"
formatQuantity(2.66); // "2 2/3"
formatQuantity(3.875, true); // "3⅞"

The return value will be null if the provided argument is not a number or a string that evaluates to a number using parseFloat. The return value will be an empty string ("") if the provided argument is 0 or "0" (this is done to fit the primary use case of recipe ingredient quantities).

Options

The second parameter to formatQuantity can be a boolean value or an options object.

vulgarFractions

| Type | Default | | --------- | ------: | | boolean | false |

Returns vulgar fractions when appropriate. This option has the same effect as passing a plain boolean value as the second parameter.

formatQuantity(3.875, { vulgarFractions: true }); // "3⅞"
// is the same as
formatQuantity(3.875, true); // "3⅞"

tolerance

| Type | Default | | -------- | ------: | | number | 0.009 |

This option determines how close the decimal portion of a number has to be to the actual quotient of a fraction to be considered a match. For example, consider the fraction 1⁄3: 1 ÷ 3 = 0.3333... repeating forever. The number 0.333 (333 thousandths) is not equivalent to 1⁄3, but it's very close. So even though 0.333 !== (1 / 3), formatQuantity(0.333) will return "1/3" the same as formatQuantity(1/3).

A lower tolerance increases the likelihood that formatQuantity will return a decimal representation instead of a fraction or mixed number since the matching algorithm will be stricter. An greater tolerance increases the likelihood that formatQuantity will return a fraction or mixed number, but at the risk of arbitrarily matching an incorrect fraction simply because it gets evaluated first (see src/index.ts for the actual order of evaluation).

// Low tolerance - returns a decimal since 0.333 is not close enough to 1/3
formatQuantity(0.333, { tolerance: 0.00001 }); // "0.333"
// High tolerance - matches "1/3" even for "3/10"
formatQuantity(0.3, { tolerance: 0.1 }); // "1/3"
// Way too high tolerance - incorrect result because thirds get evaluated before halves
formatQuantity(0.5, { tolerance: 0.5 }); // "1/3"

fractionSlash

| Type | Default | | --------- | ------: | | boolean | false |

Uses the fraction slash character ("\u2044") to separate the numerator and denominator instead of the regular "solidus" slash ("\u002f"). This option is ignored if the vulgarFractions option is also true.

formatQuantity(3.875, { fractionSlash: true }); // "3 7⁄8"
formatQuantity(3.875, { fractionSlash: true, vulgarFractions: true }); // "3⅞"

Other exports

| Name | Type | Description | | ------------------------ | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | | defaultTolerance | number | 0.009 | | fractionDecimalMatches | [number, VulgarFraction][] | List of fractions and the decimal values that are close enough to match them (inputs are evaluated against the decimal values in the order of this array) | | vulgarToPlainMap | object | Map of vulgar fraction characters to their equivalent ASCII strings ("⅓" to "1/3", "⅞" to "7/8", etc.) | | FormatQuantityOptions | interface | Shape of formatQuantity's second parameter, if not a boolean value | | VulgarFraction | type | The set of vulgar fraction characters ("\u00bc", "\u00bd", "\u00be", and "\u2150" through "\u215e") |