Skip to content

Instantly share code, notes, and snippets.

@alenaksu
alenaksu / vitePluginSvgSpritesheet.ts
Created December 14, 2024 08:50
Vite SVG Spritesheet Plugin
const vitePluginSvgSpritesheet = ({
path,
output,
}: {
path: string | string[];
output: string;
}): Plugin => {
let spritesheet: string;
return {
@alenaksu
alenaksu / xorShift32.ts
Last active November 14, 2024 17:15
PRNG XORShift32
function *XORShift32(seed: number | bigint): Generator<Number> {
const base = 2 ** 32;
seed = BigInt(seed);
while(true) {
let x = seed as bigint;
x = BigInt.asUintN(32, x);
x ^= x << 13n;
x ^= x >> 17n;
x ^= x << 5n;
@alenaksu
alenaksu / example.ts
Last active January 16, 2024 10:42
Async Generator Queue
import { createQueue } from './queue.js';
const fakeAsyncCall = (i: number) =>
new Promise<void>((resolve) =>
setTimeout(() => {
console.log(i);
resolve();
}, 1000)
);
@alenaksu
alenaksu / classNames.ts
Last active June 23, 2022 13:09
classNames.ts
export const classNames = (
...classes: Array<string | string[] | Record<string]: boolean }>
): string | undefined => classes
.map(cls => cls?.constructor.name === 'Object' ? Object.keys(cls).filter(key => cls[key]) : cls)
.flat()
.filter(Boolean)
.join(' ') || undefined;
@alenaksu
alenaksu / LZW.js
Last active December 17, 2024 09:31
LZW Implementation
/**
Namespace for LZW compression and decompression.
Methods:
LZW.compress(uncompressed)
LZW.decompress(compressed)
*/
function createDictionary({ size = 256, inverse = false } = {}) {
const dictionary = new Map();
@alenaksu
alenaksu / Document.elementsFromPoint.js
Last active April 14, 2021 09:17
Document.elementsFromPoint polyfill
if (!document.elementsFromPoint) {
document.elementsFromPoint =
document.msElementsFromPoint ||
function elementsFromPoint(x, y) {
var stack = [];
var element = document.elementFromPoint(x, y);
try {
while (element !== null) {
stack.push({
/**
* Generate a random UUIDv4 (rfc4122 compliant)
*/
export function uuid(): string {
const uuid = [8, 4, 4, 4, 12].map((segmentLength: number) => {
let segment = Array(segmentLength);
for (let i = 0; i < segmentLength; i++)
// ToUint32 http://www.ecma-international.org/ecma-262/5.1/#sec-11.7.3
segment[i] = (Math.random() * 0xf) >>> 0;
{
"trailingComma": "none",
"tabWidth": 4,
"semi": true,
"singleQuote": true,
"printWidth": 120
}
@alenaksu
alenaksu / .editorconfig
Created February 25, 2019 15:34
Code formatting settings
# editorconfig.org
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
@alenaksu
alenaksu / fibonacci.ts
Last active November 8, 2024 11:24
Fibonacci number generator using the Binet's formula
const fibonacci = (n: number): number => {
const delta = Math.sqrt(5);
return Math.floor(((1 + delta) ** n - (1 - delta) ** n) / (2 ** n * delta));
};