Skip to content

Instantly share code, notes, and snippets.

@FlameWolf
FlameWolf / UnicodeStringSplitRegEx.md
Last active November 6, 2023 14:05
Regular expression to split a Unicode string into approximate constituent graphemes
/\p{L}\p{M}?|\S|\s/gu

Usage:

"മാസങ്ങളിൽ മേടം പ്രധാനം".match(/\p{L}\p{M}?|\S|\s/gu); // ["മാ", "സ", "ങ്", "ങ", "ളി", "ൽ", " ", "മേ", "ടം", " ", "പ്", "ര", "ധാ", "നം"]
@FlameWolf
FlameWolf / BigIntegerExtensions.cs
Created October 19, 2023 13:34
BigInteger.ToBase62String
using System;
using System.Collections.Generic;
using System.Numerics;
public static class BigIntegerExtensions
{
private static readonly char[] digits = new char[]
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
@FlameWolf
FlameWolf / getRandomIdString_v3.js
Last active October 26, 2023 05:30
Generate random ID string V3
Object.defineProperty(BigInt.prototype, "toBase62String", {
value: function () {
const base = 62n;
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const sign = this < 0n ? "-" : "";
let [result, quotient] = ["", sign ? 0n - this : this];
do {
result = `${digits[Number(quotient % base)]}${result}`;
} while (quotient /= base);
return `${sign}${result}`;
@FlameWolf
FlameWolf / BigInt.prototype.toBase62String.js
Last active October 26, 2023 05:29
Convert a BigInt to a base-62 number representation where the digits include (in the ascending order of value): 0-9, a-z, and A-Z
Object.defineProperty(BigInt.prototype, "toBase62String", {
value: function () {
const base = 62n;
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const sign = this < 0n ? "-" : "";
let [result, quotient] = ["", sign ? 0n - this : this];
do {
result = `${digits[Number(quotient % base)]}${result}`;
} while (quotient /= base);
return `${sign}${result}`;
@FlameWolf
FlameWolf / BigInt.prototype.toBase64String.js
Last active October 26, 2023 05:28
Convert a BigInt to a base-64 number representation where the digits include (in the ascending order of value): 0-9, a-z, A-Z, &, and #
Object.defineProperty(BigInt.prototype, "toBase64String", {
value: function () {
const base = 64n;
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#";
const sign = this < 0n ? "-" : "";
let [result, quotient] = ["", sign ? 0n - this : this];
do {
result = `${digits[Number(quotient % base)]}${result}`;
} while (quotient /= base);
return `${sign}${result}`;
@FlameWolf
FlameWolf / bigIntToBase64.js
Last active October 26, 2023 05:27
Convert a BigInt to a base-64 number representation where the digits include (in the ascending order of value): 0-9, a-z, A-Z, &, and #
function bigIntToBase64(value) {
const base = 64n;
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#";
const sign = value < 0n ? "-" : "";
let [result, quotient] = ["", sign ? 0n - this : this];
do {
result = `${digits[Number(quotient % base)]}${result}`;
} while (quotient /= base);
return `${sign}${result}`;
}
@FlameWolf
FlameWolf / getRandomIdString_v2.js
Last active October 17, 2023 05:52
Generate random ID string V2
const getRandomIdString = (steps = 4) => {
let seed = "";
for (let i = 0; i < steps; i++) {
seed += Math.random().toString().substring(2);
}
return BigInt(seed).toString(36).toUpperCase();
};
@FlameWolf
FlameWolf / Object.prototype.toString.js
Created August 18, 2023 07:30
Get `Object.prototype.toString()` to return JSON string without circular reference error
Object.defineProperty(Object.prototype, "toString", {
value: function () {
return JSON.stringify(
this,
(function () {
const ancestors = [];
return function (key, value) {
if (typeof value !== "object" || value === null) {
return value;
}
@FlameWolf
FlameWolf / onedrive-response.json
Created July 17, 2023 05:55
OneDrive API Response Format
{
"@content.downloadUrl": "https://public.dm.files.1drv.com/[200+ characters long random string]",
"createdBy": {
"application": {
"displayName": "OneDrive",
"id": "[file_id]"
},
"user": {
"displayName": "[user_display_name]",
"id": "[user_id]"
@FlameWolf
FlameWolf / PressAndRelease.js
Last active May 31, 2023 12:25
Global mouse hold event
(function () {
let isPressed = false;
let pressTimer = false;
const pressedEvent = new CustomEvent("press", {
bubbles: true,
cancelable: true,
composed: true
});
const releasedEvent = new CustomEvent("release", {
bubbles: true,