Skip to content

Instantly share code, notes, and snippets.

View FrameMuse's full-sized avatar
:shipit:
Creating

Valery Zinchenko FrameMuse

:shipit:
Creating
View GitHub Profile
@FrameMuse
FrameMuse / EventEmitter.ts
Created May 2, 2024 08:53
EventEmitter - Super light weight, Simple and Powerful
type EventEmitterListener = (...args: never[]) => void
class EventEmitter<Events extends Record<EventName, EventEmitterListener>, EventName extends keyof Events = keyof Events> {
private callbacks: Partial<Record<keyof never, Set<EventEmitterListener>>> = {}
public on<Event extends keyof Events>(event: Event, callback: Events[Event]) {
this.callbacks[event] ??= new Set
this.callbacks[event]?.add(callback as EventEmitterListener)
}
public off<Event extends keyof Events>(event: Event, callback: Events[Event]) {
@FrameMuse
FrameMuse / ProcessMarkdown.ts
Created April 27, 2023 14:47
Tiny React markdown converter | Converts directly to React elements, so it's completle safe.
import type { marked } from "marked"
import { Lexer, Renderer } from "marked"
import { createElement, Key, ReactNode, useState } from "react"
// You can use your own external link component.
const ExternalLink = "a"
/**
*
* Tries to create `ReactElement`, if text is not `marked`, return as it is.
*
{"name":"ES React","shortName":"ER","settings":"{\"settings\":\"{\\r\\n \\\"explorer.confirmDragAndDrop\\\": false,\\r\\n \\\"css.lint.emptyRules\\\": \\\"ignore\\\",\\r\\n \\\"editor.fontLigatures\\\": true,\\r\\n \\\"terminal.integrated.fontFamily\\\": \\\"Roboto Mono, JetBrains Mono\\\",\\r\\n \\\"explorer.confirmDelete\\\": false,\\r\\n \\\"html.format.indentInnerHtml\\\": true,\\r\\n \\\"git.enableSmartCommit\\\": true,\\r\\n \\\"editor.fontSize\\\": 13,\\r\\n \\\"prettier.eslintIntegration\\\": true,\\r\\n\\t\\\"editor.formatOnSave\\\": true,\\r\\n \\\"vsicons.dontShowNewVersionMessage\\\": true,\\r\\n \\\"editor.tabSize\\\": 2,\\r\\n \\\"editor.accessibilitySupport\\\": \\\"off\\\",\\r\\n \\\"javascript.updateImportsOnFileMove.enabled\\\": \\\"always\\\",\\r\\n \\\"typescript.updateImportsOnFileMove.enabled\\\": \\\"always\\\",\\r\\n \\\"search.useGlobalIgnoreFiles\\\": true,\\r\\n \\\"workbench.editor.highlightModifiedTabs\\\": true,\\r\\n \\\"workben
@FrameMuse
FrameMuse / BiMap.ts
Created December 1, 2022 09:38
Simple bidirectional (two ways) map class
/**
* This class represent keys and values mapping (swapping).
*
* ### Research
* - https://www.google.com/search?q=bidirectional+map+js&oq=bidirectional+map+js&aqs=chrome..69i57.2532j0j7&sourceid=chrome&ie=UTF-8
* - https://www.google.com/search?q=bilateral+mapping+npm
* - https://startfunction.com/2020/11/26/bidirectional-map-javascript/#initialize
* - https://startfunction.com/bidirectional-map-javascript/
* - https://www.npmjs.com/package/bi-directional-map
*/
@FrameMuse
FrameMuse / FromSnakeToCamelCase.ts
Last active October 2, 2022 09:53
Trasnfoms string literal snake case to camel
type SnakeToCamelCase<S extends string> = S extends `${infer Start}_${infer Rest}` ? `${Start}${Capitalize<SnakeToCamelCase<Rest>>}` : S
type SnakeToCamelCase__TEST__ = SnakeToCamelCase<"my_account_profile"> // myAccountProfile
@FrameMuse
FrameMuse / Aswagger-schema-reducer.md
Last active October 19, 2022 10:27
OpenAPI Swagger Schema TypeScript Mapping, Parser, Reducer

OpenAPI Swagger Schema TypeScript Mapping, Parser

Reduces (or parses or maps) OpenAPI Swagger Schema to interface (or type) saving links to origin.

Motivation

I am a laziest person ever, I never wanted to write a bit of boilerplate code. I dreamed of having a parser of Swagger schema, so I just download it once and I have all actions (or path or endpoints) in one place. In the beginning, I wrote all endpoints manually, then I wrote this parser to parse to Actions.ts and Schemas.ts files, that was enough for that time.

@FrameMuse
FrameMuse / interpolation.ts
Created April 5, 2022 12:18
Just a plain interpolation utility but with extracted interpolations to have a look of what variables you can interpolate into
type ExtractInterpolations<T extends string> = T extends `${infer _Start}{${infer V}}${infer Rest}` ? V | ExtractInterpolations<Rest> : never
/**
* Interpolates {variable} in string
*/
function interpolate<T extends string>(value: T, vars: Record<ExtractInterpolations<T>, string | number>): string {
const varKeys = Object.keys(vars) as ExtractInterpolations<T>[]
return varKeys.reduce((result: string, next) => result.replace(new RegExp(`{${next}}`, "g"), String(vars[next])), value)
}