Skip to content

Instantly share code, notes, and snippets.

@bigmistqke
bigmistqke / ts-helpers.ts
Last active February 15, 2023 18:55
Typescript Helpers
/**
* FilterArray
* FilterArray filters the values of an array (generic 1) according to a type (generic 2)
*/
export type FilterArray<T extends readonly any[], V> = T extends readonly [infer L, ...infer R]
? L extends V
? [L, ...FilterArray<R, V>]
: [...FilterArray<R, V>]
: []
@bigmistqke
bigmistqke / createSharedArrayBuffer.ts
Last active March 19, 2024 22:59
createSharedArrayBuffer
import { createSignal } from "solid-js";
const signal = (buffer: Int32Array, index: number) => {
const s = createSignal(undefined, { equals: false });
const read = (debug?: string) => {
s[0]();
return buffer[index];
};
const write = (value: any) => {
@bigmistqke
bigmistqke / solid-three.d.ts
Last active July 17, 2023 15:43
solid-three-bundled
// Generated by dts-bundle v0.7.3
// Dependencies for this module:
// ../solid-js/jsx-runtime
// ../three
// ../utility-types
// ../solid-js
// ../solid-js/store
declare module 'solid-three' {
import * as ReactThreeFiber from 'solid-three/three-types';
@bigmistqke
bigmistqke / cursor.ts
Last active July 21, 2023 09:02
cursor.ts
import { Vector } from '@ndbx/types';
import { MouseEvent as MouseEventReact } from 'react';
/**
* cursor
*
* @param e MouseEvent
* @param callback called every onMouseMove
* @returns Promise resolved onMouseUp
*/
@bigmistqke
bigmistqke / when.ts
Last active July 30, 2023 21:44
when.ts
import { Accessor } from 'solid-js'
export function when<
TAccessors extends Array<Accessor<any>>,
const TValues extends { [TKey in keyof TAccessors]: Exclude<ReturnType<TAccessors[TKey]>, null | undefined | false> }
>(...accessors: TAccessors) {
function callback<const TResult>(): TValues | undefined
function callback<const TResult>(callback: (...values: TValues) => TResult): TResult | undefined
function callback<const TResult>(callback?: (...values: TValues) => TResult) {
const values = new Array(accessors.length)
@bigmistqke
bigmistqke / valibot-to-ts.ts
Created November 2, 2023 19:24
valibot-to-ts
const genPadding = (layer: number) =>
new Array(layer).fill(" ").flat().join("");
function genObject({object}: any, layer: number) {
const entries = Object.entries(object);
let result = "{\n";
const padding = genPadding(layer);
for (const [key, value] of entries) {
result += padding;
result += `${key}: ${valibotToTs(value, layer + 1)},\n`;
@bigmistqke
bigmistqke / module.ts
Last active November 5, 2023 03:17
dynamic esm modules with tag template literal
import {
onCleanup,
createMemo,
createSignal,
type Accessor,
createEffect,
createResource,
} from "solid-js";
function modifyImportPaths(code) {
@bigmistqke
bigmistqke / ts-esm-module.ts
Last active February 12, 2024 14:05
dynamic esm module tag template literal with typescript-support
import ts from 'typescript'
type Accessor<T> = () => T
const tsModules: Record<string, { content: string; version: number }> = {}
function modifyImportPaths(code: string) {
return code.replace(/import ([^"']+) from ["']([^"']+)["']/g, (match, varName, path) => {
if (path.startsWith('blob:') || path.startsWith('http:') || path.startsWith('https:') || path.startsWith('.')) {
return `import ${varName} from "${path}"`
} else {
@bigmistqke
bigmistqke / ArrayInPlace.ts
Last active December 11, 2023 09:56
experiments with mutable array-methods
class ArrayInPlace<T = number> extends Array {
commands: Command<T>[] = [];
constructor(values: T[]) {
super();
for (let i = 0; i < Math.floor(values.length / 100_000); i++) {
Array.prototype.push.apply(
this,
values.slice(i * 100_000, (i + 1) * 100_000),
);
}
@bigmistqke
bigmistqke / ArrayInPlace.ts
Created December 11, 2023 09:53
ArrayInPlace
/**
* array extended with mutable/in-place array-methods
*/
class ArrayInPlace<T = number> extends Array {
commands: Command<T>[] = [];
constructor(values: T[]) {
super();
Array.prototype.push.apply(this, values);
}