Skip to content

Instantly share code, notes, and snippets.

@eszterkv
eszterkv / machine.js
Last active April 20, 2021 16:25
Generated by XState Viz: https://xstate.js.org/viz
const signUp = Machine({
id: 'signup',
initial: 'init',
context: {
retries: 0
},
states: {
init: {
on: {
@eszterkv
eszterkv / create-nextjs-sitemap.js
Created September 1, 2019 15:30
Create XML sitemap for Next.js projects
#! /usr/bin/env node
const path = require('path');
const glob = require('glob');
const fs = require('fs');
const SITE_ROOT = 'https://example.com';
const SOURCE = path.join(__dirname, '..', 'pages', '/**/*.js');
const DESTINATION = path.join(__dirname, '..', 'static', 'sitemap.xml');
@eszterkv
eszterkv / pre-commit.sh
Last active September 1, 2019 15:27
Pre-commit git hook that runs all unit tests before commit
yarn test
RESULT=$?
if [ $RESULT == 1 ]; then
echo "Commit rejected: some tests failed. Please fix the tests before committing."
exit 1
fi
@eszterkv
eszterkv / countries.js
Created July 30, 2019 12:21
List of countries of the world with name and 2- and 3-letter countrycodes
const allCountries = [
{name: 'Australia', code: 'AUS', code2: 'AU'},
{name: 'Canada', code: 'CAN', code2: 'CA'},
{name: 'Finland', code: 'FIN', code2: 'FI'},
{name: 'Ireland', code: 'IRL', code2: 'IE'},
{name: 'Japan', code: 'JPN', code2: 'JP'},
{name: 'Norway', code: 'NOR', code2: 'NO'},
{name: 'South Africa', code: 'ZAF', code2: 'ZA'},
{name: 'Sweden', code: 'SWE', code2: 'SE'},
{name: 'United Kingdom', code: 'GBR', code2: 'GB', alt: ['Great Britain', 'England', 'UK']},
@eszterkv
eszterkv / img-resize.py
Created September 30, 2017 12:24
Python Image Resizer
import os
import sys
from PIL import Image
def is_image(filename):
return filename[-3:].lower() in ["png", "jpg", "jpeg", "bmp"]
def resize_images_in(folder):
for filename in os.listdir(folder):
if not is_image(filename):
@eszterkv
eszterkv / fix_npm_command_not_found.md
Last active October 17, 2016 15:26
Fix: npm packages not installing correctly

You are reading the right document if you install packages with npm, e.g.:
npm i -g unicorns and after typing unicorns -v, you get unicorns: command not found.

Solution 1:

sudo chown -R $USER /usr/local should solve the problem.
Make sure your prefix is /usr/local, (you can check it with npm config get prefix).
If not, you can set it with npm config set prefix /usr/local

Solution 2:

@eszterkv
eszterkv / random_hexcolor.py
Created September 12, 2016 09:46
Generates a random hexadecimal color (e.g. #ff0000)
def random_hexcolor():
color = '#'
while len(color) < 7:
hex = '{0:x}'.format(random.randrange(256))
if len(hex) < 2:
hex = '0' + hex
color += hex
return color
@eszterkv
eszterkv / myio.py
Last active September 9, 2016 11:34
Python module for basic I/O operations
def read(filename):
'''
Opens a file and prints its lines in the console.
Takes 1 arg (string): filename
'''
with open(filename) as f:
for line in f:
print(line.rstrip())
def add(filename, text):
@eszterkv
eszterkv / downloader.py
Created September 9, 2016 09:08
Downloads the file on the given url and saves it to the current directory.
#!/usr/bin/env python3
import os
import os.path
import requests
def dl(url):
'''
Downloads the file on the given url and saves it to the current directory.
'''
r = requests.get(url)
@eszterkv
eszterkv / string_calculator.js
Created July 14, 2016 12:28
Calculator in num1(operation(num2())); format, just for fun.
// Link to kata: http://www.codewars.com/kata/calculating-with-functions/javascript
// How it works:
// seven(times(five())); // returns 35
// four(plus(nine())); // returns 13
// eight(minus(three())); // returns 5
// six(dividedBy(two())); // returns 3
function zero() {
if (arguments.length == 0) return 0;