Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
ValentaTomas / gcp-compute.yml
Last active June 9, 2021 18:06
GitHub Action to build and deploy Docker container to Google Compute Engine on master branch push
name: Build and Deploy to Google Compute Engine
on:
push:
branches:
- master
env:
PROJECT_ID: ${{ secrets.GCE_PROJECT }}
GCE_IMAGE: ${{ secrets.GCE_IMAGE }}
@ValentaTomas
ValentaTomas / node16.Dockerfile
Last active June 15, 2021 14:57
Node.js 16 two stage build setup
# This stage installs modules
FROM node:16.2.0 as modules
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --production
# This stage builds TypeScript
@ValentaTomas
ValentaTomas / getRandomHexColor.ts
Last active August 26, 2021 14:07
Get random bright color in HEX format
function randomDiscreteNumber(max: number) {
return Math.floor(Math.random() * max) + 1
}
export function getRandomColor() {
// Use HSL model to create pastel colors.
// It is easier to do that by randomizing hue while keeping saturation and lightness in reasonable boundaries.
const hue = 6 * randomDiscreteNumber(60)
const saturation = 80 + 10 * Math.random()
const lightness = (60 + 10 * Math.random()) / 100
@ValentaTomas
ValentaTomas / getFileExtension.ts
Created August 24, 2021 19:19
Get the file extension
function getFileExtension(file: string) {
return file.slice((Math.max(0, file.lastIndexOf('.')) || Infinity) + 1)
}
@ValentaTomas
ValentaTomas / forceUpdate.ts
Created August 29, 2021 14:12
React hooks for forcing component update
const forceUpdate = useReducer(() => ({}), {})[1]
@ValentaTomas
ValentaTomas / .gitpod.yml
Created October 16, 2021 10:48
Basic NPM setup for Gitpod
tasks:
- init: npm i
github:
prebuilds:
branches: true
image:
file: .gitpod.Dockerfile
@ValentaTomas
ValentaTomas / useElement.ts
Created December 20, 2021 15:31
React hook for handling changes to referenced HTML elements
import {
useState,
useCallback,
RefCallback,
} from 'react'
function useElement<T>(cb?: (element: T) => any): [T | undefined, (instance: T | null) => void] {
const [element, setElement] = useState<T>()
const initElementRef = useCallback<RefCallback<T>>((element) => {
@ValentaTomas
ValentaTomas / hash.ts
Created May 20, 2022 20:52
Create hash from a string
// Coverts a signed number to its unsigned hexadecimal representation
// `>>> 0` is a conversion to an unsigned 32-bit integer
// `000000` adds padding and `.slice(-8)` removes the excessive padding
function toHex(value: number) {
return ('0000000' + (value >>> 0).toString(16)).slice(-8)
}
// Creates hash by multiplying each char value
// `hash * 31` is an optimized version of `((hash << 5) - hash)` and the number 31 is used for its mathematical properties:
// https://stackoverflow.com/questions/299304/why-does-javas-hashcode-in-string-use-31-as-a-multiplier
@ValentaTomas
ValentaTomas / id.ts
Created May 20, 2022 20:54
Very primitive ID generator
export function customAlphabet(alphabet: string, size: number) {
return () => {
let id = ''
// A compact alternative for `for (var i = 0; i < step; i++)`.
let i = size
while (i--) {
// `| 0` is more compact and faster than `Math.floor()`.
id += alphabet[(Math.random() * alphabet.length) | 0]
}
return id
@ValentaTomas
ValentaTomas / id.go
Created May 20, 2022 20:54
Very primitive ID generator
import (
"math/rand"
)
var alphabet = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
func genRandom(length int) string {
b := make([]rune, length)
for i := range b {
b[i] = alphabet[rand.Intn(len(alphabet))]