@blocklet/util 0.13.1 → 0.13.3

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/dist/index.cjs CHANGED
@@ -40,6 +40,41 @@ function calcPercent(input, percent, points = "100") {
40
40
  ].join(".");
41
41
  return removeTrailingZeros(finalResultStr);
42
42
  }
43
+ function inputToBN(input) {
44
+ const precision = new util.BN(10).pow(new util.BN(18));
45
+ if (typeof input === "number") {
46
+ input = input.toString();
47
+ }
48
+ if (input.includes(".")) {
49
+ const parts = input.split(".");
50
+ const integerPart = parts[0];
51
+ const decimalPart = parts[1];
52
+ const decimalPrecision = new util.BN(10).pow(new util.BN(decimalPart.length));
53
+ const integerBN = new util.BN(integerPart + decimalPart);
54
+ const adjustedInteger = integerBN.mul(precision).div(decimalPrecision);
55
+ return adjustedInteger;
56
+ }
57
+ return new util.BN(input).mul(precision);
58
+ }
59
+ function bnToInput(bn) {
60
+ const s = bn.toString();
61
+ const decimalPlaces = 18;
62
+ if (s.length <= decimalPlaces) {
63
+ return removeTrailingZeros(`0.${s.padStart(decimalPlaces, "0")}`);
64
+ }
65
+ const index = s.length - decimalPlaces;
66
+ return removeTrailingZeros(`${s.substring(0, index)}.${s.substring(index)}`);
67
+ }
68
+ function calcAdd(inputs) {
69
+ let res = new util.BN(0);
70
+ inputs.forEach((input) => {
71
+ if (input !== "" && input !== void 0) {
72
+ const bn = inputToBN(input);
73
+ res = res.add(bn);
74
+ }
75
+ });
76
+ return bnToInput(res);
77
+ }
43
78
 
