0.0.7 • Published 4 months ago

@sil/id v0.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

ID

Create a random ID using the characters you prefer and in the format you want.

Installation

npm install @sil/id

Usage

As a composable

import { useId } from "@sil/id";

const id = useId();

// id() can be used anywhere and will create a unique key

Or directly

import { createId } from "@sil/id";

const id = createId();

// id is now a string with a key.

Settings

Alphabet

The alphabet defines the characters which can be used to create any ID. By default an ID can use all letters in upper and lowercase plus the numbers. But you can change this by settings the alphabet.

import { useId, IdAlphabet } from "@sil/id";

const id = useId({
  alphabet: IdAlphabet["A-Z"],
});

Options:

OptionValueDescription
a-zabcdefghijklmnopqrstuvwxyzAll 26 letters in lowercase
A-ZABCDEFGHIJKLMNOPQRSTUVWXYZAll 26 letters in uppercase
0-90123456789All numbers from 0 to 9
a-Zabcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZAll 26 letters in uppercase + lowercase
a-Z0-9abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789All 26 letters in uppercase + lowercase + all numbers
a-z0-9abcdefghijklmnopqrstuvwxy 0123456789All 26 letters in lowercase + all numbers
A-Z0-9ABCDEFGHIJKLMNOPQRSTUVWXY 0123456789All 26 letters in uppercase + all numbers
customAny stringYou can give a string of any type of charactesr which can be used ID

Custom alphabet sample

import { useId } from "@sil/id";

const id = useId({
  alphabet: "!@#$%^&*()",
});

// example:  %(%$-&@(@-(@@&-*(*)

Or using another alphabet:

import { useId } from "@sil/id";

const id = useId({
  alphabet: "աբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆուև",
});

// example:  բէպո-ֆտիօ-դոպի-րտռս

Total

You can set the total, in this case it will just create an ID without a format with a random string of characters totalling the total defined.

import { useId } from "@sil/id";

const id = useId({
  total: 10,
});

// example: tzeNNbXTnq

Format

By default a format like XXXX-XXXX-XXXX-XXXX is used for an id, you can change this to whatever you like. The X in the format will be replaced by the characters defined in alphabet.

code example:

import { useId } from "@sil/id";

const id = useId({
  format: "XXXX_XX",
});

// example: pKCM_Lr

Custom formats

X Will be replaced by a random letter defined in alphabet or by default a-Z0-9 A Will be replaced by a random uppercase letter from the alphabet a Will be replaced by a random lowercase letter from the alphabet 0 Will be replaced by a random number

code example:

import { useId } from "@sil/id";

const id = useId({
  format: "AAA_aaa_000_XXX",
});

// example: BVR_bcu_629_yc7

gist=2d9aff65094156a9f52f67594e8000d0

0.0.7

4 months ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago