Return a unique identifier with the given `len`. - Node.js Security

Node.js examples for Security:GUID

Description

Return a unique identifier with the given `len`.

Demo Code

/*//from   w  ww  .  j  a  va 2  s .  c  om
 *
 *     utils.uid(10);
 *     // => "FDaS435D2z"
 *
 * @param {Number} len
 * @return {String}
 * @api private
 */
module.exports = function(len) {
  var buf = [],
  chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  charlen = chars.length;

  for (var i = 0; i < len; ++i) {
    buf.push(chars[getRandomInt(0, charlen - 1)]);
  }

  return buf.join('');
};

Related Tutorials