Skip to content

Instantly share code, notes, and snippets.

c0derabbit / randomchoice.js
Created Jul 14, 2016
A JavaScript solution for random choice from an array.
View randomchoice.js
function randomChoice(arr) {
return arr[Math.floor(Math.random() * arr.length)];
c0derabbit / create-nextjs-sitemap.js
Created Sep 1, 2019
Create XML sitemap for Next.js projects
View create-nextjs-sitemap.js
#! /usr/bin/env node
const path = require('path');
const glob = require('glob');
const fs = require('fs');
const SITE_ROOT = '';
const SOURCE = path.join(__dirname, '..', 'pages', '/**/*.js');
const DESTINATION = path.join(__dirname, '..', 'static', 'sitemap.xml');
c0derabbit /
Last active Sep 1, 2019
Pre-commit git hook that runs all unit tests before commit
yarn test
if [ $RESULT == 1 ]; then
echo "Commit rejected: some tests failed. Please fix the tests before committing."
exit 1
c0derabbit / countries.js
Created Jul 30, 2019
List of countries of the world with name and 2- and 3-letter countrycodes
View countries.js
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']},
c0derabbit / fontsizer.js
Last active Jul 22, 2018
JS snippet for increasing or decreasing font size in a HTML document using jQuery.
View fontsizer.js
function resize(increase_or_decrease) {
const direction = increase_or_decrease;
// First arg is the direction, elements to change follow
for (let i = 1; i < arguments.length; i++) {
let fontsize = parseInt($(arguments[i]).css('font-size'));
if (direction === 'increase') {
if (fontsize > 20) fontsize = 20; // Prevent text from getting too big
} else if (direction === 'decrease') {
c0derabbit / unique_id.js
Last active Sep 30, 2017
Creates a unique ID in JavaScript
View unique_id.js
const id = + Math.random().toString(16).substr(2);
c0derabbit /
Created Sep 30, 2017
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):
c0derabbit /
Last active Oct 17, 2016
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:

c0derabbit /
Created Sep 12, 2016
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
c0derabbit /
Last active Sep 9, 2016
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:
def add(filename, text):
You can’t perform that action at this time.