44
79
  async function verifyDownloadToken({
45
80
  blockletDid,
@@ -82,9 +117,18 @@ const parsePaymentPriceLabel = (pricing) => {
82
117
  }
83
118
  return `${pricing.price} ${pricing.symbol || "ABT"}`;
84
119
  };
120
+ const parseOldPaymentPriceLabel = (list) => {
121
+ if (!list || !list.length) {
122
+ return "";
123
+ }
124
+ const values = list.map((x) => `${x.value}`);
125
+ return `${calcAdd(values)} ${list[0].symbol || "ABT"}`;
126
+ };
85
127
 
128
+ exports.calcAdd = calcAdd;
86
129
  exports.calcPercent = calcPercent;
87
130
  exports.isFreeBlocklet = isFreeBlocklet;
131
+ exports.parseOldPaymentPriceLabel = parseOldPaymentPriceLabel;
88
132
  exports.parsePaymentPriceLabel = parsePaymentPriceLabel;
89
133
  exports.removeTrailingZeros = removeTrailingZeros;
90
134
  exports.verifyDownloadToken = verifyDownloadToken;
package/dist/index.d.cts CHANGED
@@ -4,6 +4,7 @@ declare function removeTrailingZeros(numStr: string): string;
4
4
  * @example
5
5
  */
6
6
  declare function calcPercent(input: string | number, percent: string, points?: string): string;
7
+ declare function calcAdd(inputs: (string | number)[]): string;
7
8
 
8
9
  declare function verifyDownloadToken({ blockletDid, downloadToken, storePublicKey, serverDid, serverPublicKey, serverSignature, }: {
9
10
  blockletDid: string;
@@ -25,5 +26,9 @@ declare const parsePaymentPriceLabel: (pricing?: {
25
26
  price?: string;
26
27
  symbol?: string;
27
28
  }) => string;
29
+ declare const parseOldPaymentPriceLabel: (list: {
30
+ value?: number | string;
31
+ symbol: string;
32
+ }[]) => string;
28
33
 
29
- export { calcPercent, isFreeBlocklet, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
34
+ export { calcAdd, calcPercent, isFreeBlocklet, parseOldPaymentPriceLabel, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
package/dist/index.d.mts CHANGED
@@ -4,6 +4,7 @@ declare function removeTrailingZeros(numStr: string): string;
4
4
  * @example
5
5
  */
6
6
  declare function calcPercent(input: string | number, percent: string, points?: string): string;
7
+ declare function calcAdd(inputs: (string | number)[]): string;
7
8
 
8
9
  declare function verifyDownloadToken({ blockletDid, downloadToken, storePublicKey, serverDid, serverPublicKey, serverSignature, }: {
9
10
  blockletDid: string;
@@ -25,5 +26,9 @@ declare const parsePaymentPriceLabel: (pricing?: {
25
26
  price?: string;
26
27
  symbol?: string;
27
28
  }) => string;
29
+ declare const parseOldPaymentPriceLabel: (list: {
30
+ value?: number | string;
31
+ symbol: string;
32
+ }[]) => string;
28
33
 
29
- export { calcPercent, isFreeBlocklet, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
34
+ export { calcAdd, calcPercent, isFreeBlocklet, parseOldPaymentPriceLabel, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ declare function removeTrailingZeros(numStr: string): string;
4
4
  * @example
5
5
  */
6
6
  declare function calcPercent(input: string | number, percent: string, points?: string): string;
7
+ declare function calcAdd(inputs: (string | number)[]): string;
7
8
 
8
9
  declare function verifyDownloadToken({ blockletDid, downloadToken, storePublicKey, serverDid, serverPublicKey, serverSignature, }: {
9
10
  blockletDid: string;
@@ -25,5 +26,9 @@ declare const parsePaymentPriceLabel: (pricing?: {
25
26
  price?: string;
26
27
  symbol?: string;
27
28
  }) => string;
29
+ declare const parseOldPaymentPriceLabel: (list: {
30
+ value?: number | string;
31
+ symbol: string;
32
+ }[]) => string;
28
33
 
29
- export { calcPercent, isFreeBlocklet, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
34
+ export { calcAdd, calcPercent, isFreeBlocklet, parseOldPaymentPriceLabel, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
package/dist/index.mjs CHANGED
@@ -34,6 +34,41 @@ function calcPercent(input, percent, points = "100") {
34
34
  ].join(".");
35
35
  return removeTrailingZeros(finalResultStr);
36
36
  }
37
+ function inputToBN(input) {
38
+ const precision = new BN(10).pow(new BN(18));
39
+ if (typeof input === "number") {
40
+ input = input.toString();
41
+ }
42
+ if (input.includes(".")) {
43
+ const parts = input.split(".");
44
+ const integerPart = parts[0];
45
+ const decimalPart = parts[1];
46
+ const decimalPrecision = new BN(10).pow(new BN(decimalPart.length));
47
+ const integerBN = new BN(integerPart + decimalPart);
48
+ const adjustedInteger = integerBN.mul(precision).div(decimalPrecision);
49
+ return adjustedInteger;
50
+ }
51
+ return new BN(input).mul(precision);
52
+ }
53
+ function bnToInput(bn) {
54
+ const s = bn.toString();
55
+ const decimalPlaces = 18;
56
+ if (s.length <= decimalPlaces) {
57
+ return removeTrailingZeros(`0.${s.padStart(decimalPlaces, "0")}`);
58
+ }
59
+ const index = s.length - decimalPlaces;
60
+ return removeTrailingZeros(`${s.substring(0, index)}.${s.substring(index)}`);
61
+ }
62
+ function calcAdd(inputs) {
63
+ let res = new BN(0);
64
+ inputs.forEach((input) => {
65
+ if (input !== "" && input !== void 0) {
66
+ const bn = inputToBN(input);
67
+ res = res.add(bn);
68
+ }
69
+ });
70
+ return bnToInput(res);
71
+ }
37
72
 
38
73
  async function verifyDownloadToken({
39
74
  blockletDid,
@@ -76,5 +111,12 @@ const parsePaymentPriceLabel = (pricing) => {
76
111
  }
77
112
  return `${pricing.price} ${pricing.symbol || "ABT"}`;
78
113
  };
114
+ const parseOldPaymentPriceLabel = (list) => {
115
+ if (!list || !list.length) {
116
+ return "";
117
+ }
118
+ const values = list.map((x) => `${x.value}`);
119
+ return `${calcAdd(values)} ${list[0].symbol || "ABT"}`;
120
+ };
79
121
 
80
- export { calcPercent, isFreeBlocklet, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
122
+ export { calcAdd, calcPercent, isFreeBlocklet, parseOldPaymentPriceLabel, parsePaymentPriceLabel, removeTrailingZeros, verifyDownloadToken };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/util",
3
- "version": "0.13.1",
3
+ "version": "0.13.3",
4
4
  "author": "skypesky ye",
5
5
  "license": "ISC",
6
6
  "description": "",
@@ -46,5 +46,5 @@
46
46
  "@ocap/wallet": "^1.18.123",
47
47
  "lodash": "^4.17.21"
48
48
  },
49
- "gitHead": "1b805eda64ad9cb719c1798d4b84df403edede50"
49
+ "gitHead": "0f295bd0bdbc04e8c8e89dc74b813d6f692e5047"
50
50
  }