未验证 提交 5aa01f26 编写于 作者: M Mr.doob 提交者: GitHub

MathUtils: Use crypto.randomUUID() when available. (#22556)

* MathUtils: Use crypto.randomUUID() when available.

* MathUtils: Define generateUUID outside statement.

* MathUtils: Fixed linting issues.

* MathUtils: Made crypto check more robust.

* MathUtils: Added TODO note.

* MathUtils: Fixed linting issues again.
上级 a8db1441
const _lut = [];
let _seed = 1234567;
for ( let i = 0; i < 256; i ++ ) {
const DEG2RAD = Math.PI / 180;
const RAD2DEG = 180 / Math.PI;
_lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 );
let generateUUID;
}
if ( typeof crypto !== 'undefined' && 'randomUUID' in crypto ) {
let _seed = 1234567;
generateUUID = function generateUUID() {
return crypto.randomUUID().toUpperCase();
const DEG2RAD = Math.PI / 180;
const RAD2DEG = 180 / Math.PI;
};
} else {
// TODO Remove this code when crypto.randomUUID() is available everywhere
const _lut = [];
for ( let i = 0; i < 256; i ++ ) {
_lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 );
}
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
generateUUID = function generateUUID() {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
function generateUUID() {
const d0 = Math.random() * 0xffffffff | 0;
const d1 = Math.random() * 0xffffffff | 0;
const d2 = Math.random() * 0xffffffff | 0;
const d3 = Math.random() * 0xffffffff | 0;
const uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' +
_lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' +
_lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] +
_lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ];
const d0 = Math.random() * 0xffffffff | 0;
const d1 = Math.random() * 0xffffffff | 0;
const d2 = Math.random() * 0xffffffff | 0;
const d3 = Math.random() * 0xffffffff | 0;
const uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' +
_lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' +
_lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] +
_lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ];
// .toUpperCase() here flattens concatenated strings to save heap memory space.
return uuid.toUpperCase();
// .toUpperCase() here flattens concatenated strings to save heap memory space.
return uuid.toUpperCase();
};
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册