Skip to content

Instantly share code, notes, and snippets.

View polRk's full-sized avatar
💯

Vladislav Polyakov polRk

💯
View GitHub Profile
@enobufs
enobufs / before_after_each_test.go
Created May 7, 2022 23:07
beforeEach and afterEach with go-test
package main
import (
"fmt"
"os"
"runtime"
"testing"
)
func TestMain(m *testing.M) {
@konstantin24121
konstantin24121 / verifyTelegramWebAppData.tsx
Last active February 17, 2024 10:58
Telegram Bot 6.0 Validating data received via the Web App node implementation
const TELEGRAM_BOT_TOKEN = '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw'; // https://core.telegram.org/bots#creating-a-new-bot
export const verifyTelegramWebAppData = async (telegramInitData: string): boolean => {
// The data is a query string, which is composed of a series of field-value pairs.
const encoded = decodeURIComponent(telegramInitData);
// HMAC-SHA-256 signature of the bot's token with the constant string WebAppData used as a key.
const secret = crypto
@smnbbrv
smnbbrv / promisified-grpc-client.ts
Last active November 4, 2023 21:22
Promisify @grpc-js service client with typescript
import { Client, ServiceError, Metadata, CallOptions, ClientUnaryCall } from '@grpc/grpc-js';
import { Message } from 'google-protobuf';
type OriginalCall<T, U> = (request: T, metadata: Metadata, options: Partial<CallOptions>, callback: (error: ServiceError, res: U) => void) => ClientUnaryCall;
type PromisifiedCall<T, U> = ((request: T, metadata?: Metadata, options?: Partial<CallOptions>) => Promise<U>);
export type Promisified<C> = { $: C; } & {
[prop in Exclude<keyof C, keyof Client>]: (C[prop] extends OriginalCall<infer T, infer U> ? PromisifiedCall<T, U> : never);
}
@polRk
polRk / useWidthObserver.ts
Created May 11, 2020 16:45
Watch component width changes
import debounce from 'lodash/debounce'
import { RefObject, useEffect } from 'react'
export const useWidthObserver = <T extends HTMLElement>(
refObject: RefObject<T>,
setWidth: (width: number) => void
) => {
useEffect(() => {
function resizeListener() {
if (refObject.current) {
@polRk
polRk / App.tsx
Created May 11, 2020 16:02
React Apollo + Firebase authorization
import Dashboard from 'pages/Dashboard'
import Layout from 'components/Layout'
import Login from 'pages/Login'
import React from 'react'
import { useRoutes, Navigate, useLocation } from 'react-router-dom'
import { useAuth, userContext } from 'hooks/useAuth'
function App() {
const storedToken = localStorage.getItem('token')
const location = useLocation()
@polRk
polRk / useClient.ts
Last active May 11, 2020 16:47
React Apollo useClient with authorization
import {
ApolloClient,
ApolloLink,
HttpLink,
InMemoryCache,
} from '@apollo/client'
import { getMainDefinition } from '@apollo/client/utilities'
import { setContext } from '@apollo/link-context'
import { onError } from '@apollo/link-error'
import { RetryLink } from '@apollo/link-retry'
@polRk
polRk / useAuth.ts
Created May 11, 2020 15:54
React Firebase useAuth
import * as firebase from 'firebase/app'
import { useEffect, useState, createContext } from 'react'
export type AuthState = {
initializing: boolean
user: firebase.User | null
}
export const userContext = createContext<AuthState>({
initializing: true,
@mrkara
mrkara / read-key-press-cpp-linux.cpp
Last active March 18, 2023 18:59
[Read Key Press from Terminal under Linux with C++] #cpp #snippet
#include<iostream>
int main() {
char c;
// Set the terminal to raw mode
while(1) {
system("stty raw");
c = getchar();
// terminate when "." is pressed
system("stty cooked");
@Potherca
Potherca / README.md
Last active March 4, 2024 23:40
The search for a Regex to match BEM CSS class-names

The search for a Regex to match BEM CSS class-names

TL;DR

Use this regular expression to match BEM class-names:

^\.[a-z]([a-z0-9-]+)?(__([a-z0-9]+-?)+)?(--([a-z0-9]+-?)+){0,2}$
@polRk
polRk / calendar.ts
Created June 8, 2019 17:53
Generate month days calendar on TypeScript and date-fns
import { isSameMonth, startOfMonth } from 'date-fns'
export const generateCalendar = (
firstDateOfMonth: Date
): number[][] => {
const date = startOfMonth(firstDateOfMonth)
const getDay = (date: Date) => {
let day = date.getDay()
if (day === 0) day = 7