Skip to content

Instantly share code, notes, and snippets.

View Gergling's full-sized avatar
🦇
Behind a keyboard

Greg Gergling

🦇
Behind a keyboard
View GitHub Profile
@Gergling
Gergling / index.js
Created October 20, 2018 19:56
Star Trek Engineering Crisis
const getRandomItem = list => list[Math.floor(Math.random() * list.length)];
const components = [
'matter/antimatter manifold',
'main deflector dish',
'kettle',
'Captain\'s copy of TMNT Issue #1 signed by Roger Moore',
'baffle plate',
'navigational laser',
'secondary plasma relay'
@Gergling
Gergling / file-size-listing.js
Last active October 10, 2018 13:48
Script to print the sizes of files in order of largest to smallest within their parent folders. Useful for investigating large folders for things which should be deleted or archived for space.
var fs = require("fs"); //Load the filesystem module
const { join } = require('path');
class FileNode {
constructor(path) {
const isDirectory = fs.lstatSync(path).isDirectory();
this.path = path;
if (isDirectory) {
function stringifyExplicitObject(subject) {
return '{' + Object.entries(subject).map(function (entry) {
return entry[0] + ':' + stringify(entry[1]);
}).join(',') + '}';
}
function stringifyArray(subject) {
return '[' + subject.map(function (element) {
return stringify(element);
}).join(',') + ']';
}
@Gergling
Gergling / MutableObject.js
Created April 12, 2018 09:46
A mutable object for functional programming.
// Deliberately not using class because I want data to be private.
function MutableObject() {
const data = {};
const get = name => (name === undefined) ? data : data[name];
const setObject = value => Object.assign(data, value);
const setByFunction = fnc => fnc(this);
const setNameValue = (name, value) => data[name] = value;
const set = (a, b) => {
if (typeof a === 'object') {
@Gergling
Gergling / mischief.js
Created April 9, 2018 13:50
Canvas attachment for mischief
(function (document) {
var canvas = document.createElement('canvas');
canvas.style.width='100%';
canvas.style.height='100%';
canvas.style.top=0;
canvas.style.left=0;
canvas.style.position='absolute';
document.body.appendChild(canvas);
return canvas;
}(document));
@Gergling
Gergling / functionalise.js
Last active April 9, 2018 13:26
Putting the FUN in FUNctional programming.
// Takes a function with two parameters and allows them to be used in the format fnc(x)(y)().
function functionalise(fnc) {
return function functionalised(x) {
return function (y) {
if (y === undefined) {
return x;
} else {
return functionalised(fnc(x, y));
}
};
@Gergling
Gergling / pdf-fields.php
Last active April 9, 2018 13:22
Takes a `pdftk` field dump and turns it into a PHP-copyable output.
<?php
// Usage example: pdftk stuff.pdf dump_data_fields | php pdf-fields.php
$stdin = file('php://stdin');
// print_r($stdin);
$data = [];
$i = 0;
foreach($stdin as $idx => $line) {
$chunks = explode(': ', $line);
@Gergling
Gergling / auto.sh
Created October 13, 2017 13:00
Bash scripts for restarting wifi when it fails.
#!/bin/bash
recent_restart=1
while true; do
printf $(date +"%Y-%m-%d")
printf " "
printf $(date +"%H:%M:%S")
printf " "
if [[ $(ping -c 1 google.com) ]] > /dev/null 2>&1 ; then
if [ $recent_restart -eq 0 ] ;
@Gergling
Gergling / install-everything.sh
Last active September 19, 2015 15:36
Installs various dev tools
# Multi-window terminal
sudo apt-get install terminator
# Basic IDE
sudo apt-get install vim
# JRE
sudo apt-get install default-jre
# Clipboard, useful for copying rsa keys
@Gergling
Gergling / printR
Created February 17, 2015 15:07
A recursive printing function, similar to PHP's print_r.
var printR = function (obj, indent) {
indent = indent || 0;
if (typeof obj === "string") {
grunt.log.writeln("'" + obj + "'");
} else if (typeof obj === "object") {
grunt.log.writeln("{");
Object.keys(obj).forEach(function (key) {
grunt.log.write(repeat(" ", indent + 2) + key + ": ");
printR(obj[key], indent + 1);
});