Skip to content

Instantly share code, notes, and snippets.

View mathiasbynens's full-sized avatar

Mathias Bynens mathiasbynens

View GitHub Profile
@mathiasbynens
mathiasbynens / LICENSE.txt
Last active October 5, 2022 10:38 — forked from 140bytes/LICENSE.txt
UTF-8 byte counter in 49 bytes
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Mathias Bynens <http://mathiasbynens.be/>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@mathiasbynens
mathiasbynens / README.md
Created April 22, 2011 08:06
Improved swipe gestures plugin for jQuery

Improved swipe gestures plugin for jQuery

What was changed (compared to the original v0.1.2):

  • Don’t modify the default settings internally.
  • Merge the options recursively (deep copy) so that the custom threshold values actually get used.
  • Allow overriding the options globally through $.fn.swipe.options.
  • Use namespaced jQuery events (using the swipe namespace) instead of DOM2 addEventListener to prevent errors in old IEs.
  • Simplified and optimized code.
  • Changed swipeLeft and swipeRight functions to be called within the context of the bound element. Thanks, @kswedberg!
@mathiasbynens
mathiasbynens / jquery.crash-ie.js
Created April 8, 2010 11:53
jQuery Crash plugin — updated to work with IE6, IE7 and IE8
/*!
* jQuery Crash for IE6, IE7 and IE8
* @description This is an expansion of the jQuery Crash plugin for IE6 by Chad Smith.
* @author Mathias Bynens <http://mathiasbynens.be/>
* Use $.crashIE6(); and/or $.crashIE7(); and/or $.crashIE8();
*/
;
$.crashIE6 = function(x) {
if ($.browser.msie && $.browser.version < 7) {
for (x in document.open);
@mathiasbynens
mathiasbynens / deterministic-math-random.js
Last active July 19, 2022 06:52
Here’s a 100% deterministic (predictable) alternative to `Math.random`. Useful when benchmarking.
// Here’s a 100% deterministic alternative to `Math.random`. Google’s V8 and
// Octane benchmark suites use this to ensure predictable results.
Math.random = (function() {
var seed = 0x2F6E2B1;
return function() {
// Robert Jenkins’ 32 bit integer hash function
seed = ((seed + 0x7ED55D16) + (seed << 12)) & 0xFFFFFFFF;
seed = ((seed ^ 0xC761C23C) ^ (seed >>> 19)) & 0xFFFFFFFF;
seed = ((seed + 0x165667B1) + (seed << 5)) & 0xFFFFFFFF;
@mathiasbynens
mathiasbynens / programming-is-like-doing-a-massive-sudoku.md
Created June 28, 2011 12:46
Why programming is like solving a massive sudoku, by @peterbraden

Ok, I came up with an example.

Programming is like doing a massive sudoku. But you’re not just doing your own square; you have to line up the edges with squares that you’ve already done, or squares other people in your team are working on.

And it’s not just squares that you’ve done — you have to anticipate the sudokus you’ll be doing days, weeks or months from now, and leave easy numbers at the edges so it isn’t impossible to do those squares.

And that’s why some programmers are so engrossed in it, and get all worked up, because they’re like “You left a 5 in the middle of the square, what kind of asshole does that, now I’m gonna have to line all my squares up with that.”

And then someone points out a bug, and you have to trace it back to the square it came from, and then redo that square without screwing up all the other ones.

@mathiasbynens
mathiasbynens / unicodeEscape.js
Created September 26, 2011 19:50
Escape all characters in a string using both Unicode and hexadecimal escape sequences
// Ever needed to escape '\n' as '\\n'? This function does that for any character,
// using hex and/or Unicode escape sequences (whichever are shortest).
// Demo: http://mothereff.in/js-escapes
function unicodeEscape(str) {
return str.replace(/[\s\S]/g, function(character) {
var escape = character.charCodeAt().toString(16),
longhand = escape.length > 2;
return '\\' + (longhand ? 'u' : 'x') + ('0000' + escape).slice(longhand ? -4 : -2);
});
}
// Original code from http://www.blog.highub.com/mobile-2/a-fix-for-iphone-viewport-scale-bug/
var metas = document.getElementsByTagName('meta');
var i;
if (navigator.userAgent.match(/iPhone/i)) {
for (i=0; i<metas.length; i++) {
if (metas[i].name == "viewport") {
metas[i].content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0";
}
}
@mathiasbynens
mathiasbynens / md5-collision.js
Last active October 22, 2021 23:36
Verify the most famous MD5 collision example in JavaScript, using nothing but built-in Node libraries.
#!/usr/bin/env node
// Verify the most famous MD5 collision example in JavaScript, using nothing but
// built-in Node modules.
var crypto = require('crypto');
var ucs2encode = require('punycode').ucs2.encode;
var assert = require('assert');
var md5 = function(string) {
function relativeDate(str) {
var s = (+new Date() - Date.parse(str)) / 1e3,
m = s / 60,
h = m / 60,
d = h / 24,
w = d / 7,
y = d / 365.242199,
M = y * 12,
R = Math.round;
return s <= 5 ? 'just now'
@mathiasbynens
mathiasbynens / regex-lone-surrogates.js
Created May 26, 2014 06:43
A regular expression to match lone surrogates only
var assert = require('assert');
// The goal is to match lone surrogates, i.e. any high surrogates
// (`[\uD800-\uDBFF]`) that are not directly followed by a low surrogate
// (`[\uDC00-\uDFFF]`), and any low surrogates (`[\uDC00-\uDFFF]`) that are not
// directly preceded by a high surrogate (`[\uD800-\uDBFF]`).
var regex = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
assert.equal(regex.test('foo\uDC00bar'), true);