Last change
on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Line | |
---|
1 | import validate from './validate.js';
|
---|
2 | /**
|
---|
3 | * Convert array of 16 byte values to UUID string format of the form:
|
---|
4 | * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
|
---|
5 | */
|
---|
6 |
|
---|
7 | var byteToHex = [];
|
---|
8 |
|
---|
9 | for (var i = 0; i < 256; ++i) {
|
---|
10 | byteToHex.push((i + 0x100).toString(16).substr(1));
|
---|
11 | }
|
---|
12 |
|
---|
13 | function stringify(arr) {
|
---|
14 | var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
---|
15 | // Note: Be careful editing this code! It's been tuned for performance
|
---|
16 | // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
|
---|
17 | var uuid = (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase(); // Consistency check for valid UUID. If this throws, it's likely due to one
|
---|
18 | // of the following:
|
---|
19 | // - One or more input array values don't map to a hex octet (leading to
|
---|
20 | // "undefined" in the uuid)
|
---|
21 | // - Invalid input values for the RFC `version` or `variant` fields
|
---|
22 |
|
---|
23 | if (!validate(uuid)) {
|
---|
24 | throw TypeError('Stringified UUID is invalid');
|
---|
25 | }
|
---|
26 |
|
---|
27 | return uuid;
|
---|
28 | }
|
---|
29 |
|
---|
30 | export default stringify; |
---|
Note:
See
TracBrowser
for help on using the repository browser.