Skip to content

Commit 1b6604b

Browse files
committed
Update dependencies; misc tweaks
1 parent 07d8e65 commit 1b6604b

File tree

5 files changed

+59
-43
lines changed

5 files changed

+59
-43
lines changed

bun.lockb

6.85 KB
Binary file not shown.

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@
4949
"pretty-print": "prettier --write *.{mjs,ts,json} src/*.*"
5050
},
5151
"devDependencies": {
52-
"@types/node": "^20.6.1",
53-
"@types/web": "^0.0.114",
54-
"bun-types": "^1.0.1",
55-
"np": "^8.0.4",
52+
"@types/node": "^20.10.1",
53+
"@types/web": "^0.0.123",
54+
"bun-types": "^1.0.14",
55+
"np": "^9.0.0",
5656
"open": "^9.1.0",
57-
"prettier": "^3.0.3",
58-
"tsup": "^7.2.0",
59-
"typedoc": "^0.25.1",
57+
"prettier": "^3.1.0",
58+
"tsup": "^8.0.1",
59+
"typedoc": "^0.25.4",
6060
"typedoc-plugin-katex": "^0.1.2",
61-
"typescript": "^5.2.2"
61+
"typescript": "^5.3.2"
6262
},
6363
"publishConfig": {
6464
"registry": "https://registry.npmjs.org"

src/formatQuantity.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import type {
1818
const closeEnough = (n1: number, n2: number, tolerance: number) =>
1919
Math.abs(n1 - n2) < tolerance;
2020

21+
/**
22+
* Applies the `vulgarFractions` or `fractionSlash` options as necessary.
23+
*/
2124
const getFraction = (
2225
vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,
2326
{ fractionSlash, vulgarFractions }: FormatQuantityOptions
@@ -37,6 +40,9 @@ const getFraction = (
3740
return plainFraction;
3841
};
3942

43+
/**
44+
* Merges options object with default options, converting boolean to object if necessary.
45+
*/
4046
const normalizeOptions = (
4147
options: Parameters<FormatQuantity>[1]
4248
): Required<FormatQuantityOptions> => ({
@@ -72,6 +78,7 @@ export const formatRomanNumerals = (qty: number) => {
7278
while (i--) {
7379
roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;
7480
}
81+
7582
return `${Array(+digits.join('') + 1).join('M')}${roman}`;
7683
};
7784

@@ -89,12 +96,12 @@ export const formatQuantity: FormatQuantity = (
8996
// TODO: use numericQuantity instead of parseFloat?
9097
const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : qty;
9198

92-
// Return `null` if input is not number-like
99+
// Return `null` if input is not number-like.
93100
if (isNaN(qtyAsNumber) || qtyAsNumber === null) {
94101
return null;
95102
}
96103

97-
// Return an empty string if the value is zero
104+
// Return an empty string if the value is zero.
98105
if (qtyAsNumber === 0) {
99106
return '';
100107
}
@@ -115,17 +122,18 @@ export const formatQuantity: FormatQuantity = (
115122
}`;
116123
const decimalValue = absoluteValue - flooredAbsVal;
117124

118-
// For integers just return the given value as a string
125+
// For integers just return the given value as a string.
119126
if (decimalValue === 0) {
120127
return `${qtyAsNumber}`;
121128
}
122129

123130
for (const [num, vf] of fractionDecimalMatches) {
124131
if (closeEnough(decimalValue, num, opts.tolerance)) {
125132
const fraction = getFraction(vf, opts);
126-
const int = Object.hasOwn(vulgarToAsciiMap, fraction)
127-
? flooredAbsValStr.trim()
128-
: flooredAbsValStr;
133+
const int =
134+
fraction in vulgarToAsciiMap
135+
? flooredAbsValStr.trim()
136+
: flooredAbsValStr;
129137
return `${int}${fraction}`;
130138
}
131139
}

src/formatQuantityTests.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,39 +124,42 @@ export const formatQuantityTests: FormatQuantityTests = {
124124
sixteenths: [
125125
[1 + 1 / 16, '1 1/16'],
126126
[1.0625, '1 1/16'],
127-
[1.0625, '1 1/16', { vulgarFractions /* , tolerance: 0.001 */ }],
128-
[1.0625, '1 1⁄16', { fractionSlash /* , tolerance: 0.001 */ }],
127+
[1.0625, '1 1/16', { vulgarFractions }],
128+
[1.0625, '1 1⁄16', { fractionSlash }],
129129
[1 + 3 / 16, '1 3/16'],
130130
[1.1875, '1 3/16'],
131-
[1.1875, '1 3/16', { vulgarFractions /* , tolerance: 0.001 */ }],
132-
[1.1875, '1 3⁄16', { fractionSlash /* , tolerance: 0.001 */ }],
131+
[1.1875, '1 3/16', { vulgarFractions }],
132+
[1.1875, '1 3⁄16', { fractionSlash }],
133133
[1 + 5 / 16, '1 5/16'],
134134
[1.3125, '1 5/16'],
135-
[1.3125, '1 5/16', { vulgarFractions /* , tolerance: 0.001 */ }],
136-
[1.3125, '1 5⁄16', { fractionSlash /* , tolerance: 0.001 */ }],
135+
[1.3125, '1 5/16', { vulgarFractions }],
136+
[1.3125, '1 5⁄16', { fractionSlash }],
137137
[1 + 7 / 16, '1 7/16'],
138138
[1.4375, '1 7/16'],
139-
[1.4375, '1 7/16', { vulgarFractions /* , tolerance: 0.001 */ }],
140-
[1.4375, '1 7⁄16', { fractionSlash /* , tolerance: 0.001 */ }],
139+
[1.4375, '1 7/16', { vulgarFractions }],
140+
[1.4375, '1 7⁄16', { fractionSlash }],
141141
[1 + 9 / 16, '1 9/16'],
142142
[1.5625, '1 9/16'],
143-
[1.5625, '1 9/16', { vulgarFractions /* , tolerance: 0.001 */ }],
144-
[1.5625, '1 9⁄16', { fractionSlash /* , tolerance: 0.001 */ }],
143+
[1.5625, '1 9/16', { vulgarFractions }],
144+
[1.5625, '1 9⁄16', { fractionSlash }],
145145
[1 + 11 / 16, '1 11/16'],
146146
[1.6875, '1 11/16'],
147-
[1.6875, '1 11/16', { vulgarFractions /* , tolerance: 0.001 */ }],
148-
[1.6875, '1 11⁄16', { fractionSlash /* , tolerance: 0.001 */ }],
147+
[1.6875, '1 11/16', { vulgarFractions }],
148+
[1.6875, '1 11⁄16', { fractionSlash }],
149149
[1 + 13 / 16, '1 13/16'],
150150
[1.8125, '1 13/16'],
151-
[1.8125, '1 13/16', { vulgarFractions /* , tolerance: 0.001 */ }],
152-
[1.8125, '1 13⁄16', { fractionSlash /* , tolerance: 0.001 */ }],
151+
[1.8125, '1 13/16', { vulgarFractions }],
152+
[1.8125, '1 13⁄16', { fractionSlash }],
153153
[1 + 15 / 16, '1 15/16'],
154154
[1.9375, '1 15/16'],
155-
[1.9375, '1 15/16', { vulgarFractions /* , tolerance: 0.001 */ }],
156-
[1.9375, '1 15⁄16', { fractionSlash /* , tolerance: 0.001 */ }],
155+
[1.9375, '1 15/16', { vulgarFractions }],
156+
[1.9375, '1 15⁄16', { fractionSlash }],
157157
],
158-
'options as null and empty object': [
158+
'empty or invalid options': [
159+
[1.5, '1 1/2', 42 as any],
160+
[1.5, '1 1/2', 'string' as any],
159161
[1.5, '1 1/2', null as any],
162+
[1.5, '1 1/2', [] as any],
160163
[1.5, '1 1/2', {}],
161164
],
162165
'vulgarFractions option': [

src/types.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,30 @@ export interface FormatQuantityOptions {
2828
romanNumerals?: boolean;
2929
}
3030

31-
/** Function signature of {@link formatQuantity}. */
32-
export type FormatQuantity = (
33-
qty: string | number,
34-
options?: boolean | FormatQuantityOptions
35-
) => string | null;
31+
/**
32+
* Function signature of {@link formatQuantity}.
33+
*/
34+
export interface FormatQuantity {
35+
(
36+
qty: string | number,
37+
options?: boolean | FormatQuantityOptions
38+
): string | null;
39+
}
3640

37-
/** Any numeric character except '0'. */
38-
type NonZeroNumChar = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
3941
/** Any numeric character. */
40-
type NumChar = '0' | NonZeroNumChar;
42+
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
43+
/** Any numeric character except '0'. */
44+
type NonZeroDigit = Exclude<Digit, '0'>;
4145

4246
/**
4347
* Fraction string with either one or two numeric characters in both the
44-
* numerator and denominator.
48+
* numerator and denominator (but not two characters in the numerator while
49+
* the denominator only has one).
4550
*/
4651
export type SimpleFraction =
47-
| `${NonZeroNumChar}/${NonZeroNumChar}`
48-
| `${NonZeroNumChar}/${NonZeroNumChar}${NumChar}`
49-
| `${NonZeroNumChar}${NumChar}/${NonZeroNumChar}${NumChar}`;
52+
| `${NonZeroDigit}/${NonZeroDigit}`
53+
| `${NonZeroDigit}/${NonZeroDigit}${Digit}`
54+
| `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
5055

5156
/**
5257
* Odd numerator sixteenth fraction strings.

0 commit comments

Comments
 (0)