@blocklet/aigne-hub 0.7.13 → 0.7.14

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.
@@ -3,25 +3,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.CREDIT_DISPLAY_DECIMAL_PLACES = void 0;
7
6
  exports.formatNumber = formatNumber;
8
7
  const trimEnd_1 = __importDefault(require("lodash/trimEnd"));
9
8
  const numbro_1 = __importDefault(require("numbro"));
10
- // Decimal places for credit display in UI (different from backend calculation precision)
11
- exports.CREDIT_DISPLAY_DECIMAL_PLACES = 6;
12
- function formatNumber(n, precision = 6, trim = true, thousandSeparated = true) {
13
- if (!n || n === '0') {
9
+ function formatNumber(n, precision = 2, trim = false, thousandSeparated = true, maxPrecision = 6) {
10
+ const num = (0, numbro_1.default)(n || 0);
11
+ const value = num.value();
12
+ if (!value) {
14
13
  return '0';
15
14
  }
16
- const num = (0, numbro_1.default)(n);
17
- const options = {
18
- thousandSeparated,
19
- ...((precision || precision === 0) && { mantissa: precision }),
15
+ const trimZeros = (valueString) => {
16
+ const [left, right] = valueString.split('.');
17
+ return right ? [left, (0, trimEnd_1.default)(right, '0')].filter(Boolean).join('.') : left;
20
18
  };
21
- const result = num.format(options);
19
+ const isZeroString = (valueString) => {
20
+ const trimmed = trimZeros(valueString);
21
+ return trimmed === '0' || trimmed === '-0';
22
+ };
23
+ let result = value.toString();
24
+ for (let p = precision; p <= Math.max(precision, maxPrecision); p += 1) {
25
+ result = num.format({
26
+ thousandSeparated,
27
+ mantissa: p,
28
+ roundingFunction: (n) => (n < 0 ? Math.ceil(n) : Math.floor(n)),
29
+ });
30
+ if (!isZeroString(result))
31
+ break;
32
+ }
22
33
  if (!trim) {
23
34
  return result;
24
35
  }
25
- const [left, right] = result.split('.');
26
- return right ? [left, (0, trimEnd_1.default)(right, '0')].filter(Boolean).join('.') : left;
36
+ return trimZeros(result);
27
37
  }
@@ -1,20 +1,31 @@
1
1
  import trimEnd from 'lodash/trimEnd';
2
2
  import numbro from 'numbro';
3
- // Decimal places for credit display in UI (different from backend calculation precision)
4
- export const CREDIT_DISPLAY_DECIMAL_PLACES = 6;
5
- export function formatNumber(n, precision = 6, trim = true, thousandSeparated = true) {
6
- if (!n || n === '0') {
3
+ export function formatNumber(n, precision = 2, trim = false, thousandSeparated = true, maxPrecision = 6) {
4
+ const num = numbro(n || 0);
5
+ const value = num.value();
6
+ if (!value) {
7
7
  return '0';
8
8
  }
9
- const num = numbro(n);
10
- const options = {
11
- thousandSeparated,
12
- ...((precision || precision === 0) && { mantissa: precision }),
9
+ const trimZeros = (valueString) => {
10
+ const [left, right] = valueString.split('.');
11
+ return right ? [left, trimEnd(right, '0')].filter(Boolean).join('.') : left;
13
12
  };
14
- const result = num.format(options);
13
+ const isZeroString = (valueString) => {
14
+ const trimmed = trimZeros(valueString);
15
+ return trimmed === '0' || trimmed === '-0';
16
+ };
17
+ let result = value.toString();
18
+ for (let p = precision; p <= Math.max(precision, maxPrecision); p += 1) {
19
+ result = num.format({
20
+ thousandSeparated,
21
+ mantissa: p,
22
+ roundingFunction: (n) => (n < 0 ? Math.ceil(n) : Math.floor(n)),
23
+ });
24
+ if (!isZeroString(result))
25
+ break;
26
+ }
15
27
  if (!trim) {
16
28
  return result;
17
29
  }
18
- const [left, right] = result.split('.');
19
- return right ? [left, trimEnd(right, '0')].filter(Boolean).join('.') : left;
30
+ return trimZeros(result);
20
31
  }
@@ -1,2 +1 @@
1
- export declare const CREDIT_DISPLAY_DECIMAL_PLACES = 6;
2
- export declare function formatNumber(n: number | string, precision?: number, trim?: boolean, thousandSeparated?: boolean): string | undefined;
1
+ export declare function formatNumber(n: number | string, precision?: number, trim?: boolean, thousandSeparated?: boolean, maxPrecision?: number): string | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/aigne-hub",
3
- "version": "0.7.13",
3
+ "version": "0.7.14",
4
4
  "description": "The react.js component library for AIGNE Hub",
5
5
  "publishConfig": {
6
6
  "access": "public"