@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.
- package/lib/cjs/utils/util.js +22 -12
- package/lib/esm/utils/util.js +22 -11
- package/lib/types/utils/util.d.ts +1 -2
- package/package.json +1 -1
package/lib/cjs/utils/util.js
CHANGED
|
@@ -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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (!
|
|
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
|
|
17
|
-
|
|
18
|
-
|
|
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
|
|
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
|
-
|
|
26
|
-
return right ? [left, (0, trimEnd_1.default)(right, '0')].filter(Boolean).join('.') : left;
|
|
36
|
+
return trimZeros(result);
|
|
27
37
|
}
|
package/lib/esm/utils/util.js
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
import trimEnd from 'lodash/trimEnd';
|
|
2
2
|
import numbro from 'numbro';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (!
|
|
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
|
|
10
|
-
|
|
11
|
-
|
|
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
|
|
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
|
-
|
|
19
|
-
return right ? [left, trimEnd(right, '0')].filter(Boolean).join('.') : left;
|
|
30
|
+
return trimZeros(result);
|
|
20
31
|
}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export declare
|
|
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;
|