# @dylanirlbeck/re-nanoid

> A tiny, secure, URL-friendly, unique string ID generator for Reason/OCaml

Latest version **0.2.1** (published 2020-03-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @dylanirlbeck/re-nanoid
pnpm add @dylanirlbeck/re-nanoid
yarn add @dylanirlbeck/re-nanoid
bun add @dylanirlbeck/re-nanoid
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2020-03-29 |
| First published | 2020-03-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 234.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | dylanirlbeck |
| Maintainers | dylanirlbeck |
| Keywords | reason, ocaml, bucklescript, reasonml |

## Links

- npm: https://www.npmjs.com/package/@dylanirlbeck/re-nanoid
- Repository: https://github.com/dylanirlbeck/re-nanoid
- Homepage: https://github.com/dylanirlbeck/re-nanoid#readme
- Issues: https://github.com/dylanirlbeck/re-nanoid/issues
- npm.io page: https://npm.io/package/@dylanirlbeck/re-nanoid

## Dependencies (1)

- [nanoid](https://npm.io/package/nanoid.md) ^3.0.1

## Recent versions

- 0.2.1 (latest) — 2020-03-29
- 0.2.0 — 2020-03-29
- 0.1.0 — 2020-03-29
- 0.0.2 — 2020-03-29
- 0.0.1 — 2020-03-28

## README

# re-nanoid

A tiny, secure, URL-friendly, unique string ID generator for ReasonML/OCaml,
inspired by [`ai/nanoid`](https://github.com/ai/nanoid).

```reason
open NanoId

let id = nanoid(); // "V1StGXR8_Z5jdHi6B-myT"W
```

### Why re-nanoid?

**Fast**. It's 16% faster than UUID package.

**Safe.** It uses cryptographically strong random APIs
and guarantees a proper distribution of symbols.

**Compact.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`)
and has a similar number of unique IDs in just 21 symbols instead of 36.

See `ai/nanoid`'s [excellent documentation](https://github.com/ai/nanoid/blob/master/README.md#table-of-contents) for more background about random identifier generation and comparisons with UUID v4.

## Installation

### BuckleScript

```
$ yarn add @dylanirlbeck/re-nanoid
```

Then add `@dylanirlbeck/re-nanoid` to your `bs-dependencies` in `bsconfig.json`

```
{
   ...
   "bs-dependencies": ["@dylanirlbeck/re-nanoid"]
}
```

### Native

Coming soon...

## Usage

> As of now, the following examples work for BuckleScript. The Native library
> will be released soon.

### Simple

The main module uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID with
21 characters by default, though this size is variable (see below).

```reason
open NanoId;

let id = nanoid(); // "V1StGXR8_Z5jdHi6B-myT"
```

If you want to reduce the ID size (and increase collision probability), you can
pass the size as an argument. You can also increase the ID size (to a maximum of
36).

```reason
open NanoId;

let id = nanoid(~size=10, ()); // "1s_t232nj_"
```

### Custom length or alphabet

Likewise, if you want to use a different alphabet, you can use the
`customAlphabet` function. Note that this function also takes a `size`
parameter.

```reason
open NanoId;

let alphabet = "#$@jasssfaª•¶";
let size = 7;

let nanoid = customAlphabet(~size, ~alphabet, ());

let id = nanoid(); // "jfa##$·"
```

> Note the use of the `()` as the last parameter of the function call.

### Custom random bytes generator

> Note that this function might not work yet.

`customRandom` allows you to create a `nanoid` and replace the default random
bytes generator.

```reason
open NanoId;

let rbg = size: int => {
  // Put generator logic here
};

let nanoid = customRandom(~alphabet="abcdef", ~size=10, rbg, ());

let id = nanoid(); // "fbaefaadeb"
```

## Contributing

Pull requests are welcome!

## Testing

### BucklesScript - Jest

In order to run the tests as they stand currently, open a new terminal window
and run `yarn watch` to re-compile upon Reason file changes. In yet another
window, run `yarn test` to watch for changes to compiled files and re-run the
tests if changes are detected.

### Native - Rely

Native tests do not yet exist, but they should be present soon.

### License

`re-nanoid` is [MIT licensed](https://github.com/dylanirlbeck/re-nanoid/blob/master/LICENSE).

### Other

This library was born out of a project at HackIllinois 2020. See our Devpost submission [here](https://devpost.com/software/re-nanoid#updates)!

---
_Source: https://npm.io/package/@dylanirlbeck/re-nanoid · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
