Skip to content

Generate a short unique ID from the integer for egg.js

License

Notifications You must be signed in to change notification settings

weihongyu12/egg-hashids

Repository files navigation

egg-hashids

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Hashids is small JavaScript library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.

This plugin is only for egg.js

Install

$ npm i eggjs-hashids --save

Usage

// {app_root}/config/plugin.js
exports.hashids = {
  enable: true,
  package: 'eggjs-hashids',
};

Configuration

// {app_root}/config/config.default.js
exports.hashids = {
  salt: '',
  minLength: 0,
  alphabet: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
};

see config/config.default.js and hashids for more detail.

Example

this.app.hashids.encode(1);   // => 'jR'
this.app.hashids.decode('jR'); // => [ 1 ]
this.app.hashids.encodeHex('507f1f77bcf86cd799439011'); // => 'y42LW46J9luq3Xq9XMly'
this.app.hashids.decodeHex('y42LW46J9luq3Xq9XMly'); // => '507f1f77bcf86cd799439011'

Questions & Suggestions

Please open an issue here.

License

MIT

About

Generate a short unique ID from the integer for egg.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published