Skip to content

Instantly share code, notes, and snippets.

View jonmaim's full-sized avatar
🌍
Back in Europe!

Jon Maim jonmaim

🌍
Back in Europe!
View GitHub Profile
@jonmaim
jonmaim / gist:0729e81358189791ec91
Created October 15, 2014 05:51
Hijack console.log
var c = console;
console = {
log : function() {
var args = Array.prototype.slice.apply(arguments);
setTimeout(function() {
c.log.apply(c, args);
}, 5000);
}
};
@jonmaim
jonmaim / csv2js.js
Last active November 23, 2020 11:00
Function takes a CSV (header + data) string as input and gives back a JS object.
// Start from https://gist.github.com/iwek/7154578#file-csv-to-json-js
// and fix the issue with double quoted values
function csvTojs(csv) {
var lines=csv.split("\n");
var result = [];
var headers = lines[0].split(",");
for(var i=1; i<lines.length; i++) {
var obj = {};
@jonmaim
jonmaim / check_disk_usage.js
Last active June 6, 2022 19:03
NodeJS: check disk usage and output remaining space if under 10%
var disk = require('diskusage');
disk.check('/', function(err, info) {
function toGB(x) { return (x / (1024 * 1024 * 1024)).toFixed(1); }
var percentAvailable = ((info.available / info.total) * 100);
if (percentAvailable < 10) { console.log('Warning only ' + toGB(info.available) + 'GB (' + percentAvailable.toFixed(1) + '%) space available!'); }
});
@jonmaim
jonmaim / iterate_dbs.js
Last active February 13, 2019 07:22
Mongo shell: iterate over all dbs
/* switch to admin database and get list of databases */
db = db.getSiblingDB("admin");
dbs = db.runCommand({ "listDatabases": 1 }).databases;
/* iterate through each database and get its collections */
dbs.forEach(function(database) {
print(database.name);
db = db.getSiblingDB(database.name);
cols = db.getCollectionNames();
@jonmaim
jonmaim / iterate_bouncing_email_mbox.js
Last active October 25, 2016 05:04
Iterate over emails in a .mbox file and output bouncing emails
var Mbox = require('node-mbox');
var MailParser = require("mailparser").MailParser;
var mbox = new Mbox('inbox.mbox', { /* options */ });
var bouncingEmails = [];
mbox.on('message', function(msg) {
var mailparser = new MailParser();
mailparser.on("end", function(mail) {
@jonmaim
jonmaim / index.js
Last active December 31, 2016 08:56
Load JSON file
module.exports = function(path, cb) {
var xhr = new XMLHttpRequest();
xhr.overrideMimeType("application/json");
xhr.open('GET', path, true);
xhr.onreadystatechange = function() { xhr.readyState === 4 && xhr.status === 200 && cb(JSON.parse(xhr.responseText)); };
xhr.send(null);
};
@jonmaim
jonmaim / jqlite2html.js
Last active March 20, 2017 11:58
Karma tests jqlite element HTML beautifier - adapted from http://stackoverflow.com/a/26361620/418831
function format(node, level) {
var indentBefore = new Array(level++ + 1).join(' ');
var indentAfter = new Array(level - 1).join(' ');
var textNode;
for (var i = 0; i < node.children.length; i++) {
textNode = document.createTextNode('\n' + indentBefore);
node.insertBefore(textNode, node.children[i]);
format(node.children[i], level);
if (node.lastElementChild === node.children[i]) {
@jonmaim
jonmaim / oauth_node_example.js
Created March 21, 2017 10:53
OAuth 1.0 3-legged server side flow (motorsportreg example)
'use strict';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var localtunnel = require('localtunnel');
var request = require('request');
var qs = require('querystring');
var url = require('url');
var http = require('http');
@jonmaim
jonmaim / bot1.js
Last active November 29, 2018 15:08
First steem voting bot
'use strict';
const steem = require('steem');
/* to be filled up */
const selfName = '';
const postingWif = '';
const eol = '\n';
@jonmaim
jonmaim / get_commenters_from_last_post.js
Created December 16, 2018 15:48
Get the commenters list from your last Steem post
'use strict';
/* to be filled up */
const selfName = 'jonmaim';
const postingWif = 'xxx';
const steem = require('steem');
const timeout = (ms) => new Promise(resolve => setTimeout(resolve, ms));