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 2.1.0 to 3.0.0

dist/cjs/format-quantity.cjs.development.d.ts

6

dist/cjs/format-quantity.cjs.development.js

@@ -23,3 +23,2 @@ "use strict";

__export(src_exports, {
default: () => src_default,
defaultOptions: () => defaultOptions,

@@ -176,3 +175,3 @@ defaultTolerance: () => defaultTolerance,

const fraction = getFraction(vf, opts);
const int = Object.hasOwn(vulgarToAsciiMap, fraction) ? flooredAbsValStr.trim() : flooredAbsValStr;
const int = fraction in vulgarToAsciiMap ? flooredAbsValStr.trim() : flooredAbsValStr;
return `${int}${fraction}`;

@@ -183,5 +182,2 @@ }

};
// src/index.ts
var src_default = formatQuantity;
// Annotate the CommonJS export names for ESM import in node:

@@ -188,0 +184,0 @@ 0 && (module.exports = {

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

"use strict";var l=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var d=(t,o)=>{for(var r in o)l(t,r,{get:o[r],enumerable:!0})},y=(t,o,r,a)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of X(o))!b.call(t,e)&&e!==r&&l(t,e,{get:()=>o[e],enumerable:!(a=C(o,e))||a.enumerable});return t};var I=t=>y(l({},"__esModule",{value:!0}),t);var N={};d(N,{default:()=>Q,defaultOptions:()=>n,defaultTolerance:()=>D,formatQuantity:()=>c,fractionDecimalMatches:()=>u,vulgarToAsciiMap:()=>i});module.exports=I(N);var D=.0075,n={vulgarFractions:!1,tolerance:.0075,fractionSlash:!1,romanNumerals:!1},i={"\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"},u=[[.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"],[.0625,"1/16"],[.1875,"3/16"],[.3125,"5/16"],[.4375,"7/16"],[.5625,"9/16"],[.6875,"11/16"],[.8125,"13/16"],[.9375,"15/16"]];var g=(t,o,r)=>Math.abs(t-o)<r,h=(t,{fractionSlash:o,vulgarFractions:r})=>{if(r)return t;let a=i[t]??t;return o?a.replace("/","\u2044"):a},x=t=>({...n,...typeof t=="boolean"?{vulgarFractions:t}:t}),$=["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","I","II","III","IV","V","VI","VII","VIII","IX"],M=t=>{if(typeof t!="number"||isNaN(t))return null;if(t<1||t>=4e3)return"";let r=`${Math.floor(t)}`.split(""),a="",e=3;for(;e--;)a=`${$[+r.pop()+e*10]||""}${a}`;return`${Array(+r.join("")+1).join("M")}${a}`},c=(t,o=n)=>{let r=typeof t=="string"?parseFloat(t):t;if(isNaN(r)||r===null)return null;if(r===0)return"";let a=x(o??n);if(a.romanNumerals)return M(r);let e=Math.abs(r),s=Math.floor(e),m=`${r<0?"-":""}${s===0?"":`${s} `}`,f=e-s;if(f===0)return`${r}`;for(let[F,V]of u)if(g(f,F,a.tolerance)){let p=h(V,a);return`${Object.hasOwn(i,p)?m.trim():m}${p}`}return`${r}`};var Q=c;0&&(module.exports={defaultOptions,defaultTolerance,formatQuantity,fractionDecimalMatches,vulgarToAsciiMap});
"use strict";var l=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var I=(t,r)=>{for(var o in r)l(t,o,{get:r[o],enumerable:!0})},b=(t,r,o,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of X(r))!y.call(t,n)&&n!==o&&l(t,n,{get:()=>r[n],enumerable:!(a=C(r,n))||a.enumerable});return t};var d=t=>b(l({},"__esModule",{value:!0}),t);var Q={};I(Q,{defaultOptions:()=>e,defaultTolerance:()=>D,formatQuantity:()=>p,fractionDecimalMatches:()=>c,vulgarToAsciiMap:()=>i});module.exports=d(Q);var D=.0075,e={vulgarFractions:!1,tolerance:.0075,fractionSlash:!1,romanNumerals:!1},i={"\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"},c=[[.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"],[.0625,"1/16"],[.1875,"3/16"],[.3125,"5/16"],[.4375,"7/16"],[.5625,"9/16"],[.6875,"11/16"],[.8125,"13/16"],[.9375,"15/16"]];var g=(t,r,o)=>Math.abs(t-r)<o,$=(t,{fractionSlash:r,vulgarFractions:o})=>{if(o)return t;let a=i[t]??t;return r?a.replace("/","\u2044"):a},h=t=>({...e,...typeof t=="boolean"?{vulgarFractions:t}:t}),x=["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","I","II","III","IV","V","VI","VII","VIII","IX"],M=t=>{if(typeof t!="number"||isNaN(t))return null;if(t<1||t>=4e3)return"";let o=`${Math.floor(t)}`.split(""),a="",n=3;for(;n--;)a=`${x[+o.pop()+n*10]||""}${a}`;return`${Array(+o.join("")+1).join("M")}${a}`},p=(t,r=e)=>{let o=typeof t=="string"?parseFloat(t):t;if(isNaN(o)||o===null)return null;if(o===0)return"";let a=h(r??e);if(a.romanNumerals)return M(o);let n=Math.abs(o),s=Math.floor(n),u=`${o<0?"-":""}${s===0?"":`${s} `}`,m=n-s;if(m===0)return`${o}`;for(let[F,V]of c)if(g(m,F,a.tolerance)){let f=$(V,a);return`${f in i?u.trim():u}${f}`}return`${o}`};0&&(module.exports={defaultOptions,defaultTolerance,formatQuantity,fractionDecimalMatches,vulgarToAsciiMap});
//# sourceMappingURL=format-quantity.cjs.production.js.map

@@ -159,3 +159,3 @@ var __defProp = Object.defineProperty;

const fraction = getFraction(vf, opts);
const int = Object.hasOwn(vulgarToAsciiMap, fraction) ? flooredAbsValStr.trim() : flooredAbsValStr;
const int = fraction in vulgarToAsciiMap ? flooredAbsValStr.trim() : flooredAbsValStr;
return `${int}${fraction}`;

@@ -166,7 +166,3 @@ }

};
// src/index.ts
var src_default = formatQuantity;
export {
src_default as default,
defaultOptions,

@@ -173,0 +169,0 @@ defaultTolerance,

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

"use strict";var FormatQuantity=(()=>{var l=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var d=(t,o)=>{for(var r in o)l(t,r,{get:o[r],enumerable:!0})},y=(t,o,r,a)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of X(o))!b.call(t,e)&&e!==r&&l(t,e,{get:()=>o[e],enumerable:!(a=C(o,e))||a.enumerable});return t};var I=t=>y(l({},"__esModule",{value:!0}),t);var N={};d(N,{default:()=>Q,defaultOptions:()=>n,defaultTolerance:()=>D,formatQuantity:()=>c,fractionDecimalMatches:()=>u,vulgarToAsciiMap:()=>i});var D=.0075,n={vulgarFractions:!1,tolerance:.0075,fractionSlash:!1,romanNumerals:!1},i={"\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"},u=[[.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"],[.0625,"1/16"],[.1875,"3/16"],[.3125,"5/16"],[.4375,"7/16"],[.5625,"9/16"],[.6875,"11/16"],[.8125,"13/16"],[.9375,"15/16"]];var g=(t,o,r)=>Math.abs(t-o)<r,h=(t,{fractionSlash:o,vulgarFractions:r})=>{if(r)return t;let a=i[t]??t;return o?a.replace("/","\u2044"):a},x=t=>({...n,...typeof t=="boolean"?{vulgarFractions:t}:t}),$=["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","I","II","III","IV","V","VI","VII","VIII","IX"],M=t=>{if(typeof t!="number"||isNaN(t))return null;if(t<1||t>=4e3)return"";let r=`${Math.floor(t)}`.split(""),a="",e=3;for(;e--;)a=`${$[+r.pop()+e*10]||""}${a}`;return`${Array(+r.join("")+1).join("M")}${a}`},c=(t,o=n)=>{let r=typeof t=="string"?parseFloat(t):t;if(isNaN(r)||r===null)return null;if(r===0)return"";let a=x(o??n);if(a.romanNumerals)return M(r);let e=Math.abs(r),s=Math.floor(e),m=`${r<0?"-":""}${s===0?"":`${s} `}`,f=e-s;if(f===0)return`${r}`;for(let[F,V]of u)if(g(f,F,a.tolerance)){let p=h(V,a);return`${Object.hasOwn(i,p)?m.trim():m}${p}`}return`${r}`};var Q=c;return I(N);})();
"use strict";var FormatQuantity=(()=>{var l=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var I=(t,r)=>{for(var o in r)l(t,o,{get:r[o],enumerable:!0})},b=(t,r,o,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of X(r))!y.call(t,n)&&n!==o&&l(t,n,{get:()=>r[n],enumerable:!(a=C(r,n))||a.enumerable});return t};var d=t=>b(l({},"__esModule",{value:!0}),t);var Q={};I(Q,{defaultOptions:()=>e,defaultTolerance:()=>D,formatQuantity:()=>p,fractionDecimalMatches:()=>c,vulgarToAsciiMap:()=>i});var D=.0075,e={vulgarFractions:!1,tolerance:.0075,fractionSlash:!1,romanNumerals:!1},i={"\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"},c=[[.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"],[.0625,"1/16"],[.1875,"3/16"],[.3125,"5/16"],[.4375,"7/16"],[.5625,"9/16"],[.6875,"11/16"],[.8125,"13/16"],[.9375,"15/16"]];var g=(t,r,o)=>Math.abs(t-r)<o,$=(t,{fractionSlash:r,vulgarFractions:o})=>{if(o)return t;let a=i[t]??t;return r?a.replace("/","\u2044"):a},h=t=>({...e,...typeof t=="boolean"?{vulgarFractions:t}:t}),x=["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","I","II","III","IV","V","VI","VII","VIII","IX"],M=t=>{if(typeof t!="number"||isNaN(t))return null;if(t<1||t>=4e3)return"";let o=`${Math.floor(t)}`.split(""),a="",n=3;for(;n--;)a=`${x[+o.pop()+n*10]||""}${a}`;return`${Array(+o.join("")+1).join("M")}${a}`},p=(t,r=e)=>{let o=typeof t=="string"?parseFloat(t):t;if(isNaN(o)||o===null)return null;if(o===0)return"";let a=h(r??e);if(a.romanNumerals)return M(o);let n=Math.abs(o),s=Math.floor(n),u=`${o<0?"-":""}${s===0?"":`${s} `}`,m=n-s;if(m===0)return`${o}`;for(let[F,V]of c)if(g(m,F,a.tolerance)){let f=$(V,a);return`${f in i?u.trim():u}${f}`}return`${o}`};return d(Q);})();
//# sourceMappingURL=format-quantity.umd.min.js.map
{
"version": "2.1.0",
"version": "3.0.0",
"name": "format-quantity",

@@ -14,8 +14,13 @@ "author": "Jake Boone <jakeboone02@gmail.com>",

".": {
"types": "./dist/format-quantity.d.ts",
"import": "./dist/format-quantity.mjs",
"require": "./dist/cjs/index.js"
"import": {
"types": "./dist/format-quantity.d.mts",
"default": "./dist/format-quantity.mjs"
},
"require": {
"types": "./dist/cjs/format-quantity.cjs.development.d.ts",
"default": "./dist/cjs/index.js"
}
}
},
"types": "./dist/format-quantity.d.ts",
"types": "./dist/cjs/format-quantity.cjs.production.d.ts",
"unpkg": "./dist/format-quantity.umd.min.js",

@@ -44,6 +49,7 @@ "license": "MIT",

"scripts": {
"start": "bun ./server.ts",
"start": "bun --hot ./server.ts",
"build": "tsup",
"test": "jest",
"watch": "jest --watch",
"docs": "bunx typedoc",
"test": "bun test",
"watch": "bun test --watch",
"publish:npm": "np",

@@ -53,11 +59,12 @@ "pretty-print": "prettier --write *.{mjs,ts,json} src/*.*"

"devDependencies": {
"@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"
"@types/node": "^20.11.2",
"@types/web": "^0.0.135",
"bun-types": "^1.0.22",
"np": "^9.2.0",
"open": "^10.0.3",
"prettier": "^3.2.2",
"tsup": "^8.0.1",
"typedoc": "^0.25.7",
"typedoc-plugin-katex": "^0.1.2",
"typescript": "^5.3.3"
},

@@ -64,0 +71,0 @@ "publishConfig": {

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

# format-quantity
[![npm][badge-npm]](https://www.npmjs.com/package/format-quantity)

@@ -11,7 +9,9 @@ ![workflow status](https://github.com/jakeboone02/format-quantity/actions/workflows/main.yml/badge.svg)

**[Full documentation](https://jakeboone02.github.io/format-quantity/)**
Features:
- To use vulgar fraction characters like "⅞", pass `true` as the second argument (see other [options](#options), like Roman numerals, below).
- To use vulgar fraction characters like "⅞", pass `true` as the second argument. Other options like Roman numerals are described 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.
- The return value will be an empty string (`""`) if the first argument is `0` or `"0"`, which fits the primary use case of formatting recipe ingredient quantities.

@@ -122,16 +122,2 @@ > _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._

## Other exports
| 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

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

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

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