format-quantity
Advanced tools
Comparing version 2.0.1 to 2.1.0-beta.0
{ | ||
"version": "2.0.1", | ||
"version": "2.1.0-beta.0", | ||
"name": "format-quantity", | ||
@@ -9,6 +9,14 @@ "author": "Jake Boone <jakeboone02@gmail.com>", | ||
], | ||
"main": "./dist/format-quantity.cjs.js", | ||
"module": "./dist/format-quantity.es.js", | ||
"unpkg": "./dist/format-quantity.umd.js", | ||
"typings": "dist/index.d.ts", | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/format-quantity.legacy-esm.js", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"types": "./dist/format-quantity.d.ts", | ||
"import": "./dist/format-quantity.mjs", | ||
"require": "./dist/cjs/index.js" | ||
} | ||
}, | ||
"types": "./dist/format-quantity.d.ts", | ||
"unpkg": "./dist/format-quantity.umd.min.js", | ||
"license": "MIT", | ||
@@ -23,3 +31,5 @@ "keywords": [ | ||
"fractions", | ||
"imperial" | ||
"imperial", | ||
"roman", | ||
"numerals" | ||
], | ||
@@ -35,26 +45,26 @@ "bugs": { | ||
"scripts": { | ||
"start": "vite", | ||
"build": "yarn build:main && yarn build:types", | ||
"build:main": "vite build", | ||
"build:types": "tsc --project ./tsconfig.build.json", | ||
"test": "jest --coverage", | ||
"start": "bun ./server.ts", | ||
"build": "tsup", | ||
"test": "jest", | ||
"watch": "jest --watch", | ||
"publish:npm": "np" | ||
"publish:npm": "np", | ||
"pretty-print": "prettier --write *.{mjs,ts,json} src/*.*" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.19.1", | ||
"@babel/preset-env": "^7.19.1", | ||
"@babel/preset-typescript": "^7.18.6", | ||
"@types/jest": "^29.0.3", | ||
"gh-pages": "^4.0.0", | ||
"jest": "^29.0.3", | ||
"np": "^7.6.2", | ||
"prettier": "^2.7.1", | ||
"typescript": "^4.8.3", | ||
"vite": "^3.1.3" | ||
"@types/jest": "^29.5.2", | ||
"@types/node": "^20.3.1", | ||
"bun-types": "^0.6.9", | ||
"jest": "^29.5.0", | ||
"np": "^8.0.4", | ||
"prettier": "^2.8.8", | ||
"ts-jest": "^29.1.0", | ||
"tsup": "^7.0.0", | ||
"typescript": "^5.1.3" | ||
}, | ||
"packageManager": "yarn@3.2.3", | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org" | ||
}, | ||
"engines": { | ||
"node": ">=16" | ||
} | ||
} |
109
README.md
# format-quantity | ||
[![npm version](https://badge.fury.io/js/format-quantity.svg)](//npmjs.com/package/format-quantity) | ||
![workflow status](https://github.com/jakeboone02/format-quantity/workflows/Continuous%20Integration/badge.svg) | ||
[![codecov.io](https://codecov.io/github/jakeboone02/format-quantity/coverage.svg?branch=master)](https://codecov.io/github/jakeboone02/format-quantity?branch=master) | ||
[![npm][badge-npm]](https://www.npmjs.com/package/format-quantity) | ||
![workflow status](https://github.com/jakeboone02/format-quantity/actions/workflows/main.yml/badge.svg) | ||
[![codecov.io](https://codecov.io/github/jakeboone02/format-quantity/coverage.svg?branch=main)](https://codecov.io/github/jakeboone02/format-quantity?branch=main) | ||
[![downloads](https://img.shields.io/npm/dm/format-quantity.svg)](http://npm-stat.com/charts.html?package=format-quantity&from=2015-08-01) | ||
[![MIT License](https://img.shields.io/npm/l/format-quantity.svg)](http://opensource.org/licenses/MIT) | ||
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. | ||
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". | ||
For the inverse operation, converting a string (which may include mixed numbers or vulgar fractions) to a `number`, check out [numeric-quantity](https://www.npmjs.com/package/numeric-quantity) or, if you're interested in parsing recipe ingredient strings, try [parse-ingredient](https://www.npmjs.com/package/parse-ingredient). | ||
Features: | ||
## Installation | ||
- To use vulgar fraction characters like "⅞", pass `true` as the second argument (see other [options](#options), like Roman numerals, below). | ||
- The return value will be `null` if the first argument is neither a number nor a string that evaluates to a number using `parseFloat`. | ||
- The return value will be an empty string (`""`) if the first argument is `0` or `"0"`, which is done to fit the primary use case of formatting recipe ingredient quantities. | ||
### npm | ||
> _For the inverse operation—converting a string to a `number`—check out [numeric-quantity](https://www.npmjs.com/package/numeric-quantity). It handles mixed numbers, vulgar fractions, comma/underscore separators, and Roman numerals._ | ||
> | ||
> _If you're interested in parsing recipe ingredient strings, try [parse-ingredient](https://www.npmjs.com/package/parse-ingredient)._ | ||
```shell | ||
# npm | ||
npm i format-quantity | ||
## Usage | ||
# yarn | ||
yarn add format-quantity | ||
### Installed | ||
```js | ||
import { formatQuantity } from 'format-quantity'; | ||
formatQuantity(1.5); // "1 1/2" | ||
formatQuantity(2.66); // "2 2/3" | ||
formatQuantity(3.875, true); // "3⅞" | ||
``` | ||
### Browser | ||
### CDN | ||
In the browser, all exports including the `formatQuantity` function are available on the global object `FormatQuantity`. | ||
As an ES module: | ||
```html | ||
<script type="module"> | ||
import { formatQuantity } from 'https://cdn.jsdelivr.net/npm/format-quantity/+esm'; | ||
console.log(formatQuantity(10.5)); // "10 1/2" | ||
</script> | ||
``` | ||
As UMD (all exports are properties of the global object `FormatQuantity`): | ||
```html | ||
<script src="https://unpkg.com/format-quantity"></script> | ||
@@ -36,14 +54,2 @@ <script> | ||
## Usage | ||
```js | ||
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 | ||
@@ -69,2 +75,15 @@ | ||
### `fractionSlash` | ||
| Type | Default | | ||
| --------- | ------: | | ||
| `boolean` | `false` | | ||
Uses the [fraction slash character](<https://en.wikipedia.org/wiki/Slash_(punctuation)#Fractions>) (`"\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`. | ||
```js | ||
formatQuantity(3.875, { fractionSlash: true }); // "3 7⁄8" | ||
formatQuantity(3.875, { fractionSlash: true, vulgarFractions: true }); // "3⅞" | ||
``` | ||
### `tolerance` | ||
@@ -76,5 +95,5 @@ | ||
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)`. | ||
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 \div 3 = 0.\overline{333}$, repeating forever. The number `0.333` (exactly 333 thousandths) is not equivalent to 1⁄3, but it's very close. So even though $0.333 \neq 1 \div 3$, both `formatQuantity(0.333)` and `formatQuantity(1/3)` will return `"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`](src/index.ts) for the actual order of evaluation). | ||
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 higher 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 (the export `fractionDecimalMatches` defines the order of evaluation). | ||
@@ -84,9 +103,9 @@ ```js | ||
formatQuantity(0.333, { tolerance: 0.00001 }); // "0.333" | ||
// High tolerance - matches "1/3" even for "3/10" | ||
// 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 | ||
// *Way* too high tolerance - incorrect result because thirds get evaluated before halves | ||
formatQuantity(0.5, { tolerance: 0.5 }); // "1/3" | ||
``` | ||
### `fractionSlash` | ||
### `romanNumerals` | ||
@@ -97,7 +116,9 @@ | Type | Default | | ||
Uses the [fraction slash character](<https://en.wikipedia.org/wiki/Slash_(punctuation)#Fractions>) (`"\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`. | ||
Coerces the number into an integer using `Math.floor`, then formats the value as Roman numerals. The algorithm uses strict, modern rules, so the number must be between 1 and 3999 (inclusive). | ||
When this option is `true`, all other options are ignored. | ||
```js | ||
formatQuantity(3.875, { fractionSlash: true }); // "3 7⁄8" | ||
formatQuantity(3.875, { fractionSlash: true, vulgarFractions: true }); // "3⅞" | ||
formatQuantity(1214, { romanNumerals: true }); // "MCCXIV" | ||
formatQuantity(12.14, { romanNumerals: true, vulgarFractions: true }); // "XII" | ||
``` | ||
@@ -107,8 +128,14 @@ | ||
| Name | Type | Description | | ||
| ------------------------ | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `defaultTolerance` | `number` | `0.0075` | | ||
| `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](https://en.wikipedia.org/wiki/Number_Forms) (`"\u00bc"`, `"\u00bd"`, `"\u00be"`, and `"\u2150"` through `"\u215e"`) | | ||
| Name | Type | Description | | ||
| ------------------------ | ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `defaultTolerance` | `number` | `0.0075` | | ||
| `defaultOptions` | `type` | Object representing the default options | | ||
| `fractionDecimalMatches` | <code>[number, VulgarFraction \| Sixteenth][]</code> | List of decimal values that are close enough to match the associated fraction (inputs are evaluated against the decimal values in the order of this array) | | ||
| `vulgarToAsciiMap` | `object` | Map of vulgar fraction characters to their equivalent ASCII strings (`"⅓"` to `"1/3"`, `"⅞"` to `"7/8"`, etc.) | | ||
| `formatRomanNumerals` | `function` | Formats a number as Roman numerals (used internally by `formatQuantity` when the `romanNumerals` option is `true`) | | ||
| `FormatQuantityOptions` | `interface` | Shape of `formatQuantity`'s second parameter (if not a `boolean` value) | | ||
| `SimpleFraction` | `type` | String template type for valid (positive, no division by zero) ASCII fraction strings with either one or two digits in the numerator and denominator each | | ||
| `VulgarFraction` | `type` | The set of [vulgar fraction characters](https://en.wikipedia.org/wiki/Number_Forms) (`"\u00bc"`, `"\u00bd"`, `"\u00be"`, and `"\u2150"` through `"\u215e"`) | | ||
| `Sixteenth` | `type` | Union type of all ASCII representations of odd-numbered sixteenth fractions less than one, (`"1/16"`, `"3/16"`, etc.) | | ||
[badge-npm]: https://img.shields.io/npm/v/numeric-quantity.svg?cacheSeconds=3600&logo=npm |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86461
9
17
630
136
1
1
1