Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

format-quantity

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

format-quantity - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

dist/constants.d.ts

5

CHANGELOG.md

@@ -0,1 +1,6 @@

## 1.0.2 (2022-04-16)
- Update dependencies
- Migrate from tsdx to Vite
## 1.0.1 (2021-02-15)

@@ -2,0 +7,0 @@

2

dist/format-quantity.cjs.js

@@ -1,2 +0,2 @@

"use strict";const r=(f,e)=>Math.abs(f-e)<.009;function c(f,e){const n=typeof f=="string"?parseFloat(f):f;if(isNaN(n)||n===null)return null;if(n===0)return"";const s=Math.abs(n),o=Math.floor(s),u=o===0?"":`${n<0?"-":""}${o} `,t=s-o;if(t===0)return`${n}`;const $=e?u.trim():u;if(r(t,.33))return`${$}${e?"\u2153":"1/3"}`;if(r(t,.66))return`${$}${e?"\u2154":"2/3"}`;if(r(t,.2))return`${$}${e?"\u2155":"1/5"}`;if(r(t,.4))return`${$}${e?"\u2156":"2/5"}`;if(r(t,.6))return`${$}${e?"\u2157":"3/5"}`;if(r(t,.8))return`${$}${e?"\u2158":"4/5"}`;switch(t){case .125:return`${$}${e?"\u215B":"1/8"}`;case .25:return`${$}${e?"\xBC":"1/4"}`;case .375:return`${$}${e?"\u215C":"3/8"}`;case .5:return`${$}${e?"\xBD":"1/2"}`;case .625:return`${$}${e?"\u215D":"5/8"}`;case .75:return`${$}${e?"\xBE":"3/4"}`;case .875:return`${$}${e?"\u215E":"7/8"}`}return`${n}`}module.exports=c;
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i=.009,n={"\xBC":"1/4","\xBD":"1/2","\xBE":"3/4","\u2150":"1/7","\u2151":"1/9","\u2152":"1/10","\u2153":"1/3","\u2154":"2/3","\u2155":"1/5","\u2156":"2/5","\u2157":"3/5","\u2158":"4/5","\u2159":"1/6","\u215A":"5/6","\u215B":"1/8","\u215C":"3/8","\u215D":"5/8","\u215E":"7/8"},d=[[.33,"\u2153"],[.66,"\u2154"],[.2,"\u2155"],[.4,"\u2156"],[.6,"\u2157"],[.8,"\u2158"],[.166,"\u2159"],[.833,"\u215A"],[.143,"\u2150"],[.111,"\u2151"],[.1,"\u2152"],[.125,"\u215B"],[.25,"\xBC"],[.375,"\u215C"],[.5,"\xBD"],[.625,"\u215D"],[.75,"\xBE"],[.875,"\u215E"]],h=(a,t,u)=>Math.abs(a-t)<u,M=(a,t)=>{var f;const u=typeof a=="string"?parseFloat(a):a;if(isNaN(u)||u===null)return null;if(u===0)return"";const l=Math.abs(u),r=Math.floor(l),c=`${u<0?"-":""}${r===0?"":`${r} `}`,s=l-r;if(s===0)return`${u}`;const o=typeof t=="boolean"?{vulgarFractions:t}:t!=null?t:{},g=o.vulgarFractions?c.trim():c,m=e=>o.vulgarFractions?e:o.fractionSlash?n[e].replace("/","\u2044"):n[e];for(const[e,B]of d)if(h(s,e,(f=o.tolerance)!=null?f:i))return`${g}${m(B)}`;return`${u}`};exports.default=M;exports.defaultTolerance=i;exports.formatQuantity=M;exports.fractionDecimalMatches=d;exports.vulgarToPlainMap=n;
//# sourceMappingURL=format-quantity.cjs.js.map

@@ -1,3 +0,45 @@

