rc-plugin/utils/bilibili-bv-av-convert.js
RrOrange 1cd916d48f feat: 更改哔哩哔哩解析逻辑 & 🎈 perf: 提升论文解析速度
1. 增加bv av互转
2. 删除代码中的av逻辑,出现av一律转换为bv
3. 论文解析从race转换为any
2023-03-27 17:41:29 +08:00

30 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF'
const tr = Array.from(table).reduce((o, c, i) => Object.assign(o, { [c]: i }), {})
const s = [11, 10, 3, 8, 4, 6]
const xor = 177451812
const add = 8728348608
/** 算法来源https://www.zhihu.com/question/381784377/answer/1099438784 **/
/**
* Convert a BV string to AV number
* @param {string} bv The BV string to be converted to AV number
* @returns {number} The AV number converted from the provided BV string
*/
function bv2AV(bv) {
return (new Uint8Array(6).reduce((r, _, i) => r + tr[bv[s[i]]] * 58 ** i, 0) - add) ^ xor
}
/**
* Convert a AV number to BV string
* @param {number} av The AV number to be converted to BV string
* @returns {string} The BV string converted from the provided AV number
*/
function av2BV(av) {
return (new Uint8Array(6).reduce((r, _, i) => { r.splice(s[i], 1, table[Math.floor(((av ^ xor) + add) / 58 ** i % 58)]); return r }, Array.from('BV1 4 1 7 '))).join('')
}
export {
bv2AV,
av2BV
}