Skip to content

Instantly share code, notes, and snippets.

@nkint
nkint / types.ts
Created November 10, 2022 10:39
Typescript refine string array
// fail silently
type Age = "6" | "12" | "24" | "54" | "72" | "84"
type AgeOld = Extract<Age, "72" | "84" | "62">
// fail kabum
type Age = "6" | "12" | "24" | "54" | "72" | "84"
type ExtractOrFail<T, U extends T> = U
type AgeOld = ExtractOrFail<Age, "72" | "84" | "62">
@nkint
nkint / gist:077b6deccdf61351f016dee5b83a2021
Created October 28, 2022 08:34
map fit mapLinear implementations
umbrella:
https://github.com/thi-ng/umbrella/blob/develop/packages/math/src/fit.ts
arduino:
https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/WMath.cpp#L52
p5:
https://github.com/processing/p5.js/blob/main/src/math/calculation.js#L448
openframework:
@nkint
nkint / isTruncated.tsx
Created May 4, 2021 13:48
react hook is truncated
import { useLayoutEffect, useState } from 'react';
/**
* Determine if the input DOM element is truncated by CSS (using ellipse for example)
* @param domElement
* @returns boolean
*/
export function isTruncated(domElement: Element): boolean {
// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth
return domElement.scrollWidth > domElement.clientWidth;
@nkint
nkint / index.ts
Created April 2, 2021 15:59
Map Index Values in typescript
export function mapIndexValues<Key extends string | number | symbol, ValueInput, ValueOutput>(
input: Record<Key, ValueInput>,
iteratee: (key: Key, val: ValueInput, index: number) => ValueOutput
): Record<Key, ValueOutput> {
const entries = Object.entries(input).map(([key, value], index) => [
key,
iteratee(key as Key, value as ValueInput, index)
]);
return Object.fromEntries(entries);
@nkint
nkint / gist:1120843ad5292ac4fb4887c97b606332
Created July 23, 2020 15:08
Dayjs group by time-range
var dayjs = require('dayjs')
var minMax = require('dayjs/plugin/minMax')
dayjs.extend(minMax)
const input = [
{time: '2020-07-23T11:30:00Z', value: 1},
{time: '2020-07-24T11:30:00Z', value: 1},
{time: '2020-07-05T11:30:00Z', value: 2},
{time: '2020-07-30T11:30:00Z', value: 3},
@nkint
nkint / index.ts
Last active July 26, 2020 13:20
umbrella webgl-to-pixel
/*
yarn add @thi.ng/hdom @thi.ng/pixel @thi.ng/hdom-canvas @thi.ng/geom @thi.ng/transducers @thi.ng/hdom-components @thi.ng/webgl @thi.ng/memoize @thi.ng/shader-ast @thi.ng/shader-ast-stdlib webgl-shadertoy
*/
import { renderOnce } from "@thi.ng/hdom";
import { PackedBuffer, canvas2d, ABGR8888, GRAY8 } from "@thi.ng/pixel";
import { canvas as hdomCanvas } from "@thi.ng/hdom-canvas";
import * as g from "@thi.ng/geom";
import { range, map, normRange } from "@thi.ng/transducers";
import { glCanvas } from "@thi.ng/webgl";
@nkint
nkint / fitIntoBounds3.ts
Created February 13, 2020 18:04
fitIntoBounds3
// 3d version of the 2d fitIntoBounds2
// https://github.com/thi-ng/umbrella/blob/feaf8d3f8c1dd3e9141a151b4e473423a6f62242/packages/geom/src/ops/fit-into-bounds.ts#L34
import { Vec, MAX3, MIN3, ReadonlyVec, neg, div4, div3, abs3, sub3, dist3 } from '@thi.ng/vectors';
import { concat, translation44, ReadonlyMat, scale44, mulV344 } from '@thi.ng/matrices';
import { bounds, centroid } from '@thi.ng/geom-poly-utils';
import { isArray, isNull } from '@thi.ng/checks';
/**
@nkint
nkint / gist:3564dcf246b09c2dfbe4e9d8af9a4713
Created January 31, 2020 18:16
Umbrella, wireframe lines from Vec[]
import {
comp,
juxtR,
map,
mapcat,
partition,
reducer,
transduce,
wrapSides,
} from '@thi.ng/transducers'
import { Type, typedArray } from '@thi.ng/api'
import { MemPool } from '@thi.ng/malloc'
import { Vec } from '@thi.ng/vectors'
import { computeNewData } from './buffers'
const { numSliceRows, numSliceColumns, resolution } = globalState.deref()
const poolSize = numSliceRows * numSliceColumns * (resolution + 3)
const POOL = new MemPool({ size: poolSize })
console.log('poolSize of', { poolSize })
@nkint
nkint / pil-on-the-fly.py
Last active May 7, 2021 09:54
Flask return numpy array on the fly
#!/usr/bin/env python3
from io import BytesIO
import numpy as np
import numpy.linalg as linalg
import scipy.interpolate as interpolate
from PIL import Image
from flask import Flask, send_file, escape, request
app = Flask(__name__)