Skip to content

Instantly share code, notes, and snippets.

View vezaynk's full-sized avatar
🤔

Slava Knyazev vezaynk

🤔
View GitHub Profile
@vezaynk
vezaynk / AsyncQueue.ts
Created May 4, 2023 20:02
AsyncQueue.ts
class AsyncQueue<T> {
#elements: T[] = [];
#waiting: ((el: T) => void)[] = [];
get size() { return this.#elements.length }
enqueue(el: T) {
const next = this.#waiting.shift();
if (next) {
next(el);
} else {
@vezaynk
vezaynk / HOC.ts
Created February 1, 2023 23:35
HOC helpers. reduceHOCs and applyHOCs.
interface HOC<T> {
(Component: React.ComponentType<T>): (props: T) => JSX.Element
}
const reduceHOCs = <T>(...hocs: HOC<T>[]): HOC<T> => hocs
.reduce((reduced, next) => (c) => next(reduced(c)));
const applyHOCs = <T>(...hocs: HOC<T>[]) {
const reducedHoc = reduceHOCs(...hocs);
@vezaynk
vezaynk / hocfromhook.tsx
Created December 7, 2022 04:28
Create HOC from Hook
import React, { useContext, createContext } from "react";
function createHOCFromHook<T,>(cb: () => T) {
const Context = createContext<T>(null as T);
const withProvider = <T,>(Component: React.ComponentType<T>) =>
function (props: T & JSX.IntrinsicAttributes) {
return (
<Context.Provider value={cb()}>
<Component {...props} />
</Context.Provider>
@vezaynk
vezaynk / stats.sh
Created September 29, 2022 16:55
TypeScript adoption over time
#!/bin/bash
# Generate TypeScript adoption data
# Google Docs Spreadsheet:
# https://docs.google.com/spreadsheets/d/13iDFWEhGbE8ZGu1LoXmRINy1ZSLd227mAFj99Evl6fc
# Usage:
# bash ./stats.sh > results.tsv
# Copy paste data into sheet
DATE="2022-07-01" # Tune this file to set start date of adoption
@vezaynk
vezaynk / .eslintrc
Created August 21, 2022 18:57
My eslint
{
"env": {
"es2021": true,
"jest/globals": true,
"browser": true
},
"extends": [
"airbnb",
"airbnb-typescript",
"airbnb/hooks",
@vezaynk
vezaynk / VueSimple.ts
Created July 13, 2022 03:50
Solution to Simple Vue by Anthony Fu (@antfu) from typescript-challenge
type ToGetter<T> = {
[P in keyof T]: T[P] extends (...args: any) => any ? ReturnType<T[P]> : never;
}
interface VueOptions<D, C, M> {
data: () => D;
computed: C;
methods: M;
}
@vezaynk
vezaynk / MinusOne.ts
Created July 12, 2022 19:11
MinusOne.ts
// Lookup table to seed the counting array
// Skips to length of 10, 100, 1000 instead of counting
// Allows going up to 1998 instead of 999!
// With some work, this technique can be pushed further for higher numbers
interface InitialLengths {
'2': [0, 0, 0, 0, 0,
0, 0, 0, 0, 0],
'3': [...InitialLengths[2], ...InitialLengths[2], ...InitialLengths[2], ...InitialLengths[2], ...InitialLengths[2],
...InitialLengths[2],...InitialLengths[2], ...InitialLengths[2], ...InitialLengths[2], ...InitialLengths[2]],
'4': [...InitialLengths[3], ...InitialLengths[3], ...InitialLengths[3], ...InitialLengths[3], ...InitialLengths[3],
interface StringDigitToNumber {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
interface SpacingProps {
__typename: string,
property1: {
__typename: string;
a: 2;
nested: {
__typename: string,
x: 3
}
},
@vezaynk
vezaynk / useTitle.test.tsx
Last active September 13, 2021 22:57
A useTitle hook for React
/**
* @jest-environment jsdom
*/
import React, { useState } from 'react';
import { renderHook, cleanup } from '@testing-library/react-hooks';
import { act, fireEvent, render } from '@testing-library/react';
import useTitle from './useTitle';
import * as LayoutWrappedElement from ':hyperloop/src/layout/getLayoutWrappedElement';