2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* UUID operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const UUID = {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate UUID operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runGenerateV4: function(input, args) {
|
2017-03-30 00:40:20 +01:00
|
|
|
if (window && typeof(window.crypto) !== "undefined" && typeof(window.crypto.getRandomValues) !== "undefined") {
|
2017-04-13 18:08:50 +01:00
|
|
|
let buf = new Uint32Array(4),
|
2016-11-28 10:42:58 +00:00
|
|
|
i = 0;
|
|
|
|
window.crypto.getRandomValues(buf);
|
|
|
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let r = (buf[i >> 3] >> ((i % 8) * 4)) & 0xf,
|
2016-11-28 10:42:58 +00:00
|
|
|
v = c === "x" ? r : (r & 0x3 | 0x8);
|
|
|
|
i++;
|
|
|
|
return v.toString(16);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let r = Math.random() * 16 | 0,
|
2016-11-28 10:42:58 +00:00
|
|
|
v = c === "x" ? r : (r & 0x3 | 0x8);
|
|
|
|
return v.toString(16);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default UUID;
|