Skip to content

Instantly share code, notes, and snippets.

View tadeaspetak's full-sized avatar

Tadeáš Peták tadeaspetak

View GitHub Profile
@tadeaspetak
tadeaspetak / headingNumbering.gs
Last active November 1, 2016 15:12
Google Documents - Add/Remove Heading Numbers, based on https://github.com/lpanebr/autoNumberHeadings
function addMenu() {
DocumentApp.getUi().createMenu('Headings Tools')
.addItem('Auto Number Headings', 'numberHeadingsAdd')
.addItem('Clear Heading Numbers', 'numberHeadingsClear')
.addToUi();
}
function numberHeadingsAdd(){
numberHeadings(true);
}
@tadeaspetak
tadeaspetak / utils-dom.js
Last active April 20, 2017 07:38
DOM utility functions.
// document body dimensions (exluding scrollbars)
export function getBodyDimensions() {
return {
width: document.body.clientWidth,
height: document.body.clientHeight
};
}
// client offset (typically non-existent)
export function getClientOffset() {
import { useEffect, useState, useRef } from "react";
export const useAnimateNumber = ({
start = 0,
end,
delay = 0,
duration = 0.5,
easing = (t) => t * (2 - t), // ease out quad
}: {
start?: number;
@tadeaspetak
tadeaspetak / profiler.ts
Created June 30, 2020 10:22
Simple js profiler.
class Timer {
start: number;
constructor() {
this.start = performance.now();
}
elapsed() {
return performance.now() - this.start;
}
}
import path from "path";
export const joinUrl = (base: string, ...paths: string[]) => {
return path.posix
.join(base, ...paths.map((p) => p.toString()))
.replace(":/", "://");
};
@tadeaspetak
tadeaspetak / Spinner.tsx
Created April 26, 2021 04:47
Simple react + tailwind spinner
export const Spinner: React.FC<{ size: 4 | 8 | 12 | 16; stroke?: number }> = ({
size,
stroke = 1,
}) => {
return (
<svg
className={`text-green-700 animate-spin w-${size}`}
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="-2 -2 28 28"
@tadeaspetak
tadeaspetak / assert.ts
Created July 2, 2021 09:06
Simple assert equals, practical where a testing framework would be an overkill.
function isEquivalent<T>(received: T, expected: T) {
return received && expected && typeof received === "object"
? isValueEquivalent(received, expected)
: received === expected;
}
function isValueEquivalent(a: Record<string, any>, b: Record<string, any>) {
const aPropertyNames = Object.getOwnPropertyNames(a);
const bPropertyNames = Object.getOwnPropertyNames(b);
@tadeaspetak
tadeaspetak / getOs.ts
Last active July 29, 2021 14:36
Get OS, basic info
export enum OS {
Android = "Android",
iOS = "iOS",
Linux = "Linux",
Mac = "Mac",
Unix = "Unix",
Windows = "Windows",
Other = "Other",
}
@tadeaspetak
tadeaspetak / quicksort-iterative.js
Created November 8, 2021 08:53
Iterative quick sort
const swap = (arr, i, j) => {
const temp = arr[i]
arr[i] = arr[j]
arr[j] = temp;
}
const partition = (arr, start, end) => {
let pivot = arr[end];
let low = start;
@tadeaspetak
tadeaspetak / fullstack-security-server.ts
Last active February 27, 2022 05:43
Front & Fullstack Security
import express from "express";
import fs from "fs";
import helmet from "helmet";
import https from "https";
import path from "path";
const app = express();
// avoid having to manually tweak CSP, HSTS, X-Powered-By, MIME-sniffing, etc.; set the strictest CSP possible
app.use(helmet({ contentSecurityPolicy: { directives: { defaultSrc: "'self'" } } }));
app.use(express.json());