Skip to content

Instantly share code, notes, and snippets.

View nikoheikkila's full-sized avatar
🇺🇦
Stand with Ukraine

Niko Heikkilä nikoheikkila

🇺🇦
Stand with Ukraine
View GitHub Profile
@nikoheikkila
nikoheikkila / README.md
Last active April 15, 2024 17:15
Fish Shell function for sourcing standard .env files

envsource

⚠️ NOTE (20.5.2023): I don't really use this function at all anymore since there are now tools such as Taskfile, which reads the .env file for me sparing me the need to pollute my session with environment variables.


I've been using Fish shell for years which is great and all, but one thing that has got me frustrated is using it with .env files.

When attempting to run source .env in a project, I usually encounter this problem:

@nikoheikkila
nikoheikkila / startup.py
Created June 6, 2020 16:19
Startup file for IPython
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This is my startup file for IPython parser.
# Licensed under MIT by Niko Heikkilä
from decimal import *
from typing import List
# Too lazy to type this always...
@nikoheikkila
nikoheikkila / tts.js
Last active January 23, 2021 14:31
Javascript: Text-to-Speech (TTS) in Browser
class TextToSpeech {
constructor({ volume = 1, rate = 1, pitch = 1, lang = "en" } = {}) {
if (typeof window === 'undefined' || !"speechSynthesis" in window) {
throw new Error("Text to speech is not supported in your browser.");
}
this.engine = new SpeechSynthesisUtterance();
this.setLanguage(lang)
.setPitch(pitch)
.setRate(rate)
@nikoheikkila
nikoheikkila / validate.js
Last active April 19, 2020 19:46
Validate passwords using higher-order functions in Javascript
/** Helper for printing validator reason */
const warn = msg => {
console.warn('Invalid:', msg)
return false
}
/** Validators */
const longEnough = (password, minLength = 12) => password.length >= minLength || warn(`Password should contain ${minLength} or more characters.`)
const hasUpperCase = password => /[A-Z]+/.test(password) || warn('Password should have at least one uppercase letter.')
const hasLowerCase = password => /[a-z]+/.test(password) || warn('Password should have at least one lowercase letter.')
@nikoheikkila
nikoheikkila / formatMoney.ts
Last active March 27, 2020 15:31
TypeScript: Format floating euros to string notation
// Solution
type Formatter<T> = (a: T) => (b: T) => string
const numberFormatter: Formatter<number> = decimals => euros => euros.toFixed(decimals).replace(/\./, ',') + ' €'
const toCurrencyString = numberFormatter(2)
// Tests
const suites: [number, string][] = [
[-1, "-1,00 €"],
[0, "0,00 €"],
[1, "1,00 €"],
@nikoheikkila
nikoheikkila / git-commit.json
Last active August 15, 2020 13:07
Use conventional commits through a VS Code shortcut
{
"Conventional Commit": {
"prefix": "cc",
"body": [
"${1:type}(${2:scope}): ${3:title}",
"",
"${4:body}",
"",
"${5:footer}"
],
@nikoheikkila
nikoheikkila / update_starship.sh
Last active August 10, 2019 05:39
Updates Starship to Latest Version
#!/usr/bin/env bash
set -euo pipefail
post_install() {
# Checks whether user's shell configuration needs adjustment for Spaceship
# This function does not do anything when user has already activated the
# necessary configuration.
echo "Checking shell configuration"
@nikoheikkila
nikoheikkila / pipes.py
Created July 6, 2019 15:38
Playing with pipeable lists in Python 3
from __future__ import annotations
from typing import Any, Callable, Union, Tuple
from functools import reduce
class Pipe(list):
"""Type declarations"""
Predicate = Callable[[Any], bool]
Mappable = Callable[[Any], Any]
@nikoheikkila
nikoheikkila / pipes.php
Created June 30, 2019 11:48
Functional Piping in PHP
<?php declare(strict_types = 1);
function pipe(...$args) {
return function ($arg) use ($args) {
$reducer = function ($prev, $fn) {
return $fn($prev);
};
return array_reduce($args, $reducer, $arg);
};
@nikoheikkila
nikoheikkila / leet.js
Created June 22, 2019 13:57
Make text L337'er!
const leet = char => {
const charMap = {
'A': 4,
'E': 3,
'I': 1,
'S': 5,
'T': 7,
}
char = char.toString().toUpperCase()