@betit/orion
Version:
Pluggable microservice framework
30 lines (29 loc) • 653 B
JavaScript
;
const crypto = require('crypto');
const debug = require('debug');
/**
* @module utils
*/
function debugLog(section) {
return debug(section);
}
exports.debugLog = debugLog;
// @private
const UIDCHARS = 'abcdefghijklmnopqrstuvwxyz0123456789';
/**
* Generate short unique ids.
*/
function generateId(len = 7, hex) {
const buf = crypto.randomBytes(len);
if (hex) {
return buf.toString('hex');
}
else {
const str = [];
for (let i = 0; i < buf.length; i++) {
str.push(UIDCHARS[buf[i] % UIDCHARS.length]);
}
return str.join('');
}
}
exports.generateId = generateId;