Skip to content

Instantly share code, notes, and snippets.

View nikoheikkila's full-sized avatar
:electron:
Nice to meet you!

Niko Heikkilä nikoheikkila

:electron:
Nice to meet you!
View GitHub Profile
@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()
@nikoheikkila
nikoheikkila / duplicate_count.py
Created June 8, 2019 11:29
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string.
def duplicate_count(text: str) -> int:
"""Write a function that will return the count of distinct case-insensitive
alphabetic characters and numeric digits that occur more than once in the
input string.
The input string can be assumed to contain only alphabets
(both uppercase and lowercase) and numeric digits.
"abcde" -> 0 # no characters repeat more than once
"aabbcde" -> 2 # 'a' and 'b'
@nikoheikkila
nikoheikkila / cobalt2.json
Last active June 7, 2019 13:42
Cobalt2 theme for Mattermost
{
"sidebarBg": "#15232d",
"sidebarText": "#dddddd",
"sidebarUnreadText": "#9effff",
"sidebarTextHoverBg": "#0d3a58",
"sidebarTextActiveBorder": "#15232d",
"sidebarTextActiveColor": "#ffffff",
"sidebarHeaderBg": "#15232d",
"sidebarHeaderTextColor": "#ffc600",
"onlineIndicator": "#60e019",