Skip to content

Commit

Permalink
docs: add chinese language support
Browse files Browse the repository at this point in the history
  • Loading branch information
logeast committed Jul 29, 2023
1 parent b133809 commit 825fccd
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 15 deletions.
26 changes: 11 additions & 15 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ export default defineConfig({
base: "/magic-lottery/",
title: "Magic Lottery",
description: "A magic tool makes your lucky draws simpler.",
locales: {
root: {
label: "English",
lang: "en-US",
},
zh: {
label: "简体中文",
lang: "zh",
title: "魔法抽奖",
},
},
head: [
["meta", { name: "theme-color", content: "#ED4192" }],
[
Expand All @@ -27,33 +38,18 @@ export default defineConfig({
themeConfig: {
// https://vitepress.dev/reference/default-theme-config
logo: "https://logeast.cc/cdn/imghub/logo.png",

editLink: {
pattern: "https://github.com/logeast/magic-lottery/tree/main/docs/:path",
text: "Suggest changes to this page",
},

nav: [
{ text: "Guide", link: "/guide/index.md" },
{ text: "API", link: "/api/index.md" },
{ text: "Cases", link: "/cases/index.md" },
],

// sidebar: [
// {
// text: "Examples",
// items: [
// { text: "Guide", link: "/guide" },
// { text: "API", link: "/api" },
// { text: "Examples", link: "/examples" },
// ],
// },
// ],

search: {
provider: "local",
},

socialLinks: [
{ icon: "twitter", link: "https://twitter.com/logeast4" },
{ icon: "github", link: "https://github.com/logeast/magic-lottery" },
Expand Down
252 changes: 252 additions & 0 deletions docs/zh/api/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
# API 文档

以下是 Magic Lottery 的完整类结构,包括类型签名和方法描述。

```ts
class MagicLottery<T> {
private entries: T[];
private shuffledEntries: T[];
private shuffle: (input: T[]) => T[];
public channelName?: string;

constructor(
entries: T[],
shuffle?: (input: T[]) => T[],
channelName?: string
);

setChannelName(channelName: string): void;
getChannelName(): string | undefined;
add(entries: T[]): void;
draw(): T[];
drawOriginal(): T[];
drawWinner(): T;
drawWinners(num: number): T[];
setShuffle(shuffle: (input: T[]) => T[]): void;
getShuffle(): (input: T[]) => T[];
remove(entry: T): void;
hasEntry(entry: T): boolean;
size(): number;
isEmpty(): boolean;
reset(): void;
async nextWinner(): Promise<T | undefined>;
}
```

## 导入或引用

要在项目中使用 Magic Lottery 类,需要导入它。

### 浏览器

在使用 ES6 模块的浏览器环境中:

```ts
import { MagicLottery } from "magic-lottery";
//
import { Lottery } from "magic-lottery";
//
import MagicLottery from "magic-lottery";
```

### Node.js 环境

在 Node.js 环境中,可以使用 CommonJS 的 require 语法来导入 Magic Lottery 类:

```ts
const { MagicLottery } = require("magic-lottery");
//
const { Lottery } = require("magic-lottery");
//
const MagicLottery = require("magic-lottery");
```

如果你的 Node.js 环境支持 ES6 模块,你可以像在浏览器环境中一样使用 import 语法。

## 构造函数

- **类型:** `(entries: T[], shuffle?: (input: T[]) => T[], channelName?: string) => MagicLottery<T>`

`constructor` 初始化一个新的 `MagicLottery` 实例。它接受一个条目数组和一个可选的洗牌函数。如果没有提供洗牌函数,将使用默认的 Fisher-Yates 洗牌实现。也可以提供一个可选的频道名称。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);

// 使用自定义洗牌方法和频道名称
const anotherLottery = new MagicLottery<number>(
[1, 2, 3, 4, 5],
(input) => input.reverse(),
"Weekly Draw"
);
```

在第二个例子中,抽奖初始化了 1 到 5 的条目,使用了一个将列表反转的洗牌方法,并设置了 `channelName` 为 'Weekly Draw'。

## setChannelName

- **类型:** `(channelName: string) => void`

`setChannelName` 允许你为当前的抽奖实例指定一个频道名称。

```ts
const lottery = new MagicLottery<number>([1, 2, 3]);
lottery.setChannelName("Daily Draw");
```

## getChannelName

- **类型:** `() => string | undefined`

`getChannelName` 返回当前抽奖频道的名称,如果没有设置,返回 undefined。

```ts
const lottery = new MagicLottery<number>([1, 2, 3]);
lottery.setChannelName("Daily Draw");
console.log(lottery.getChannelName()); // 'Daily Draw'
```

## add

- **类型:** `(entries: T[]) => void`

`add` 允许你向抽奖池中添加更多条目。

```ts
const lottery = new MagicLottery<number>([1, 2, 3]);
lottery.add([4, 5, 6]); // The lottery now includes 1 through 6.
```

## draw

- **类型:** `() => T[]`

`draw` 允许你从抽奖中抽取所有洗牌后的条目。

```ts
const lottery = new MagicLottery<number>([1, 2, 3]);
console.log(lottery.draw()); // 洗牌后的数字数组
```

## drawOriginal

- **类型:** `() => T[]`

`drawOriginal` 从抽奖中抽取所有原始的、未洗牌的条目。

```ts
const lottery = new MagicLottery<number>([1, 2, 3]);
console.log(lottery.drawOriginal()); // [1, 2, 3]
```

## drawWinner

- **类型:** `() => T`

`drawWinner` 从洗牌后的条目中抽取第一个获奖者。

```ts
const lottery = new MagicLottery<number>([1, 2, 3]);
console.log(`The winner is: ${lottery.drawWinner()}`);
```

## drawWinners

- **类型:** `(num: number) => T[]`

`drawWinners` 从洗牌后的条目中抽取指定数量的获奖者。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);
console.log(`The winners are: ${lottery.drawWinners(2)}`);
```

## setShuffle

- **类型:** `(shuffle: (input: T[]) => T[]) => void`

`setShuffle` 允许你为抽奖定义一个自定义的洗牌方法。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);
lottery.setShuffle((input) => input.reverse());
```

## getShuffle

- **类型:** `() => (input: T[]) => T[]`

`getShuffle` 返回抽奖实例当前使用的洗牌方法。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);
const currentShuffleMethod = lottery.getShuffle();
```

## remove

- **类型:** `(entry: T) => void`

`remove` 从抽奖中移除一个特定的条目。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);
lottery.remove(3); // 从条目列表中移除 '3'。
```

## hasEntry

- **类型:** `(entry: T) => boolean`

`hasEntry` 检查抽奖中是否存在特定的条目。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);
console.log(lottery.hasEntry(3)); // True
```

## size

- **类型:** `() => number`

`size` 获取当前抽奖中的条目数量。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);
console.log(lottery.size()); // '5'
```

## isEmpty

- **类型:** `() => boolean`

`isEmpty` 验证抽奖是否为空。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);
console.log(lottery.isEmpty()); // False
```

## reset

- **类型:** `() => void`

`reset` 帮助你清除当前抽奖实例中的所有条目。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);
lottery.reset();
console.log(lottery.isEmpty()); // True
```

## nextWinner

- **类型:** `() => Promise<T | undefined>`

`nextWinner` 从洗牌后的条目中移除第一个元素并解析它,返回一个 Promise。

```ts
const lottery = new MagicLottery<number>([1, 2, 3, 4, 5]);
lottery.nextWinner().then((winner) => console.log(winner)); // 记录获奖者
```

请注意,这个方法是异步的。建议处理可能没有更多条目留在抽奖中的情况,这将导致 Promise 拒绝。
3 changes: 3 additions & 0 deletions docs/zh/cases/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 案例

即将上线,请保持关注。
56 changes: 56 additions & 0 deletions docs/zh/guide/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 入门指南

Magic Lottery 是一个直观的库,旨在简化你的抽奖体验,使其更简单,更愉快,更公平。

Magic Lottery 默认使用 [Fisher-Yates Shuffle Algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) 作为洗牌方法。

## 安装

可以使用 npm/yarn/pnpm 来安装 Magic Lottery。在终端中执行以下命令:

```bash
npm install magic-lottery
# or
yarn add magic-lottery
# or
pnpm add magic-lottery
```

## 使用示例

这是 Magic Lottery 的一个简单使用案例。

```js
import MagicLottery from "magic-lottery";

// 创建一个新的MagicLottery实例
const lottery = new MagicLottery(["Alice", "Bob", "Charlie"]);

// 向抽奖中添加更多条目
lottery.add(["David", "Eve"]);

// 抽取所有洗牌后的条目
console.log(lottery.draw());

// 从洗牌后的条目中抽取第一名获奖者
console.log(lottery.drawWinner());

// 从洗牌后的条目中抽取指定数量的获奖者
console.log(lottery.drawWinners(2));

// 检查条目是否在抽奖中
console.log(lottery.hasEntry("Alice"));

// 获取抽奖的大小
console.log(lottery.size());

// 检查抽奖是否为空
console.log(lottery.isEmpty());

// 重置抽奖
lottery.reset();
```

## 使用 Magic Lottery 的项目

欢迎与我们分享你的项目。

0 comments on commit 825fccd

Please sign in to comment.