Skip to content

Instantly share code, notes, and snippets.

View felixzapata's full-sized avatar

Félix Zapata felixzapata

View GitHub Profile
@felixzapata
felixzapata / gist:5939488
Created July 6, 2013 10:18
isArrayLike: returns true if `obj` is an array or array-like object (NodeList, Arguments, ...)
// https://github.com/angular/angular.js/blob/master/src/Angular.js#L72
function isArrayLike(obj) {
if (!obj || (typeof obj.length !== 'number')) return false;
// We have on object which has length property. Should we treat it as array?
if (typeof obj.hasOwnProperty != 'function' &&
typeof obj.constructor != 'function') {
// This is here for IE8: it is a bogus object treat it as array;
return true;
@felixzapata
felixzapata / gist:5791368
Created June 16, 2013 08:19
JavaScript AJAX request
function ajax(url, opts){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
var completed = 4;
if(xhr.readyState === completed){
if(xhr.status === 200){
opts.success(xhr.responseText, xhr);
}else{
opts.error(xhr.responseText, xhr);
@felixzapata
felixzapata / gist:5656306
Created May 27, 2013 10:09
Comprobar CIF
function esCif(c){
var ultima,
uletra = ["J", "A", "B", "C", "D", "E", "F", "G", "H", "I"],
par = 0,
non = 0,
regular =/^[ABCDEFGHJKLMNPQRSUVW]\d\d\d\d\d\d\d[0-9,A-J]$/g,
nn, parcial, control;
c = c.toUpperCase();
if (!regular.exec(c)) {return false; }
@felixzapata
felixzapata / gist:5656301
Created May 27, 2013 10:08
Comprobar NIE
function esNie(c){
var aux = c.charAt(0);
if(aux.toUpperCase()!=="X" && aux.toUpperCase()!=="Y"){ return false;}
return comprobarNIE(c.substr(1,c.length), aux);
}
function comprobarNIE(c, letraInicio){
if(!/^[0-9]{7}([A-Za-z]{1})$/.test(c)) return false;
var letras = 'TRWAGMYFPDXBNJZSQVHLCKET',
letraFin = c.substr(7,8).toUpperCase(),
numero = c.substr(0,7);
@felixzapata
felixzapata / gist:5343817
Created April 9, 2013 07:57
Averiguamos el más alto de filas de n elementos
function controlHeight(elems, num){
var len = elems.length;
if(len !== 0){
for(var i = 0; i < len; i+=num) {
var divs = elems.slice(i, i+num),
height, numArray = [];
for(var j = 0; j < num; j++){
numArray.push(divs.eq(j).height());
}
height = Math.max.apply(null, numArray);
@felixzapata
felixzapata / gist:5106824
Created March 7, 2013 09:45
Pila de funciones
function Stack(stackLimit)
{
this.stack = new Array();
this.stackLimit = stackLimit;
}
Stack.prototype.isEmpty = function()
{
return (this.stack.length == 0);
}
@felixzapata
felixzapata / gist:4550829
Created January 16, 2013 20:50
JavaScript tips: cadena aleatoria
// http://www.yeikos.com/2013/01/javascript-tips-cadena-aleatoria.html
Math.random().toString(36).substring(2); // 9xfq8ssn2hjjor
@felixzapata
felixzapata / gist:4339343
Created December 19, 2012 18:53
How to determine if a number is odd in JavaScript
// http://stackoverflow.com/questions/5016313/how-to-determine-if-a-number-is-odd-in-javascript
function oddOrEven(x) {
return ( x & 1 ) ? "odd" : "even";
}
@felixzapata
felixzapata / gist:4098274
Created November 17, 2012 18:01
how to empty an array in JavaScript
// http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript
// related: Splicing is the worst by far: http://jsperf.com/emptying-arrays/4
matrix.splice(0,matrix.length);
@felixzapata
felixzapata / gist:4094723
Created November 17, 2012 10:27
use an efficient for loop as there may be a lot to cycle through
var i, len = titles.length;
for(i = len-1; i >- 1; i--){
}