Skip to content

Instantly share code, notes, and snippets.

:shipit:
Shippin' things, if you know what I mean

Niko Heikkilä nikoheikkila

:shipit:
Shippin' things, if you know what I mean
View GitHub Profile
@nikoheikkila
nikoheikkila / startup.py
Created Jun 6, 2020
Startup file for IPython
View startup.py
#!/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
Created May 20, 2020
Javascript: Text-to-Speech (TTS) in Browser
View tts.js
class TextToSpeech {
constructor(text, { volume = 1, rate = 1, pitch = 1, lang = 'en' } = {}) {
if (! 'speechSynthesis' in window) {
throw new 'Text to speech not supported in your browser.'
}
this.engine = new SpeechSynthesisUtterance()
this.setLanguage(lang)
@nikoheikkila
nikoheikkila / validate.js
Last active Apr 19, 2020
Validate passwords using higher-order functions in Javascript
View validate.js
/** 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 Mar 27, 2020
TypeScript: Format floating euros to string notation
View formatMoney.ts
// 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 Mar 16, 2020
Use conventional commits through a VS Code shortcut
View git-commit.json
{
"Conventional Commit": {
"prefix": "cc",
"body": [
"${1:type}(${2:scope}): ${3:title}",
"",
"${4:body}",
"",
"${5:footer}"
],
@nikoheikkila
nikoheikkila / update_starship.sh
Last active Aug 10, 2019
Updates Starship to Latest Version
View update_starship.sh
#!/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 Jul 6, 2019
Playing with pipeable lists in Python 3
View pipes.py
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 Jun 30, 2019
Functional Piping in PHP
View pipes.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 Jun 22, 2019
Make text L337'er!
View leet.js
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 Jun 8, 2019
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.
View duplicate_count.py
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'
You can’t perform that action at this time.