@lightsparkdev/core 1.0.10 → 1.0.12

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.
@@ -7,26 +7,53 @@ import { isNumber, round } from "./numbers.js";
7
7
 
8
8
  export const defaultCurrencyCode = "USD";
9
9
 
10
- /** This enum identifies the unit of currency associated with a CurrencyAmount. **/
10
+ /**
11
+ * This enum identifies the unit of currency associated with a CurrencyAmount.
12
+ * *
13
+ */
11
14
  export enum CurrencyUnit {
12
15
  /**
13
- * This is an enum value that represents values that could be added in the future.
14
- * Clients should support unknown values as more of them could be added without notice.
16
+ * This is an enum value that represents values that could be added in the
17
+ * future. Clients should support unknown values as more of them could be
18
+ * added without notice.
15
19
  */
16
20
  FUTURE_VALUE = "FUTURE_VALUE",
17
- /** Bitcoin is the cryptocurrency native to the Bitcoin network. It is used as the native medium for value transfer for the Lightning Network. **/
21
+ /**
22
+ * Bitcoin is the cryptocurrency native to the Bitcoin network.
23
+ * It is used as the native medium for value transfer for the Lightning
24
+ * Network. *
25
+ */
18
26
  BITCOIN = "BITCOIN",
19
- /** 0.00000001 (10e-8) Bitcoin or one hundred millionth of a Bitcoin. This is the unit most commonly used in Lightning transactions. **/
27
+ /**
28
+ * 0.00000001 (10e-8) Bitcoin or one hundred millionth of a Bitcoin.
29
+ * This is the unit most commonly used in Lightning transactions.
30
+ * *
31
+ */
20
32
  SATOSHI = "SATOSHI",
21
- /** 0.001 Satoshi, or 10e-11 Bitcoin. We recommend using the Satoshi unit instead when possible. **/
33
+ /**
34
+ * 0.001 Satoshi, or 10e-11 Bitcoin. We recommend using the Satoshi unit
35
+ * instead when possible. *
36
+ */
22
37
  MILLISATOSHI = "MILLISATOSHI",
23
38
  /** United States Dollar. **/
24
39
  USD = "USD",
25
- /** 0.000000001 (10e-9) Bitcoin or a billionth of a Bitcoin. We recommend using the Satoshi unit instead when possible. **/
40
+ /**
41
+ * 0.000000001 (10e-9) Bitcoin or a billionth of a Bitcoin.
42
+ * We recommend using the Satoshi unit instead when possible.
43
+ * *
44
+ */
26
45
  NANOBITCOIN = "NANOBITCOIN",
27
- /** 0.000001 (10e-6) Bitcoin or a millionth of a Bitcoin. We recommend using the Satoshi unit instead when possible. **/
46
+ /**
47
+ * 0.000001 (10e-6) Bitcoin or a millionth of a Bitcoin.
48
+ * We recommend using the Satoshi unit instead when possible.
49
+ * *
50
+ */
28
51
  MICROBITCOIN = "MICROBITCOIN",
29
- /** 0.001 (10e-3) Bitcoin or a thousandth of a Bitcoin. We recommend using the Satoshi unit instead when possible. **/
52
+ /**
53
+ * 0.001 (10e-3) Bitcoin or a thousandth of a Bitcoin.
54
+ * We recommend using the Satoshi unit instead when possible.
55
+ * *
56
+ */
30
57
  MILLIBITCOIN = "MILLIBITCOIN",
31
58
  }
32
59
 
@@ -39,13 +66,15 @@ export type CurrencyAmountType = {
39
66
  /** The unit of user's preferred currency. **/
40
67
  preferredCurrencyUnit: CurrencyUnit;
41
68
  /**
42
- * The rounded numeric value for this CurrencyAmount in the very base level of user's preferred
43
- * currency. For example, for USD, the value will be in cents.
69
+ * The rounded numeric value for this CurrencyAmount in the very base level
70
+ * of user's preferred currency. For example, for USD, the value will be in
71
+ * cents.
44
72
  **/
45
73
  preferredCurrencyValueRounded: number;
46
74
  /**
47
- * The approximate float value for this CurrencyAmount in the very base level of user's preferred
48
- * currency. For example, for USD, the value will be in cents.
75
+ * The approximate float value for this CurrencyAmount in the very base level
76
+ * of user's preferred currency. For example, for USD, the value will be in
77
+ * cents.
49
78
  **/
50
79
  preferredCurrencyValueApprox: number;
51
80
  };
