@escape-game-over/atlas 0.1.87 → 0.1.88
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.ts +3 -0
- package/src/money.ts +40 -0
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -215,14 +215,17 @@ export {
|
|
|
215
215
|
export {
|
|
216
216
|
type AudiencePrice,
|
|
217
217
|
type AudienceTier,
|
|
218
|
+
amountAt,
|
|
218
219
|
assertAudiences,
|
|
219
220
|
assertPriceTiers,
|
|
220
221
|
type CurrencyCode,
|
|
222
|
+
formatPrice,
|
|
221
223
|
formatQuantities,
|
|
222
224
|
type Price,
|
|
223
225
|
type PriceTier,
|
|
224
226
|
type Quantity,
|
|
225
227
|
type QuantityRange,
|
|
228
|
+
quantitiesOf,
|
|
226
229
|
type TieredPrice,
|
|
227
230
|
tierRange,
|
|
228
231
|
} from "./money.ts";
|
package/src/money.ts
CHANGED
|
@@ -480,3 +480,43 @@ export function assertPriceTiers(price: TieredPrice, at: string): void {
|
|
|
480
480
|
);
|
|
481
481
|
}
|
|
482
482
|
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* What one unit costs at `quantity`: the amount of the band that covers it, or
|
|
486
|
+
* `undefined` where the table has no band for it — a group too small, too
|
|
487
|
+
* large, or in a hole between bands.
|
|
488
|
+
*
|
|
489
|
+
* The first band that covers it wins. `assertPriceTiers` refuses overlapping
|
|
490
|
+
* bands, so in a table that passed it there is only ever one.
|
|
491
|
+
*/
|
|
492
|
+
export function amountAt(
|
|
493
|
+
price: TieredPrice,
|
|
494
|
+
quantity: number
|
|
495
|
+
): number | undefined {
|
|
496
|
+
for (const tier of price.tiers) {
|
|
497
|
+
const band = tierRange(tier);
|
|
498
|
+
if (quantity >= band.min && quantity <= band.max) {
|
|
499
|
+
return tier.amountPerUnit;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return undefined;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* A price as a reader in `locale` writes it: the currency's symbol and that
|
|
507
|
+
* language's separators, and no decimals on a whole amount — "£25", not
|
|
508
|
+
* "£25.00", while "€12.50" keeps its cents.
|
|
509
|
+
*
|
|
510
|
+
* Whole amounts drop the decimals because that is how a price list reads; a
|
|
511
|
+
* table of "25.00, 22.00, 20.00" says nothing "25, 22, 20" does not. An amount
|
|
512
|
+
* with cents shows two, so a price is never rounded on the way to the page.
|
|
513
|
+
*/
|
|
514
|
+
export function formatPrice(price: Price, locale: string): string {
|
|
515
|
+
const whole = Number.isInteger(price.amount);
|
|
516
|
+
return new Intl.NumberFormat(locale, {
|
|
517
|
+
style: "currency",
|
|
518
|
+
currency: price.currency,
|
|
519
|
+
minimumFractionDigits: whole ? 0 : 2,
|
|
520
|
+
maximumFractionDigits: whole ? 0 : 2,
|
|
521
|
+
}).format(price.amount);
|
|
522
|
+
}
|