Skip to content

Instantly share code, notes, and snippets.

View lean8086's full-sized avatar

Leandro Linares lean8086

View GitHub Profile
@lean8086
lean8086 / vendor_prefix.js
Created May 10, 2012 15:22
Grab the vendor prefix of the current browser.
// Based on: http://lea.verou.me/2009/02/find-the-vendor-prefix-of-the-current-browser/
var VENDOR_PREFIX = (function () {
var regex = /^(Webkit|Khtml|Moz|ms|O)(?=[A-Z])/,
styleDeclaration = document.getElementsByTagName("script")[0].style;
for (var prop in styleDeclaration) {
if (regex.test(prop)) {
return prop.match(regex)[0].toLowerCase();
@lean8086
lean8086 / namespace.js
Created May 10, 2012 15:47
Namespace template
(function (window) {
"use strict";
/**
* Helpers
*/
var document = window.document,
WebSocket = window.WebSocket,
Worker = window.Worker,
localStorage = window.localStorage,
@lean8086
lean8086 / friendly.js
Created May 11, 2012 16:07
Convert a normal string into a friendly string
function friendly(str) {
"use strict";
// Turn string into lower cases
str = str.toLowerCase();
// Normalize vocals
str = str.replace(/\á|\à|\ä|\â|\ã|\ª/g, "a");
str = str.replace(/\é|\è|\ë|\ê/g, "e");
str = str.replace(/\í|\ì|\ï|\î/g, "i");
@lean8086
lean8086 / Database.php
Created May 15, 2012 17:29
Allows to connect to a database from PHP
<?php
class Database {
private $HOST = "...Your hostname or IP here...";
private $USER = "...Username to log into database...";
private $PASS = "...Password to log into database...";
private $DDBB = "...Name of database...";
private function connect() {
$c = mysql_connect($this->HOST, $this->USER, $this->PASS);
@lean8086
lean8086 / canvasContext.js
Created May 22, 2012 19:27
Set the 2D context of a canvas
var context = (function () {
"use strict";
// Get the HTML Canvas element
var canvas = document.querySelector("canvas");
// Set canvas size
canvas.width = 200;
canvas.height = 200;
@lean8086
lean8086 / friendlyDate.php
Created May 22, 2012 19:34
Get a friendly date from unformated date
function friendlyDate($s) {
$minutes = (time() - strtotime($s)) / 60;
if ($minutes < 1) return "Seconds ago";
if ($minutes < 2) return "1 minute ago";
if ($minutes < 60) return floor($minutes)." minutes ago";
$hours = $minutes / 60;
@lean8086
lean8086 / bind.js
Created May 23, 2012 13:47
Binds multiple events into one listener
function bind(listener, events, callback, useCapture) {
events.split(" ").forEach(function (ev) {
listener.addEventListener(ev, callback, useCapture);
});
}
// Example:
// bind(document, "mousedown mouseup keydown keyup", proccess);
@lean8086
lean8086 / rAF.js
Created May 25, 2012 19:56
Request Animation Frame polyfill
var fps = 1000 / 60,
rAF = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, fps);
};
@lean8086
lean8086 / sniffing.jade
Created June 4, 2012 18:30
Browser sniffing for Jade
!!! 5
//if lt IE 7
html.no-js.lt-ie10.lt-ie9.lt-ie8.lt-ie7.ie6(lang="en")
//if IE 7
html.no-js.lt-ie10.lt-ie9.lt-ie8.ie7(lang="en")
//if IE 8
html.no-js.lt-ie10.lt-ie9.ie8(lang="en")
//if IE 9
html.no-js.lt-ie10.ie9(lang="en")
//[if gt IE 9] <!
@lean8086
lean8086 / customEvent.js
Created July 20, 2012 00:23
Creating, triggering and listening a custom event
// Defining the custom event
var customEvent = (function () {
"use strict";
// Create a generic event
var e = document.createEvent("Event");
// Define the custom event name
e.initEvent("custom", true, true);
// Grab event to trigger
return e;
}());