@@ -211,8 +240,12 @@ export type CurrencyMap = {
211
240
  };
212
241
 
213
242
  export type CurrencyAmountObj = {
214
- /* Technically the generated graphql schema has value as `any` but it's always a number.
215
- We are intentionally widening the type here to allow for more forgiving input: */
243
+ /*
244
+ * Technically the generated graphql schema has value as `any` but it's
245
+ * always a number.
246
+ * We are intentionally widening the type here to allow for more forgiving
247
+ * input:
248
+ */
216
249
  value?: number | string | null;
217
250
  /* assume satoshi if not provided */
218
251
  unit?: CurrencyUnit;
@@ -402,7 +435,10 @@ export function formatCurrencyStr(
402
435
  let { value: num } = currencyAmount;
403
436
  const { unit } = currencyAmount;
404
437
 
405
- /* Currencies should always be represented in the smallest unit, e.g. cents for USD: */
438
+ /**
439
+ * Currencies should always be represented in the smallest unit, e.g.
440
+ * cents for USD:
441
+ */
406
442
  if (unit === CurrencyUnit.USD) {
407
443
  num = num / 100;
408
444
  }
@@ -415,7 +451,8 @@ export function formatCurrencyStr(
415
451
  : maxFractionDigits;
416
452
  }
417
453
 
418
- // Symbol handled by toLocaleString for USD. These rely on the LightsparkIcons font
454
+ // Symbol handled by toLocaleString for USD.
455
+ // These rely on the LightsparkIcons font
419
456
  const symbol = !showBtcSymbol
420
457
  ? ""
421
458
  : unit === CurrencyUnit.BITCOIN
@@ -472,7 +509,8 @@ export function localeToCurrencySymbol(locale: string) {
472
509
  maximumFractionDigits: 0,
473
510
  }).format(0);
474
511
 
475
- // Remove numeric and non-breaking space characters to extract the currency symbol
512
+ // Remove numeric and non-breaking space characters to extract the currency
513
+ // symbol
476
514
  const { symbol } = separateCurrencyStrParts(formatted);
477
515
  return symbol;
478
516
  }
@@ -1,3 +1,5 @@
1
+ import { type JSONType } from "./types.js";
2
+
1
3
  export const isError = (e: unknown): e is Error => {
2
4
  return Boolean(
3
5
  typeof e === "object" &&
@@ -34,3 +36,17 @@ export const isErrorMsg = (e: unknown, msg: string) => {
34
36
  }
35
37
  return false;
36
38
  };
39
+
40
+ export function errorToJSON(err: unknown) {
41
+ if (
42
+ typeof err === "object" &&
43
+ err !== null &&
44
+ "toJSON" in err &&
45
+ typeof err.toJSON === "function"
46
+ ) {
47
+ return err.toJSON() as JSONType;
48
+ }
49
+ return JSON.parse(
50
+ JSON.stringify(err, Object.getOwnPropertyNames(err)),
51
+ ) as JSONType;
52
+ }
@@ -1,5 +1,7 @@
1
1
  /* From https://github.com/tadeegan/locale-currency. For now only USD conversion from
2
- BTC is supported by sparkcore, strip additional currency codes from the bundle: */
2
+ * BTC is supported by sparkcore, strip additional currency codes from the
3
+ * bundle:
4
+ */
3
5
  export const countryCodesToCurrencyCodes = {
4
6
  AD: "EUR",
5
7
  // AE: "AED",
@@ -4,7 +4,7 @@ export type Maybe<T> = T | null | undefined;
4
4
 
5
5
  export type ExpandRecursively<T> = T extends object
6
6
  ? T extends infer O
7
- ? { [K in keyof O]: ExpandRecursively<O[K]> } // eslint-disable-line @typescript-eslint/no-unused-vars
7
+ ? { [K in keyof O]: ExpandRecursively<O[K]> }
8
8
  : never
9
9
  : T;
10
10
 
@@ -27,3 +27,7 @@ export type DeepPartial<T> = T extends object
27
27
  [P in keyof T]?: DeepPartial<T[P]>;
28
28
  }
29
29
  : T;
30
+
31
+ export type JSONLiteral = string | number | boolean | null;
32
+ export type JSONType = JSONLiteral | JSONType[] | { [key: string]: JSONType };
33
+ export type JSONObject = { [key: string]: JSONType };