Skip to content

Instantly share code, notes, and snippets.

Andreas Hultgren ahultgren

Block or report user

Report or block ahultgren

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ahultgren
ahultgren / Agent-forwarding.js
Created May 26, 2014
Reproduction of a bug in ssh2 when using agent forwarding
View Agent-forwarding.js
var Connection = require('ssh2');
var conn = new Connection();
conn.on('ready', function() {
exec(conn, 'pwd', function () {
exec(conn, 'pwd', function () {
conn.end();
});
});
});
View keybase.md

Keybase proof

I hereby claim:

  • I am ahultgren on github.
  • I am ahultgren (https://keybase.io/ahultgren) on keybase.
  • I have a public key whose fingerprint is 270E BFC7 7B5D C59D 1422 3466 DA26 60E7 34DF 1FD7

To claim this, I am signing this object:

@ahultgren
ahultgren / hopping.js
Last active Aug 29, 2015
node ssh2 connection hopping attempt
View hopping.js
var Connection = require('ssh2'),
net = require('net');
var conn1 = new Connectiweon(),
conn2 = new Connection();
conn1.on('ready', function() {
console.log('FIRST :: connection ready');
conn1.exec('nc github.com 22', function(err, stream) {
if (err) return console.log('FIRST :: exec error: ' + err);
@ahultgren
ahultgren / validatePersonnummer.js
Created Jan 20, 2014
Validerar svenska personnummer. Kollar längd, att det är ett giltigt datum samt validerar checksumman.
View validatePersonnummer.js
(function (exports) {
function validatePersonnummer (personnummer) {
// Ensure string in case of number
personnummer = "" + personnummer;
// Trim non-digits
// This discards (and thus accepts) any non-digits anywhere in the personnummer.
// Depending on your case, that might not be the desired behavior.
personnummer = personnummer.replace(/[^\d]/g, '');
@ahultgren
ahultgren / Gruntfile.js
Last active Dec 26, 2015
A template to generate a retina/high-res less spritesheet using grunt-spritesmith _without_ using both hi/low res images. Also it generates a bit less bloated rules than default. You might want to modify the template (eg classname and selector) depending on how you prefer to name stuff.
View Gruntfile.js
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
sprite: {
icons: {
src: 'img/icons/*.{png,jpg,gif}',
destImg: 'img/icons.png',
destCSS: 'less/general/icons.less',
algorithm: 'binary-tree',
cssTemplate: 'path/to/icon.template.mustache'
@ahultgren
ahultgren / cloneFn.js
Created Aug 31, 2013
Clone a function. Creates a new function that is exactly the same as the original, but without its properties. Essential if you want to construct a "callable object".
View cloneFn.js
function cloneFn (fn) {
var args,
strFn = fn.toString();
// Extract argument names
// Match first occurence of "(<anything but end-parenthesis>)",
// split it on "," and use as args
args = [Function].concat(strFn
.match(/\(([^\)]*)\)/)[1]
.trim()
@ahultgren
ahultgren / README.md
Last active Dec 19, 2015
Once upon a time there was a show where a guest could win The Grand Prize™.
View README.md

Once upon a time there was a show where a guest could win The Grand Prize™. To do so he/she just had to pick one box of three. Of these three boxes, two were empty. Thus, only one very randomly chosen box contained the grand prize. When the guest had picked a box of his/her liking, however, there was a twist. The host of the show, who knew which box was the very randomly chosen one, would remove a box that was empty. Now the guest was faced with the option to keep the already picked box, or swap to the other one. What is the correct thing to do?

Well, according to common sense, it really shouldn't matter. Math disagrees though. According to math, since we from the beginning know that the chosen box had 1/3 chance of containing the grand prize, and the second box is removed, the last one has to have the rest of the 2/3s of chance. Since this is obviously ridiculous I decided to once and for all, for the greater sanity of the world, actually test it.

And, in short, to the dismay of my common sense, math preva

@ahultgren
ahultgren / undiacritics.js
Last active Sep 16, 2019
A little helper to remove so called diacritics from latin characters (eg convert international characters to url-friendly ones). Please tell me if you find some missing characters or a way to make this more efficient.
View undiacritics.js
"use strict";
var accents = 'àáäâæåèéëêęìíïîòóöôøœōõùúüûūýçñ',
normals = 'aaaaaaeeeeeiiiioooooooouuuuuycn',
diacriticsRegexp = new RegExp('[' + accents + ']', 'ig');
module.exports = exports = function undiacritics (string) {
return string.replace(diacriticsRegexp, function(s){
return normals[accents.indexOf(s)];
});
@ahultgren
ahultgren / gist:5037567
Last active Dec 14, 2015
Basic password generator. Larger feed base than most other generators (and thus higher entropy) and let's you choose which character composition to use. Quotes are for example confusing to put in a session secret on the server, so exclude chars.ambigious. The PRNG can definitely be improved, as could the character collection. Right now, all char…
View gist:5037567
var chars = {
numbers: "123456789",
letters: "qwertyupasdfghjkzxcvbnm",
upper: "qwertyupasdfghjkzxcvbnm".toUpperCase(),
special: "!#€%&/@\\(){}[]=?`´^¨*,.;:-_><§°",
foreign: "ñõìàéèëêãâîïÇÉçéûüåäöæø",
foreignUpper: "ñõìàéèëêãâîïÇÉçéûüåäöæø".toUpperCase(),
ambigious: "\"'’“iloILO0 ",
insane: "¡¥¢‰¶≠¿•˝√‡˜ˆŒ∏»«ˇ⁄—·◊∑∆∫¯˘¬ºflØÆ⁄ˇ≥Ω鮆µüıœπ˙ß∂ƒ¸˛ªfi÷≈ç‹›‘‚…"
};
@ahultgren
ahultgren / gist:3636088
Created Sep 5, 2012
Actually working fishfish script for displaying git branch in terminal
View gist:3636088
set fish_git_dirty_color red
set fish_git_clean_color brown
function parse_git_dirty
if test (git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)"
echo (set_color $fish_git_dirty_color)
else
echo (set_color $fish_git_clean_color)
end
end
function fish_prompt --description 'Write out the prompt'
You can’t perform that action at this time.