Skip to content

Instantly share code, notes, and snippets.

View mhingston's full-sized avatar

Mark Hingston mhingston

View GitHub Profile
@mhingston
mhingston / swal-override.js
Created June 11, 2015 10:01
Override native alert, confirm and prompt functions with prettier versions and optional callbacks
/*
Include SweetAlert before this script
http://t4t5.github.io/sweetalert
*/
function alert(message, callback)
{
callback = callback || function(){}
var options =
{
def pascalsTriangle(n):
triangle = [[1]]
for i in xrange(1, n):
row = []
for j in xrange(len(triangle[i-1]) + 1):
if j-1 >= 0 and j < len(triangle[i-1]):
row.append(triangle[i-1][j-1] + triangle[i-1][j])
@mhingston
mhingston / forEach.js
Created November 13, 2015 19:30
Faster implementation of native Array.forEach method
Array.prototype.forEach = function(fn)
{
"use strict"
var l = this.length
for(var i = 0; i < l; ++i)
{
fn(this[i], i, this)
}
}
@mhingston
mhingston / htmlToPlainText.js
Last active December 3, 2015 12:08
A quick and dirty function to convert a string of HTML to plain text. This is primarily intended for generating plain text emails from HTML
function htmlToPlainText(html)
{
var text = html
var aRe = /<a\s+href=['"]([^'"\s]+)['"][^>]+>((?!<\/a>).+)<\/a>/i
while(aRe.test(text)) // replace anchors
{
text = text.replace(aRe, "$2 ($1)")
}
text = text.replace(/<!--(?:(?!-->)[\w\W]*?)-->/gi, "") // remove comments
// Bookmarklet
javascript:(function(){var windowSize=[];while(windowSize.length!==2){var windowSize=prompt("Window Size:","1024x768").split("x")}window.open(top.location.href,document.title,"left=0,top=0,height="+windowSize[1]+",width="+windowSize[0])}())
@mhingston
mhingston / fail2ban.md
Last active April 13, 2016 17:59
Fail2ban permanent bans

/etc/fail2ban/jail.local

Add to JAILS section:

[ip-blacklist]

enabled   = true
banaction = iptables-allports
port      = anyport
filter    = ip-blacklist
logpath = /etc/fail2ban/ip.blacklist
#!/usr/bin/env python3
import http.server
import socketserver
socketserver.ThreadingTCPServer.allow_reuse_address = True
HOST = "127.0.0.1"
PORT = 8000
handler = http.server.SimpleHTTPRequestHandler
"use strict"
// Iterator with generator
let fibonacci =
{
[Symbol.iterator]: function*()
{
let pre = 0, cur = 1
while(true)
#!/usr/bin/env node
// Refer to: https://github.com/saadq/koa2-skeleton for more
if(process.env.NODE_ENV === "production")
{
require("newrelic")
}
import {install} from "source-map-support"
install()
@mhingston
mhingston / mergeObjects.js
Created June 3, 2016 10:04
Deeply merge 2 objects, i.e. deep Object.assign
const mergeObjects = (target, source) =>
{
const isObject = item => item && typeof item === "object" && !Array.isArray(item) && item !== null
if(isObject(target) && isObject(source))
{
for(let key in source)
{
if(source.hasOwnProperty(key) && isObject(source[key]))
{