@sumor/short-id

short-id

A Sumor Cloud Tool.
More Documentation

This is a short-id library for Node.js and the browser. You can easily use it to generate a short id from number.

CI Test Coverage Audit

Installation

npm i @sumor/short-id --save

Prerequisites

Node.JS version

Require Node.JS version 16.x or above

require Node.JS ES module

As this package is written in ES module, please change the following code in your package.json file:

{
  "type": "module"
}

Usage

Standard Usage

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

Custom Usage

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