优化 isHashable

This commit is contained in:
陆柯 2021-01-24 11:57:38 +08:00
parent 069a3ff28c
commit 73c33e3175

View File

@ -46,7 +46,7 @@ class TICrypto {
*/ */
static isHashable(data, { strict = false } = {}) { static isHashable(data, { strict = false } = {}) {
if (strict) { if (strict) {
return data ? true : false && typeof data !== 'boolean' && data !== Infinity // 允许大多数数据除了null、''、0、布尔值、无限数。注意 data 要放在最后,否则会被 return 直接返回,而不是返回 Boolean return typeof data !== 'boolean' && data !== Infinity && data ? true : false // 允许大多数数据除了null、''、0、布尔值、无限数。注意 data 要放在最后,否则会被 return 直接返回,而不是返回 Boolean
} }
return typeof data !== 'undefined' // 允许一切数据,除非 undefined return typeof data !== 'undefined' // 允许一切数据,除非 undefined
} }
@ -1310,14 +1310,22 @@ class TICrypto {
* @memberof TICrypto * @memberof TICrypto
*/ */
static regcode2aiid(code) { static regcode2aiid(code) {
if (typeof code === 'string' && /^[a-zA-Z0-9]+$/.test(code)) {
const alphabet = 'e5fcdg3hqa4b1n0pij2rstuv67mwx89klyz' const alphabet = 'e5fcdg3hqa4b1n0pij2rstuv67mwx89klyz'
const base = 16367 const base = 16367
code = code.toLowerCase()
let len = code.length let len = code.length
let num = 0 let num = 0
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
num += alphabet.indexOf(code[i]) * Math.pow(alphabet.length, i) num += alphabet.indexOf(code[i]) * Math.pow(alphabet.length, i)
} }
return num / (base - alphabet.length) - base let aiid = num / (base - alphabet.length) - base
if (aiid >= 0 && Number.isInteger(aiid)) {
// 允许 aiid===0当第一个用户aiid==1登录时需要一个系统默认的邀请码。
return aiid
}
}
return null // null 代表一切非法的regcode
} }
/** /**