You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
1.7 KiB
91 lines
1.7 KiB
7 years ago
|
/** Make a node */
|
||
7 years ago
|
function mk (e) {
|
||
7 years ago
|
return document.createElement(e)
|
||
|
}
|
||
7 years ago
|
|
||
|
/** Find one by query */
|
||
7 years ago
|
function qs (s) {
|
||
7 years ago
|
return document.querySelector(s)
|
||
|
}
|
||
7 years ago
|
|
||
|
/** Find all by query */
|
||
7 years ago
|
function qsa (s) {
|
||
7 years ago
|
return document.querySelectorAll(s)
|
||
|
}
|
||
7 years ago
|
|
||
|
/** Convert any to bool safely */
|
||
7 years ago
|
function bool (x) {
|
||
|
return (x === 1 || x === '1' || x === true || x === 'true')
|
||
7 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Filter 'spacebar' and 'return' from keypress handler,
|
||
|
* and when they're pressed, fire the callback.
|
||
|
* use $(...).on('keypress', cr(handler))
|
||
|
*/
|
||
7 years ago
|
function cr (hdl) {
|
||
7 years ago
|
return function (e) {
|
||
7 years ago
|
if (e.which === 10 || e.which === 13 || e.which === 32) {
|
||
7 years ago
|
hdl()
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
/** HTML escape */
|
||
7 years ago
|
function esc (str) {
|
||
7 years ago
|
return $.htmlEscape(str)
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Check for undefined */
|
||
7 years ago
|
function undef (x) {
|
||
|
return typeof x == 'undefined'
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Safe json parse */
|
||
7 years ago
|
function jsp (str) {
|
||
7 years ago
|
try {
|
||
7 years ago
|
return JSON.parse(str)
|
||
7 years ago
|
} catch (e) {
|
||
7 years ago
|
console.error(e)
|
||
|
return null
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Create a character from ASCII code */
|
||
7 years ago
|
function Chr (n) {
|
||
|
return String.fromCharCode(n)
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Decode number from 2B encoding */
|
||
7 years ago
|
function parse2B (s, i = 0) {
|
||
|
return (s.charCodeAt(i++) - 1) + (s.charCodeAt(i) - 1) * 127
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Decode number from 3B encoding */
|
||
7 years ago
|
function parse3B (s, i = 0) {
|
||
|
return (s.charCodeAt(i) - 1) + (s.charCodeAt(i + 1) - 1) * 127 + (s.charCodeAt(i + 2) - 1) * 127 * 127
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Encode using 2B encoding, returns string. */
|
||
7 years ago
|
function encode2B (n) {
|
||
|
let lsb, msb
|
||
|
lsb = (n % 127)
|
||
|
n = ((n - lsb) / 127)
|
||
|
lsb += 1
|
||
|
msb = (n + 1)
|
||
|
return Chr(lsb) + Chr(msb)
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Encode using 3B encoding, returns string. */
|
||
7 years ago
|
function encode3B (n) {
|
||
|
let lsb, msb, xsb
|
||
|
lsb = (n % 127)
|
||
|
n = (n - lsb) / 127
|
||
|
lsb += 1
|
||
|
msb = (n % 127)
|
||
|
n = (n - msb) / 127
|
||
|
msb += 1
|
||
|
xsb = (n + 1)
|
||
|
return Chr(lsb) + Chr(msb) + Chr(xsb)
|
||
7 years ago
|
}
|