const closeEnough = (a, b) => Math.abs(a - b) < 9e-3;
function formatQuantity(qty, useVulgarFractions) {
const defaultTolerance = 9e-3;
const vulgarToPlainMap = {
"\xBC": "1/4",
"\xBD": "1/2",
"\xBE": "3/4",
"\u2150": "1/7",
"\u2151": "1/9",
"\u2152": "1/10",
"\u2153": "1/3",
"\u2154": "2/3",
"\u2155": "1/5",
"\u2156": "2/5",
"\u2157": "3/5",
"\u2158": "4/5",
"\u2159": "1/6",
"\u215A": "5/6",
"\u215B": "1/8",
"\u215C": "3/8",
"\u215D": "5/8",
"\u215E": "7/8"
};
const fractionDecimalMatches = [
[0.33, "\u2153"],
[0.66, "\u2154"],
[0.2, "\u2155"],
[0.4, "\u2156"],
[0.6, "\u2157"],
[0.8, "\u2158"],
[0.166, "\u2159"],
[0.833, "\u215A"],
[0.143, "\u2150"],
[0.111, "\u2151"],
[0.1, "\u2152"],
[0.125, "\u215B"],
[0.25, "\xBC"],
[0.375, "\u215C"],
[0.5, "\xBD"],
[0.625, "\u215D"],
[0.75, "\xBE"],
[0.875, "\u215E"]
];
const closeEnough = (n1, n2, tolerance) => Math.abs(n1 - n2) < tolerance;
const formatQuantity = (qty, options) => {
var _a;
const dQty = typeof qty === "string" ? parseFloat(qty) : qty;

@@ -12,3 +54,3 @@ if (isNaN(dQty) || dQty === null) {

const iFloor = Math.floor(dQtyAbs);
const sFloor = iFloor === 0 ? "" : `${dQty < 0 ? "-" : ""}${iFloor} `;
const sFloor = `${dQty < 0 ? "-" : ""}${iFloor === 0 ? "" : `${iFloor} `}`;
const dDecimal = dQtyAbs - iFloor;

@@ -18,36 +60,13 @@ if (dDecimal === 0) {

}
const sFloorFinal = useVulgarFractions ? sFloor.trim() : sFloor;
if (closeEnough(dDecimal, 0.33)) {
return `${sFloorFinal}${useVulgarFractions ? "\u2153" : "1/3"}`;
} else if (closeEnough(dDecimal, 0.66)) {
return `${sFloorFinal}${useVulgarFractions ? "\u2154" : "2/3"}`;
} else if (closeEnough(dDecimal, 0.2)) {
return `${sFloorFinal}${useVulgarFractions ? "\u2155" : "1/5"}`;
} else if (closeEnough(dDecimal, 0.4)) {
return `${sFloorFinal}${useVulgarFractions ? "\u2156" : "2/5"}`;
} else if (closeEnough(dDecimal, 0.6)) {
return `${sFloorFinal}${useVulgarFractions ? "\u2157" : "3/5"}`;
} else if (closeEnough(dDecimal, 0.8)) {
return `${sFloorFinal}${useVulgarFractions ? "\u2158" : "4/5"}`;
} else {
switch (dDecimal) {
case 0.125:
return `${sFloorFinal}${useVulgarFractions ? "\u215B" : "1/8"}`;
case 0.25:
return `${sFloorFinal}${useVulgarFractions ? "\xBC" : "1/4"}`;
case 0.375:
return `${sFloorFinal}${useVulgarFractions ? "\u215C" : "3/8"}`;
case 0.5:
return `${sFloorFinal}${useVulgarFractions ? "\xBD" : "1/2"}`;
case 0.625:
return `${sFloorFinal}${useVulgarFractions ? "\u215D" : "5/8"}`;
case 0.75:
return `${sFloorFinal}${useVulgarFractions ? "\xBE" : "3/4"}`;
case 0.875:
return `${sFloorFinal}${useVulgarFractions ? "\u215E" : "7/8"}`;
const opts = typeof options === "boolean" ? { vulgarFractions: options } : options != null ? options : {};
const sFloorFinal = opts.vulgarFractions ? sFloor.trim() : sFloor;
const getFraction = (vulgarFraction) => opts.vulgarFractions ? vulgarFraction : opts.fractionSlash ? vulgarToPlainMap[vulgarFraction].replace("/", "\u2044") : vulgarToPlainMap[vulgarFraction];
for (const [num, vf] of fractionDecimalMatches) {
if (closeEnough(dDecimal, num, (_a = opts.tolerance) != null ? _a : defaultTolerance)) {
return `${sFloorFinal}${getFraction(vf)}`;
}
}
return `${dQty}`;
}
export { formatQuantity as default };
};
export { formatQuantity as default, defaultTolerance, formatQuantity, fractionDecimalMatches, vulgarToPlainMap };
//# sourceMappingURL=format-quantity.es.js.map

@@ -1,2 +0,2 @@

(function(n,r){typeof exports=="object"&&typeof module!="undefined"?module.exports=r():typeof define=="function"&&define.amd?define(r):(n=typeof globalThis!="undefined"?globalThis:n||self,n.formatQuantity=r())})(this,function(){"use strict";const n=(o,e)=>Math.abs(o-e)<.009;function r(o,e){const f=typeof o=="string"?parseFloat(o):o;if(isNaN(f)||f===null)return null;if(f===0)return"";const u=Math.abs(f),i=Math.floor(u),d=i===0?"":`${f<0?"-":""}${i} `,$=u-i;if($===0)return`${f}`;const t=e?d.trim():d;if(n($,.33))return`${t}${e?"\u2153":"1/3"}`;if(n($,.66))return`${t}${e?"\u2154":"2/3"}`;if(n($,.2))return`${t}${e?"\u2155":"1/5"}`;if(n($,.4))return`${t}${e?"\u2156":"2/5"}`;if(n($,.6))return`${t}${e?"\u2157":"3/5"}`;if(n($,.8))return`${t}${e?"\u2158":"4/5"}`;switch($){case .125:return`${t}${e?"\u215B":"1/8"}`;case .25:return`${t}${e?"\xBC":"1/4"}`;case .375:return`${t}${e?"\u215C":"3/8"}`;case .5:return`${t}${e?"\xBD":"1/2"}`;case .625:return`${t}${e?"\u215D":"5/8"}`;case .75:return`${t}${e?"\xBE":"3/4"}`;case .875:return`${t}${e?"\u215E":"7/8"}`}return`${f}`}return r});
(function(u,o){typeof exports=="object"&&typeof module!="undefined"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(u=typeof globalThis!="undefined"?globalThis:u||self,o(u.FormatQuantity={}))})(this,function(u){"use strict";const r={"\xBC":"1/4","\xBD":"1/2","\xBE":"3/4","\u2150":"1/7","\u2151":"1/9","\u2152":"1/10","\u2153":"1/3","\u2154":"2/3","\u2155":"1/5","\u2156":"2/5","\u2157":"3/5","\u2158":"4/5","\u2159":"1/6","\u215A":"5/6","\u215B":"1/8","\u215C":"3/8","\u215D":"5/8","\u215E":"7/8"},f=[[.33,"\u2153"],[.66,"\u2154"],[.2,"\u2155"],[.4,"\u2156"],[.6,"\u2157"],[.8,"\u2158"],[.166,"\u2159"],[.833,"\u215A"],[.143,"\u2150"],[.111,"\u2151"],[.1,"\u2152"],[.125,"\u215B"],[.25,"\xBC"],[.375,"\u215C"],[.5,"\xBD"],[.625,"\u215D"],[.75,"\xBE"],[.875,"\u215E"]],M=(n,t,e)=>Math.abs(n-t)<e,i=(n,t)=>{var h;const e=typeof n=="string"?parseFloat(n):n;if(isNaN(e)||e===null)return null;if(e===0)return"";const s=Math.abs(e),c=Math.floor(s),d=`${e<0?"-":""}${c===0?"":`${c} `}`,m=s-c;if(m===0)return`${e}`;const l=typeof t=="boolean"?{vulgarFractions:t}:t!=null?t:{},T=l.vulgarFractions?d.trim():d,g=a=>l.vulgarFractions?a:l.fractionSlash?r[a].replace("/","\u2044"):r[a];for(const[a,y]of f)if(M(m,a,(h=l.tolerance)!=null?h:.009))return`${T}${g(y)}`;return`${e}`};u.default=i,u.defaultTolerance=.009,u.formatQuantity=i,u.fractionDecimalMatches=f,u.vulgarToPlainMap=r,Object.defineProperties(u,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
//# sourceMappingURL=format-quantity.umd.js.map

@@ -1,8 +0,5 @@

/**
* 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 fractions, e.g. "½",
* pass `true` as the second argument.
*/
declare function formatQuantity(qty: string | number, useVulgarFractions?: boolean): string | null;
import { formatQuantity } from './formatQuantity';
export * from './constants';
export * from './types';
export { formatQuantity };
export default formatQuantity;
{
"version": "1.0.2",
"version": "1.1.0",
"name": "format-quantity",

@@ -20,3 +20,5 @@ "author": "Jake Boone <jakeboone02@gmail.com>",

"format",
"string"
"string",
"fractions",
"imperial"
],

@@ -31,9 +33,9 @@ "bugs": {

},
"engines": {
"node": ">=10"
},
"scripts": {
"start": "vite",
"build": "vite build && tsc",
"build": "yarn build:main && yarn build:types",
"build:main": "vite build",
"build:types": "tsc --project ./tsconfig.build.json",
"test": "jest --coverage",
"watch": "jest --watch",
"publish:npm": "np"

@@ -40,0 +42,0 @@ },

@@ -9,5 +9,5 @@ # format-quantity

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 unicode vulgar fractions 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". 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](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).
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).

@@ -28,3 +28,3 @@ ## Installation

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

@@ -34,3 +34,3 @@ ```html

<script>
console.log(formatQuantity(10.5)); // "10 1/2"
console.log(FormatQuantity.formatQuantity(10.5)); // "10 1/2"
</script>

@@ -42,9 +42,69 @@ ```

```js
import formatQuantity from 'format-quantity';
import { formatQuantity } from 'format-quantity';
console.log(formatQuantity(1.5)); // "1 1/2"
console.log(formatQuantity(2.66)); // "2 2/3"
console.log(formatQuantity(3.875, true)); // "3⅞"
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 ingredients).
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.
```js
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`](src/index.ts) for the actual order of evaluation).
```js
// 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](<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⅞"
```
## 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](https://en.wikipedia.org/wiki/Number_Forms) (`"\u00bc"`, `"\u00bd"`, `"\u00be"`, and `"\u2150"` through `"\u215e"`) |

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc