@goa/uuid
Version: 
[fork] Simple, fast generation of RFC4122 UUIDS.
25 lines (22 loc) • 760 B
JavaScript
/**
 * Convert array of 16 byte values to UUID string format of the form:
 * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
 */
var byteToHex = []
for (var j = 0; j < 256; ++j) {
  byteToHex[j] = (j + 0x100).toString(16).substr(1)
}
function bytesToUuid(buf, offset = 0) {
  var i = offset
  var bth = byteToHex
  // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
  return ([bth[buf[i++]], bth[buf[i++]],
    bth[buf[i++]], bth[buf[i++]], '-',
    bth[buf[i++]], bth[buf[i++]], '-',
    bth[buf[i++]], bth[buf[i++]], '-',
    bth[buf[i++]], bth[buf[i++]], '-',
    bth[buf[i++]], bth[buf[i++]],
    bth[buf[i++]], bth[buf[i++]],
    bth[buf[i++]], bth[buf[i++]]]).join('')
}
module.exports = bytesToUuid