Reemplace shortid with nanoid in background with alphabet and length configurable #810
This commit is contained in:
parent
aed115a64b
commit
8b900c94ba
3 changed files with 32 additions and 0 deletions
27
server/lib/shortid.js
Normal file
27
server/lib/shortid.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
"use strict";
|
||||
|
||||
// Modules
|
||||
const nanoid = require('nanoid');
|
||||
const config = require('./config');
|
||||
|
||||
// Default hardcoded values
|
||||
let alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
let customlength = 10;
|
||||
|
||||
// Gets from config if defined
|
||||
if (config.cid && config.cid.alphabet) alphabet=config.cid.alphabet;
|
||||
if (config.cid && config.cid.length) customlength=config.cid.length;
|
||||
|
||||
// Create custom nanoid
|
||||
const customnanoid = nanoid.customAlphabet(alphabet, customlength);
|
||||
|
||||
const re = new RegExp('['+alphabet+']{'+customlength+'}');
|
||||
|
||||
// Implements the public methods of shortid module with nanoid and export them
|
||||
module.exports.generate = function() {
|
||||
return customnanoid();
|
||||
}
|
||||
|
||||
module.exports.isValid = function(id) {
|
||||
return re.test(id);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue