Skip to content

Instantly share code, notes, and snippets.

@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 / 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.js
Last active February 20, 2019 12:35
Fibonacci number generator using the Binet's formula
function fibonacci(n) {
const delta = Math.sqrt(5);
return (
(Math.pow(1 + delta, n) - Math.pow(1 - delta, n)) / (Math.pow(2, n) * delta)
);
}
@alenaksu
alenaksu / config.json
Last active January 18, 2019 17:12
Node.js Telnet Chat Server
{
"server": {
"maxConnections": 100,
"host": "0.0.0.0",
"port": 10000
},
"chat": {
"messageLength": 150,
"messages": {
"welcome": "Welcome to chat server. Press CTRL+C to leave.",
@alenaksu
alenaksu / Array.prototype.flat.js
Last active January 14, 2019 12:37
Array.flat polyfill
export const flat = arr => {
// Create a copy of the original array
const result = [...arr];
for (let i = 0; i < result.length; i++) {
while (Array.isArray(result[i])) {
// insert in the current position a copy of the original item
result.splice(i, 1, ...result[i]);
}
}
@alenaksu
alenaksu / polyline.js
Last active February 11, 2019 17:36
Encoded Polyline Algorithm Format
const encodeNumber = (n, precision) => {
// Multiple number by 10^precision to round the result
n = Math.round(n * +`1e${precision}`);
/**
* Number is already in two's complement so just shift left
* by 1-bit and negate if and only if number is negative
*/
n = n < 0 ? ~(n << 1) : n << 1;