Skip to content

Instantly share code, notes, and snippets.

View pgarciacamou's full-sized avatar
:octocat:

Pablo Garcia pgarciacamou

:octocat:
View GitHub Profile
@pgarciacamou
pgarciacamou / osver.js
Created April 16, 2015 22:26
Get OS version
function getOSver() {
var ua = navigator.userAgent;
var uaindex, userOS, userOSver;
if(ua.match(/iPad/i) || ua.match(/iPod/i) || ua.match(/iPhone/i)) {
userOS = "iOS";
uaindex = ua.indexOf("OS ");
} else {
userOS = "unknown";
}
@pgarciacamou
pgarciacamou / forin.js
Last active August 29, 2015 14:19
for in
for(var prop in id_list){
if(!id_list.hasOwnProperty(prop)) continue;
id_list[prop]["details"] = id_list[prop]["details"] || {};
id_list[prop]["details"]["isNovice"] = false;
}
@pgarciacamou
pgarciacamou / custom-event-example.js
Last active September 11, 2015 20:43
Custom Events
// Object that implements the Event Handler Interface
var a = {
handleEvent: function (e){
console.log('hola', e.detail);
}
};
// Custom Event
var custom = new Event('myevent');
@pgarciacamou
pgarciacamou / binary-search-recursive.js
Last active August 29, 2015 14:19
JavaScript Algorithms
// Recursive Binary Search
// Array must be sorted in ascending order
Array.prototype.binarySearch = function(num, from, to) {
to = to || this.length-1;
from = from || 0;
if(!num || from > to) return null;
var index = Math.floor((to-from) / 2) + from;
if(num < this[index]) return this.binarySearch(num, from, index-1);
@pgarciacamou
pgarciacamou / default.js
Created April 8, 2015 23:55
Default properties without using external libraries using prototype inheritance
function MyConstructor(options){
options = options || {};
// Default properties:
options.__proto__ = {
threshold: 3,
quality: 1
etc: "whatever"
};
// Use case:
@pgarciacamou
pgarciacamou / images.html
Created February 23, 2015 00:50
This piece of code will use the CSS3 column-count property to display images in a -Pinterest-esque way.
<!DOCTYPE html>
<html>
<head>
<style>
.images {
-webkit-column-count: 3; /* Chrome, Safari, Opera */
-moz-column-count: 3; /* Firefox */
column-count: 3;
}
.images img { width: 100%; }
@pgarciacamou
pgarciacamou / on.js
Last active August 29, 2015 14:15
Adds events in a backwards compatible way (legacy versions of IE).
(function(){
Object.defineProperties(document, {
"on": {
value: function(evName, callback, useCapture){
return addEvent.call(this, evName, callback, useCapture);
}
}
});
Object.defineProperties(window, {
"on": {
@pgarciacamou
pgarciacamou / css.js
Created February 18, 2015 19:28
CSSRule is a constructor that allows to create classes on the run, it detects the prefix needed and you can use it by sending a callback function instead of a string to the add method.
var CSSRule = (function (){
var prefix = (function getPropertiesPrefix(){
var t, el = document.createElement("fakeelement")
,animations = {
"animation": "",
"OAnimation": "-o-",
"msAnimation": "-ms-",
"MozAnimation": "-moz-",
"WebkitAnimation": "-webkit-"
};
@pgarciacamou
pgarciacamou / keyframe.js
Created February 18, 2015 19:27
Allows to create a set of keyframes with javascript and inject it to a stylesheet.
var Keyframe = (function (){
var keyframePrefix = (function getKeyframePrefix(){
var t, el = document.createElement("fakeelement")
,animations = {
"animation": "",
"OAnimation": "-o-",
"msAnimation": "-ms-",
"MozAnimation": "-moz-",
"WebkitAnimation": "-webkit-"
};
@pgarciacamou
pgarciacamou / animation.js
Created February 18, 2015 19:26
Animation Detector. Watches for the end of and animation to execute a callback and on start you can add the proper classes to start the animation.
var Animation = (function (){
var animationEvent = (function whichAnimationEvent(){
var t, el = document.createElement("fakeelement")
,animation = null
,animations = {
"WebkitAnimation": "webkitAnimationEnd",
"MozAnimation": "animationend",
"OAnimation": "oAnimationEnd",
"animation": "animationend"
};