Skip to content

Instantly share code, notes, and snippets.

@ahultgren
ahultgren / undiacritics.js
Last active November 20, 2019 13:35
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.
"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 / validatePersonnummer.js
Created January 20, 2014 16:31
Validerar svenska personnummer. Kollar längd, att det är ett giltigt datum samt validerar checksumman.
(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 December 26, 2015 10:39
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.
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 August 31, 2013 12:33
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".
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 December 19, 2015 23:18
Once upon a time there was a show where a guest could win The Grand Prize™.

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 / gist:5037567
Last active December 14, 2015 05:48
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…
var chars = {
numbers: "123456789",
letters: "qwertyupasdfghjkzxcvbnm",
upper: "qwertyupasdfghjkzxcvbnm".toUpperCase(),
special: "!#€%&/@\\(){}[]=?`´^¨*,.;:-_><§°",
foreign: "ñõìàéèëêãâîïÇÉçéûüåäöæø",
foreignUpper: "ñõìàéèëêãâîïÇÉçéûüåäöæø".toUpperCase(),
ambigious: "”\"'’“iloILO0 ",
insane: "¡¥¢‰¶≠¿•˝√‡˜ˆŒ∏»«ˇ⁄—·◊∑∆∫¯˘¬ºflØÆ⁄ˇ≥Ω鮆µüıœπ˙ß∂ƒ¸˛ªfi÷≈ç‹›‘‚…"
};
@ahultgren
ahultgren / gist:3636088
Created September 5, 2012 12:51
Actually working fishfish script for displaying git branch in terminal
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'
@ahultgren
ahultgren / dabblet.css
Created April 24, 2012 20:27
Bouncing ball that stops bouncing
/**
* Bouncing ball that stops bouncing
*/
#boll {
background: #f00;
border-radius: 10px;
height: 20px;
width: 20px;
position: absolute;
@ahultgren
ahultgren / Agent-forwarding.js
Created May 26, 2014 21:18
Reproduction of a bug in ssh2 when using agent forwarding
var Connection = require('ssh2');
var conn = new Connection();
conn.on('ready', function() {
exec(conn, 'pwd', function () {
exec(conn, 'pwd', function () {
conn.end();
});
});
});

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: