Skip to content

Instantly share code, notes, and snippets.

View ebeloded's full-sized avatar

Evgenij (Eugene) Beloded ebeloded

View GitHub Profile
@ebeloded
ebeloded / NestedKeysAtPath.ts
Last active November 7, 2024 09:02
NestedKeys & AtPath utility types
type NestedKeys<T> = T extends object
? {
[K in keyof T]-?: K extends string
? T[K] extends infer U
? U extends object
? K | `${K}.${NestedKeys<U>}`
: K
: never
: never;
}[keyof T]
@ebeloded
ebeloded / createPath.ts
Last active June 9, 2023 23:24
createPath
type ProxyType<T> = {
[P in keyof T]: Record<string, ProxyType<T[P]> & Callable> & Callable
}
type Callable = {
(arg?: string): string
}
export function createPath<T extends {}>(_input: T): ProxyType<T> {
const proxy: any = (path: string[]) =>
@ebeloded
ebeloded / dbPath.ts
Created June 6, 2023 07:53
dbPath.ts
type PathConfig = { [key: string]: PathConfig }
type Collection<T extends PathConfig> = ((id?: string) => string) & {
[K in keyof T]: Collection<T[K]> & ((id?: string) => string)
}
function collection<T extends PathConfig>(
basePath: string,
name: string,
subCollections?: T
function getStringId(index) {
const symbols =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
let num = index
let id = ''
do {
id = symbols[num % 62] + id
num = Math.floor(num / 62)
} while (num > 0)
return id
@ebeloded
ebeloded / hash.ts
Created May 22, 2023 07:16
simpleHash
const getN2 =
(maxN: number) =>
(s: string): number => {
let hash = 0
for (let i = 0; i < s.length; i++) {
// Compute a hash by rotating the previous hash and XORing with the current character
hash = (hash << 5) ^ (hash >> 27) ^ s.charCodeAt(i)
}
@ebeloded
ebeloded / NestedPartial.ts
Last active June 12, 2023 12:32
NestedPartal.ts
type Primitive = string | number | boolean | undefined | null
type UnionToIntersection<U> = (
U extends unknown ? (k: U) => void : never
) extends (k: infer I) => void
? I
: never
type AddPrefixToKeys<
Prefix extends string,
@ebeloded
ebeloded / auth.ts
Last active March 31, 2022 06:40
Playwright auth
import esbuild from 'esbuild'
import { firebaseConfig } from '@lumiere/config'
import { initializeApp } from 'firebase-admin/app'
import { getAuth } from 'firebase-admin/auth'
import { expect } from '@playwright/test'
import { test } from './setup'
const app = initializeApp()
const auth = getAuth(app)
const objectify = <T, EXT extends Object>(
f: (v: string) => T,
ext?: EXT,
root?: string
): Record<string, T> & EXT =>
new Proxy(f, {
get: (target, prop) =>
typeof prop === 'string'
? ext?.[prop] || target(prop as string)
: () => root,
@ebeloded
ebeloded / dynamically-load-languages.ts
Created February 17, 2022 21:18
dynamically load languages
Object.entries(import.meta.glob('../locales/*.yaml'))
.map(([path, f]) => [/locales\/(.+?)\.yaml/.exec(path)?.[1], f] as any)
.forEach(([key, f]) => {
console.log({ key, f })
locales[key] = true
if (key !== 'en') register(key, f)
})