Skip to content

Instantly share code, notes, and snippets.

@bigmistqke
bigmistqke / process-props.tsx
Last active June 3, 2024 17:22
`processProps` and `defaultProps` utilities for solid-js
import { MergeProps, mergeProps, splitProps } from 'solid-js'
type KeyOfOptionals<T> = keyof {
[K in keyof T as T extends Record<K, T[K]> ? never : K]: T[K]
}
export function processProps<
TProps extends Record<string, any>,
TKey extends KeyOfOptionals<TProps>,
TSplit extends (keyof TProps)[],
@bigmistqke
bigmistqke / conditionals.ts
Last active May 17, 2024 11:23
utilities for conditionals in solid-js
import { Accessor } from 'solid-js'
/**
* Executes a callback with a value derived from an accessor if the value is truthy.
*
* @param accessor The value or function returning a value that is checked for truthiness.
* @param callback The callback function to be executed if the accessor's value is truthy.
* @returns The result of the callback if executed, otherwise undefined.
*/
export function when<
@bigmistqke
bigmistqke / type-registry.ts
Created March 28, 2024 17:31
Monaco Auto Import Type Registry
import { Monaco } from '@monaco-editor/loader'
const regex = {
import:
/import\s+(?:type\s+)?(?:\{[^}]*\}|\* as [^\s]+|\w+\s*,\s*\{[^}]*\}|\w+)?\s+from\s*"(.+?)";?/gs,
export:
/export\s+(?:\{[^}]*\}|\* as [^\s]+|\*|\w+(?:,\s*\{[^}]*\})?|type \{[^}]*\})?\s+from\s*"(.+?)";?/gs,
require: /require\s*\(["']([^"']+)["']\)/g,
}
export class TypeRegistry {
@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);
}
@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 / 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 / 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 / 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 / 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 / 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
*/