-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1953a94
commit c2f9e4b
Showing
8 changed files
with
237 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.