Skip to content

Instantly share code, notes, and snippets.

View mhingston's full-sized avatar

Mark Hingston mhingston

View GitHub Profile
@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)
}
}
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 / 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])}())
"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 / 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
@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]))
{
@mhingston
mhingston / XOR.js
Created October 6, 2016 16:32
XOR encryption/decryption for the browser (IE10+)
var XOR =
{
encrypt: function(input, key)
{
input = typeof input === 'object' ? JSON.stringify(input) : input.toString();
key = typeof key === 'object' ? JSON.stringify(key) : key.toString();
var cipherText = '';
var length = input.length;
for(var i=0; i < length; i++)
@mhingston
mhingston / calculate.js
Created October 14, 2016 10:54
Calculate without eval
const calculate = (expression) =>
{
return new Function(`return ${expression}`)();
}
calculate('1+2/3*4')