Skip to content

Instantly share code, notes, and snippets.

View VladSez's full-sized avatar

Vlad Sazonau VladSez

View GitHub Profile
// split by ": " or "; "
const splitted = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green".split(/: |; /g);
// res: ['Game 1', '3 blue, 4 red', '1 red, 2 green, 6 blue', '2 green']
@VladSez
VladSez / greedy-regex.js
Created December 4, 2023 01:54
greedy js reg ex
// https://stackoverflow.com/questions/49744804/how-to-match-an-overlapping-pattern-multiple-times-using-regexp
// the problem is that by default regexp cant do overlapping patterns
// for example we want: "eighthree" -> ['eight', 'three'] and for "sevenine" -< ['seven', 'nine']
// but with default .match we get "eighthree" -> ['eight'] and for "sevenine" -> ['seven']
const greedyRegex = (str: string) => {
let reg = /one|two|three|four|five|six|seven|eight|nine|\d/g,
next = reg.exec(str),
res = [];
@VladSez
VladSez / recursive.js
Created February 17, 2023 15:20
Recursively render form in React. Demo: https://codesandbox.io/s/hackathon-jyswt0
// DEMO: https://codesandbox.io/s/hackathon-jyswt0
const FEATURES_LP_PD = {
Skills: {
'Show company data': 'cms.pages.skills.content.list.item.showCompanyData',
'Search whole company': 'cms.pages.skills.skillsShare.isSearchingWholeCompany',
'Knowledge Exchange': 'cms.pages.skills.hasSharedSkillsFeature',
'Replace default logo': 'cms.pages.skills.buttonOptions.replaceDefaultLogo',
'Logo change on hover': 'cms.pages.skills.buttonOptions.shouldLogoChangeOnHover',
},
@VladSez
VladSez / allPaths.ts
Last active February 13, 2023 14:08
AllPaths
// https://mobile.twitter.com/AndaristRake/status/1625130160277028864
// https://twitter.com/GabrielVergnaud/status/1623814585445675008?s=20&t=iG8sUzPLNZNgog-tAttHiw
type AllPaths<T> =
// if `T` is an object with string keys
T extends Record<string, unknown>
// Assign the union `keyof T` to a variable `K`
? keyof T extends infer K
// We distribute K.
// For each member in `K`:
@VladSez
VladSez / gist:44ebe3624c072de881becd7dd408dcf5
Last active December 23, 2022 01:00
Ts quirk on “object”
https://github.com/typescript-eslint/typescript-eslint/issues/5947
Don't use `{}` as a type. `{}` actually means "any non-nullish value".
- If you want a type meaning "any object", you probably want `object` instead.
- If you want a type meaning "any value", you probably want `unknown` instead.
- If you want a type meaning "empty object", you probably want `Record<string, never>` instead.
@VladSez
VladSez / pathToWriteTo.ts
Last active December 19, 2022 20:08
Write to the specific key of the object -> `pathToWriteTo(['/', 'a'], 'myData')` -> `{'/': {'a': 'myData'}}`
// Write to the specific key of the object
// pathToWriteTo(['/', 'a'], 'myData')
// will write to -> {'/': {'a': 'myData'}}
function pathToWriteTo(
pathArr: string[],
input: { [key: string]: {} },
fs: { [key: string]: {} }
) {
let level: { [key: string]: {} } = fs;
let index = 0;
@VladSez
VladSez / get.ts
Last active February 13, 2023 13:55
Typed get. Dot notation string type-safe
// https://tsplay.dev/WvaG4m
// Source: https://github.com/ghoullier/awesome-template-literal-types#dot-notation-string-type-safe
// alternative: https://github.com/g-makarov/dot-path-value
type PathImpl<T, Key extends keyof T> =
Key extends string
? T[Key] extends Record<string, any>
? | `${Key}.${PathImpl<T[Key], Exclude<keyof T[Key], keyof any[]>> & string}`
// @ts-check
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation.
* This is especially useful for Docker builds.
*/
!process.env.SKIP_ENV_VALIDATION && (await import("./src/env/server.mjs"));
/** @type {import("next").NextConfig} */
const config = {
reactStrictMode: true,
@VladSez
VladSez / gist:b4b3478f6ff068303949dbbb4b14a049
Created November 30, 2022 22:34
dangerousSetInnerHtml on fragment in react
@VladSez
VladSez / question.ts
Created August 21, 2022 12:23
Interview question
// Timer interview question
// https://twitter.com/erikras/status/1560681872002277379
class Timer {
q = Promise.resolve()
call(cb) {
this.q = this.q.then(() => cb())
return this
}