Skip to content

Instantly share code, notes, and snippets.

View KEIII's full-sized avatar
😎
show me the code

Ivan Kasenkov KEIII

😎
show me the code
View GitHub Profile
@KEIII
KEIII / crdt-parody.js
Created November 18, 2023 13:28
CRDT parody
const randomUint32 = () => (Math.random() * 2 ** 32) >>> 0;
const merge = (operations) => {
const result = [];
for (const operation of operations) {
switch (operation.kind) {
case "insert": {
if (!result[operation.position]) {
result[operation.position] = operation;
/**
* Generate set of permutations for given length.
*/
function* permute(len) {
if (len <= 0 || len > 10) throw "argument must be in range 0..10";
function* go(n, permutation, members) {
if (n === 0) {
yield permutation;
} else {
// hack
const f1 = (n) => String(n).length;
// log10
const f2 = (n) => Math.floor(Math.log10(n) + 1);
// recursive
const f3 = (n) => {
if (n < 10) return 1;
return 1 + fc(n / 10);
@KEIII
KEIII / openssl.cnf.diff
Created May 13, 2022 15:50 — forked from rdh27785/openssl.cnf.diff
diff -uN /etc/ssl/openssl.cnf\~original /etc/ssl/openssl.cnf for Nextcloud with OpenSSL 3
--- /etc/ssl/openssl.cnf~original 2022-03-16 08:35:51.000000000 +0000
+++ /etc/ssl/openssl.cnf 2022-05-04 02:37:30.336530711 +0000
@@ -56,6 +56,7 @@
# List of providers to load
[provider_sect]
default = default_sect
+legacy = legacy_sect
# The fips section name should match the section name inside the
# included fipsmodule.cnf.
# fips = fips_sect
@KEIII
KEIII / flatten.js
Created October 20, 2021 18:53
Flatten a deep object into a one level object with it’s dot separated path as key
const flatten = object => {
const result = {};
const go = (curr, parents) => {
Object.entries(curr).forEach(([key, value]) => {
const path = [...parents, key];
if (value !== null && typeof value === 'object') {
go(value, path);
} else {
result[path.join('.')] = value;
}
@KEIII
KEIII / qsort.js
Created October 13, 2021 19:11
Quicksort
const swap = (items, first, second) => {
const tmp = items[first];
items[first] = items[second];
items[second] = tmp;
};
const partition = (items, left, right) => {
const pivot = items[Math.trunc((right + left) / 2)];
while (left <= right) {
@KEIII
KEIII / atom.js
Last active August 29, 2021 11:39
Atom
const observers = [];
const call = f => f();
const callAll = x => () => x.forEach(call);
let t;
const notify = () => {
clearTimeout(t);
t = setTimeout(callAll(observers), 0);
@KEIII
KEIII / useFactory.ts
Last active September 7, 2021 19:20
React hook useFactory()
import { useLayoutEffect, useRef, useState } from 'react';
export type Dispose = () => void | undefined;
export type Disposable<T> = {
instance: T;
dispose: Dispose;
};
export type Factory<Arguments extends Tuple, Instance> = (
@KEIII
KEIII / iterators.ts
Last active August 25, 2021 10:58
Iterators
import assert from "assert";
import { Option, isSome, none, some } from '@keiii/k-stream';
const range = (
[a, b]: [number, number],
inclusive = false
): Iterable<number> => {
assert(b >= a, "Invalid range");
const isDone = inclusive ? (x: number) => x > b : (x: number) => x >= b;
return {
/**
* @link https://bugs.chromium.org/p/v8/issues/detail?id=3547
* @link https://github.com/nodejs/node/issues/33306
*/
const oldTimezoneOffsetFix = (d: Date): number => {
const ms = d.getTime();
if (isNaN(ms)) return 0;
const utc = new Date(d.toISOString().slice(0, -1)).getTime();
const offset = d.getTimezoneOffset() * 60_000;
return ms - utc + offset;