Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// generated by LWYS-JS-Combiner v0.0.2-private1
// -------------- description (manaully inserted)
// A BVID encoder/decoder written in JavaScript.
// Based on a Python script: https://www.zhihu.com/question/381784377/answer/1099438784
// (semi)written by Dobby233Liu
// WTFPL
// -------------- imports
// js/comp/pythonic.js
var pythonic = {};
pythonic.range = function range(start, stop, step) { // https://stackoverflow.com/a/8273091
if (typeof stop == 'undefined') {
// one param defined
stop = start;
start = 0;
}
if (typeof step == 'undefined') {
step = 1;
}
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
return [];
}
var result = [];
for (var i = start; step > 0 ? i < stop : i > stop; i += step) {
result.push(i);
}
return result;
}
pythonic.list = (tup) => tup.split('');
pythonic.print = (txt) => console.log(txt);
pythonic.specialSplit = (txt) => {
var ret = {};
for (i in pythonic.range(txt.length)){
ret[txt[i]] = i;
}
return ret;
}
// imported code end
// js/stringcode/constants/base58-bilibili.js
var table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF', tr = pythonic.specialSplit(table), s = [11, 10, 3, 8, 4, 6], xor = 177451812, add = 8728348608;
// imported code end
// js/stringcode/encdec/base58-bilibili.js
var bv = {};
bv.decode = function dec(x){
var r = 0;
for (i in pythonic.range(6)) {
r += tr[x[s[i]]] * (58 ** i);
}
return (r - add) ^ xor;
}
bv.encode = function enc(x){
x = (x ^ xor) + add;
var r = pythonic.list('BV1 4 1 7 ');
for (i in pythonic.range(6)) r[s[i]] = table[Math.floor(x / 58 ** i) % 58];
return r.join('');
}
// imported code end
// js/examples/stringcode/encdec/base58-bilibili/1.js
function bvCoderTest(bv, print){
print(bv.decode('BV17x411w7KC'));
print(bv.decode('BV1Q541167Qg'));
print(bv.decode('BV1mK4y1C7Bz'));
print(bv.encode(170001));
print(bv.encode(455017605));
print(bv.encode(882584971));
}
bvCoderTest(bv, pythonic.print);
// imported code end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment