Skip to content

Instantly share code, notes, and snippets.

/**
* Assign to `target` all values from `default`,
* if value under corresponding key in `target` is undefined or null.
*
* ---
*
* <b>Example</b>
*
* With given `target` type:
*
@jeron-diovis
jeron-diovis / useSet.ts
Created July 10, 2024 10:05
`useSet` hook
import { useCallback, useMemo } from 'react'
import { useImmer } from 'use-immer'
type FnOrValue<T> = T | (() => T)
type InitialValue<T> = Set<T> | T[]
interface SetMethods<T> {
add: (value: T) => void
delete: (value: T) => void
rules:
no-console:
- warn
- allow: [warn, error]
no-unreachable: warn
no-duplicate-imports: warn
no-var: error
prefer-const: warn
@jeron-diovis
jeron-diovis / install.sh
Last active June 20, 2024 11:43
stylelint
#!/bin/sh
npm install -D stylelint \
stylelint-config-css-modules \
stylelint-config-standard-scss
npm pkg set scripts."lint:css"="stylelint \"src/**/*.{s,}css\""
@jeron-diovis
jeron-diovis / isIntersects.ts
Last active January 4, 2024 11:38
isIntersects
const id = <T>(x: T): T => x
type EQ<A = unknown, B = A> = (a: A, b: B) => boolean
const eq: EQ = (a, b) => a === b
type Opts<A = unknown, B = A, C = unknown> = {
by: (x: A | B) => C
with: EQ<C>
}
@jeron-diovis
jeron-diovis / promised.ts
Created February 24, 2023 00:42
Reduce a list, returning sync value if everything is sync, and promise is there are any promises
type Promised<T = unknown> = T | Promise<T>
type PromisedBy<T, R> = T extends Promise<unknown> ? Promise<R> : R
type Wait = <R, V>(x: Promised<V>, cb: (x: V) => R) => PromisedBy<typeof x, R>
type ReduceAsync = <T, R>(
fn: (memo: R, x: T) => Promised<R>,
xs: Promised<T>[],
seed: Promised<R>
) => Promised<R>
function use(promise) {
if (promise.status === 'fulfilled') {
return promise.value;
} else if (promise.status === 'rejected') {
throw promise.reason;
} else if (promise.status === 'pending') {
throw promise;
} else {
promise.status = 'pending';
promise.then(
@jeron-diovis
jeron-diovis / run-module.sh
Created October 19, 2022 15:59
Call a method in JS / TS module directly from CLI
#!/bin/sh
DIR=.
VERBOSE=false
LOG_TRANSFORM=''
while getopts "d:vj" optname
do
case "$optname" in
"d") DIR=${OPTARG} ;;
"v") VERBOSE=true ;;
@jeron-diovis
jeron-diovis / janus-gateway.d.ts
Created September 22, 2021 10:41
janus-gateway.d.ts
/**
* Borrowed from {@link https://github.com/mika-go-13/janus-typescript-client/blob/c18b41ccdcb2e948829844ef0e1e2302d50accc8/index.d.ts}
*/
/* eslint-disable @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any */
declare module 'janus-gateway' {
declare namespace JanusJS {
interface Dependencies {
adapter: any
import React from 'react'
import { useOnMount } from './effects'
// ---
export function useConst<T>(value: T): T {
return React.useRef(value).current
}