Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
alpheustangs committed Jun 22, 2024
1 parent 1953a94 commit c2f9e4b
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 149 deletions.
112 changes: 18 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,125 +1,49 @@
# RowID

RowID, a time based unique ID solution.
A time based unique ID solution.

## Install
## Installation

For NPM:
Install RowID as a dependency:

```bash
npm i rowid
```
# NPM
npm install rowid

For PNPM:
# Yarn
yarn add rowid

```bash
pnpm i rowid
# PNPM
pnpm add rowid
```

## Usage

Usage of RowID:

#### `RowID`

This function generates a 32-character unique ID that is almost impossible to duplicate.

```typescript
import RowID from "rowid";

const id: string = RowID();
## Quick Start

console.log(typeof id === "string"); // true
```

Or you can specify the number of randomness, a larger number will generate a longer ID, with less chance of collision.
You may create a RowID with the following code:

```typescript
import RowID from "rowid";

const id: string = RowID(6);

console.log(id.length === (10 + 6)); // true
```

#### `encode`

This function encodes the timestamp into a ID without randomness.

```typescript
import { encode } from "rowid";

const result: string = encode(new Date().getTime());

console.log(typeof result === "string"); // true
```

#### `decode`

This function decodes the ID into a Date.

```typescript
import { RowID, decode } from "rowid";

const id: string = RowID();
const result: Date = decode(id);

console.log(result instanceof Date); // true
```

#### `generate`

This function generates a ID based on the input.

```typescript
import { generate } from "rowid";

generate(new Date().getTime(), 22);
```

#### `verify`

This function verifies if the ID is valid and natural.

```typescript
import { RowID, verify } from "rowid";

const id: string = RowID();

verify(id);
```

#### `getRandomness`

This function generates randomness. It use different methods to generate randomness based on the environment, such as window.crypto on web, node:crypto on Node, and Math.random if all else fails.
Or customize the RowID with the following code:

```typescript
import { getRandomness } from "rowid";

const result: string = getRandomness(6);

console.log(typeof result === "string"); // true
console.log(result.length === 6); // true
```

#### `RowIDWithConfig`

This function allows you to customize how RowID works, and returns the modified functions based on the parameters.

```typescript
import type { RowIDWithConfigResult } from "rowid";

import { RowIDWithConfig } from "rowid";

const { RowID }: RowIDWithConfigResult = RowIDWithConfig({
charList: "0123456789ABCDEFGHJKMNPQRSTVWXYZ",
randomnessLength: 22,
});

const id: string = RowID(6);

console.log(id.length === (10 + 6)); // true
const id: string = RowID();
```

For more information, please refer to [docs/usage.md](./docs/usage.md).

For the CLI, please refer to [docs/cli.md](./docs/cli.md).

## License

This project is MIT licensed, you can find the license file [here](./LICENSE).
56 changes: 56 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# RowID CLI Usage

This documentation provides the usage of RowID CLI.

## Installation

Install RowID CLI as a global dependency:

```bash
# NPM
npm install --global rowid-cli

# Yarn
yarn global add rowid-cli

# PNPM
pnpm add --global rowid-cli
```

## Commands

RowID CLI provides the following commands:

#### `rowid`

This command generates a 32-character unique ID that is almost impossible to duplicate. You may pass the `-r` / `--randomness` option to specify the number of randomness, a larger number will generate a longer ID, with less chance of collision.

#### `rowid encode <date>`

This command encodes the timestamp into a ID without randomness.

#### `rowid decode <encoded>`

This command decodes the ID into a Date.

#### `rowid generate <date> [number]`

This command generates a ID based on the input. Since the `number` argument is optional, you may omit it.

#### `rowid verify <encoded>`

This command verifies if the ID is valid and natural.

#### `rowid random <number>`

This command generates randomness.

## Customization

After installing the CLI and running it once, you may customize its behavior by editing the `.rowid.json` file located in the current user's home directory. For type checking, please reference to the `schema.json` file included in the root directory of the CLI project.

```json
{
"$schema": "/path/to/cli/schema.json"
}
```
123 changes: 123 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# RowID Usage

This documentation provides the usage of RowID.

## Installation

Install RowID as a dependency:

```bash
# NPM
npm install rowid

# Yarn
yarn add rowid

# PNPM
pnpm add rowid
```

## Functions

RowID provides the following functions:

#### `RowID`

This function generates a 32-character unique ID that is almost impossible to duplicate.

```typescript
import RowID from "rowid";

const id: string = RowID();

console.log(typeof id === "string"); // true
```

Or you can specify the number of randomness, a larger number will generate a longer ID, with less chance of collision.

```typescript
import RowID from "rowid";

const id: string = RowID(6);

console.log(id.length === (10 + 6)); // true
```

#### `encode`

This function encodes the timestamp into a ID without randomness.

```typescript
import { encode } from "rowid";

const result: string = encode(new Date().getTime());

console.log(typeof result === "string"); // true
```

#### `decode`

This function decodes the ID into a Date.

```typescript
import { RowID, decode } from "rowid";

const id: string = RowID();
const result: Date = decode(id);

console.log(result instanceof Date); // true
```

#### `generate`

This function generates a ID based on the input.

```typescript
import { generate } from "rowid";

generate(new Date().getTime(), 22);
```

#### `verify`

This function verifies if the ID is valid and natural.

```typescript
import { RowID, verify } from "rowid";

const id: string = RowID();

verify(id);
```

#### `getRandomness`

This function generates randomness with different methods based on the environment.

```typescript
import { getRandomness } from "rowid";

const result: string = getRandomness(6);

console.log(typeof result === "string"); // true
console.log(result.length === 6); // true
```

#### `RowIDWithConfig`

This function allows you to customize how RowID works, and returns the modified functions based on the parameters.

```typescript
import type { RowIDWithConfigResult } from "rowid";

import { RowIDWithConfig } from "rowid";

const { RowID }: RowIDWithConfigResult = RowIDWithConfig({
charList: "0123456789ABCDEFGHJKMNPQRSTVWXYZ",
randomnessLength: 6,
});

const id: string = RowID();

console.log(id.length === (10 + 6)); // true
```
28 changes: 5 additions & 23 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,18 @@

CLI for RowID

## Installation
## Quick Start

For NPM:
You may run the following command to generate RowID:

```bash
npm i -g rowid-cli
rowid
```

For PNPM:
Or ask for help with the following command:

```bash
pnpm i -g rowid-cli
```

## Usage

Check the usage of RowID CLI with the following command:

```bash
rowid -h
```

## Customization

After installing the CLI and running it once, you may customize its behavior by editing the `.rowid.json` file located in the current user's home directory. For type checking, please reference to the `schema.json` file included in the root directory of the CLI project.

```json
{
"$schema": "/path/to/cli/schema.json"
}
rowid --help
```

## License
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import { readPackageJson } from "#/functions/readPackageJson";
program
.command("decode")
.description("decode a RowID to date")
.argument("<rowid>", "the RowID to decode")
.argument("<encoded>", "the encoded to decode")
.action(async (encoded: string): Promise<void> => {
try {
console.log(decode(encoded));
Expand Down Expand Up @@ -127,7 +127,7 @@ import { readPackageJson } from "#/functions/readPackageJson";
program
.command("verify")
.description("verify a RowID")
.argument("<rowid>", "the RowID to verify")
.argument("<encoded>", "the encoded to verify")
.action(async (encoded: string): Promise<void> => {
try {
console.log(JSON.stringify(verify(encoded)));
Expand Down
Loading

0 comments on commit c2f9e4b

Please sign in to comment.