Skip to content

Instantly share code, notes, and snippets.

View fvdm's full-sized avatar
🦖
I may be slow to respond

Franklin fvdm

🦖
I may be slow to respond
View GitHub Profile
# Force download for certain URLs with ?force_download
set $forceDownload "";
set $filename "";
if ($request_filename ~ /([^/]+)$) {
set $filename $1;
}
if ($request_uri ~ [\?&]force_download(=(?<filename>[^&$]+)|&|$)) {
set $forceDownload "attachment; filename=\"$filename\"";
#!/usr/bin/env node
/*
USAGE: cat file.json | parsejson [OPTION] [PROPERTY]
Name: parsejson
Description: Parse JSON text to human-readable tree with syntax coloring.
Source: https://frankl.in/code/json-cli-parser-with-syntax-coloring
Author: Franklin (https://github.com/fvdm)
License: Unlicense / Public Domain
var geo = require ('geoip2ws') ('userId', 'licenseKey');
// console.log is limited to only 3 levels
function output (err, data) {
console.dir (err || data, {depth: 10});
}
// Lookup Google
geo ('insights', '173.194.65.100', output);
var bol = require ('bolcom') ('apikey');
// Find something
bol.catalog.search ({ q: 'node.js' }, function (err, data) {
if (err) {
console.log ('Search failed');
console.log (err);
} else {
for (var i = 0; i < data.products.length; i++) {
var product = data.products [i];
function busy (ms) {
var arr = '|/―\\', i = 0;
setInterval (function() {
i = !arr[i] ? 0 : i;
process.stdout.write ('\r' + arr[i] + ' ');
i++;
}, ms);
}
busy (90);
#!/bin/sh
vn="/usr/bin/vnstat -i eth0 -s"
echo
$vn | head -n 2 | tail -n 1 # header
$vn | tail -n 2 | head -n 1 # today
$vn | head -n 5 | tail -n 1 # month
// Add pre-exit script
process.on ('exit', code => {
console.log (`Whoa! Exit code ${code}, cleaning up...`);
// i.e. close database
});
// Add another for fun
process.on ('exit', code => {
if (code > 0) {
console.log ('Hmm something went wrong');
#!/bin/sh
if [ "$1" == "-h" ]; then
echo "Colorful Homebrew update script"
echo
echo "USAGE: update [-y]"
echo
echo " -y skip questions"
echo " -h display this help"
echo
exit 0
@fvdm
fvdm / number_humanBytes.js
Created January 29, 2015 11:01
Number.humanBytes()
// Convert bytes to string '123.6 KiB', MiB, etc.
// var bytes = 8765432
// var human = bytes.humanBytes( 2 )
// 8.36 MiB
Number.prototype.humanBytes = function( decimals ) {
var units = ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
var unit = 'B';
var bytes = this.valueOf();
var dec = decimals ? Math.pow( 10, decimals ) : 1;
var i = 0;
@fvdm
fvdm / object_foreach.js
Created January 29, 2015 10:39
Object.forEach()
// Object.forEach( function( value, key, index ) {} )
Object.prototype.forEach = function( cb ) {
var keys = Object.keys(this);
for( var i = 0; i < keys.length; i++ ) {
var key = keys[i];
cb( this[key], key, i );
}
}