Skip to content

Instantly share code, notes, and snippets.

View robinpokorny's full-sized avatar

Robin Pokorny robinpokorny

View GitHub Profile
@robinpokorny
robinpokorny / parseUuid7Date.js
Created May 8, 2023 12:30
Validate UUIDv7 and parse the timestamp
const uuid7Re = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
const parseUuid7Date = (uuid) => {
if (typeof uuid !== `string` || !uuid7Re.test(uuid)) {
throw new TypeError(`Expected UUIDv7. Received: ${String(uuid)} (${typeof uuid})`)
}
const timestampHex = uuid.slice(0, 13).replace(`-`, ``)
const timestamp = Number.parseInt(timestampHex, 16)
return new Date(timestamp)
@robinpokorny
robinpokorny / uuid4to7.js
Created April 26, 2023 10:40
Convert UUID v4 to v7 in JavaScript (or generate one)
const uuid4to7 = (uuid, now = Date.now()) => {
const ts = now.toString(16).padStart(12, `0`)
return `${ts.slice(0, 8)}-${ts.slice(8)}-7${uuid.slice(15)}`
}
// generate new UUIDv7
uuid4to7(crypto.randomUUID())
// Conforms to example in the spec RFC4122bis C.6
@robinpokorny
robinpokorny / railway_oriented_typescript.ts
Created November 3, 2022 08:47
Heapcon 2022: Railway Oriented Typescript
/*
=============================
Railway Oriented Typescript
=============================
by @robinpokorny
*/
/* === 1. Union basics === */
const a: string | number = 42;
@robinpokorny
robinpokorny / mirrorGitHub.sh
Last active August 19, 2022 13:26
Mirror GitHub repos locally on NAS
#!/bin/bash
# Mirrors all GitHub repositories the current user has read access to.
# See license at the end of the file.
# Token from https://github.com/settings/tokens
OAUTH_TOKEN="<TODO TOKEN>"
# where should the files be saved
DIR="<TODO PATH>"
@robinpokorny
robinpokorny / index.ts
Last active June 13, 2022 08:51
WebExpo 2022: Railway Oriented Typescript
/*
=============================
Railway Oriented Typescript
=============================
by @robinpokorny
*/
/* === 1. Union basics === */
const a: string | number = 42;
@robinpokorny
robinpokorny / tagged_unions_showcase.ts
Last active January 23, 2022 12:03
Tagged Unions in TypeScript Showcase
// And you will know my name is TypeScript
// When I lay my Tagged Union upon thee
// by @robinpokorny
// 1. Union of literals
// The cornerstone of any nutritious breakfast.
type Burger1 = `McDonalds` | `BigKahuna`;
const brettsBreakfast1_1: Burger1 = `BigKahuna`;
const brettsBreakfast1_2: Burger1 = `Whooper`; // Caught
@robinpokorny
robinpokorny / first.js
Created October 13, 2021 15:16
Most profit from stock quotes [solution] [JS]
const sum = (a, b) => a + b
const getMostProfitFromStockQuotes = (quotes, current = 0) => {
if (quotes.length < 2) return current
const max = Math.max(...quotes)
const maxAt = quotes.indexOf(max)
const left = quotes.slice(0, maxAt)
@robinpokorny
robinpokorny / HttpStatusCode.ts
Created April 13, 2021 21:38
All HTTP status codes in TypeScript const Enums
export const enum InformationalResponse {
Continue = 100,
SwitchingProtocols = 101,
Processing = 102,
EarlyHints = 103,
}
export const enum Success {
OK = 200,
Created = 201,
@robinpokorny
robinpokorny / titlecaseWords.js
Created June 23, 2020 07:36
For vs map/filter/reduce
const words = ["hello", " ", "world"];
for (let i = 0; i <= words.length; i++) {
const word = words[i];
if (word.length <= 0) continue;
words[i] = uppercaseFirstLetter(word.trim());
}
const titlecasedWords = words
.filter(isNotEmpty)
@robinpokorny
robinpokorny / .bash_profile
Created March 4, 2020 13:10
In case of fire
function fire() {
local BRANCH=emergency-`date +%s`-`git config user.email`
git checkout -b $BRANCH
git add -A
git commit -m 'EMERGENCY!'
git push -f origin $BRANCH
}
# Inspired by https://www.reddit.com/r/ProgrammerHumor/comments/3nc531/in_case_of_fire/cvn1k27/