# @consolelabs/mochi-ui 中文文档

规范地址：https://npmcn.com/package/%40consolelabs/mochi-ui/

版本：17\.0\.3
npm 元数据更新时间：2023-12-26T05:02:24.418Z
中文译文更新时间：2026-11-14T09:42:46.735Z
软件包许可：ISC

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40consolelabs/mochi-ui
原文对照：https://npmcn.com/package/%40consolelabs/mochi-ui/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：390 kB，47 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：9 个
- peer 依赖：ioredis ^5\.x\.x，@consolelabs/mochi-rest ^5\.1\.9
- 首次发布：2023-08-21T07:11:27.162Z
- 最新版本发布：2023-12-26T05:02:24.212Z
- 近 30 天下载量：2092（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：114（近 12 个月 0 个）

## 中文简介

用于配置、数字、文本的 Mochi UI

## 安装

```sh
npm install @consolelabs/mochi-ui
```

也可以使用：`pnpm add @consolelabs/mochi-ui`、`yarn add @consolelabs/mochi-ui`、`bun add @consolelabs/mochi-ui`

## README

![mochi pose 13](https://github.com/consolelabs/mochi.js/assets/25856620/aa8250b3-4f3e-4231-aa9e-e788b8883ea4)

# 莫奇内 ![npm latest version](https://badgen.net/npm/v/@consolelabs/mochi-formatter?cache=300)

本套件包括您需要制作任何Mochi数据的基本组件,一般有3个主要目标:

-   伦德·莫奇身份
-   使用余额列表,活动,交易列表等组件.
-   使用文本/ 数字格式化器等帮助...

## 使用量

```typescript
import API from "@consolelabs/mochi-rest";
import UI, { utils } from "@consolelabs/mochi-formatter";
import Redis from "ioredis";

// optionally use redis for better perf
UI.redis = new Redis();

const api = new API({});
api.init().then(() => {
  UI.api = api;
});

const [account, otherAccount] = await UI.resolve(
  Platform.Web,
  profile_id,
  other_profile_id
);

const markdownText = await UI.components.balance({
  /* props */
});

const formattedUsd = utils.formatUsdDigit(23.12563);
console.log(formattedUsd); // "$23.12"
```

## • API

### `resolve(on: Platform.Web | Platform.Discord | Platform.Telegram, profile_id_A: string, profile_id_B?: string)`

```typescript
const [account, otherAccount] = await UI.resolve(
  Platform.Web, // if you're using UI library on web
  profile_id,
  other_profile_id
);

// or using it on self
const [account] = await UI.resolve(Platform.Web, profile_id);
```

在一个你正在渲染的平台上 一对需要解决的特征, `profile_id_B` 默认为 `profile_id_A` 如果不是通过。 返回值是一个具有以下属性的对象

```typescript
type UsernameFmt = {
  // this value is markdown
  value: string;
  // the id of that platform
  id: string;
  // url of this account
  url: string;
  // same as `value` but in plain text format
  plain: string;
  // in case of invalid account, this will be null
  platform?:
    | Platform.App
    | Platform.Mochi
    | Platform.Discord
    | Platform.Telegram
    | Platform.Twitter
    | Platform.Vault
    | null;
};
```

### 公用事业

-   `formatUsdDigit(input: string | number | object): string`: 返回带有 $ 前缀的 usd 值的字符串表达式
-   `formatPercentDigit(input: string | number | object): string`: 返回带有%后缀的百分比值的字符串表示符
-   `formatTokenDigit(input: string | number | object): string`: 返回符号值的字符串表示符
-   `formatDigit(options: object): string`: 基物,选项形状为:

```typescript
type Options = {
  value: string | number;
  // how many numbers to keep after decimal point
  fractionDigits?: number;
  // decides whether the result should have commas
  withoutCommas?: boolean;
  // use shorten form e.g. 200,000 -> 200K, $1,234,567,890 -> $1B
  // note that by using this option some precision of number will be lost
  shorten?: boolean;
  // use for very small numbers e.g. 1e-8
  scientificFormat?: boolean;
};
```

注意: 所有格式函数除外 `formatDigit` 遵循相同的小数点格式化规则,即:

-   对于百分比值,如果值为%\\ 10,则自动隐藏小数点
    
-   对于美元值,如果值为 QQ 100,则自动隐藏小数点
    
-   对于符号值,如果值为 QQ 1000,则自动隐藏小数点
    
-   如果必须显示小数点,我们只取最大2位数而不是从点起的0,如果两者均为0,那么取第一个不是0的数字,一些例子:
    

```
0.02345 -> 0.02
0.2103 -> 0.2
0.00003981 -> 0.0003
```

### (即将到来)

## 常见问题

### @consolelabs/mochi-ui 是什么？

用于配置、数字、文本的 Mochi UI

### 如何安装 @consolelabs/mochi-ui？

在项目目录运行 npm install @consolelabs/mochi-ui，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：ioredis（^5\.x\.x）、@consolelabs/mochi-rest（^5\.1\.9）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @consolelabs/mochi-ui 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @consolelabs/mochi-ui 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @consolelabs/mochi-ui 可以商用吗？

@consolelabs/mochi-ui 使用 ISC 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @consolelabs/mochi-ui 的下载量有多少？

根据 npm 官方下载统计，@consolelabs/mochi-ui 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,092 次，日均约 70 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @consolelabs/mochi-ui 还在维护吗？

最新版本 17\.0\.3 发布于 2023/12/26（2 年前）；至今共发布 114 个版本，其中过去 12 个月发布了 0 个；首次发布于 2023/08/21。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

