在 uuid 创建中替代按位运算符
Alternative to bitwise operators in uuid creation
我正在使用以下打字稿方法生成 UUID
s。代码本身基本上就是 Whosebug answer.
的打字稿版本
generateUUID(): string {
let date = new Date().getTime();
if (window.performance && typeof window.performance.now === 'function') {
date += performance.now();
}
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = (date + Math.random() * 16) % 16 | 0;
date = Math.floor(date / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
};
我们的开发团队使用 TSLint
来保持代码干净,我们有一条规则禁止使用 bitwise operators
。我不知道如何在不损害 UUID 生成器的加密方面的情况下重写此代码。这段代码怎么改写或者这根本就没有意义?
TSLint 强调这一点的原因是因为按位运算符更有可能被意外使用(例如,在 if 语句中)而不是故意使用它。
告诉 TSLint 您真的 打算使用按位运算符应该是完全可以接受的。只需将它们包装在特殊的 TSLint 注释中。 :
/* tslint:disable:no-bitwise */
// Your code...
/* tslint:enable:no-bitwise */
export abstract class SystemGuid{
constructor() {
// no-op
}
public static UUID(): string {
if (typeof window !== 'undefined' && typeof window.crypto !== 'undefined'
&& typeof window.crypto.getRandomValues !== 'undefined') {
const buf: Uint16Array = new Uint16Array(8);
window.crypto.getRandomValues(buf);
return (
this.pad4(buf[0]) +
this.pad4(buf[1]) +
'-' +
this.pad4(buf[2]) +
'-' +
this.pad4(buf[3]) +
'-' +
this.pad4(buf[4]) +
'-' +
this.pad4(buf[5]) +
this.pad4(buf[6]) +
this.pad4(buf[7])
);
} else {
return (
this.random4() +
this.random4() +
'-' +
this.random4() +
'-' +
this.random4() +
'-' +
this.random4() +
'-' +
this.random4() +
this.random4() +
this.random4()
);
}
}
private static pad4(num: number): string {
let ret: string = num.toString(16);
while (ret.length < 4) {
ret = '0' + ret;
}
return ret;
}
private static random4(): string {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
public static generate(): string {
return SystemGuid.UUID();
}
}
我正在使用以下打字稿方法生成 UUID
s。代码本身基本上就是 Whosebug answer.
generateUUID(): string {
let date = new Date().getTime();
if (window.performance && typeof window.performance.now === 'function') {
date += performance.now();
}
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = (date + Math.random() * 16) % 16 | 0;
date = Math.floor(date / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
};
我们的开发团队使用 TSLint
来保持代码干净,我们有一条规则禁止使用 bitwise operators
。我不知道如何在不损害 UUID 生成器的加密方面的情况下重写此代码。这段代码怎么改写或者这根本就没有意义?
TSLint 强调这一点的原因是因为按位运算符更有可能被意外使用(例如,在 if 语句中)而不是故意使用它。
告诉 TSLint 您真的 打算使用按位运算符应该是完全可以接受的。只需将它们包装在特殊的 TSLint 注释中。 :
/* tslint:disable:no-bitwise */
// Your code...
/* tslint:enable:no-bitwise */
export abstract class SystemGuid{
constructor() {
// no-op
}
public static UUID(): string {
if (typeof window !== 'undefined' && typeof window.crypto !== 'undefined'
&& typeof window.crypto.getRandomValues !== 'undefined') {
const buf: Uint16Array = new Uint16Array(8);
window.crypto.getRandomValues(buf);
return (
this.pad4(buf[0]) +
this.pad4(buf[1]) +
'-' +
this.pad4(buf[2]) +
'-' +
this.pad4(buf[3]) +
'-' +
this.pad4(buf[4]) +
'-' +
this.pad4(buf[5]) +
this.pad4(buf[6]) +
this.pad4(buf[7])
);
} else {
return (
this.random4() +
this.random4() +
'-' +
this.random4() +
'-' +
this.random4() +
'-' +
this.random4() +
'-' +
this.random4() +
this.random4() +
this.random4()
);
}
}
private static pad4(num: number): string {
let ret: string = num.toString(16);
while (ret.length < 4) {
ret = '0' + ret;
}
return ret;
}
private static random4(): string {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
public static generate(): string {
return SystemGuid.UUID();
}
}