Skip to content

Instantly share code, notes, and snippets.

@lac5
lac5 / zid.js
Last active June 17, 2024 15:06
export default function index(...n) {
const base = 26;
const base0 = base * 10;
const aCode = 'a'.charCodeAt(0);
const z = String.fromCharCode(base - 1 + aCode);
return n.map(function(n) {
let ret = String.fromCharCode(Math.floor(n / 10) % base + aCode) + String(Math.floor(n % 10));
while (n >= base0) {
ret = z + ret;
n -= base0;
export default function partialDeepEquals(a, b) {
if (!a || typeof a !== 'object') {
if (b instanceof RegExp) {
return b.test(a);
}
return a == b;
}
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false;
function midSuffle(a) {
var length = a.length;
var mid = Math.floor(length / 2);
var b = [a[mid]];
b.length = length;
for (var i = 1; i < length; i++) {
if (i % 2 === 0) {
b[i] = a[mid + i / 2];
} else {
b[i] = a[mid - ((i + 1) / 2)];
let seen = new WeakMap();
function safeJson(key, value) {
if (key.startsWith('_') || key.startsWith('$')) return;
if (typeof value === 'function') return;
if (typeof value === 'bigint' || typeof value === 'symbol') {
return String(value);
}
function* genBirthdayChances(length) {
for (let i = 0; i < length; i++) {
yield [ 1, 1, 0.0019126241045620209 ];
yield [ 1, 2, 0.0022844959626743748 ];
yield [ 1, 3, 0.0026541586810355663 ];
yield [ 1, 4, 0.002704723435339952 ];
yield [ 1, 5, 0.0026885230771647606 ];
yield [ 1, 6, 0.0026782137583260023 ];
yield [ 1, 7, 0.0026816501979389216 ];
yield [ 1, 8, 0.0026043303066482343 ];
export default class StyleMap extends Map {
toString(space, newline) {
let style = '';
for (let [key, value] of this) {
style += key;
style += ':';
if (typeof space === 'number') {
for (let i = 0; i < space; i++) {
style += ' ';
}
@lac5
lac5 / base64-convert.js
Created October 10, 2019 18:21
Conversion methods between Base64-URL and standard Base64.
export function base64url(base64) {
return String(base64).replace(/[^A-Za-z0-9_-]/g, m =>
m === '+' ? '-' :
m === '/' ? '_' :
''
);
}
export function base64standard(base64) {
base64 = String(base64).replace(/[^A-Za-z0-9+/]/g, m =>
/**
* @param {boolean} [noDashes=false] Set to true to remove hyphens in output.
* @returns {string} The output UUID string.
*/
export default function uuidGen(noDashes) {
return [
('00000000' + Math.floor(Math.random() * 0x100000000).toString(16)).slice(-8),
('0000' + Math.floor(Math.random() * 0x10000).toString(16)).slice(-4),
('0000' + Math.floor(Math.random() * 0x10000).toString(16)).slice(-4),
('0000' + Math.floor(Math.random() * 0x10000).toString(16)).slice(-4),
/**
* Promise.any is the inverse of Promise.all
*
* Promise.all = reject first error or resolve all successes
* Promise.any = resolve first success or reject all errors
*/
Promise.any = function(promises) {
return Promise.all(promises.map(p => {
return p.then(
resolve => { throw resolve; },
@lac5
lac5 / twitter-show-id.meta.js
Last active June 17, 2024 15:04
Gets the ID of the user and puts it in the URL.
// ==UserScript==
// @name Show User-ID on Twitter
// @namespace larryc5
// @version 1.0
// @description Gets the ID of the user and puts it in the URL.
// @author Larry Costigan
// @include /^https?:\/\/(?:[^\/?#.]+\.)*twitter\.com\/[^\/?#]+(?:[\?#].*)?$/
// @require https://code.jquery.com/jquery.min.js
// @grant none
// @downloadURL https://gist.githubusercontent.com/larryc5/a1b7271f2ba8ef0cf2d26a6577167ed8/raw/twitter-show-id.user.js