@moneypot/hub 1.9.0-dev.3 → 1.9.0-dev.4

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.
@@ -0,0 +1,9 @@
1
+ export declare function formatCurrency(amount: number, currency: {
2
+ displayUnitScale: number;
3
+ displayUnitName: string;
4
+ }, options?: {
5
+ excludeUnit?: boolean;
6
+ }): string;
7
+ export declare function pluralize(word: string, count: number, suffix?: string): string;
8
+ export declare function getDecimalPlaces(displayUnitScale: number): number;
9
+ export declare function truncateDecimalPrecision(value: number, decimals: number): number;
@@ -0,0 +1,40 @@
1
+ export function formatCurrency(amount, currency, options = {
2
+ excludeUnit: false,
3
+ }) {
4
+ const decimalPlaces = getDecimalPlaces(currency.displayUnitScale);
5
+ const scaledAmount = amount / (currency.displayUnitScale || 1);
6
+ const truncatedAmount = truncateDecimalPrecision(scaledAmount, decimalPlaces);
7
+ const formatter = new Intl.NumberFormat("en-US", {
8
+ minimumFractionDigits: decimalPlaces,
9
+ maximumFractionDigits: decimalPlaces,
10
+ useGrouping: true,
11
+ });
12
+ const formatted = formatter.format(truncatedAmount);
13
+ if (options.excludeUnit) {
14
+ return formatted;
15
+ }
16
+ return `${formatted} ${pluralize(currency.displayUnitName, truncatedAmount)}`;
17
+ }
18
+ export function pluralize(word, count, suffix = "s") {
19
+ return count === 1 ? word : word + suffix;
20
+ }
21
+ export function getDecimalPlaces(displayUnitScale) {
22
+ return displayUnitScale < 10 ? 0 : Math.log10(displayUnitScale);
23
+ }
24
+ export function truncateDecimalPrecision(value, decimals) {
25
+ if (decimals < 0) {
26
+ throw new Error("Decimals must be a non-negative integer");
27
+ }
28
+ if (!Number.isFinite(value)) {
29
+ return value;
30
+ }
31
+ const str = value.toString();
32
+ const dotIndex = str.indexOf(".");
33
+ if (dotIndex === -1) {
34
+ return value;
35
+ }
36
+ const truncatedStr = decimals === 0
37
+ ? str.substring(0, dotIndex)
38
+ : str.substring(0, dotIndex + decimals + 1);
39
+ return parseFloat(truncatedStr);
40
+ }
@@ -8,7 +8,7 @@ import { dbInsertHubHash, dbLockHubHashChain, } from "../hash-chain/db-hash-chai
8
8
  import { getIntermediateHash, getPreimageHash, } from "../hash-chain/get-hash.js";
9
9
  import { makeFinalHash, pickRandomOutcome } from "../hash-chain/util.js";
10
10
  import { logger } from "../logger.js";
11
- import { formatCurrency } from "@moneypot/frontend-utils";
11
+ import { formatCurrency } from "../format-currency.js";
12
12
  const FLOAT_EPSILON = 1e-10;
13
13
  function sum(ns) {
14
14
  return ns.reduce((a, b) => a + b, 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moneypot/hub",
3
- "version": "1.9.0-dev.3",
3
+ "version": "1.9.0-dev.4",
4
4
  "author": "moneypot.com",
5
5
  "homepage": "https://moneypot.com/hub",
6
6
  "keywords": [
@@ -46,7 +46,6 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "@graphile-contrib/pg-omit-archived": "^4.0.0-beta.4",
49
- "@moneypot/frontend-utils": "^0.0.3",
50
49
  "@moneypot/hash-herald": "^1.0.0",
51
50
  "@moneypot/pg-upgrade-schema": "^2.0.4",
52
51
  "@noble/curves": "^1.5.0",