@sumor/short-id
Version:
This is a short-id library for Node.js and the browser. You can easily use it to generate a short id from number.
72 lines (51 loc) • 1.95 kB
Markdown
A [Sumor Cloud](https://sumor.cloud) Tool.
[](https://sumor.cloud/short-id)
This is a short-id library for Node.js and the browser.
You can easily use it to generate a short id from number.
[](https://github.com/sumor-cloud/short-id/actions/workflows/ci.yml)
[](https://github.com/sumor-cloud/short-id/actions/workflows/ut.yml)
[](https://github.com/sumor-cloud/short-id/actions/workflows/coverage.yml)
[](https://github.com/sumor-cloud/short-id/actions/workflows/audit.yml)
```bash
npm i @sumor/short-id --save
```
Require Node.JS version 18.x or above
As this package is written in ES module,
please change the following code in your `package.json` file:
```json
{
"type": "module"
}
```
```js
import { encode, decode } from '@sumor/short-id'
// by default using rule 0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ
const shortId1 = encode(10)
console.log(shortId1) // 'a'
const shortId2 = encode(72)
console.log(shortId2) // '1a'
const number1 = decode('a')
console.log(number1) // 10
const number2 = decode('1a')
console.log(number2) // 72
```
```js
import { encode, decode } from '@sumor/short-id'
const rule = '0123456789abcdefghigklmnopqrstuvwxyz'
const shortId1 = encode(10, rule)
console.log(shortId1) // 'a'
const shortId2 = encode(46, rule)
console.log(shortId2) // '1a'
const number1 = decode('a', rule)
console.log(number1) // 10
const number2 = decode('1a', rule)
console.log(number2) // 46
```